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