Clover Coverage Report
Coverage timestamp: Sat Sep 18 2010 04:09:52 UTC
185   374   34   10.28
16   284   0.18   18
18     1.89  
1    
 
  GenerateLatexTest       Line # 47 185 34 85.8% 0.8584475
 
  (17)
 
1    /* This file is part of the project "Hilbert II" - http://www.qedeq.org
2    *
3    * Copyright 2000-2010, 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   
22    import org.qedeq.base.io.IoUtility;
23    import org.qedeq.base.test.QedeqTestCase;
24    import org.qedeq.base.trace.Trace;
25    import org.qedeq.kernel.bo.QedeqBo;
26    import org.qedeq.kernel.bo.logic.wf.LogicalCheckException;
27    import org.qedeq.kernel.bo.module.InternalKernelServices;
28    import org.qedeq.kernel.bo.module.KernelQedeqBo;
29    import org.qedeq.kernel.bo.service.latex.Qedeq2LatexPlugin;
30    import org.qedeq.kernel.bo.service.latex.QedeqBoDuplicateLanguageChecker;
31    import org.qedeq.kernel.bo.test.KernelFacade;
32    import org.qedeq.kernel.common.DefaultModuleAddress;
33    import org.qedeq.kernel.common.DefaultSourceFileExceptionList;
34    import org.qedeq.kernel.common.ModuleAddress;
35    import org.qedeq.kernel.common.ModuleDataException;
36    import org.qedeq.kernel.common.Plugin;
37    import org.qedeq.kernel.common.SourceFileException;
38    import org.qedeq.kernel.common.SourceFileExceptionList;
39    import org.xml.sax.SAXParseException;
40   
41    /**
42    * Test generating LaTeX files for all known samples.
43    *
44    * @version $Revision: 1.1 $
45    * @author Michael Meyling
46    */
 
47    public class GenerateLatexTest extends QedeqTestCase {
48   
49    /** This class. */
50    private static final Class CLASS = GenerateLatexTest.class;
51   
52    /** Here should the result get into. */
53    private File genDir;
54   
55    /** Here are the documents within. */
56    private File docDir;
57   
 
58  16 toggle public void setUp() throws Exception {
59  16 super.setUp();
60  16 docDir = new File("../QedeqDoc");
61  16 genDir = new File("../../../qedeq_gen");
62    // test if we are in the normal development environment, where a project with name
63    // "../QedeqDoc" exists, otherwise we assume to run within the release directory
64    // structure where the docs are in the directory ../doc
65  16 if (!docDir.exists()) {
66  16 docDir = getFile("doc");
67    // or are we testing automatically?
68  16 if (!docDir.exists()) {
69  0 throw new IOException("unknown source directory for QEDEQ modules");
70    }
71  16 genDir = new File(getOutdir(), "doc");
72    }
73  16 KernelFacade.startup();
74    }
75   
 
76  16 toggle public void tearDown() throws Exception {
77  16 super.tearDown();
78  16 KernelFacade.shutdown();
79    }
80   
81    /**
82    * Start main process.
83    *
84    * @throws Exception
85    */
 
86  2 toggle public void testGeneration() throws Exception {
87  2 generate(docDir, "math/qedeq_logic_v1.xml", genDir, false);
88  2 generate(docDir, "math/qedeq_sample1.xml", genDir, false);
89  2 generate(docDir, "math/qedeq_set_theory_v1.xml", genDir, false);
90  2 generate(docDir, "project/qedeq_basic_concept.xml", genDir, false);
91  2 generate(docDir, "project/qedeq_logic_language.xml", genDir, true);
92    }
93   
 
94  2 toggle public void testNegative02() throws IOException {
95  2 try {
96  2 generate(getIndir(), "qedeq_sample2_error.xml", "de", new File(genDir, "null"));
97  0 fail("IllegalModuleDataException expected");
98    } catch (SourceFileExceptionList list) {
99  2 DefaultSourceFileExceptionList ex = (DefaultSourceFileExceptionList) list;
100  2 assertEquals(1, ex.size());
101  2 SourceFileException e = ex.get(0);
102  2 assertTrue(e.getCause() instanceof ModuleDataException);
103  2 assertEquals(10002, e.getErrorCode());
104  2 assertEquals(221, e.getSourceArea().getStartPosition().getRow());
105  2 assertEquals(9, e.getSourceArea().getStartPosition().getColumn());
106    }
107    }
108   
 
109  2 toggle public void testNegative03() throws IOException {
110  2 try {
111  2 generate(getIndir(), "qedeq_sample3_error.xml", "en", new File(genDir, "null"));
112  0 fail("IllegalModuleDataException expected");
113    } catch (SourceFileExceptionList list) {
114  2 DefaultSourceFileExceptionList ex = (DefaultSourceFileExceptionList) list;
115  2 assertEquals(1, ex.size());
116  2 SourceFileException e = ex.get(0);
117  2 assertTrue(e.getCause() instanceof SAXParseException);
118  2 assertEquals(9001, e.getErrorCode());
119  2 assertEquals(313, e.getSourceArea().getStartPosition().getRow());
120  2 assertEquals(1, e.getSourceArea().getStartPosition().getColumn());
121  2 assertEquals(313, e.getSourceArea().getEndPosition().getRow());
122  2 assertEquals(30, e.getSourceArea().getEndPosition().getColumn());
123    }
124    }
125   
 
126  2 toggle public void testNegative04() throws IOException {
127  2 try {
128  2 generate(getIndir(), "qedeq_sample4_error.xml", "en", new File(genDir, "null"));
129  0 fail("IllegalModuleDataException expected");
130    } catch (SourceFileExceptionList list) {
131  2 DefaultSourceFileExceptionList ex = (DefaultSourceFileExceptionList) list;
132  2 assertEquals(7, ex.size());
133  2 SourceFileException e = ex.get(0);
134  2 assertTrue(e.getCause() instanceof SAXParseException);
135  2 assertEquals(9001, e.getErrorCode());
136  2 assertEquals(13, e.getSourceArea().getStartPosition().getRow());
137  2 assertEquals(1, e.getSourceArea().getStartPosition().getColumn());
138  2 assertEquals(13, e.getSourceArea().getEndPosition().getRow());
139  2 assertEquals(13, e.getSourceArea().getEndPosition().getColumn());
140  2 e = ex.get(1);
141  2 assertTrue(e.getCause() instanceof SAXParseException);
142  2 assertEquals(9001, e.getErrorCode());
143  2 assertEquals(16, e.getSourceArea().getStartPosition().getRow());
144  2 assertEquals(1, e.getSourceArea().getStartPosition().getColumn());
145  2 assertEquals(16, e.getSourceArea().getEndPosition().getRow());
146  2 assertEquals(16, e.getSourceArea().getEndPosition().getColumn());
147  2 e = ex.get(2);
148  2 assertTrue(e.getCause() instanceof SAXParseException);
149  2 assertEquals(9001, e.getErrorCode());
150  2 assertEquals(19, e.getSourceArea().getStartPosition().getRow());
151  2 assertEquals(1, e.getSourceArea().getStartPosition().getColumn());
152  2 assertEquals(19, e.getSourceArea().getEndPosition().getRow());
153  2 assertEquals(15, e.getSourceArea().getEndPosition().getColumn());
154  2 e = ex.get(3);
155  2 assertTrue(e.getCause() instanceof SAXParseException);
156  2 assertEquals(9001, e.getErrorCode());
157  2 assertEquals(22, e.getSourceArea().getStartPosition().getRow());
158  2 assertEquals(1, e.getSourceArea().getStartPosition().getColumn());
159  2 assertEquals(22, e.getSourceArea().getEndPosition().getRow());
160  2 assertEquals(15, e.getSourceArea().getEndPosition().getColumn());
161  2 e = ex.get(4);
162  2 assertTrue(e.getCause() instanceof SAXParseException);
163  2 assertEquals(9001, e.getErrorCode());
164  2 assertEquals(26, e.getSourceArea().getStartPosition().getRow());
165  2 assertEquals(1, e.getSourceArea().getStartPosition().getColumn());
166  2 assertEquals(26, e.getSourceArea().getEndPosition().getRow());
167  2 assertEquals(23, e.getSourceArea().getEndPosition().getColumn());
168  2 e = ex.get(5);
169  2 assertTrue(e.getCause() instanceof SAXParseException);
170  2 assertEquals(9001, e.getErrorCode());
171  2 assertEquals(69, e.getSourceArea().getStartPosition().getRow());
172  2 assertEquals(1, e.getSourceArea().getStartPosition().getColumn());
173  2 assertEquals(69, e.getSourceArea().getEndPosition().getRow());
174  2 assertEquals(47, e.getSourceArea().getEndPosition().getColumn());
175  2 e = ex.get(6);
176  2 assertTrue(e.getCause() instanceof SAXParseException);
177  2 assertEquals(9001, e.getErrorCode());
178  2 assertEquals(98, e.getSourceArea().getStartPosition().getRow());
179  2 assertEquals(1, e.getSourceArea().getStartPosition().getColumn());
180  2 assertEquals(98, e.getSourceArea().getEndPosition().getRow());
181  2 assertEquals(47, e.getSourceArea().getEndPosition().getColumn());
182    }
183    }
184   
 
185  2 toggle public void testNegative05() throws IOException {
186  2 try {
187  2 generate(getIndir(), "qedeq_sample5_error.xml", "en", new File(genDir, "null"));
188  0 fail("IllegalModuleDataException expected");
189    } catch (SourceFileExceptionList list) {
190  2 DefaultSourceFileExceptionList ex = (DefaultSourceFileExceptionList) list;
191  2 assertEquals(1, ex.size());
192  2 SourceFileException e = ex.get(0);
193  2 assertTrue(e.getCause() instanceof LogicalCheckException);
194  2 assertEquals(30550, e.getErrorCode());
195  2 assertEquals(168, e.getSourceArea().getStartPosition().getRow());
196  2 assertEquals(15, e.getSourceArea().getStartPosition().getColumn());
197    }
198    }
199   
 
200  2 toggle public void testNegative06() throws IOException {
201  2 try {
202  2 generate(getIndir(), "qedeq_sample6_error.xml", "en", new File(genDir, "null"));
203  0 fail("IllegalModuleDataException expected");
204    } catch (SourceFileExceptionList list) {
205  2 DefaultSourceFileExceptionList ex = (DefaultSourceFileExceptionList) list;
206  2 assertEquals(1, ex.size());
207  2 SourceFileException e = ex.get(0);
208  2 assertTrue(e.getCause() instanceof LogicalCheckException);
209  2 assertEquals(30590, e.getErrorCode());
210  2 assertEquals(286, e.getSourceArea().getStartPosition().getRow());
211  2 assertEquals(21, e.getSourceArea().getStartPosition().getColumn());
212    }
213    }
214   
 
215  2 toggle public void testNegative07() throws IOException {
216  2 try {
217  2 generate(getIndir(), "qedeq_sample7_error.xml", "en", new File(genDir, "null"));
218  0 fail("IllegalModuleDataException expected");
219    } catch (SourceFileExceptionList list) {
220  2 DefaultSourceFileExceptionList ex = (DefaultSourceFileExceptionList) list;
221  2 assertEquals(1, ex.size());
222  2 SourceFileException e = ex.get(0);
223  2 assertTrue(e.getCause() instanceof LogicalCheckException);
224  2 assertEquals(30780, e.getErrorCode());
225  2 assertEquals(296, e.getSourceArea().getStartPosition().getRow());
226  2 assertEquals(17, e.getSourceArea().getStartPosition().getColumn());
227    }
228    }
229   
 
230  2 toggle public void testNegative08() throws IOException {
231  2 try {
232  2 generate(getIndir(), "qedeq_sample8_error.xml", "en", new File(genDir, "null"));
233  0 fail("SourceFileExceptionList expected");
234    } catch (SourceFileExceptionList list) {
235  2 DefaultSourceFileExceptionList ex = (DefaultSourceFileExceptionList) list;
236  2 assertEquals(1, ex.size());
237  2 SourceFileException e = ex.get(0);
238  2 assertTrue(e.getCause() instanceof ModuleDataException);
239  2 assertEquals(1001, e.getErrorCode());
240  2 assertEquals(306, e.getSourceArea().getStartPosition().getRow());
241  2 assertEquals(15, e.getSourceArea().getStartPosition().getColumn());
242    }
243    }
244   
245    /**
246    * Call the generation of one LaTeX file and copy XML source to same destination directory for
247    * all supported languages.
248    *
249    * @param dir Start directory.
250    * @param xml Relative path to XML file. Must not be <code>null</code>.
251    * @param destinationDirectory Directory path for LaTeX file. Must not be <code>null</code>.
252    * @param onlyEn Generate only for language "en".
253    * @throws Exception Failure.
254    */
 
255  5 toggle public void generate(final File dir, final String xml, final File destinationDirectory,
256    final boolean onlyEn) throws Exception {
257  5 generate(dir, xml, "en", destinationDirectory);
258  5 if (!onlyEn) {
259  4 generate(dir, xml, "de", destinationDirectory);
260    }
261    }
262   
263    /**
264    * Call the generation of one LaTeX file and copy XML source to same destination directory.
265    *
266    * @param dir Start directory.
267    * @param xml Relative path to XML file. Must not be <code>null</code>.
268    * @param language Generate text in this language. Can be <code>null</code>.
269    * @param destinationDirectory Directory path for LaTeX file. Must not be <code>null</code>.
270    * @throws IOException File IO failed.
271    * @throws XmlFilePositionException File data is invalid.
272    */
 
273  32 toggle public void generate(final File dir, final String xml, final String language,
274    final File destinationDirectory) throws IOException, SourceFileExceptionList {
275  32 final File xmlFile = new File(dir, xml);
276  32 final ModuleAddress address = KernelFacade.getKernelContext().getModuleAddress(
277    IoUtility.toUrl(xmlFile));
278  32 final KernelQedeqBo prop = (KernelQedeqBo) KernelFacade.getKernelContext().loadModule(
279    address);
280  26 if (prop.hasErrors()) {
281  0 throw prop.getErrors();
282    }
283  26 KernelFacade.getKernelContext().loadRequiredModules(prop.getModuleAddress());
284  26 if (prop.hasErrors()) {
285  0 throw prop.getErrors();
286    }
287  26 KernelFacade.getKernelContext().checkModule(prop.getModuleAddress());
288  26 if (prop.hasErrors()) {
289  6 throw prop.getErrors();
290    }
291  20 QedeqBoDuplicateLanguageChecker.check(new Plugin() {
 
292  0 toggle public String getPluginId() {
293  0 return QedeqBoDuplicateLanguageChecker.class.getName();
294    }
295   
 
296  0 toggle public String getPluginName() {
297  0 return "duplicate language checker";
298    }
299   
 
300  0 toggle public String getPluginDescription() {
301  0 return "Test for duplicate language entries within LaTeX sections";
302    }
303   
304    }, prop);
305  18 if (prop.hasErrors()) {
306  0 throw prop.getErrors();
307    }
308   
309  18 final String web = "http://www.qedeq.org/"
310    + KernelFacade.getKernelContext().getKernelVersionDirectory() + "/doc/" + xml;
311  18 final InternalKernelServices services = (InternalKernelServices) IoUtility.getFieldContent(
312    KernelFacade.getKernelContext(), "services");
313  18 final ModuleAddress webAddress = new DefaultModuleAddress(web);
314  18 services.getLocalFilePath(webAddress);
315  18 IoUtility.copyFile(xmlFile, services.getLocalFilePath(webAddress));
316   
317  18 KernelFacade.getKernelContext().checkModule(webAddress);
318  18 final QedeqBo webBo = KernelFacade.getKernelContext().getQedeqBo(webAddress);
319  18 final File texFile = new File(destinationDirectory, xml.substring(0, xml.lastIndexOf('.'))
320    + "_" + language + ".tex");
321  18 generate((KernelQedeqBo) webBo, texFile, language, "1");
322  18 final File texCopy = new File(dir, new File(new File(xml).getParent(), texFile.getName())
323    .getPath());
324  18 final File xmlCopy = new File(destinationDirectory, xml);
325  18 IoUtility.createNecessaryDirectories(xmlCopy);
326  18 IoUtility.copyFile(xmlFile, xmlCopy);
327  18 IoUtility.copyFile(texFile, texCopy);
328    }
329   
330    /**
331    * Generate LaTeX file out of XML file.
332    *
333    * @param prop Take this QEDEQ module.
334    * @param to Write to this file. Could be <code>null</code>.
335    * @param language Resulting language. Could be <code>null</code>.
336    * @param level Resulting detail level. Could be <code>null</code>.
337    * @return File name of generated LaTeX file.
338    * @throws SourceFileExceptionList Something went wrong.
339    */
 
340  18 toggle public String generate(final KernelQedeqBo prop, final File to, final String language,
341    final String level) throws SourceFileExceptionList {
342  18 final String method = "generate(String, String, String, String)";
343  18 try {
344  18 Trace.begin(CLASS, method);
345  18 Trace.param(CLASS, method, "prop", prop);
346  18 Trace.param(CLASS, method, "to", to);
347  18 Trace.param(CLASS, method, "language", language);
348  18 Trace.param(CLASS, method, "level", level);
349  18 final InputStream latex = (new Qedeq2LatexPlugin()).createLatex(prop, language, level, true);
350  18 if (to != null) {
351  18 IoUtility.createNecessaryDirectories(to);
352  18 IoUtility.saveFile(latex, to);
353  18 return to.getCanonicalPath();
354    } else {
355  0 latex.close();
356  0 return prop.getName();
357    }
358    } catch (IOException e) {
359  0 Trace.trace(CLASS, method, e);
360  0 throw new RuntimeException(e);
361    } finally {
362  18 Trace.end(CLASS, method);
363    }
364    }
365   
 
366  0 toggle public File getGenDir() {
367  0 return genDir;
368    }
369   
 
370  0 toggle public File getDocDir() {
371  0 return docDir;
372    }
373   
374    }