From d072c523829f9be6522b983962b0e1ea15788dad Mon Sep 17 00:00:00 2001 From: erik Date: Mon, 27 Mar 2006 22:06:55 +0000 Subject: [PATCH] --- .../resources/test/org/wamblee/xml/report.xml | 41 +++++ .../test/org/wamblee/xml/reportToHtml.xsl | 98 ++++++++++++ .../test/org/wamblee/xml/utilities.xsl | 142 ++++++++++++++++++ support/src/org/wamblee/xml/DomUtils.java | 99 ++++++++++++ .../org/wamblee/io/ClassPathResourceTest.java | 1 - .../test/org/wamblee/io/FileResourceTest.java | 1 - .../wamblee/{test => io}/FileSystemUtils.java | 2 +- .../org/wamblee/io/StreamResourceTest.java | 1 - support/test/org/wamblee/io/TestResource.java | 3 - .../wamblee/xml/ClasspathUriResolverTest.java | 73 +++++++++ support/test/org/wamblee/xml/XmlUtils.java | 115 ++++++++++++++ .../org/wamblee/xml/XslTransformerTest.java | 113 ++++++++++++++ 12 files changed, 682 insertions(+), 7 deletions(-) create mode 100644 support/resources/test/org/wamblee/xml/report.xml create mode 100644 support/resources/test/org/wamblee/xml/reportToHtml.xsl create mode 100644 support/resources/test/org/wamblee/xml/utilities.xsl create mode 100644 support/src/org/wamblee/xml/DomUtils.java rename support/test/org/wamblee/{test => io}/FileSystemUtils.java (99%) create mode 100644 support/test/org/wamblee/xml/ClasspathUriResolverTest.java create mode 100644 support/test/org/wamblee/xml/XmlUtils.java create mode 100644 support/test/org/wamblee/xml/XslTransformerTest.java diff --git a/support/resources/test/org/wamblee/xml/report.xml b/support/resources/test/org/wamblee/xml/report.xml new file mode 100644 index 00000000..391aea94 --- /dev/null +++ b/support/resources/test/org/wamblee/xml/report.xml @@ -0,0 +1,41 @@ + + + + Wintertijd + Some description MINSK - De presidentsverkiezingen in Wit-Rusland zijn zondag met ruime cijfers gewonnen door zittend president Aleksandr Loekasjenko. Dat bleek zondag uit exitpolls uitgevoerd in opdracht van het totalitaire regime. Het staatshoofd zou kunnen rekenen op ruim 82 procent van de stemmen. Volgens de eerste gedeeltelijke uitslagen zou Loekasjenko zelfs kunnen rekenen op bijna 89 procent. + Documentaire + Nederland 1 + + 23:25 + 00:10 + + + + + + + Brainiac + Humor + science + Discovery Channel + + 23:30 + 00:15 + + + + + Andere tijden + Documentaire + docu + Nederland 1 + + 23:30 + 00:15 + + + + + + + diff --git a/support/resources/test/org/wamblee/xml/reportToHtml.xsl b/support/resources/test/org/wamblee/xml/reportToHtml.xsl new file mode 100644 index 00000000..44765a32 --- /dev/null +++ b/support/resources/test/org/wamblee/xml/reportToHtml.xsl @@ -0,0 +1,98 @@ + + + + + + + KiSS crawler report + + +

KiSS crawler report

+ + +

Possibly interesting programs

+ +
+ + No suitable programs found + + + + +
+ + +

+ + + +
+
+

+
+ + +

+ + + Successfully recorded programs + + + + Already recorded programs + + + + Conflicts with other recorded programs + + + + Programs that could not be recorded for + technical reasons. + + + +

+
+ + + + - : + + (/) + + + + +
+ + + +
+ + + + +
+ + + + + + + + + + + +

Category:

+ +
+
+ diff --git a/support/resources/test/org/wamblee/xml/utilities.xsl b/support/resources/test/org/wamblee/xml/utilities.xsl new file mode 100644 index 00000000..0dd7e62b --- /dev/null +++ b/support/resources/test/org/wamblee/xml/utilities.xsl @@ -0,0 +1,142 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/support/src/org/wamblee/xml/DomUtils.java b/support/src/org/wamblee/xml/DomUtils.java new file mode 100644 index 00000000..4fdb52e7 --- /dev/null +++ b/support/src/org/wamblee/xml/DomUtils.java @@ -0,0 +1,99 @@ +/* + * Copyright 2005 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.wamblee.xml; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; + +import org.apache.xml.serialize.OutputFormat; +import org.apache.xml.serialize.XMLSerializer; +import org.dom4j.DocumentException; +import org.dom4j.io.DOMReader; +import org.dom4j.io.DOMWriter; +import org.w3c.dom.Document; +import org.xml.sax.SAXException; + +/** + * Some basic XML utilities for common reoccuring tasks for + * DOM documents. + */ +public final class DomUtils { + + /** + * Disabled default constructor. + * + */ + private DomUtils() { + // Empty. + } + + + /** + * Parses an XML document from a stream. + * @param aIs Input stream. + * @return + */ + public static Document read(InputStream aIs) throws SAXException, ParserConfigurationException, IOException { + DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); + return builder.parse(aIs); + } + + /** + * Serializes an XML document to a stream. + * @param aDocument Document to serialize. + * @param aOs Output stream. + */ + public static void serialize(Document aDocument, OutputStream aOs) throws IOException { + XMLSerializer serializer = new XMLSerializer(aOs, new OutputFormat()); + serializer.serialize(aDocument); + } + + /** + * Serializes an XML document. + * @param aDocument Document to serialize. + * @return Serialized document. + */ + public static String serialize(Document aDocument) throws IOException { + ByteArrayOutputStream os = new ByteArrayOutputStream(); + serialize(aDocument, os); + return os.toString(); + } + + /** + * Converts a dom4j document into a w3c DOM document. + * @param aDocument Document to convert. + * @return W3C DOM document. + */ + public static Document convert(org.dom4j.Document aDocument) throws DocumentException { + return new DOMWriter().write(aDocument); + } + + /** + * Converts a W3C DOM document into a dom4j document. + * @param aDocument Document to convert. + * @return Dom4j document. + */ + public static org.dom4j.Document convert(Document aDocument) { + return new DOMReader().read(aDocument); + } +} diff --git a/support/test/org/wamblee/io/ClassPathResourceTest.java b/support/test/org/wamblee/io/ClassPathResourceTest.java index 467da846..70b05ab5 100644 --- a/support/test/org/wamblee/io/ClassPathResourceTest.java +++ b/support/test/org/wamblee/io/ClassPathResourceTest.java @@ -19,7 +19,6 @@ package org.wamblee.io; import java.io.IOException; import java.io.InputStream; -import org.wamblee.test.FileSystemUtils; import junit.framework.TestCase; diff --git a/support/test/org/wamblee/io/FileResourceTest.java b/support/test/org/wamblee/io/FileResourceTest.java index 86ae9342..cc085e77 100644 --- a/support/test/org/wamblee/io/FileResourceTest.java +++ b/support/test/org/wamblee/io/FileResourceTest.java @@ -20,7 +20,6 @@ import java.io.File; import java.io.IOException; import java.io.InputStream; -import org.wamblee.test.FileSystemUtils; import junit.framework.TestCase; diff --git a/support/test/org/wamblee/test/FileSystemUtils.java b/support/test/org/wamblee/io/FileSystemUtils.java similarity index 99% rename from support/test/org/wamblee/test/FileSystemUtils.java rename to support/test/org/wamblee/io/FileSystemUtils.java index 7d1c2d7e..cffd0780 100644 --- a/support/test/org/wamblee/test/FileSystemUtils.java +++ b/support/test/org/wamblee/io/FileSystemUtils.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.wamblee.test; +package org.wamblee.io; import java.io.File; import java.io.IOException; diff --git a/support/test/org/wamblee/io/StreamResourceTest.java b/support/test/org/wamblee/io/StreamResourceTest.java index 73f2d6c4..11a9bfed 100644 --- a/support/test/org/wamblee/io/StreamResourceTest.java +++ b/support/test/org/wamblee/io/StreamResourceTest.java @@ -21,7 +21,6 @@ import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; -import org.wamblee.test.FileSystemUtils; import junit.framework.TestCase; diff --git a/support/test/org/wamblee/io/TestResource.java b/support/test/org/wamblee/io/TestResource.java index f4d407d6..ec28290f 100644 --- a/support/test/org/wamblee/io/TestResource.java +++ b/support/test/org/wamblee/io/TestResource.java @@ -17,10 +17,7 @@ package org.wamblee.io; import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import org.wamblee.test.FileSystemUtils; /** * Test resource for locating resources in the classpath. diff --git a/support/test/org/wamblee/xml/ClasspathUriResolverTest.java b/support/test/org/wamblee/xml/ClasspathUriResolverTest.java new file mode 100644 index 00000000..2d98a399 --- /dev/null +++ b/support/test/org/wamblee/xml/ClasspathUriResolverTest.java @@ -0,0 +1,73 @@ +/* + * Copyright 2005 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.wamblee.xml; + +import java.io.IOException; + +import javax.xml.transform.Source; +import javax.xml.transform.TransformerException; +import javax.xml.transform.URIResolver; +import javax.xml.transform.stream.StreamSource; + +import junit.framework.TestCase; + +import org.springframework.core.io.ClassPathResource; +import org.wamblee.io.FileSystemUtils; + +/** + * Tests for {@link org.wamblee.xml.ClasspathUriResolver}. + */ +public class ClasspathUriResolverTest extends TestCase { + + private URIResolver _resolver; + + /* (non-Javadoc) + * @see junit.framework.TestCase#setUp() + */ + @Override + protected void setUp() throws Exception { + _resolver = new ClasspathUriResolver(); + } + + /** + * Resolves an existing file. Verifies the file is resolved correctly. + * @throws TransformerException + * @throws IOException + */ + public void testResolveExistingFile() throws TransformerException, IOException { + Source source = _resolver.resolve("org/wamblee/xml/reportToHtml.xsl", ""); + assertTrue(source instanceof StreamSource); + String resolved = FileSystemUtils.read(((StreamSource)source).getInputStream()); + + ClassPathResource resource = new ClassPathResource("org/wamblee/xml/reportToHtml.xsl"); + String expected = FileSystemUtils.read(resource.getInputStream()); + assertEquals(expected, resolved); + } + + /** + * Resolves a non-existing file. Verifies that a TransformerException is thrown. + * + */ + public void testResolveNonExistingFile() { + try { + Source source = _resolver.resolve("org/wamblee/xml/reportToHtml-nonexisting.xsl", ""); + } catch (TransformerException e) { + return; // ok + } + fail(); + } +} diff --git a/support/test/org/wamblee/xml/XmlUtils.java b/support/test/org/wamblee/xml/XmlUtils.java new file mode 100644 index 00000000..c83bdae8 --- /dev/null +++ b/support/test/org/wamblee/xml/XmlUtils.java @@ -0,0 +1,115 @@ +/* + * Copyright 2005 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.wamblee.xml; + +import java.util.Collections; +import java.util.Comparator; +import java.util.List; + +import junit.framework.TestCase; + +import org.dom4j.Attribute; +import org.dom4j.Document; +import org.dom4j.Element; + + + +/** + * XML test support utilities. + */ +public final class XmlUtils { + + /** + * Disabled constructor. + * + */ + private XmlUtils() { + // Empty + } + + /** + * Checks equality of two XML documents excluding comment and processing nodes and + * trimming the text of the elements. + * @param aMsg + * @param aExpected + * @param aActual + */ + public static void assertEquals(String aMsg, Document aExpected, Document aActual) { + assertEquals(aMsg, aExpected.getRootElement(), aActual.getRootElement()); + } + + /** + * Checks equality of two XML elements excluding comment and processing nodes and trimming + * the text of the elements. + * @param aMsg + * @param aExpected + * @param aActual + */ + public static void assertEquals(String aMsg, Element aExpected, Element aActual) { + + // Name. + TestCase.assertEquals(aMsg + "/name()", aExpected.getName(), aActual.getName()); + + // Text + TestCase.assertEquals(aMsg + "/text()", aExpected.getTextTrim(), aActual.getTextTrim()); + + // Attributes + List expectedAttrs = aExpected.attributes(); + Collections.sort(expectedAttrs, new AttributeComparator()); + List actualAttrs = aActual.attributes(); + Collections.sort(actualAttrs, new AttributeComparator()); + + TestCase.assertEquals(aMsg + "/#attributes", expectedAttrs.size(), actualAttrs.size()); + for (int i = 0; i < expectedAttrs.size(); i++) { + String msg = aMsg + "/attribute(" + i + ")"; + assertEquals(msg, expectedAttrs.get(i), actualAttrs.get(i)); + } + + // Nested elements. + List expectedElems = aExpected.elements(); + List actualElems = aActual.elements(); + TestCase.assertEquals(aMsg + "/#elements", expectedElems.size(), actualElems.size()); + for (int i = 0; i < expectedElems.size(); i++) { + String msg = aMsg + "/element(" + i + ")"; + assertEquals(msg, expectedElems.get(i), actualElems.get(i)); + } + } + + /** + * Checks equality of two attributes. + * @param aMsg + * @param aExpected + * @param aActual + */ + public static void assertEquals(String aMsg, Attribute aExpected, Attribute aActual) { + TestCase.assertEquals(aMsg + "@", aExpected.getName() + ":name", aActual.getName()); + TestCase.assertEquals(aMsg + "@" + aExpected.getName() + ":value", + aExpected.getValue(), aActual.getValue()); + } + + /** + * Comparator which compares attributes by name. + */ + private static final class AttributeComparator implements Comparator { + /* (non-Javadoc) + * @see java.util.Comparator#compare(T, T) + */ + public int compare(Attribute aAttribute1, Attribute aAttribute2) { + return aAttribute1.getName().compareTo(aAttribute2.getName()); + } + } +} diff --git a/support/test/org/wamblee/xml/XslTransformerTest.java b/support/test/org/wamblee/xml/XslTransformerTest.java new file mode 100644 index 00000000..8352cb0d --- /dev/null +++ b/support/test/org/wamblee/xml/XslTransformerTest.java @@ -0,0 +1,113 @@ +/* + * Copyright 2005 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.wamblee.xml; + +import java.io.ByteArrayOutputStream; +import java.io.File; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.transform.Result; +import javax.xml.transform.Source; +import javax.xml.transform.stream.StreamResult; +import javax.xml.transform.stream.StreamSource; + +import junit.framework.TestCase; + +import org.w3c.dom.Document; +import org.wamblee.io.FileSystemUtils; +import org.wamblee.io.InputResource; +import org.wamblee.io.TestResource; + +/** + * Tests the XSL transformer. + */ +public class XslTransformerTest extends TestCase { + + private static final String REPORT_XML = "report.xml"; + private static final String REPORT_TO_HTML_XSLT = "reportToHtml.xsl"; + + /** + * Transforms a file while using the default resolver, + * where the included file can be found. + * Verifies the transformation is done correctly. + * + */ + public void testTransformUsingDefaultResolver() throws Exception { + XslTransformer transformer = new XslTransformer(); + + InputResource xmlResource = new TestResource(XslTransformerTest.class, REPORT_XML); + Source xslt = new StreamSource(new File( FileSystemUtils.getTestInputDir(XslTransformerTest.class), REPORT_TO_HTML_XSLT)); + + byte[] documentData = FileSystemUtils.read(xmlResource.getInputStream()).getBytes(); + DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); + Document document = builder.parse(xmlResource.getInputStream()); + Source documentSource = new StreamSource(xmlResource.getInputStream()); + + Document output1 = transformer.transform(documentData, xslt); + Document output2 = transformer.transform(document, xslt); + ByteArrayOutputStream os = new ByteArrayOutputStream(); + Result output = new StreamResult(os); + transformer.transform(documentSource, output, xslt); + transformer.textTransform(documentData, xslt); + } + + /** + * Transforms a file using the default resolver where the + * included file cannot be found. + * Verifies that a TransformerException is thrown. + * + */ + public void testTransformUsingDefaultResolverFails() { + fail(); + } + + /** + * Transforms a file using an invalid Xslt. + * Verifies that a TransformerException is thrown. + * + */ + public void testTransformInvalidXslt() { + fail(); + } + + /** + * Transforms a file using a non-well formed xslt. + * Verifies that a TransformerException is thrown. + * + */ + public void testTransformNonWellformedXslt() { + fail(); + } + + /** + * Transforms a file using a class path resolver. + * + */ + public void testTransformUsingClassPathResolver() { + fail(); + } + + /** + * Transforms a file to text output. + * Verifies the file is transformed correctly. + * + */ + public void testTransformToTextOutput() { + fail(); + } +} -- 2.31.1