1
2
3
4
5
6
7
8
9
10
11 package org.astrogrid.test;
12 import java.util.Iterator;
13 import java.util.Properties;
14
15 import junit.framework.TestCase;
16 import junit.framework.TestResult;
17
18 /*** Extension of the standard junit TestCase that allows tests to be enabled / disabled.
19 <p>
20 Test classes that extend OptionalTestCase, rather than the normal junit.framework.TestCase, can be disabled / enabled as needed.
21 <p>
22 Class provides static methods to turn individual tests cases on and off.
23 <p>
24 Works from settings in System.properties (which can be set on the command-line using the <tt>-D<i>key</i>=<i>value</i></tt> option)
25 <p>
26 Setting the system property <tt>optional.test.skip.ALL</tt> to <tt>true</tt> will cause the execution of all optional test cases to be skipped
27 (a log message is printed out for each test instead)
28 <p>
29 An siingle test case named <tt>org.foo.wibbleTest</tt> can be disabled by setting the system property
30 <tt>optional.test.skip.org.foo.wibbleTest</tt> to <tt>true</tt>
31
32 <hr>
33 <h2>Setting System Properties</h2>
34 System properties can be set on the java commandline like this..
35 <tt>java -Doptional.test.skip.All=true ....</tt>
36 <p>
37 Most IDEs provide a way to pass system properties to unit test executions.
38 For eclipse its 'Run' -> 'Run..' -> 'Arguments', then add <tt>-D<i>key</i>=<i>value</i></tt> to the 'VM Arguments' field
39 <p>
40 To pass system properties to tests in maven, its best to set them in your~/build.properties. The property <tt>maven.junit.sysproperties</tt>
41 should be set to the list of properties to pass to the tests. (separated by spaces).
42 <p>
43 An example, that skips all optional tests apart from foo.bar.ChooTest
44 <pre>
45 optional.test.skip.ALL=false
46 optional.test.skip.foo.bar.ChooTest=true
47 maven.junit.sysproperties=optional.test.skip.All optional.test.skip.foo.bar.ChooTest
48 </pre>
49 @author Noel Winstanley nw@jb.man.ac.uk 23-Jan-2004
50
51 */
52 public class OptionalTestCase extends TestCase {
53 /***
54 *
55 */
56 public OptionalTestCase() {
57 super();
58 }
59 /***
60 * @param arg0
61 */
62 public OptionalTestCase(String arg0) {
63 super(arg0);
64 }
65
66 /***
67 * overridden - only runs a test if this optional test case is enabled.
68 */
69 public void run(TestResult tr) {
70 if (this.isDisabled()) {
71 tr.startTest(this);
72 System.out.println("Optional test " + this.getClass().getName() + "#" + this.getName() + " is disabled - Skipping..");
73 tr.endTest(this);
74 } else {
75 super.run(tr);
76 }
77 }
78
79 /*** check whether this test case is disabled */
80 public final boolean isDisabled() {
81 String val = System.getProperty(mkKey(this.getClass().getName()));
82 if (val == null) {
83 return Boolean.getBoolean(DISABLED_BY_DEFAULT);
84 } else {
85 return Boolean.valueOf(val).booleanValue();
86 }
87
88 }
89 /*** set enablement for this test */
90 public final void setDisabled(boolean isDisabled) {
91 OptionalTestCase.setTestDisabled(this.getClass().getName(),isDisabled);
92 }
93 /*** if true, by default disable optional test cases, otherwise enable by default. */
94 public final static void setDisabledByDefault(boolean isDisabled) {
95 System.setProperty(DISABLED_BY_DEFAULT,Boolean.toString(isDisabled));
96 }
97 /*** set enablement for a named test */
98 public final static void setTestDisabled(String classname, boolean isDisabled) {
99 System.setProperty(mkKey(classname),Boolean.toString(isDisabled));
100 }
101
102 private final static String mkKey(String classname) {
103 return "optional.test.skip." + classname;
104 }
105
106 /*** key used in System.properties. If true, by default disable optional test cases */
107 public final static String DISABLED_BY_DEFAULT="optional.test.skip.ALL";
108
109 /*** reset enablements back to original settings - all optional tests enabled */
110 public final static void reset() {
111
112 Properties sysProps = System.getProperties();
113 for (Iterator i = sysProps.keySet().iterator();i.hasNext(); ) {
114 String key = (String)i.next();
115 if (key.startsWith("optional.test.skip")) {
116 i.remove();
117 }
118 }
119 System.setProperties(sysProps);
120 }
121
122 }
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146