Clover Coverage Report
Coverage timestamp: Fri May 24 2013 13:47:27 UTC
76   192   14   5.43
0   137   0.18   14
14     1  
1    
 
  XPathLocationFinderTest       Line # 29 76 14 100% 1.0
 
  (9)
 
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.kernel.xml.tracker;
17   
18    import java.io.ByteArrayOutputStream;
19    import java.io.File;
20    import java.io.PrintStream;
21   
22    import org.qedeq.base.test.QedeqTestCase;
23   
24    /**
25    * Test {@link org.qedeq.kernel.xml.tracker.XPathLocationFinder}.
26    *
27    * @author Michael Meyling
28    */
 
29    public class XPathLocationFinderTest extends QedeqTestCase {
30   
 
31  9 toggle protected void setUp() throws Exception {
32  9 super.setUp();
33    }
34   
 
35  9 toggle protected void tearDown() throws Exception {
36  9 super.tearDown();
37    }
38   
39    /**
40    * Test {@link XPathLocationFinder}.
41    *
42    * @throws Exception Test failed.
43    */
 
44  1 toggle public final void testGetXPathLocation() throws Exception {
45  1 checkPosition("xpathLocationFinder.xml",
46    "/QEDEQ",
47    2, 1, 304, 9);
48  1 checkPosition("xpathLocationFinder.xml",
49    "/QEDEQ[1]",
50    2, 1, 304, 9);
51  1 checkPosition("xpathLocationFinder.xml",
52    "/QEDEQ/CHAPTER/SECTION/NODE",
53    100, 7, 139, 14);
54  1 checkPosition("xpathLocationFinder.xml",
55    "/QEDEQ/CHAPTER[1]/SECTION/NODE[1]",
56    100, 7, 139, 14);
57  1 checkPosition("xpathLocationFinder.xml",
58    "/QEDEQ/CHAPTER/SECTION/NODE@label",
59    100, 13, 100, 23);
60  1 checkPosition("xpathLocationFinder.xml",
61    "/QEDEQ/CHAPTER/SECTION/NODE[2]",
62    140, 7, 212, 14);
63  1 checkPosition("xpathLocationFinder.xml",
64    "/QEDEQ/CHAPTER/SECTION/NODE[2]/AXIOM/FORMULA/FORALL/FORALL/IMPL/FORALL/VAR@id",
65    165, 26, 165, 32);
66    }
67   
 
68  1 toggle public void testHelp() throws Exception {
69  1 String result = executeError(new String[] {"-h"});
70  1 assertTrue(0 <= result.indexOf("Synopsis"));
71  1 assertTrue(0 <= result.indexOf("Description"));
72  1 assertTrue(0 <= result.indexOf("Options and Parameters"));
73  1 assertTrue(0 <= result.indexOf("Example"));
74  1 assertTrue(0 > result.indexOf("Unknown option"));
75    }
76   
 
77  1 toggle public void testUnknownOption() throws Exception {
78  1 String result = executeError(new String[] {"-hs"});
79  1 assertTrue(0 <= result.indexOf("Synopsis"));
80  1 assertTrue(0 <= result.indexOf("Description"));
81  1 assertTrue(0 <= result.indexOf("Options and Parameters"));
82  1 assertTrue(0 <= result.indexOf("Example"));
83  1 assertTrue(0 <= result.indexOf("Unknown option"));
84    }
85   
 
86  1 toggle public void testNoXPath() throws Exception {
87  1 String result = executeError(new String[] {"test.xml"});
88  1 assertTrue(0 <= result.indexOf("Synopsis"));
89  1 assertTrue(0 <= result.indexOf("Description"));
90  1 assertTrue(0 <= result.indexOf("Options and Parameters"));
91  1 assertTrue(0 <= result.indexOf("Example"));
92  1 assertTrue(0 > result.indexOf("Unknown option"));
93  1 assertTrue(0 <= result.indexOf("XPath must be specified"));
94    }
95   
 
96  1 toggle public void testFileNotSpecified() throws Exception {
97  1 String result = executeError(new String[] {"-xp", "none"});
98  1 assertTrue(0 <= result.indexOf("Synopsis"));
99  1 assertTrue(0 <= result.indexOf("Description"));
100  1 assertTrue(0 <= result.indexOf("Options and Parameters"));
101  1 assertTrue(0 <= result.indexOf("Example"));
102  1 assertTrue(0 > result.indexOf("Unknown option"));
103  1 assertTrue(0 <= result.indexOf("XML file must be specified"));
104    }
105   
 
106  1 toggle public void testXPathNotOk() throws Exception {
107  1 String result = executeError(new String[] {"-xp", "none",
108    getFile("xpathLocationFinder.xml").toString()});
109  1 assertTrue(0 <= result.indexOf("XPath must start with '/'"));
110    }
111   
 
112  1 toggle public void testXPathNotOk2() throws Exception {
113  1 String result = executeError(new String[] {"-xp", "",
114    getFile("xpathLocationFinder.xml").toString()});
115  1 assertTrue(0 <= result.indexOf("XPath must not be empty"));
116    }
117   
 
118  1 toggle public void testTagNotFound() throws Exception {
119  1 final boolean locationFailures = "true".equalsIgnoreCase(
120    System.getProperty("qedeq.test.xmlLocationFailures", "false"));
121  1 System.setProperty("qedeq.test.xmlLocationFailures", "false");
122  1 String result = execute(new String[] {"-xp", "/none",
123    getFile("xpathLocationFinder.xml").toString()});
124  1 try {
125  1 assertTrue(result.endsWith(":1:1:1:1"));
126    } finally {
127  1 System.setProperty("qedeq.test.xmlLocationFailures",
128    Boolean.toString(locationFailures));
129    }
130    }
131   
 
132  1 toggle public void testFileNotOk() throws Exception {
133  1 executeError(new String[] {"-xp", "/none", "none"});
134    // LATER 20130413 m31: this should be a readable error!
135    }
136   
137    /**
138    * @param fileName Test this XML file.
139    * @param xpath XPath to find.
140    * @param startRow Expected resulting starting line.
141    * @param startCol Expected resulting starting column.
142    * @param endRow Expected resulting ending line.
143    * @param endCol Expected resulting ending column.
144    * @throws Exception Test failed.
145    */
 
146  7 toggle private void checkPosition(String fileName, String xpath, int startRow, int startCol,
147    int endRow, int endCol) throws Exception {
148  7 final File file = getFile(fileName);
149    // final SourceArea result = XPathLocationFinder.findSourceArea(file, new SimpleXPath(xpath));
150  7 final String[] args = new String[3];
151  7 args[0] = "-xp";
152  7 args[1] = xpath;
153  7 args[2] = getFile(fileName).toString();
154  7 final String result = execute(args);
155  7 assertTrue(result.endsWith(":" + startRow + ":" + startCol + ":" + endRow + ":" + endCol));
156    }
157   
 
158  8 toggle private String execute(final String[] args) throws Exception {
159  8 final ByteArrayOutputStream byteOutput = new ByteArrayOutputStream();
160  8 final PrintStream p = new PrintStream(byteOutput);
161  8 final PrintStream old = System.out;
162  8 System.setOut(p);
163  8 try {
164  8 XPathLocationFinder.main(args);
165  8 p.flush();
166  8 final String result = byteOutput.toString();
167  8 return result.substring(result.indexOf("\n")).trim();
168    } finally {
169  8 System.setOut(old);
170    }
171    }
172   
 
173  7 toggle private String executeError(final String[] args) throws Exception {
174  7 final ByteArrayOutputStream byteOutput = new ByteArrayOutputStream();
175  7 final PrintStream p = new PrintStream(byteOutput);
176  7 final PrintStream old1 = System.out;
177  7 final PrintStream old2 = System.err;
178  7 System.setOut(p);
179  7 System.setErr(p);
180  7 try {
181  7 XPathLocationFinder.main(args);
182  7 p.flush();
183  7 final String result = byteOutput.toString();
184  7 return result;
185    } finally {
186  7 System.setOut(old1);
187  7 System.setErr(old2);
188    // System.out.println(byteOutput.toString());
189    }
190    }
191   
192    }