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