Clover Coverage Report
Coverage timestamp: Sat Sep 18 2010 04:09:52 UTC
41   126   9   8.2
6   76   0.22   5
5     1.8  
1    
 
  CharsetParserTest       Line # 36 41 9 96.2% 0.96153843
 
  (2)
 
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    package org.qedeq.kernel.xml.parser;
16   
17    import java.io.IOException;
18    import java.util.ArrayList;
19    import java.util.List;
20   
21    import org.qedeq.base.io.TextInput;
22    import org.qedeq.base.test.QedeqTestCase;
23    import org.qedeq.base.utility.StringUtility;
24    import org.qedeq.kernel.bo.context.KernelContext;
25    import org.qedeq.kernel.bo.test.KernelFacade;
26    import org.qedeq.kernel.common.ModuleAddress;
27    import org.qedeq.kernel.common.SourceFileException;
28    import org.qedeq.kernel.common.SourceFileExceptionList;
29   
30    /**
31    * Tests correct charset handling by {@link SaxParser},
32    * {@link org.qedeq.kernel.xml.parser.SaxDefaultHandler} (and higher level classes).
33    *
34    * @author Michael Meyling
35    */
 
36    public class CharsetParserTest extends QedeqTestCase {
37   
 
38  2 toggle protected void setUp() throws Exception {
39  2 super.setUp();
40  2 KernelFacade.startup();
41    }
42   
 
43  2 toggle protected void tearDown() throws Exception {
44  2 KernelFacade.shutdown();
45  2 super.tearDown();
46    }
47   
48    /**
49    * Test parsing with default SAX parser.
50    *
51    * @throws Exception Something bad happened.
52    */
 
53  1 toggle public void testParse1() throws Exception {
54  1 final ModuleAddress address = KernelContext.getInstance()
55    .getModuleAddress(getFile("charset/qedeq_utf8_with_errors_01.xml"));
56  1 KernelContext.getInstance().loadModule(address);
57  1 assertFalse(KernelContext.getInstance().checkModule(address));
58  1 final String[] errors = getSourceFileExceptionList(address);
59    // for (int i = 0; i < errors.length; i++) {
60    // System.out.println(errors[i]);
61    // }
62  1 assertEquals(2, errors.length);
63  1 String[] lines = errors[0].split("\n");
64  1 assertTrue(lines[0].endsWith(":105:19"));
65  1 assertTrue(lines[1].endsWith("\"\u00e4\u00f6\u00fc\u00c4\u00d6\u00dc\u00df\u00e8\u00e9"
66    + "\u00ea\u00eb\u00c8\u00c9\u00ca\u00cb\u20ac\" [2]"));
67    }
68   
69    /**
70    * Test parsing with default SAX parser.
71    *
72    * @throws Exception Something bad happened.
73    */
 
74  1 toggle public void testParse2() throws Exception {
75  1 final ModuleAddress address = KernelContext.getInstance()
76    .getModuleAddress(getFile("charset/qedeq_utf16_with_errors_01.xml"));
77  1 KernelContext.getInstance().loadModule(address);
78  1 assertFalse(KernelContext.getInstance().checkModule(address));
79  1 final String[] errors = getSourceFileExceptionList(address);
80  1 assertEquals(2, errors.length);
81  1 String[] lines = errors[0].split("\n");
82  1 assertTrue(lines[0].endsWith(":105:19"));
83  1 assertTrue(lines[1].endsWith("\"\u00e4\u00f6\u00fc\u00c4\u00d6\u00dc\u00df\u00e8\u00e9"
84    + "\u00ea\u00eb\u00c8\u00c9\u00ca\u00cb\u20ac\" [2]"));
85    }
86   
87    /**
88    * Get description of source file exception list.
89    *
90    * @param address Get description for this module exceptions.
91    * @return Error description and location.
92    * @throws IOException Reading failed.
93    */
 
94  2 toggle public String[] getSourceFileExceptionList(final ModuleAddress address) throws IOException {
95  2 final List list = new ArrayList();
96  2 final SourceFileExceptionList sfl = KernelContext.getInstance().getQedeqBo(address)
97    .getErrors();
98  2 if (sfl != null) {
99  2 final StringBuffer buffer
100    = new StringBuffer(KernelContext.getInstance().getSource(address));
101  2 final TextInput input = new TextInput(buffer);
102  2 input.setPosition(0);
103  2 final StringBuffer buf = new StringBuffer();
104  6 for (int i = 0; i < sfl.size(); i++) {
105  4 buf.setLength(0);
106  4 final SourceFileException sf = sfl.get(i);
107  4 buf.append(sf.getDescription());
108  4 if (sf.getSourceArea() != null && sf.getSourceArea().getStartPosition()
109    != null) {
110  4 buf.append("\n");
111  4 input.setRow(sf.getSourceArea().getStartPosition().getRow());
112  4 buf.append(StringUtility.replace(input.getLine(), "\t", " "));
113  4 buf.append("\n");
114  4 final StringBuffer whitespace = StringUtility.getSpaces(
115    sf.getSourceArea().getStartPosition().getColumn() - 1);
116  4 buffer.append(whitespace);
117  4 buffer.append("^");
118    }
119  4 list.add(buf.toString());
120    }
121    }
122  2 return (String[]) list.toArray(new String[]{});
123    }
124   
125   
126    }