diff --git a/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/main/java/de/hdm_stuttgart/mi/sda1/saxhtml/tools/SaxFilter.java b/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/main/java/de/hdm_stuttgart/mi/sda1/saxhtml/tools/SaxFilter.java index bd105d01fdbf8702d9e2d6c26d58161a45ff99f1..1c1f8e4ea767d183beb0092f6dbf0efe37c4c8cb 100644 --- a/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/main/java/de/hdm_stuttgart/mi/sda1/saxhtml/tools/SaxFilter.java +++ b/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/main/java/de/hdm_stuttgart/mi/sda1/saxhtml/tools/SaxFilter.java @@ -4,10 +4,16 @@ import java.io.PrintStream; import org.xml.sax.ContentHandler; +/** + * Simplifies testing of XML output generating applications + * + */ public interface SaxFilter extends ContentHandler { /** - * Allow output redirection. + * Allow output redirection. All filter related output will be + * redirected from System.out to an arbitrary stream + * * @param out Output will be redirected here. */ public void setOutputStream(PrintStream out); diff --git a/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/main/java/de/hdm_stuttgart/mi/sda1/saxhtml/v1/Memo2HtmlHandler.java b/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/main/java/de/hdm_stuttgart/mi/sda1/saxhtml/v1/Memo2HtmlHandler.java index 32b160ac60d77338db9a66cb4b9f2b04d3c1d5e1..01d69f1f407147b5a72d3c52d534237f790949ec 100644 --- a/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/main/java/de/hdm_stuttgart/mi/sda1/saxhtml/v1/Memo2HtmlHandler.java +++ b/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/main/java/de/hdm_stuttgart/mi/sda1/saxhtml/v1/Memo2HtmlHandler.java @@ -28,7 +28,7 @@ public class Memo2HtmlHandler extends DefaultHandler implements SaxFilter { @Override public void startDocument() throws SAXException { - out.print("<html" + "\n <head>" + "\n <title>Memo message</title>" + out.print("<html>" + "\n <head>" + "\n <title>Memo message</title>" + "\n </head>" + "\n <body>"); } diff --git a/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/test/java/de/hdm_stuttgart/de/mi/exam/testing/SaxFilterTest.java b/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/test/java/de/hdm_stuttgart/de/mi/exam/testing/SaxFilterTest.java index 0cfc855027466bcf5b3a097b7f953d35d86361d2..202c560c48bf8e2159a2c70077d5f876c2bff808 100644 --- a/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/test/java/de/hdm_stuttgart/de/mi/exam/testing/SaxFilterTest.java +++ b/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/test/java/de/hdm_stuttgart/de/mi/exam/testing/SaxFilterTest.java @@ -28,89 +28,86 @@ import de.hdm_stuttgart.mi.sda1.saxhtml.tools.StandardErrorHandler; @SuppressWarnings("javadoc") public abstract class SaxFilterTest { - public final String xmlInputFileName, htmlOutputFileName; - final SaxFilter saxHandler; - protected static String initializationErrorString = null; - - protected static Element htmlRootElement = null; - - public SaxFilterTest(final String xmlInputFileName, - final SaxFilter saxHandler, - final String resultFileExtension) { - this.xmlInputFileName = xmlInputFileName; - htmlOutputFileName = xmlInputFileName + resultFileExtension; - this.saxHandler = saxHandler; - - if (null == htmlRootElement && null == initializationErrorString) { - - final PrintStream out; - - try { - out = new PrintStream(htmlOutputFileName); - } catch (FileNotFoundException e1) { - initializationErrorString = "Unable to open file '" + htmlOutputFileName - + "' for writing"; - return; - } - saxHandler.setOutputStream(out); - - final XMLReader xmlReader; - try { - final SAXParserFactory saxPf = SAXParserFactory.newInstance(); - final SAXParser saxParser = saxPf.newSAXParser(); - xmlReader = saxParser.getXMLReader(); - - xmlReader.setContentHandler(saxHandler); - xmlReader.setErrorHandler(new StandardErrorHandler()); - - } catch (SAXException | ParserConfigurationException e) { - initializationErrorString = "Unable to create XMLparser instance: " + e.getLocalizedMessage(); - return; - } - try { - xmlReader.parse(xmlInputFileName); - } catch (SAXException | IOException e) { - initializationErrorString = "Unable parse file '" + xmlInputFileName - + "': " + e.getLocalizedMessage(); - return; - } - out.close(); - printResultToStdout(); - - // Now parse the result for further analysis - // - final SAXBuilder parser = new SAXBuilder(); - - try { - htmlRootElement = parser.build(htmlOutputFileName).getRootElement(); - } catch (JDOMException | IOException e1) { - System.err.print( - initializationErrorString = "Unable to parse file '" + htmlOutputFileName - + "':\n" + e1.getLocalizedMessage()); - return; - } - } - } - + public static String xmlInputFileName, htmlOutputFileName; + static SaxFilter saxHandler; + protected static String initializationErrorString = null; + + protected static Element htmlRootElement = null; + + static protected void init (final String xmlInputFileName, + final SaxFilter saxHandler, + final String resultFileExtension) { + SaxFilterTest.xmlInputFileName = xmlInputFileName; + SaxFilterTest.htmlOutputFileName = xmlInputFileName + resultFileExtension; + SaxFilterTest.saxHandler = saxHandler; + + final PrintStream out; + + try { + out = new PrintStream(htmlOutputFileName); + } catch (FileNotFoundException e1) { + initializationErrorString = "Unable to open file '" + htmlOutputFileName + + "' for writing"; + return; + } + saxHandler.setOutputStream(out); + + final XMLReader xmlReader; + try { + final SAXParserFactory saxPf = SAXParserFactory.newInstance(); + final SAXParser saxParser = saxPf.newSAXParser(); + xmlReader = saxParser.getXMLReader(); + + xmlReader.setContentHandler(saxHandler); + xmlReader.setErrorHandler(new StandardErrorHandler()); + + } catch (SAXException | ParserConfigurationException e) { + initializationErrorString = "Unable to create XMLparser instance: " + e.getLocalizedMessage(); + return; + } + try { + xmlReader.parse(xmlInputFileName); + } catch (SAXException | IOException e) { + initializationErrorString = "Unable parse file '" + xmlInputFileName + + "': " + e.getLocalizedMessage(); + return; + } + out.close(); + printResultToStdout(); + + // Now parse the result for further analysis + // + final SAXBuilder parser = new SAXBuilder(); + + try { + htmlRootElement = parser.build(htmlOutputFileName).getRootElement(); + } catch (JDOMException | IOException e1) { + System.err.print( + initializationErrorString = "Unable to parse file '" + htmlOutputFileName + + "':\n" + e1.getLocalizedMessage()); + return; + } + } + @Before public void checkWellFormedness() { Assume.assumeTrue("Previous error:" + initializationErrorString, null == initializationErrorString); //Assert.assertNull("Unable to parse generated file:" + errorInitString, errorInitString); } - private void printResultToStdout() { - try { - BufferedReader saxResult = new BufferedReader(new FileReader( - htmlOutputFileName)); - String line; - while (null != (line = saxResult.readLine())) { - System.out.println(line); - } - saxResult.close(); - } catch (IOException e) { - initializationErrorString = "Unable to open expected result output file '" - + htmlOutputFileName + "' :" + e.getLocalizedMessage(); - } - } + private static void printResultToStdout() { + try { + BufferedReader saxResult = new BufferedReader(new FileReader( + htmlOutputFileName)); + String line; + while (null != (line = saxResult.readLine())) { + System.out.println(line); + } + saxResult.close(); + } catch (IOException e) { + initializationErrorString = "Unable to open expected result output file '" + + htmlOutputFileName + "' :" + e.getLocalizedMessage(); + } + } } \ No newline at end of file diff --git a/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/test/java/de/hdm_stuttgart/de/sda1/saxhtml/v1/test/TestSimpleSaxTransform.java b/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/test/java/de/hdm_stuttgart/de/sda1/saxhtml/v1/test/TestSimpleSaxTransform.java index ffb786ecc1367d5f420dca53f967fe9593e5fd5e..59435bc439766f0442de6d85e5a52d8a295d6a03 100644 --- a/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/test/java/de/hdm_stuttgart/de/sda1/saxhtml/v1/test/TestSimpleSaxTransform.java +++ b/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/test/java/de/hdm_stuttgart/de/sda1/saxhtml/v1/test/TestSimpleSaxTransform.java @@ -7,17 +7,17 @@ import de.hdm_stuttgart.de.mi.exam.testing.SaxFilterTest; import de.hdm_stuttgart.mi.sda1.saxhtml.v1.Memo2HtmlHandler; /** - * Unit testing XML to HTML output + * Unit testing memo to HTML output */ @SuppressWarnings("javadoc") public class TestSimpleSaxTransform extends SaxFilterTest { - public TestSimpleSaxTransform() { - super("src/main/resources/memo.xml", new Memo2HtmlHandler(), ".1.html"); + static { + init("src/main/resources/memo.xml", new Memo2HtmlHandler(), ".1.html"); } @Test public void testSenderInHeader() { - DomAssert.assertSingleNodeContent("<title>Memo message</title> must be the only child of <head>", htmlRootElement, "head/*", "title", "Memo message"); + DomAssert.assertSingleNodeContent("'<title>Memo message</title>' must be the only child of <head>", htmlRootElement, "head/*", "title", "Memo message"); } @Test public void testFrom() { DomAssert.assertSingleNodeContent("<strong>M. Goik</strong> must appear within first <h2> child of <body>", diff --git a/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/test/java/de/hdm_stuttgart/de/sda1/saxhtml/v2/test/TestComplexSaxTransform.java b/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/test/java/de/hdm_stuttgart/de/sda1/saxhtml/v2/test/TestComplexSaxTransform.java index 418be404e24b4e9cf407122e2012deedf9578761..42019426a731ab5323a761084227bdecebea62dd 100644 --- a/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/test/java/de/hdm_stuttgart/de/sda1/saxhtml/v2/test/TestComplexSaxTransform.java +++ b/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/test/java/de/hdm_stuttgart/de/sda1/saxhtml/v2/test/TestComplexSaxTransform.java @@ -13,8 +13,8 @@ import de.hdm_stuttgart.mi.sda1.saxhtml.v2.Memo2HtmlHandler; @SuppressWarnings("javadoc") public class TestComplexSaxTransform extends SaxFilterTest { - public TestComplexSaxTransform() { - super("src/main/resources/memo.xml", new Memo2HtmlHandler(), ".2.html"); + static { + init("src/main/resources/memo.xml", new Memo2HtmlHandler(), ".2.html"); } @Test public void testSenderInHeader() {