Clover Coverage Report
Coverage timestamp: Fri May 24 2013 13:47:27 UTC
96   204   9   12
0   155   0.09   8
8     1.12  
1    
 
  TextOutputTest       Line # 31 96 9 99% 0.99038464
 
  (5)
 
1    /* This file is part of the project "Hilbert II" - http://www.qedeq.org
2    *
3    * Copyright 2000-2013, 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    import java.io.PrintStream;
22   
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  5 toggle protected void setUp() throws Exception {
65  5 super.setUp();
66    }
67   
 
68  5 toggle protected void tearDown() throws Exception {
69  5 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, "ISO-8859-1");
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.print("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"");
85  1 out.println();
86  1 out.println("xsi:noNamespaceSchemaLocation=\"http://www.qedeq.org/0_01_06/xml/qedeq.xsd\">");
87  1 out.popLevel();
88  1 out.println("<HEADER email=\"mime@qedeq.org\">");
89  1 out.pushLevel();
90  1 out.print("<SPEC name=\"qedeq_sample1\" ruleVersion=\"1.00.00\">");
91  1 out.println();
92  1 out.pushLevel();
93  1 out.println("<LOCATIONS>");
94  1 out.print((Object) (" \t\r <LOCATION value=\"http://qedeq.org/0.01.06/sample1\"/>"));
95  1 out.println();
96  1 out.println("</LOCATIONS>");
97  1 out.popLevel();
98  1 out.println("</SPEC>");
99  1 out.println("<TITLE>");
100  1 out.pushLevel();
101  1 out.println("<LATEX language=\"en\">");
102  1 out.println(" Example1");
103  1 out.println("</LATEX>");
104  1 out.popLevel();
105  1 out.println("</TITLE>");
106  1 out.println("<ABSTRACT>");
107  1 out.pushLevel();
108  1 out.println("<LATEX language=\"en\">");
109  1 out.pushLevel();
110  1 out.println(" 1789.01239In this very first qedeq module the XML specification is demonstrated.");
111  1 out.popLevel();
112  1 out.println("</LATEX>");
113  1 out.popLevel();
114  1 out.println("</ABSTRACT>");
115  1 out.println("<AUTHORS>");
116  1 out.pushLevel();
117  1 out.println("<AUTHOR email=\"michael@meyling.com\">");
118  1 out.pushLevel();
119  1 out.println("<LATEX language=\"de\">");
120  1 out.pushLevel();
121  1 out.println(" <![CDATA[Michael Meyling]]>");
122  1 out.popLevel();
123  1 out.println("</LATEX>");
124  1 out.popLevel();
125  1 out.println("</AUTHOR>");
126  1 out.popLevel();
127  1 out.println("</AUTHORS>");
128  1 out.println((Object) " </HEADER>");
129  1 out.pushLevel();
130  1 out.clearLevel();
131  1 out.println("</QEDEQ>");
132  1 out.close();
133   
134    // System.out.println(to.toString());
135  1 assertEquals(XML_DATA, to.toString());
136   
137  1 assertEquals(StringUtility.string2Hex(XML_DATA, "ISO-8859-1"),
138    StringUtility.byte2Hex(to.toByteArray()));
139    }
140   
 
141  1 toggle public void testPushPop() throws Exception {
142  1 final ByteArrayOutputStream to = new ByteArrayOutputStream();
143  1 final TextOutput out = new TextOutput("jumper", to, "UTF-8");
144  1 out.popLevel();
145  1 out.popLevel();
146  1 out.print("James Bond");
147  1 assertEquals("James Bond", to.toString("UTF-8"));
148    }
149   
150    /**
151    * Test IO error handling.
152    *
153    * @throws Exception Test failed.
154    */
 
155  1 toggle public void testErrorStream() throws Exception {
156  1 final OutputStream to = new OutputStream() {
 
157  8 toggle public void write(final int i) throws IOException {
158  8 throw new IOException("i have got you");
159    }
160    };
161  1 TextOutput out = new TextOutput("flying toasters", to, "UTF-8");
162  1 assertEquals("flying toasters", out.getName());
163  1 assertFalse(out.checkError());
164  1 assertNull(out.getError());
165  1 out.flush();
166  1 assertFalse(out.checkError());
167  1 out.println("i am not written");
168  1 out.flush();
169  1 assertTrue(out.checkError());
170    // the message is still hard coded in TextOutput
171  1 assertEquals("Writing failed.", out.getError().getMessage());
172   
173    }
174   
 
175  1 toggle public void testConstructorAndGetName() throws Exception {
176  1 final ByteArrayOutputStream to = new ByteArrayOutputStream();
177  1 final TextOutput out = new TextOutput("jumper", to, "UTF-8");
178  1 assertEquals("jumper", out.getName());
179  1 assertFalse(out.checkError());
180  1 assertNull(out.getError());
181  1 try {
182  1 new TextOutput("jumper", to, "i do not exist");
183  0 fail("Exception expected");
184    } catch (RuntimeException e) {
185    // expected
186    }
187  1 final TextOutput out2 = new TextOutput("jumper", new PrintStream(to));
188  1 assertEquals("jumper", out2.getName());
189  1 assertFalse(out2.checkError());
190  1 assertNull(out2.getError());
191    }
192   
 
193  1 toggle public void testGetPosition() throws Exception {
194  1 final ByteArrayOutputStream to = new ByteArrayOutputStream();
195  1 final TextOutput out = new TextOutput("heaven", to, "UTF-16");
196  1 assertEquals(0, out.getPosition());
197  1 out.append("1234567890");
198  1 assertEquals(10, out.getPosition());
199  1 out.print("hi");
200  1 assertEquals(12, out.getPosition());
201    }
202   
203    }
204