Clover Coverage Report
Coverage timestamp: Fri Feb 14 2014 01:47:57 UTC
../../../../../img/srcFileCovDistChart4.png 90% of files have more coverage
82   284   41   3.04
18   195   0.5   27
27     1.52  
1    
 
  QedeqBoTestCase       Line # 55 82 41 36.2% 0.36220473
 
  (108)
 
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.test;
17   
18    import java.io.ByteArrayOutputStream;
19    import java.io.File;
20    import java.io.IOException;
21    import java.io.UnsupportedEncodingException;
22   
23    import org.qedeq.base.io.Parameters;
24    import org.qedeq.base.io.TextOutput;
25    import org.qedeq.base.test.QedeqTestCase;
26    import org.qedeq.base.trace.Trace;
27    import org.qedeq.base.utility.DateUtility;
28    import org.qedeq.base.utility.StringUtility;
29    import org.qedeq.base.utility.YodaUtility;
30    import org.qedeq.kernel.bo.KernelContext;
31    import org.qedeq.kernel.bo.common.QedeqBo;
32    import org.qedeq.kernel.bo.job.InternalModuleServiceCallImpl;
33    import org.qedeq.kernel.bo.log.LogListener;
34    import org.qedeq.kernel.bo.log.ModuleEventListener;
35    import org.qedeq.kernel.bo.log.ModuleEventLog;
36    import org.qedeq.kernel.bo.log.QedeqLog;
37    import org.qedeq.kernel.bo.module.InternalKernelServices;
38    import org.qedeq.kernel.bo.module.InternalModuleServiceCall;
39    import org.qedeq.kernel.bo.module.InternalServiceJob;
40    import org.qedeq.kernel.bo.module.KernelQedeqBo;
41    import org.qedeq.kernel.bo.service.internal.DefaultInternalKernelServices;
42    import org.qedeq.kernel.bo.service.internal.DefaultKernelQedeqBo;
43    import org.qedeq.kernel.bo.service.internal.ServiceProcessManager;
44    import org.qedeq.kernel.se.base.list.Element;
45    import org.qedeq.kernel.se.common.DefaultModuleAddress;
46    import org.qedeq.kernel.se.common.ModuleDataException;
47    import org.qedeq.kernel.se.visitor.InterruptException;
48    import org.qedeq.kernel.xml.dao.Qedeq2Xml;
49   
50    /**
51    * Test generating LaTeX files for all known samples.
52    *
53    * @author Michael Meyling
54    */
 
55    public class QedeqBoTestCase extends QedeqTestCase implements LogListener, ModuleEventListener {
56   
57    /** Here should the result get into. */
58    private File genDir;
59   
60    /** Here are the documents within. */
61    private File docDir;
62   
 
63  604 toggle public QedeqBoTestCase() {
64  604 super();
65    }
66   
 
67  74 toggle public QedeqBoTestCase(final String name) {
68  74 super(name);
69    }
70   
 
71  476 toggle protected void setUp() throws Exception {
72  476 super.setUp();
73  476 docDir = new File("../QedeqDoc");
74  476 genDir = new File("../../qedeq_gen");
75    // test if we are in the normal development environment, where a project with name
76    // "../QedeqDoc" exists, otherwise we assume to run within the release directory
77    // structure where the docs are in the directory ../doc
78  476 if (!docDir.exists()) {
79  476 docDir = getFile("doc");
80    // or are we testing automatically?
81  476 if (!docDir.exists()) {
82  0 throw new IOException("unknown source directory for QEDEQ modules");
83    }
84  476 genDir = new File(getOutdir(), "doc");
85    }
86  476 KernelFacade.startup();
87  476 Trace.setTraceOn(false); // disable trace because it leads to java heap space errors
88    }
89   
 
90  488 toggle protected void tearDown() throws Exception {
91  488 super.tearDown();
92  488 KernelFacade.shutdown();
93    }
94   
 
95  37 toggle public File getGenDir() {
96  37 return genDir;
97    }
98   
 
99  203 toggle public File getDocDir() {
100  203 return docDir;
101    }
102   
103    /**
104    * Get test input data file. Get file relative to {@link #getDocDir()}.
105    *
106    * @param fileName Relative file path.
107    * @return Test data file.
108    */
 
109  7 toggle public File getDocFile(final String fileName) {
110  7 return new File(getDocDir(), fileName);
111    }
112   
 
113  487 toggle public InternalKernelServices getInternalServices() {
114  487 final KernelContext c = KernelFacade.getKernelContext();
115  487 try {
116  487 return (InternalKernelServices) YodaUtility.getFieldValue(c, "services");
117    } catch (NoSuchFieldException e) {
118  0 e.printStackTrace();
119  0 throw new RuntimeException(e);
120    }
121    }
122   
 
123  988 toggle public DefaultInternalKernelServices getServices() {
124  988 final KernelContext c = KernelFacade.getKernelContext();
125  988 try {
126  988 return (DefaultInternalKernelServices) YodaUtility.getFieldValue(c, "services");
127    } catch (NoSuchFieldException e) {
128  0 e.printStackTrace();
129  0 throw new RuntimeException(e);
130    }
131    }
132   
 
133  109 toggle public InternalModuleServiceCall createServiceCall(final String name, final KernelQedeqBo prop)
134    throws InterruptException {
135  109 final InternalServiceJob process;
136  109 try {
137  109 process = ((ServiceProcessManager) YodaUtility.getFieldValue(getInternalServices(), "processManager"))
138    .createServiceProcess(name);
139    } catch (NoSuchFieldException e) {
140  0 e.printStackTrace();
141  0 throw new RuntimeException(e);
142    }
143  109 final InternalModuleServiceCallImpl call = new InternalModuleServiceCallImpl(DummyPlugin.getInstance(), prop, Parameters.EMPTY, Parameters.EMPTY, process, null);
144  109 return call;
145    }
146   
 
147  157 toggle public void endServiceCall(final InternalModuleServiceCall call) {
148  157 if (call == null) {
149  23 return;
150    }
151  134 try {
152  134 ((ServiceProcessManager) YodaUtility.getFieldValue(getInternalServices(), "processManager"))
153    .endServiceCall(call);
154    } catch (NoSuchFieldException e) {
155  0 e.printStackTrace();
156  0 throw new RuntimeException(e);
157    }
158    }
159   
 
160  0 toggle public void printlnXml(final Element element) throws ModuleDataException {
161  0 final KernelQedeqBo prop = new DefaultKernelQedeqBo(null, DefaultModuleAddress.MEMORY);
162  0 final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
163  0 final TextOutput output = new TextOutput("out", outputStream, "UTF-8");
164  0 final Qedeq2Xml visitor = new Qedeq2Xml(null, prop, output);
165  0 visitor.getTraverser().accept(element);
166  0 try {
167  0 System.out.println(outputStream.toString("UTF-8"));
168    } catch (UnsupportedEncodingException e) {
169    // should never happen
170  0 throw new RuntimeException(e);
171    }
172    }
173   
 
174  0 toggle public void setTraceOn() {
175  0 QedeqLog.getInstance().addLog(this);
176  0 ModuleEventLog.getInstance().addLog(this);
177  0 super.setTraceOn();
178   
179    }
180   
181   
 
182  0 toggle public void setErrorOn() {
183  0 QedeqLog.getInstance().addLog(this);
184  0 ModuleEventLog.getInstance().addLog(this);
185  0 super.setErrorOn();
186    }
187   
188    // LogListener
189   
190    /** For this module we logged the last event. */
191    private String lastModuleUrl = "";
192   
 
193  0 toggle public synchronized final void logMessageState(final String text, final String url) {
194  0 handleModuleUrl(url, "state: " + text);
195    }
196   
 
197  0 toggle public synchronized final void logFailureState(final String text, final String url,
198    final String description) {
199  0 handleModuleUrl(url, "failure: " + text + "\n\t" + description);
200    }
201   
 
202  0 toggle public synchronized final void logSuccessfulState(final String text, final String url) {
203  0 handleModuleUrl(url, "success: " + text);
204    }
205   
 
206  0 toggle public synchronized void logRequest(final String text, final String url) {
207  0 handleModuleUrl(url, "request: " + text);
208    }
209   
 
210  0 toggle public synchronized final void logMessage(final String text) {
211  0 handleModuleUrl("", text);
212    }
213   
 
214  0 toggle public synchronized void logSuccessfulReply(final String text, final String url) {
215  0 handleModuleUrl(url, "reply: " + text);
216    }
217   
 
218  0 toggle public synchronized void logFailureReply(final String text, final String url, final String description) {
219  0 handleModuleUrl(url, "reply: " + text + "\n\t" + description);
220    }
221   
222    // ModuleEventListener
223   
 
224  0 toggle public synchronized void addModule(final QedeqBo prop) {
225  0 handleModuleUrl(prop.getUrl(), "module added.");
226    }
227   
 
228  0 toggle public synchronized void stateChanged(final QedeqBo prop) {
229  0 handleModuleUrl(prop.getUrl(), "module state changed: " + prop.getStateDescription());
230    }
231   
 
232  0 toggle public synchronized void removeModule(final QedeqBo prop) {
233  0 handleModuleUrl(prop.getUrl(), "module removed");
234    }
235   
 
236  0 toggle private void handleModuleUrl(final String url, final String text) {
237  0 final String urlShort;
238  0 if (!lastModuleUrl.equals(url)) {
239  0 urlShort = StringUtility.alignRight(getWithoutExtension(getLastSlashString(url)), 10);
240  0 lastModuleUrl = (url != null ? url : "");
241    } else {
242  0 urlShort = StringUtility.getSpaces(10).toString();
243    }
244  0 System.out.println(DateUtility.getTimestamp() + " " + urlShort + " " + text);
245    }
246   
247   
248    /**
249    * Get last / separated string part.
250    *
251    * @param full String with / characters in it. Also <code>null</code> is accepted.
252    * @return All after the last / in <code>full</code>. Is never <code>null</code>.
253    */
 
254  0 toggle public static String getLastSlashString(final String full) {
255  0 if (full == null) {
256  0 return "";
257    }
258  0 final int p = full.lastIndexOf('/');
259  0 if (p < 0) {
260  0 return full;
261    }
262  0 return full.substring(p + 1);
263    }
264   
265   
266    /**
267    * Get string part before last dot.
268    *
269    * @param full String with dots in it. Also <code>null</code> is accepted.
270    * @return All before the last dot in <code>full</code>. Is never <code>null</code>.
271    */
 
272  0 toggle public static String getWithoutExtension(final String full) {
273  0 if (full == null) {
274  0 return "";
275    }
276  0 final int p = full.lastIndexOf('.');
277  0 if (p < 0) {
278  0 return full;
279    }
280  0 return full.substring(0, p);
281    }
282   
283   
284    }