Clover Coverage Report
Coverage timestamp: Sa Aug 2 2008 13:56:27 CEST
42   129   10   8,4
8   78   0,24   5
5     2  
1    
 
  CharsetParserTest       Line # 39 42 10 96,4% 0.96363634
 
  (2)
 
1    /* $Id: CharsetParserTest.java,v 1.1 2008/07/26 08:01:08 m31 Exp $
2    *
3    * This file is part of the project "Hilbert II" - http://www.qedeq.org
4    *
5    * Copyright 2000-2008, Michael Meyling <mime@qedeq.org>.
6    *
7    * "Hilbert II" is free software; you can redistribute
8    * it and/or modify it under the terms of the GNU General Public
9    * License as published by the Free Software Foundation; either
10    * version 2 of the License, or (at your option) any later version.
11    *
12    * This program is distributed in the hope that it will be useful,
13    * but WITHOUT ANY WARRANTY; without even the implied warranty of
14    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15    * GNU General Public License for more details.
16    */
17    package org.qedeq.kernel.xml.parser;
18   
19    import java.io.IOException;
20    import java.util.ArrayList;
21    import java.util.List;
22   
23    import org.qedeq.base.io.TextInput;
24    import org.qedeq.base.test.QedeqTestCase;
25    import org.qedeq.base.utility.StringUtility;
26    import org.qedeq.kernel.bo.context.KernelContext;
27    import org.qedeq.kernel.bo.test.KernelFacade;
28    import org.qedeq.kernel.common.ModuleAddress;
29    import org.qedeq.kernel.common.SourceFileException;
30    import org.qedeq.kernel.common.SourceFileExceptionList;
31   
32    /**
33    * Tests correct charset handling by {@link SaxParser},
34    * {@link org.qedeq.kernel.xml.parser.SaxDefaultHandler} (and higher level classes).
35    *
36    * @version $Revision: 1.1 $
37    * @author Michael Meyling
38    */
 
39    public class CharsetParserTest extends QedeqTestCase {
40   
 
41  2 toggle protected void setUp() throws Exception {
42  2 super.setUp();
43  2 KernelFacade.startup();
44    }
45   
 
46  2 toggle protected void tearDown() throws Exception {
47  2 KernelFacade.shutdown();
48  2 super.tearDown();
49    }
50   
51    /**
52    * Test parsing with default SAX parser.
53    *
54    * @throws Exception Something bad happened.
55    */
 
56  1 toggle public void testParse1() throws Exception {
57  1 final ModuleAddress address = KernelContext.getInstance()
58    .getModuleAddress(getFile("charset/qedeq_utf8_with_errors_01.xml"));
59  1 KernelContext.getInstance().loadModule(address);
60  1 assertFalse(KernelContext.getInstance().checkModule(address));
61  1 final String[] errors = getSourceFileExceptionList(address);
62  3 for (int i = 0; i < errors.length; i++) {
63    // System.out.println(errors[i]);
64    }
65  1 assertEquals(2, errors.length);
66  1 String[] lines = errors[0].split("\n");
67  1 assertTrue(lines[0].endsWith("\"\u00e4\u00f6\u00fc\u00c4\u00d6\u00dc\u00df\u00e8\u00e9"
68    + "\u00ea\u00eb\u00c8\u00c9\u00ca\u00cb\u20ac\" [2]"));
69  1 assertTrue(lines[1].endsWith(":105:19"));
70    }
71   
72    /**
73    * Test parsing with default SAX parser.
74    *
75    * @throws Exception Something bad happened.
76    */
 
77  1 toggle public void testParse2() throws Exception {
78  1 final ModuleAddress address = KernelContext.getInstance()
79    .getModuleAddress(getFile("charset/qedeq_utf16_with_errors_01.xml"));
80  1 KernelContext.getInstance().loadModule(address);
81  1 assertFalse(KernelContext.getInstance().checkModule(address));
82  1 final String[] errors = getSourceFileExceptionList(address);
83  1 assertEquals(2, errors.length);
84  1 String[] lines = errors[0].split("\n");
85  1 assertTrue(lines[0].endsWith("\"\u00e4\u00f6\u00fc\u00c4\u00d6\u00dc\u00df\u00e8\u00e9"
86    + "\u00ea\u00eb\u00c8\u00c9\u00ca\u00cb\u20ac\" [2]"));
87  1 assertTrue(lines[1].endsWith(":105:19"));
88    }
89   
90    /**
91    * Get description of source file exception list.
92    *
93    * @param address Get description for this module exceptions.
94    * @return Error description and location.
95    * @throws IOException Reading failed.
96    */
 
97  2 toggle public String[] getSourceFileExceptionList(final ModuleAddress address) throws IOException {
98  2 final List list = new ArrayList();
99  2 final SourceFileExceptionList sfl = KernelContext.getInstance().getQedeqBo(address)
100    .getException();
101  2 if (sfl != null) {
102  2 final StringBuffer buffer
103    = new StringBuffer(KernelContext.getInstance().getSource(address));
104  2 final TextInput input = new TextInput(buffer);
105  2 input.setPosition(0);
106  2 final StringBuffer buf = new StringBuffer();
107  6 for (int i = 0; i < sfl.size(); i++) {
108  4 buf.setLength(0);
109  4 final SourceFileException sf = sfl.get(i);
110  4 buf.append(sf.getDescription());
111  4 if (sf.getSourceArea() != null && sf.getSourceArea().getStartPosition()
112    != null) {
113  4 buf.append("\n");
114  4 input.setRow(sf.getSourceArea().getStartPosition().getLine());
115  4 buf.append(StringUtility.replace(input.getLine(), "\t", " "));
116  4 buf.append("\n");
117  4 final StringBuffer whitespace = StringUtility.getSpaces(
118    sf.getSourceArea().getStartPosition().getColumn() - 1);
119  4 buffer.append(whitespace);
120  4 buffer.append("^");
121    }
122  4 list.add(buf.toString());
123    }
124    }
125  2 return (String[]) list.toArray(new String[]{});
126    }
127   
128   
129    }