From 89ae38eb78c4a27fab854bc96cd52db759cd1921 Mon Sep 17 00:00:00 2001 From: Martin Goik <goik@hdm-stuttgart.de> Date: Tue, 16 Dec 2014 15:15:26 +0100 Subject: [PATCH] Static test initialization --- .../mi/sda1/saxhtml/tools/SaxFilter.java | 8 +- .../mi/sda1/saxhtml/v1/Memo2HtmlHandler.java | 2 +- .../de/mi/exam/testing/SaxFilterTest.java | 153 +++++++++--------- .../v1/test/TestSimpleSaxTransform.java | 8 +- .../v2/test/TestComplexSaxTransform.java | 4 +- 5 files changed, 89 insertions(+), 86 deletions(-) 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 bd105d01f..1c1f8e4ea 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 32b160ac6..01d69f1f4 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 0cfc85502..202c560c4 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 ffb786ecc..59435bc43 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 418be404e..42019426a 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() { -- GitLab