001: /*
002: * Copyright 2005-2007 The Kuali Foundation.
003: *
004: *
005: * Licensed under the Educational Community License, Version 1.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.opensource.org/licenses/ecl1.php
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package edu.iu.uis.eden.xml.export;
018:
019: import java.io.BufferedInputStream;
020: import java.io.ByteArrayInputStream;
021: import java.io.StringReader;
022: import java.util.Iterator;
023: import java.util.List;
024:
025: import org.jdom.Document;
026: import org.junit.Test;
027:
028: import edu.iu.uis.eden.KEWServiceLocator;
029: import edu.iu.uis.eden.edl.EDocLiteStyle;
030: import edu.iu.uis.eden.export.ExportDataSet;
031: import edu.iu.uis.eden.export.ExportFormat;
032: import edu.iu.uis.eden.util.XmlHelper;
033:
034: /**
035: * Tests exporting Styles
036: * @author Aaron Hamid (arh14 at cornell dot edu)
037: */
038: public class StyleXmlExporterTest extends XmlExporterTestCase {
039:
040: public void testExportActionConfig() throws Exception {
041: // action config has no help entries
042: }
043:
044: public void testExportEngineConfig() throws Exception {
045: // engine config has no help entries
046: }
047:
048: /**
049: * This will test exporting some styles
050: */
051: @Test
052: public void testExport() throws Exception {
053: loadXmlFile("StyleExportConfig.xml");
054: assertExport();
055: }
056:
057: protected void assertExport() throws Exception {
058: List<EDocLiteStyle> oldStyles = KEWServiceLocator
059: .getStyleService().getStyles();
060:
061: System.err.println("Styles: " + oldStyles.size());
062:
063: ExportDataSet dataSet = new ExportDataSet(ExportFormat.XML);
064: dataSet.getStyles().addAll(oldStyles);
065:
066: byte[] xmlBytes = KEWServiceLocator.getXmlExporterService()
067: .export(ExportFormat.XML, dataSet);
068: assertTrue("XML should be non empty.", xmlBytes != null
069: && xmlBytes.length > 0);
070: // quick check to verify that not only is the XML non-empty, but that it might actually contain an attempt at an exported style
071: // (otherwise the XML could not contain any styles, and the test would pass with a false positive even though the export never
072: // exported anything)
073: assertTrue("XML does not contain exported style", new String(
074: xmlBytes).contains("<styles "));
075: assertTrue("XML does not contain exported style", new String(
076: xmlBytes)
077: .contains("<style name=\"an_arbitrary_style\">"));
078:
079: // import the exported xml
080: loadXmlStream(new BufferedInputStream(new ByteArrayInputStream(
081: xmlBytes)));
082:
083: List<EDocLiteStyle> newStyles = KEWServiceLocator
084: .getStyleService().getStyles();
085: assertEquals("Should have same number of old and new Styles.",
086: oldStyles.size(), newStyles.size());
087: for (Iterator iterator = oldStyles.iterator(); iterator
088: .hasNext();) {
089: EDocLiteStyle oldStyleEntry = (EDocLiteStyle) iterator
090: .next();
091: boolean foundAttribute = false;
092: for (EDocLiteStyle newStyleEntry : newStyles) {
093: if (oldStyleEntry.getName().equals(
094: newStyleEntry.getName())) {
095: // NOTE: xmlns="http://www.w3.org/1999/xhtml" must be set on elements that contain HTML; exporter will automatically append an empty
096: // attribute, which will result in trivially unmatching content
097: assertEquals(canonicalize(oldStyleEntry
098: .getXmlContent()),
099: canonicalize(newStyleEntry.getXmlContent()));
100: foundAttribute = true;
101: }
102: }
103: assertTrue("Could not locate the new style for name "
104: + oldStyleEntry.getName(), foundAttribute);
105: }
106: }
107:
108: private String canonicalize(String xml) throws Exception {
109: Document document = XmlHelper.buildJDocument(new StringReader(
110: xml));
111: return XmlHelper.jotDocument(document);
112: }
113: }
|