Clover Coverage Report
Coverage timestamp: Fri Feb 14 2014 07:28:57 UTC
226   421   32   16.14
16   321   0.14   14
14     2.29  
1    
 
  GenerateLatexTest       Line # 50 226 32 93% 0.9296875
 
  (20)
 
1    /* This file is part of the project "Hilbert II" - http://www.qedeq.org
2    *
3    * Copyright 2000-2014, 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.bo.service.latex;
17   
18    import java.io.File;
19    import java.io.IOException;
20    import java.io.InputStream;
21    import java.util.HashMap;
22    import java.util.Map;
23   
24    import org.qedeq.base.io.IoUtility;
25    import org.qedeq.base.io.Parameters;
26    import org.qedeq.base.io.UrlUtility;
27    import org.qedeq.base.trace.Trace;
28    import org.qedeq.kernel.bo.common.QedeqBo;
29    import org.qedeq.kernel.bo.job.InternalServiceJobImpl;
30    import org.qedeq.kernel.bo.job.InternalModuleServiceCallImpl;
31    import org.qedeq.kernel.bo.logic.common.LogicalCheckException;
32    import org.qedeq.kernel.bo.module.InternalModuleServiceCall;
33    import org.qedeq.kernel.bo.module.KernelQedeqBo;
34    import org.qedeq.kernel.bo.service.internal.ModuleArbiterImpl;
35    import org.qedeq.kernel.bo.test.DummyPlugin;
36    import org.qedeq.kernel.bo.test.QedeqBoTestCase;
37    import org.qedeq.kernel.se.common.DefaultModuleAddress;
38    import org.qedeq.kernel.se.common.ModuleAddress;
39    import org.qedeq.kernel.se.common.ModuleDataException;
40    import org.qedeq.kernel.se.common.SourceFileException;
41    import org.qedeq.kernel.se.common.SourceFileExceptionList;
42    import org.qedeq.kernel.se.visitor.InterruptException;
43    import org.xml.sax.SAXParseException;
44   
45    /**
46    * Test generating LaTeX files for all known samples.
47    *
48    * @author Michael Meyling
49    */
 
50    public class GenerateLatexTest extends QedeqBoTestCase {
51   
52    /** This class. */
53    private static final Class CLASS = GenerateLatexTest.class;
54    private InternalModuleServiceCall call;
55   
 
56  20 toggle protected void tearDown() throws Exception {
57  20 endServiceCall(call);
58  20 super.tearDown();
59    }
60   
61    /**
62    * Generate main documents.
63    *
64    * @throws Exception
65    */
 
66  2 toggle public void testGeneration() throws Exception {
67    // System.setProperty("qedeq.test.xmlLocationFailures", Boolean.TRUE.toString());
68    // try {
69  2 generate(getDocDir(), "math/qedeq_logic_v1.xml", getGenDir(), false, false);
70  2 generate(getDocDir(), "math/qedeq_set_theory_v1.xml", getGenDir(), false, false);
71  2 generate(getDocDir(), "math/qedeq_formal_logic_v1.xml", getGenDir(), false, false);
72  2 generate(getDocDir(), "sample/qedeq_sample1.xml", getGenDir(), false, false);
73  2 generate(getDocDir(), "sample/qedeq_sample2.xml", getGenDir(), false, false);
74  2 generate(getDocDir(), "sample/qedeq_sample3.xml", getGenDir(), false, false);
75  2 generate(getDocDir(), "project/qedeq_basic_concept.xml", getGenDir(), false, false);
76  2 generate(getDocDir(), "project/qedeq_logic_language.xml", getGenDir(), true, false);
77  2 generate(getDocDir(), "project/qedeq_development_concept.xml", getGenDir(), true, false);
78    // } finally {
79    // System.setProperty("qedeq.test.xmlLocationFailures", Boolean.FALSE.toString());
80    // }
81    }
82   
83    /**
84    * Generate some example documents.
85    *
86    * @throws Exception
87    */
 
88  2 toggle public void testGeneration2() throws Exception {
89    // System.setProperty("qedeq.test.xmlLocationFailures", Boolean.TRUE.toString());
90  2 generate(getIndir(), "proof/proof_001.xml", getOutdir(), true, false);
91  2 generate(getIndir(), "proof/proof_002.xml", getOutdir(), true, true);
92  2 generate(getIndir(), "proof/proof_003.xml", getOutdir(), true, false);
93    // System.setProperty("qedeq.test.xmlLocationFailures", Boolean.FALSE.toString());
94    }
95   
96    /**
97    * Generate some example documents.
98    *
99    * @throws Exception
100    */
 
101  2 toggle public void testNegativeGeneration2() throws Exception {
102    // System.setProperty("qedeq.test.xmlLocationFailures", Boolean.TRUE.toString());
103  2 try {
104  2 generate(getIndir(), "proof/proof_002.xml", getOutdir(), true, false);
105    } catch (SourceFileExceptionList list) {
106  2 SourceFileExceptionList ex = list;
107  2 assertEquals(8, ex.size());
108  2 SourceFileException e0 = ex.get(0);
109  2 assertTrue(e0.getCause() instanceof ModuleDataException);
110  2 assertEquals(6100017, e0.getErrorCode());
111  2 assertEquals(550, e0.getSourceArea().getStartPosition().getRow());
112  2 assertEquals(24, e0.getSourceArea().getStartPosition().getColumn());
113  2 SourceFileException e1 = ex.get(1);
114  2 assertTrue(e1.getCause() instanceof ModuleDataException);
115  2 assertEquals(610007, e1.getErrorCode());
116  2 assertEquals(572, e1.getSourceArea().getStartPosition().getRow());
117  2 assertEquals(24, e1.getSourceArea().getStartPosition().getColumn());
118  2 SourceFileException e2 = ex.get(2);
119  2 assertTrue(e2.getCause() instanceof ModuleDataException);
120  2 assertEquals(610007, e2.getErrorCode());
121  2 assertEquals(714, e2.getSourceArea().getStartPosition().getRow());
122  2 assertEquals(23, e2.getSourceArea().getStartPosition().getColumn());
123  2 SourceFileException e3 = ex.get(3);
124  2 assertTrue(e3.getCause() instanceof ModuleDataException);
125  2 assertEquals(610007, e3.getErrorCode());
126  2 assertEquals(1020, e3.getSourceArea().getStartPosition().getRow());
127  2 assertEquals(24, e3.getSourceArea().getStartPosition().getColumn());
128  2 SourceFileException e4 = ex.get(4);
129  2 assertTrue(e4.getCause() instanceof ModuleDataException);
130  2 assertEquals(610007, e4.getErrorCode());
131  2 assertEquals(1080, e4.getSourceArea().getStartPosition().getRow());
132  2 assertEquals(34, e4.getSourceArea().getStartPosition().getColumn());
133  2 SourceFileException e5 = ex.get(5);
134  2 assertTrue(e5.getCause() instanceof ModuleDataException);
135  2 assertEquals(610011, e5.getErrorCode());
136  2 assertEquals(1734, e5.getSourceArea().getStartPosition().getRow());
137  2 assertEquals(34, e5.getSourceArea().getStartPosition().getColumn());
138  2 SourceFileException e6 = ex.get(6);
139  2 assertTrue(e5.getCause() instanceof ModuleDataException);
140  2 assertEquals(610011, e6.getErrorCode());
141  2 assertEquals(1843, e6.getSourceArea().getStartPosition().getRow());
142  2 assertEquals(23, e6.getSourceArea().getStartPosition().getColumn());
143  2 SourceFileException e7 = ex.get(7);
144  2 assertTrue(e7.getCause() instanceof ModuleDataException);
145  2 assertEquals(610011, e7.getErrorCode());
146  2 assertEquals(1843, e7.getSourceArea().getStartPosition().getRow());
147  2 assertEquals(37, e7.getSourceArea().getStartPosition().getColumn());
148    // } finally {
149    // System.setProperty("qedeq.test.xmlLocationFailures", Boolean.FALSE.toString());
150    }
151    }
152   
 
153  2 toggle public void testNegative02() throws Exception {
154  2 try {
155  2 generate(getIndir(), "qedeq_error_sample_12.xml", "de", new File(getOutdir(), "null"), false);
156  0 fail("IllegalModuleDataException expected");
157    } catch (SourceFileExceptionList list) {
158  2 SourceFileExceptionList ex = list;
159  2 assertEquals(1, ex.size());
160  2 SourceFileException e = ex.get(0);
161  2 assertTrue(e.getCause() instanceof ModuleDataException);
162  2 assertEquals(10002, e.getErrorCode());
163  2 assertEquals(221, e.getSourceArea().getStartPosition().getRow());
164  2 assertEquals(9, e.getSourceArea().getStartPosition().getColumn());
165    }
166    }
167   
 
168  2 toggle public void testNegative03() throws Exception {
169  2 try {
170  2 generate(getIndir(), "qedeq_error_sample_13.xml", "en", new File(getOutdir(), "null"), false);
171  0 fail("IllegalModuleDataException expected");
172    } catch (SourceFileExceptionList list) {
173  2 SourceFileExceptionList ex = list;
174  2 assertEquals(1, ex.size());
175  2 SourceFileException e = ex.get(0);
176  2 assertTrue(e.getCause() instanceof SAXParseException);
177  2 assertEquals(9001, e.getErrorCode());
178  2 assertEquals(315, e.getSourceArea().getStartPosition().getRow());
179  2 assertEquals(1, e.getSourceArea().getStartPosition().getColumn());
180  2 assertEquals(315, e.getSourceArea().getEndPosition().getRow());
181  2 assertEquals(30, e.getSourceArea().getEndPosition().getColumn());
182    }
183    }
184   
 
185  2 toggle public void testNegative04() throws Exception {
186  2 try {
187  2 generate(getIndir(), "qedeq_error_sample_14.xml", "en", new File(getOutdir(), "null"), false);
188  0 fail("IllegalModuleDataException expected");
189    } catch (SourceFileExceptionList list) {
190  2 SourceFileExceptionList ex = list;
191  2 assertEquals(7, ex.size());
192  2 SourceFileException e = ex.get(0);
193  2 assertTrue(e.getCause() instanceof SAXParseException);
194  2 assertEquals(9001, e.getErrorCode());
195  2 assertEquals(13, e.getSourceArea().getStartPosition().getRow());
196  2 assertEquals(1, e.getSourceArea().getStartPosition().getColumn());
197  2 assertEquals(13, e.getSourceArea().getEndPosition().getRow());
198  2 assertEquals(13, e.getSourceArea().getEndPosition().getColumn());
199  2 e = ex.get(1);
200  2 assertTrue(e.getCause() instanceof SAXParseException);
201  2 assertEquals(9001, e.getErrorCode());
202  2 assertEquals(16, e.getSourceArea().getStartPosition().getRow());
203  2 assertEquals(1, e.getSourceArea().getStartPosition().getColumn());
204  2 assertEquals(16, e.getSourceArea().getEndPosition().getRow());
205  2 assertEquals(16, e.getSourceArea().getEndPosition().getColumn());
206  2 e = ex.get(2);
207  2 assertTrue(e.getCause() instanceof SAXParseException);
208  2 assertEquals(9001, e.getErrorCode());
209  2 assertEquals(19, e.getSourceArea().getStartPosition().getRow());
210  2 assertEquals(1, e.getSourceArea().getStartPosition().getColumn());
211  2 assertEquals(19, e.getSourceArea().getEndPosition().getRow());
212  2 assertEquals(15, e.getSourceArea().getEndPosition().getColumn());
213  2 e = ex.get(3);
214  2 assertTrue(e.getCause() instanceof SAXParseException);
215  2 assertEquals(9001, e.getErrorCode());
216  2 assertEquals(22, e.getSourceArea().getStartPosition().getRow());
217  2 assertEquals(1, e.getSourceArea().getStartPosition().getColumn());
218  2 assertEquals(22, e.getSourceArea().getEndPosition().getRow());
219  2 assertEquals(15, e.getSourceArea().getEndPosition().getColumn());
220  2 e = ex.get(4);
221  2 assertTrue(e.getCause() instanceof SAXParseException);
222  2 assertEquals(9001, e.getErrorCode());
223  2 assertEquals(26, e.getSourceArea().getStartPosition().getRow());
224  2 assertEquals(1, e.getSourceArea().getStartPosition().getColumn());
225  2 assertEquals(26, e.getSourceArea().getEndPosition().getRow());
226  2 assertEquals(23, e.getSourceArea().getEndPosition().getColumn());
227  2 e = ex.get(5);
228  2 assertTrue(e.getCause() instanceof SAXParseException);
229  2 assertEquals(9001, e.getErrorCode());
230  2 assertEquals(69, e.getSourceArea().getStartPosition().getRow());
231  2 assertEquals(1, e.getSourceArea().getStartPosition().getColumn());
232  2 assertEquals(69, e.getSourceArea().getEndPosition().getRow());
233  2 assertEquals(55, e.getSourceArea().getEndPosition().getColumn());
234  2 e = ex.get(6);
235  2 assertTrue(e.getCause() instanceof SAXParseException);
236  2 assertEquals(9001, e.getErrorCode());
237  2 assertEquals(98, e.getSourceArea().getStartPosition().getRow());
238  2 assertEquals(1, e.getSourceArea().getStartPosition().getColumn());
239  2 assertEquals(98, e.getSourceArea().getEndPosition().getRow());
240  2 assertEquals(47, e.getSourceArea().getEndPosition().getColumn());
241    }
242    }
243   
 
244  2 toggle public void testNegative05() throws Exception {
245  2 try {
246  2 generate(getIndir(), "qedeq_error_sample_15.xml", "en", new File(getOutdir(), "null"), false);
247  0 fail("IllegalModuleDataException expected");
248    } catch (SourceFileExceptionList list) {
249  2 SourceFileExceptionList ex = list;
250  2 assertEquals(1, ex.size());
251  2 SourceFileException e = ex.get(0);
252  2 assertTrue(e.getCause() instanceof LogicalCheckException);
253  2 assertEquals(30550, e.getErrorCode());
254  2 assertEquals(168, e.getSourceArea().getStartPosition().getRow());
255  2 assertEquals(15, e.getSourceArea().getStartPosition().getColumn());
256    }
257    }
258   
 
259  2 toggle public void testNegative06() throws Exception {
260  2 try {
261  2 generate(getIndir(), "qedeq_error_sample_16.xml", "en", new File(getOutdir(), "null"), false);
262  0 fail("IllegalModuleDataException expected");
263    } catch (SourceFileExceptionList list) {
264  2 SourceFileExceptionList ex = list;
265  2 assertEquals(1, ex.size());
266  2 SourceFileException e = ex.get(0);
267  2 assertTrue(e.getCause() instanceof LogicalCheckException);
268  2 assertEquals(30590, e.getErrorCode());
269  2 assertEquals(288, e.getSourceArea().getStartPosition().getRow());
270  2 assertEquals(30, e.getSourceArea().getStartPosition().getColumn());
271    }
272    }
273   
 
274  2 toggle public void testNegative07() throws Exception {
275  2 try {
276  2 generate(getIndir(), "qedeq_error_sample_17.xml", "en", new File(getOutdir(), "null"), false);
277  0 fail("IllegalModuleDataException expected");
278    } catch (SourceFileExceptionList list) {
279  2 SourceFileExceptionList ex = list;
280  2 assertEquals(1, ex.size());
281  2 SourceFileException e = ex.get(0);
282  2 assertTrue(e.getCause() instanceof LogicalCheckException);
283  2 assertEquals(30780, e.getErrorCode());
284  2 assertEquals(298, e.getSourceArea().getStartPosition().getRow());
285  2 assertEquals(17, e.getSourceArea().getStartPosition().getColumn());
286    }
287    }
288   
 
289  2 toggle public void testNegative08() throws Exception {
290  2 try {
291  2 generate(getIndir(), "qedeq_error_sample_18.xml", "en", new File(getOutdir(), "null"), false);
292  0 fail("SourceFileExceptionList expected");
293    } catch (SourceFileExceptionList list) {
294  2 SourceFileExceptionList ex = list;
295  2 assertEquals(1, ex.size());
296  2 SourceFileException e = ex.get(0);
297  2 assertTrue(e.getCause() instanceof ModuleDataException);
298  2 assertEquals(1001, e.getErrorCode());
299  2 assertEquals(308, e.getSourceArea().getStartPosition().getRow());
300  2 assertEquals(15, e.getSourceArea().getStartPosition().getColumn());
301    }
302    }
303   
304    /**
305    * Call the generation of one LaTeX file and copy XML source to same destination directory for
306    * all supported languages.
307    *
308    * @param dir Start directory.
309    * @param xml Relative path to XML file. Must not be <code>null</code>.
310    * @param destinationDirectory Directory path for LaTeX file. Must not be <code>null</code>.
311    * @param onlyEn Generate only for language "en".
312    * @param ignoreWarnings Don't bother about warnings?
313    * @throws Exception Failure.
314    */
 
315  13 toggle public void generate(final File dir, final String xml, final File destinationDirectory,
316    final boolean onlyEn, final boolean ignoreWarnings) throws Exception {
317  13 generate(dir, xml, "en", destinationDirectory, ignoreWarnings);
318  12 if (!onlyEn) {
319  7 generate(dir, xml, "de", destinationDirectory, ignoreWarnings);
320    }
321    }
322   
323    /**
324    * Call the generation of one LaTeX file and copy XML source to same destination directory.
325    *
326    * @param dir Start directory.
327    * @param xml Relative path to XML file. Must not be <code>null</code>.
328    * @param language Generate text in this language. Can be <code>null</code>.
329    * @param destinationDirectory Directory path for LaTeX file. Must not be <code>null</code>.
330    * @param ignoreWarnings Don't bother about warnings?
331    */
 
332  54 toggle public void generate(final File dir, final String xml, final String language,
333    final File destinationDirectory, final boolean ignoreWarnings) throws IOException,
334    SourceFileExceptionList, InterruptException {
335  54 final File xmlFile = new File(dir, xml);
336  54 final ModuleAddress address = getServices().getModuleAddress(
337    UrlUtility.toUrl(xmlFile));
338  54 final KernelQedeqBo prop = (KernelQedeqBo) getServices().loadModule(
339    address);
340  54 if (prop.hasErrors()) {
341  6 throw prop.getErrors();
342    }
343  48 getServices().loadRequiredModules(prop.getModuleAddress());
344  48 if (prop.hasErrors()) {
345  0 throw prop.getErrors();
346    }
347  48 getServices().checkWellFormedness(prop.getModuleAddress());
348  48 if (prop.hasErrors()) {
349  6 throw prop.getErrors();
350    }
351  42 call = createServiceCall("generate LaTeX", prop);
352  42 QedeqBoDuplicateLanguageChecker.check(call);
353  40 endServiceCall(call);
354  40 if (prop.hasErrors()) {
355  0 throw prop.getErrors();
356    }
357   
358  40 final String web = "http://www.qedeq.org/"
359    + getInternalServices().getKernelVersionDirectory() + "/doc/" + xml;
360  40 final ModuleAddress webAddress = new DefaultModuleAddress(web);
361  40 getInternalServices().getLocalFilePath(webAddress);
362  40 IoUtility.copyFile(xmlFile, getInternalServices().getLocalFilePath(webAddress));
363   
364  40 getServices().checkWellFormedness(webAddress);
365  40 final QedeqBo webBo = getServices().getQedeqBo(webAddress);
366  40 final File texFile = new File(destinationDirectory, xml.substring(0, xml.lastIndexOf('.'))
367    + "_" + language + ".tex");
368  40 generate((KernelQedeqBo) webBo, texFile, language, "1");
369    // final File texCopy = new File(getOutdir(), new File(new File(xml).getParent(), texFile.getName())
370    // .getPath());
371    // IoUtility.copyFile(texFile, texCopy);
372  40 final File xmlCopy = new File(destinationDirectory, xml);
373  40 IoUtility.copyFile(xmlFile, xmlCopy);
374  40 if (webBo.hasErrors()) {
375  0 throw webBo.getErrors();
376    }
377  40 if (!ignoreWarnings && webBo.hasWarnings()) {
378  2 throw webBo.getWarnings();
379    }
380    }
381   
382    /**
383    * Generate LaTeX file out of XML file.
384    *
385    * @param prop Take this QEDEQ module.
386    * @param to Write to this file. Could be <code>null</code>.
387    * @param language Resulting language. Could be <code>null</code>.
388    * @param level Resulting detail level. Could be <code>null</code>.
389    * @return File name of generated LaTeX file.
390    * @throws SourceFileExceptionList Something went wrong.
391    */
 
392  40 toggle public String generate(final KernelQedeqBo prop, final File to, final String language,
393    final String level) throws SourceFileExceptionList {
394  40 final String method = "generate(String, String, String, String)";
395  40 try {
396  40 Trace.begin(CLASS, method);
397  40 Trace.param(CLASS, method, "prop", prop);
398  40 Trace.param(CLASS, method, "to", to);
399  40 Trace.param(CLASS, method, "language", language);
400  40 Trace.param(CLASS, method, "level", level);
401  40 final Map parameters = new HashMap();
402  40 parameters.put("info", "true");
403  40 final Parameters configParameters = new Parameters(parameters);
404  40 final InputStream latex =(new Qedeq2LatexExecutor(new Qedeq2LatexPlugin(), prop, configParameters))
405    .createLatex(new InternalModuleServiceCallImpl(DummyPlugin.getInstance(), prop, configParameters, Parameters.EMPTY,
406    new InternalServiceJobImpl(new ModuleArbiterImpl(), "generate LaTeX"), null), language, "1");
407  40 if (to != null) {
408  40 IoUtility.saveFile(latex, to);
409  40 return to.getCanonicalPath();
410    }
411  0 latex.close();
412  0 return prop.getName();
413    } catch (IOException e) {
414  0 Trace.trace(CLASS, method, e);
415  0 throw new RuntimeException(e);
416    } finally {
417  40 Trace.end(CLASS, method);
418    }
419    }
420   
421    }