KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > logicalcobwebs > proxool > GlobalTest


1 /*
2  * This software is released under a licence similar to the Apache Software Licence.
3  * See org.logicalcobwebs.proxool.package.html for details.
4  * The latest version is available at http://proxool.sourceforge.net
5  */

6 package org.logicalcobwebs.proxool;
7
8 import java.io.InputStream JavaDoc;
9 import java.lang.reflect.Field JavaDoc;
10 import java.util.Properties JavaDoc;
11
12 import junit.extensions.TestSetup;
13 import junit.framework.Test;
14 import junit.framework.TestSuite;
15
16 import org.apache.log4j.xml.DOMConfigurator;
17 import org.apache.commons.logging.Log;
18 import org.apache.commons.logging.LogFactory;
19
20 /**
21  * Provides a suite of all tests. And some utility methods for setting
22  * up the logging.
23  *
24  * The test configuration can be specified using the env property "testConfig"
25  *
26  * @version $Revision: 1.22 $, $Date: 2006/03/23 11:52:28 $
27  * @author bill
28  * @author $Author: billhorsman $ (current maintainer)
29  * @since Proxool 0.5
30  */

31 public class GlobalTest {
32
33     private static final String JavaDoc defaultConfig = "/org/logicalcobwebs/proxool/testconfig-hsqldb.properties";
34     
35     private static final Log LOG = LogFactory.getLog(GlobalTest.class);
36
37     private static boolean initialised;
38
39     public static synchronized void globalSetup() throws Exception JavaDoc
40     {
41         // return immediately if we are already initialised
42
if (initialised) {
43             return;
44         }
45         
46         // configure log4j
47
String JavaDoc log4jPath = System.getProperty("log4jPath");
48         if (log4jPath != null && log4jPath.length() > 0) {
49             try {
50                 DOMConfigurator.configureAndWatch(log4jPath);
51             } catch (Exception JavaDoc e) {
52                 LOG.debug("Can't configure logging using " + log4jPath);
53             }
54         } else {
55             // Well, at least switch on debugging
56
System.setProperty("org.apache.commons.logging.simplelog.defaultlog", "debug");
57             org.apache.log4j.BasicConfigurator.resetConfiguration();
58             org.apache.log4j.BasicConfigurator.configure();
59         }
60
61         // Load ProxoolDriver class into DriverManager
62
Class.forName(ProxoolDriver.class.getName());
63
64         // initialise test configuration
65
initTestConstants();
66
67         // remember we are correctly initialized
68
initialised = true;
69     }
70
71     
72     /**
73      *
74      * @throws Exception
75      */

76     private static void initTestConstants() throws Exception JavaDoc
77     {
78         String JavaDoc resourceName = System.getProperty("testConfig", defaultConfig);
79         initTestConstants(resourceName);
80     }
81     
82     /**
83      *
84      * @param resourceName
85      */

86     private static void initTestConstants(String JavaDoc resourceName) throws Exception JavaDoc
87     {
88         // locate and read resource file
89
InputStream JavaDoc resourceStream = null;
90         Properties JavaDoc props = new Properties JavaDoc();
91         try {
92             LOG.info("Loading test configuration from "+resourceName);
93             resourceStream = resourceName.getClass().getResourceAsStream(resourceName);
94             props.load(resourceStream);
95         }
96         catch(Exception JavaDoc e)
97         {
98             LOG.error("Problem while loading test configuration", e);
99             throw new IllegalArgumentException JavaDoc("Couldn't load resources from " + resourceName, e);
100         }
101         finally {
102             if( resourceStream != null ) {
103                 resourceStream.close();
104             }
105         }
106         
107         // parse resource file and initialize TestConstants
108
Field JavaDoc[] fields = TestConstants.class.getDeclaredFields();
109         for(int i=0; i<fields.length; i++)
110         {
111             Field JavaDoc field = fields[i];
112             
113             // locate value in property file
114
String JavaDoc propertyName = field.getName();
115             String JavaDoc value = props.getProperty(propertyName);
116             
117             if( value==null )
118             {
119                 LOG.info("Set "+propertyName+" to default value");
120             }
121             else
122             {
123                 LOG.info("Set " + propertyName+ " to '" + value + "'");
124                 field.set(null, value);
125             }
126         }
127     }
128     
129     
130     public static synchronized void globalTeardown(String JavaDoc alias) {
131         ProxoolFacade.shutdown(alias + ":teardown", 0); //, 10000);
132
}
133
134     /**
135      * Run all tests
136      *
137      * @return a composite test of all Proxool tests.
138      */

139     public static Test suite() {
140         TestSuite suite = new TestSuite();
141         suite.addTest(org.logicalcobwebs.proxool.AllTests.suite());
142         suite.addTest(org.logicalcobwebs.proxool.configuration.AllTests.suite());
143         suite.addTest(org.logicalcobwebs.proxool.admin.AllTests.suite());
144         suite.addTest(org.logicalcobwebs.proxool.admin.jmx.AllTests.suite());
145         suite.addTest(org.logicalcobwebs.proxool.util.AllTests.suite());
146
147         // create a wrapper for global initialization code.
148
TestSetup wrapper = new TestSetup(suite) {
149             public void setUp() throws Exception JavaDoc {
150                 GlobalTest.globalSetup();
151             }
152
153             protected void tearDown() throws Exception JavaDoc {
154                 GlobalTest.globalTeardown("global");
155             }
156         };
157         return wrapper;
158     }
159
160 }
161
162
163 /*
164  Revision history:
165  $Log: GlobalTest.java,v $
166  Revision 1.22 2006/03/23 11:52:28 billhorsman
167  Improve exception message
168
169  Revision 1.21 2006/01/18 14:40:06 billhorsman
170  Unbundled Jakarta's Commons Logging.
171
172  Revision 1.20 2004/05/26 22:30:21 brenuart
173  Fix issue where testConfig env property was not correctly handled
174
175  Revision 1.19 2004/05/26 17:19:09 brenuart
176  Allow JUnit tests to be executed against another database.
177  By default the test configuration will be taken from the 'testconfig-hsqldb.properties' file located in the org.logicalcobwebs.proxool package.
178  This behavior can be overriden by setting the 'testConfig' environment property to another location.
179
180  Revision 1.18 2003/10/26 16:23:20 billhorsman
181  Fixed up test suites
182
183  Revision 1.17 2003/09/30 18:58:29 billhorsman
184  Increase shutdown grace time to 10 seconds to make tests more robust.
185
186  Revision 1.16 2003/09/07 22:10:17 billhorsman
187  Default behaviour of test classes, if Log4J is not configured, is now DEBUG output to console.
188
189  Revision 1.15 2003/05/06 23:17:59 chr32
190  Added JMX tests to GlobalTest.
191
192  Revision 1.14 2003/03/11 14:51:47 billhorsman
193  more concurrency fixes relating to snapshots
194
195  Revision 1.13 2003/03/10 15:31:26 billhorsman
196  fixes
197
198  Revision 1.12 2003/03/04 10:10:52 billhorsman
199  loads ProxoolDriver
200
201  Revision 1.11 2003/03/03 17:38:47 billhorsman
202  leave shutdown to AbstractProxoolTest
203
204  Revision 1.10 2003/03/03 11:12:04 billhorsman
205  fixed licence
206
207  Revision 1.9 2003/03/01 15:27:24 billhorsman
208  checkstyle
209
210  Revision 1.8 2003/02/19 23:36:50 billhorsman
211  renamed monitor package to admin
212
213  Revision 1.7 2003/02/10 00:14:33 chr32
214  Added tests for AbstractListenerContainer.
215
216  Revision 1.6 2003/02/07 15:10:11 billhorsman
217  add admin tests
218
219  Revision 1.5 2003/02/06 17:41:03 billhorsman
220  now uses imported logging
221
222  Revision 1.4 2003/01/31 16:38:05 billhorsman
223  doc
224
225  Revision 1.3 2002/12/18 03:15:03 chr32
226  Added commented-out code that will make logging level DEBUG.
227
228  Revision 1.2 2002/12/16 17:15:12 billhorsman
229  fixes
230
231  Revision 1.1 2002/12/16 17:05:25 billhorsman
232  new test structure
233
234  */
Popular Tags