View Javadoc

1   /**
2    * Copyright 2005-2011 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kew.xml.export;
17  
18  import org.jdom.Document;
19  import org.junit.Test;
20  import org.kuali.rice.core.api.CoreApiServiceLocator;
21  import org.kuali.rice.coreservice.api.style.Style;
22  import org.kuali.rice.core.api.util.xml.XmlHelper;
23  import org.kuali.rice.core.api.util.xml.XmlJotter;
24  import org.kuali.rice.coreservice.impl.style.StyleBo;
25  import org.kuali.rice.coreservice.impl.style.StyleExportDataSet;
26  import org.kuali.rice.coreservice.api.CoreServiceApiServiceLocator;
27  import org.kuali.rice.test.BaselineTestCase;
28  
29  import java.io.BufferedInputStream;
30  import java.io.ByteArrayInputStream;
31  import java.io.StringReader;
32  import java.util.Iterator;
33  import java.util.List;
34  
35  import static org.junit.Assert.*;
36  
37  
38  /**
39   * Tests exporting Styles
40   * @author Kuali Rice Team (rice.collab@kuali.org)
41   */
42  @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.NONE)
43  public class StyleXmlExporterTest extends XmlExporterTestCase {
44  
45  	public void testExportActionConfig() throws Exception {
46  		// action config has no help entries
47      }
48  
49      public void testExportEngineConfig() throws Exception {
50      	// engine config has no help entries
51      }
52  
53      /**
54       * This will test exporting some styles
55       */
56      @Test public void testExport() throws Exception {
57          loadXmlFile("StyleExportConfig.xml");
58          assertExport();
59      }
60  
61      protected void assertExport() throws Exception {
62          List<String> oldStyleNames = CoreServiceApiServiceLocator.getStyleService().getAllStyleNames();
63          
64          StyleExportDataSet dataSet = new StyleExportDataSet();
65          for (String oldStyleName : oldStyleNames) {
66          	Style oldStyle = CoreServiceApiServiceLocator.getStyleService().getStyle(oldStyleName);
67          	assertNotNull(oldStyle);
68          	dataSet.getStyles().add(StyleBo.from(oldStyle));
69          }
70  
71          byte[] xmlBytes = CoreApiServiceLocator.getXmlExporterService().export(dataSet.createExportDataSet());
72          assertTrue("XML should be non empty.", xmlBytes != null && xmlBytes.length > 0);
73          // quick check to verify that not only is the XML non-empty, but that it might actually contain an attempt at an exported style
74          // (otherwise the XML could not contain any styles, and the test would pass with a false positive even though the export never
75          // exported anything)
76          assertTrue("XML does not contain exported style", new String(xmlBytes).contains("<styles "));
77          assertTrue("XML does not contain exported style", new String(xmlBytes).contains("<style name=\"an_arbitrary_style\">"));
78  
79          // import the exported xml
80          loadXmlStream(new BufferedInputStream(new ByteArrayInputStream(xmlBytes)));
81  
82          List<String> newStyleNames = CoreServiceApiServiceLocator.getStyleService().getAllStyleNames();
83          assertEquals("Should have same number of old and new Styles.", oldStyleNames.size(), newStyleNames.size());
84          for (Iterator<String> iterator = oldStyleNames.iterator(); iterator.hasNext();) {
85              String oldStyleName = iterator.next();
86              Style oldStyleEntry = CoreServiceApiServiceLocator.getStyleService().getStyle(oldStyleName);
87              assertNotNull(oldStyleEntry);
88              boolean foundAttribute = false;
89              for (String newStyleName : newStyleNames) {
90                  if (oldStyleEntry.getName().equals(newStyleName)) {
91                  	Style newStyleEntry = CoreServiceApiServiceLocator.getStyleService().getStyle(newStyleName);
92                  	assertNotNull(newStyleEntry);
93                      // NOTE: xmlns="http://www.w3.org/1999/xhtml" must be set on elements that contain HTML; exporter will automatically append an empty
94                      // attribute, which will result in trivially unmatching content
95                      assertEquals(canonicalize(oldStyleEntry.getXmlContent()),
96                                   canonicalize(newStyleEntry.getXmlContent()));
97                      foundAttribute = true;
98                  }
99              }
100             assertTrue("Could not locate the new style for name " + oldStyleEntry.getName(), foundAttribute);
101         }
102     }
103 
104     private String canonicalize(String xml) throws Exception {
105     	Document document = XmlHelper.buildJDocument(new StringReader(xml));
106     	return XmlJotter.jotDocument(document);
107     }
108 }