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