Clover Coverage Report
Coverage timestamp: Sat Sep 18 2010 04:09:52 UTC
74   173   6   12.33
0   129   0.08   6
6     1  
1    
 
  TextOutputTest       Line # 31 74 6 100% 1.0
 
  (3)
 
1    /* This file is part of the project "Hilbert II" - http://www.qedeq.org
2    *
3    * Copyright 2000-2010, Michael Meyling <mime@qedeq.org>.
4    *
5    * "Hilbert II" is free software; you can redistribute
6    * it and/or modify it under the terms of the GNU General Public
7    * License as published by the Free Software Foundation; either
8    * version 2 of the License, or (at your option) any later version.
9    *
10    * This program is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13    * GNU General Public License for more details.
14    */
15   
16    package org.qedeq.base.io;
17   
18    import java.io.ByteArrayOutputStream;
19    import java.io.IOException;
20    import java.io.OutputStream;
21   
22    import org.apache.commons.lang.SystemUtils;
23    import org.qedeq.base.test.QedeqTestCase;
24    import org.qedeq.base.utility.StringUtility;
25   
26    /**
27    * Test {@link TextOutput}.
28    *
29    * @author Michael Meyling
30    */
 
31    public class TextOutputTest extends QedeqTestCase {
32   
33    private static final String XML_DATA =
34    /* 1 */ "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\012"
35    /* 2 */ + "<QEDEQ \012"
36    /* 3 */ + " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"
37    /* 4 */ + " xsi:noNamespaceSchemaLocation=\"http://www.qedeq.org/0_01_06/xml/qedeq.xsd\">\n"
38    /* 5 */ + " <HEADER email=\"mime@qedeq.org\">\n"
39    /* 6 */ + " <SPEC name=\"qedeq_sample1\" ruleVersion=\"1.00.00\">\n"
40    /* 7 */ + " <LOCATIONS>\n"
41    /* 8 */ + " \t\r <LOCATION value=\"http://qedeq.org/0.01.06/sample1\"/>\n"
42    /* 9 */ + " </LOCATIONS>\n"
43    /* 10 */ + " </SPEC>\n"
44    /* 11 */ + " <TITLE>\n"
45    /* 12 */ + " <LATEX language=\"en\">\n"
46    /* 13 */ + " Example1\n"
47    /* 14 */ + " </LATEX>\n"
48    /* 15 */ + " </TITLE>\n"
49    /* 16 */ + " <ABSTRACT>\n"
50    /* 17 */ + " <LATEX language=\"en\">\n"
51    /* 18 */ + " 1789.01239In this very first qedeq module the XML specification is demonstrated.\n"
52    /* 19 */ + " </LATEX>\n"
53    /* 20 */ + " </ABSTRACT>\n"
54    /* 21 */ + " <AUTHORS>\n"
55    /* 22 */ + " <AUTHOR email=\"michael@meyling.com\">\n"
56    /* 23 */ + " <LATEX language=\"de\">\n"
57    /* 24 */ + " <![CDATA[Michael Meyling]]>\n"
58    /* 25 */ + " </LATEX>\n"
59    /* 26 */ + " </AUTHOR>\n"
60    /* 27 */ + " </AUTHORS>\n"
61    /* 28 */ + " </HEADER>\n"
62    /* 29 */ + "</QEDEQ>\n";
63   
 
64  3 toggle protected void setUp() throws Exception {
65  3 super.setUp();
66    }
67   
 
68  3 toggle protected void tearDown() throws Exception {
69  3 super.tearDown();
70    }
71   
72    /**
73    * Test various print methods.
74    *
75    * @throws Exception Test failed.
76    */
 
77  1 toggle public void testComplete() throws Exception {
78  1 final ByteArrayOutputStream to = new ByteArrayOutputStream();
79  1 final TextOutput out = new TextOutput("flying toasters", to);
80  1 out.println("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>");
81  1 out.println("<QEDEQ ");
82  1 out.pushLevel();
83  1 out.pushLevel();
84  1 out.levelPrint("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"");
85  1 out.println();
86  1 out.levelPrintln("xsi:noNamespaceSchemaLocation=\"http://www.qedeq.org/0_01_06/xml/qedeq.xsd\">");
87  1 out.popLevel();
88  1 out.levelPrintln("<HEADER email=\"mime@qedeq.org\">");
89  1 out.pushLevel();
90  1 out.levelPrint("<SPEC name=\"qedeq_sample1\" ruleVersion=\"1.00.00\">");
91  1 out.print(SystemUtils.LINE_SEPARATOR);
92  1 out.pushLevel();
93  1 out.levelPrintln("<LOCATIONS>");
94  1 out.print((Object) (" \t\r <LOCATION value=\"http://qedeq.org/0.01.06/sample1\"/>"
95    + SystemUtils.LINE_SEPARATOR));
96  1 out.levelPrintln("</LOCATIONS>");
97  1 out.popLevel();
98  1 out.levelPrintln("</SPEC>");
99  1 out.levelPrintln("<TITLE>");
100  1 out.pushLevel();
101  1 out.levelPrintln("<LATEX language=\"en\">");
102  1 out.levelPrintln(" Example1");
103  1 out.levelPrintln("</LATEX>");
104  1 out.popLevel();
105  1 out.levelPrintln("</TITLE>");
106  1 out.levelPrintln("<ABSTRACT>");
107  1 out.pushLevel();
108  1 out.levelPrintln("<LATEX language=\"en\">");
109  1 out.pushLevel();
110  1 out.levelPrintln(" 1789.01239In this very first qedeq module the XML specification is demonstrated.");
111  1 out.popLevel();
112  1 out.levelPrintln("</LATEX>");
113  1 out.popLevel();
114  1 out.levelPrintln("</ABSTRACT>");
115  1 out.levelPrintln("<AUTHORS>");
116  1 out.pushLevel();
117  1 out.levelPrintln("<AUTHOR email=\"michael@meyling.com\">");
118  1 out.pushLevel();
119  1 out.levelPrintln("<LATEX language=\"de\">");
120  1 out.pushLevel();
121  1 out.levelPrintln(" <![CDATA[Michael Meyling]]>");
122  1 out.popLevel();
123  1 out.levelPrintln("</LATEX>");
124  1 out.popLevel();
125  1 out.levelPrintln("</AUTHOR>");
126  1 out.popLevel();
127  1 out.levelPrintln("</AUTHORS>");
128  1 out.println((Object) " </HEADER>");
129  1 out.pushLevel();
130  1 out.clearLevel();
131  1 out.levelPrintln("</QEDEQ>");
132  1 out.close();
133   
134    // System.out.println(to.toString());
135  1 assertEquals(StringUtility.string2Hex(StringUtility.replace(XML_DATA,
136    "\n", SystemUtils.LINE_SEPARATOR), "ISO-8859-1"),
137    StringUtility.byte2Hex(to.toByteArray()));
138    }
139   
 
140  1 toggle public void testPushPop() throws Exception {
141  1 final ByteArrayOutputStream to = new ByteArrayOutputStream();
142  1 final TextOutput out = new TextOutput("jumper", to);
143  1 out.popLevel();
144  1 out.popLevel();
145  1 out.levelPrint("James Bond");
146  1 assertEquals("James Bond", to.toString("UTF-8"));
147    }
148   
149    /**
150    * Test IO error handling.
151    *
152    * @throws Exception Test failed.
153    */
 
154  1 toggle public void testErrorStream() throws Exception {
155  1 final OutputStream to = new OutputStream() {
 
156  2 toggle public void write(int i) throws IOException {
157  2 throw new IOException("i have got you");
158    }
159    };
160  1 TextOutput out = new TextOutput("flying toasters", to);
161  1 assertFalse(out.checkError());
162  1 assertNull(out.getError());
163  1 out.flush();
164  1 assertFalse(out.checkError());
165  1 out.println("i am not written");
166  1 out.flush();
167  1 assertTrue(out.checkError());
168    // the message is still hard coded in TextOutput
169  1 assertEquals("Writing failed.", out.getError().getMessage());
170   
171    }
172    }
173