View Javadoc

1   /*$Id: OptionalTestCase.java,v 1.6 2004/01/26 12:52:30 jdt Exp $
2    * Created on 23-Jan-2004
3    *
4    * Copyright (C) AstroGrid. All rights reserved.
5    *
6    * This software is published under the terms of the AstroGrid 
7    * Software License version 1.2, a copy of which has been included 
8    * with this distribution in the LICENSE.txt file.  
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       // remove any per-class settings
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 $Log: OptionalTestCase.java,v $
127 Revision 1.6  2004/01/26 12:52:30  jdt
128 minor bug
129 
130 Revision 1.5  2004/01/23 11:40:52  nw
131 tarted up javadoc
132 
133 Revision 1.4  2004/01/23 11:39:10  nw
134 tarted up javadoc
135 
136 Revision 1.3  2004/01/23 11:10:27  nw
137 works fine now.
138 
139 Revision 1.2  2004/01/23 11:01:03  nw
140 minor change
141 
142 Revision 1.1  2004/01/23 10:54:52  nw
143 added base test case that allows tests to be skipped
144 according to settings in system.properties
145  
146 */