KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > junit > Configuration


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.junit;
8
9
10 import java.io.FileInputStream JavaDoc;
11 import java.util.MissingResourceException JavaDoc;
12 import java.util.PropertyResourceBundle JavaDoc;
13 import java.util.ResourceBundle JavaDoc;
14
15
16 /**
17  * This class is a static method of accessing the
18  * configuration file for the testing framework. This class
19  * also allows sub-classes to be created so that new
20  * frameworks can be built on top of this framework.
21  *
22  * @author Brian Pontarelli
23  */

24 public class Configuration
25 {
26     /**
27      * The name of the system property that can be used to override the default
28      * configuration file name and location
29      */

30     public static final String JavaDoc CONFIG_PROPERTY = "junit.config.file";
31
32     /**
33      * The name of the default properties file to search the classpath for
34      */

35     public static final String JavaDoc DEFAULT_CONFIG_FILE = "junit";
36
37     /**
38      * The name of the test location property in the bundle
39      */

40     public static final String JavaDoc TEST_LOCATION_PREFIX = "test.location";
41
42     /**
43      * The name of the persisting session property in the bundle
44      */

45     public static final String JavaDoc PERSISTING_SESSION = "persisting.session";
46
47     /**
48      * The resource bundle
49      */

50     private static ResourceBundle JavaDoc bundle;
51
52
53     /** Protected so that this is a static class but can have sub-classes */
54     protected Configuration() {
55         // EMPTY
56
}
57
58     /**
59      * This static block initializes the static ResourceBundle instance that points
60      * to the configuration file. This is private so that this class controls
61      * the initialization. All sub-classes do not need to initialize the config
62      * file.
63      */

64     static {
65         // Has the user passed the location of the configuration file
66
// as a java property
67
String JavaDoc location = System.getProperty(CONFIG_PROPERTY);
68         if (location != null) {
69             try {
70                 bundle = new PropertyResourceBundle JavaDoc(new FileInputStream JavaDoc(location));
71             } catch (Exception JavaDoc e) {
72                 throw new IllegalStateException JavaDoc("System property named " + CONFIG_PROPERTY +
73                     " does not point to a valid configuration file or the file" +
74                     " could not be read. Exception: " + e.toString());
75             }
76         }
77
78         // Try to read the default configuration file from the classpath
79
try {
80             bundle = PropertyResourceBundle.getBundle(DEFAULT_CONFIG_FILE);
81         } catch (MissingResourceException JavaDoc mre) {
82             throw new IllegalStateException JavaDoc("Could not find a valid junit-framework configuration file");
83         }
84     }
85
86
87     /**
88      * <p>
89      * Returns the test location property from the configuration file using the
90      * property name set in the constant TEST_LOCATION and appending the full
91      * name of the test case class to it. This will allow different sub-classes
92      * to have different test locations.
93      * </p>
94      *
95      * <p>
96      * This property is not used if the persisting session property is set to
97      * true. If this property is set to true, then the test location will be
98      * retrieved from the property:<br/>
99      * <code>test.location.com.inversoft.junit.WebTestCase</code>
100      * </p>
101      *
102      * @param testCaseClass The Class object for the WebTestCase or a sub-class
103      */

104     public static String JavaDoc getTestLocation(Class JavaDoc testCaseClass) {
105         String JavaDoc name = TEST_LOCATION_PREFIX + "." + testCaseClass.getName();
106         String JavaDoc location;
107         try {
108             location = bundle.getString(name);
109         } catch (MissingResourceException JavaDoc mre) {
110             // Try without the class name
111
location = bundle.getString(TEST_LOCATION_PREFIX);
112         }
113
114         return location;
115     }
116
117     /**
118      * Returns whether or not the bundle contains a property named
119      * <code>persisting.session</code> and if so, the boolean equivalent of its
120      * value.
121      *
122      * @return The boolean value of the persisting.session property in the bundle
123      */

124     public static boolean isPersistingSession() {
125         try {
126             String JavaDoc value = bundle.getString(PERSISTING_SESSION);
127             if (value.equalsIgnoreCase("true")) {
128                 return true;
129             }
130         } catch (MissingResourceException JavaDoc mre) {
131             // Smother since this property is optional
132
}
133
134         return false;
135     }
136 }
Popular Tags