Clover Coverage Report
Coverage timestamp: Fri Feb 14 2014 07:28:57 UTC
../../../../img/srcFileCovDistChart5.png 86% of files have more coverage
47   189   19   4.7
14   104   0.4   10
10     1.9  
1    
 
  QedeqTestCase       Line # 39 47 19 42.3% 0.4225352
 
  (248)
 
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.base.test;
17   
18    import java.io.File;
19    import java.io.IOException;
20    import java.net.URL;
21   
22    import junit.framework.TestCase;
23   
24    import org.apache.log4j.Level;
25    import org.apache.log4j.Logger;
26    import org.apache.log4j.PatternLayout;
27    import org.apache.log4j.WriterAppender;
28    import org.apache.log4j.xml.DOMConfigurator;
29    import org.qedeq.base.io.IoUtility;
30    import org.qedeq.base.io.UrlUtility;
31    import org.qedeq.base.trace.Trace;
32    import org.qedeq.base.utility.StringUtility;
33   
34    /**
35    * Basis class for all tests.
36    *
37    * @author Michael Meyling
38    */
 
39    public abstract class QedeqTestCase extends TestCase {
40   
41    /** Destination directory for generated output files. */
42    private static final File OUTDIR =
43    new File(System.getProperty("qedeq.test.outdir", "../../qedeq_test"));
44   
45    /** Source directory for input files. */
46    private static final File INDIR =
47    new File(System.getProperty("qedeq.test.indir", "data"));
48   
49   
 
50  170 toggle static {
51    // init Log4J watchdog
52  170 try {
53  170 final File logConfig = new File(System.getProperty("qedeq.test.log4j",
54    OUTDIR + "/config/log4j.xml"));
55  170 URL url = null;
56  170 if (logConfig.canRead()) {
57  170 url = UrlUtility.toUrl(logConfig);
58    }
59  170 if (url == null) {
60    // try development environment
61  0 final StringBuffer buffer = new StringBuffer();
62  0 IoUtility.loadFile(new File("../QedeqBuild/resources/config/log4j.xml"), buffer, "UTF8");
63  0 StringUtility.replace(buffer, "\"./log/trace.log\"", "\"../../qedeq_test/log/trace.log\"");
64  0 IoUtility.saveFile(logConfig, buffer, "UTF8");
65  0 if (logConfig.canRead()) {
66  0 url = UrlUtility.toUrl(logConfig);
67    }
68    }
69  170 if (url != null) {
70    // set properties and watch file every 15 seconds
71  170 DOMConfigurator.configureAndWatch(url.getPath(), 15000);
72    } else {
73  0 Logger.getRootLogger().setLevel(Level.ERROR);
74    }
75    } catch (Exception e) {
76  0 try {
77  0 System.out.println((new File(".")).getCanonicalPath());
78    } catch (IOException ignore) {
79    }
80  0 e.printStackTrace(System.out);
81    // we ignore this
82    }
83    }
84   
85    /** Should the methods of this class execute fast? */
86    private final boolean fast;
87   
88    /**
89    * Constructor.
90    *
91    * @param name Test case name.
92    */
 
93  306 toggle public QedeqTestCase(final String name) {
94  306 super(name);
95  306 fast = "true".equalsIgnoreCase(System.getProperty("qedeq.test.fast", "true"));
96    }
97   
98    /**
99    * Constructor.
100    */
 
101  1623 toggle public QedeqTestCase() {
102  1623 super();
103  1623 fast = "true".equalsIgnoreCase(System.getProperty("qedeq.test.fast", "true"));
104    }
105   
106    /**
107    * Get output directory for test output. Might be set initially by setting the system property
108    * <code>qedeq.test.outdir</code>.
109    *
110    * @return Directory for test output.
111    */
 
112  1134 toggle public File getOutdir() {
113  1134 return OUTDIR;
114    }
115   
116    /**
117    * Get input directory for test input data. Might be set initially by setting the system
118    * property <code>qedeq.test.indir</code>.
119    *
120    * @return Directory for test input.
121    */
 
122  777 toggle public File getIndir() {
123  777 return INDIR;
124    }
125   
126    /**
127    * Get test input data file. Get file relative to {@link #getIndir()}.
128    *
129    * @param fileName Relative file path.
130    * @return Test data file.
131    */
 
132  648 toggle public File getFile(final String fileName) {
133  648 return new File(getIndir(), fileName);
134    }
135   
136    /**
137    * Should the test case be finished fast?
138    *
139    * @return Should the test case be finished fast?
140    */
 
141  4 toggle private boolean fast() {
142  4 return fast;
143    }
144   
145    /**
146    * Should a slow test method be executed?. Also prints name of current method
147    * to System.out.
148    * The test case should ask this method at the begin of long running test methods.
149    *
150    * @return Should even slow test methods be executed?
151    */
 
152  4 toggle public boolean slow() {
153  4 if (fast()) {
154  0 final StackTraceElement[] st = new Exception().getStackTrace();
155  0 final StackTraceElement e = st[1];
156    // find test method
157  0 String name = e.getMethodName();
158  0 for (int i = 1; i < st.length; i++) {
159  0 if (st[i].getMethodName().startsWith("test")) {
160  0 name = st[i].getMethodName();
161  0 break;
162    }
163    }
164  0 System.out.println("skipping slow test "
165    + e.getClassName() + "." + name);
166  0 return false;
167    }
168  4 return true;
169    }
170   
 
171  0 toggle protected void setTraceOn() {
172  0 Logger rootLogger = Logger.getRootLogger();
173  0 rootLogger.removeAllAppenders();
174  0 rootLogger.setLevel(Level.DEBUG);
175  0 rootLogger.addAppender(new WriterAppender(
176    new PatternLayout(PatternLayout.TTCC_CONVERSION_PATTERN), System.out));
177  0 Trace.setTraceOn(true);
178    }
179   
 
180  0 toggle protected void setErrorOn() {
181  0 Logger rootLogger = Logger.getRootLogger();
182  0 rootLogger.removeAllAppenders();
183  0 rootLogger.setLevel(Level.ERROR);
184  0 rootLogger.addAppender(new WriterAppender(
185    new PatternLayout(PatternLayout.TTCC_CONVERSION_PATTERN), System.out));
186  0 Trace.setTraceOn(true);
187    }
188   
189    }