001: /*
002: * This software is released under a licence similar to the Apache Software Licence.
003: * See org.logicalcobwebs.proxool.package.html for details.
004: * The latest version is available at http://proxool.sourceforge.net
005: */
006: package org.logicalcobwebs.proxool;
007:
008: import java.io.InputStream;
009: import java.lang.reflect.Field;
010: import java.util.Properties;
011:
012: import junit.extensions.TestSetup;
013: import junit.framework.Test;
014: import junit.framework.TestSuite;
015:
016: import org.apache.log4j.xml.DOMConfigurator;
017: import org.apache.commons.logging.Log;
018: import org.apache.commons.logging.LogFactory;
019:
020: /**
021: * Provides a suite of all tests. And some utility methods for setting
022: * up the logging.
023: *
024: * The test configuration can be specified using the env property "testConfig"
025: *
026: * @version $Revision: 1.22 $, $Date: 2006/03/23 11:52:28 $
027: * @author bill
028: * @author $Author: billhorsman $ (current maintainer)
029: * @since Proxool 0.5
030: */
031: public class GlobalTest {
032:
033: private static final String defaultConfig = "/org/logicalcobwebs/proxool/testconfig-hsqldb.properties";
034:
035: private static final Log LOG = LogFactory.getLog(GlobalTest.class);
036:
037: private static boolean initialised;
038:
039: public static synchronized void globalSetup() throws Exception {
040: // return immediately if we are already initialised
041: if (initialised) {
042: return;
043: }
044:
045: // configure log4j
046: String log4jPath = System.getProperty("log4jPath");
047: if (log4jPath != null && log4jPath.length() > 0) {
048: try {
049: DOMConfigurator.configureAndWatch(log4jPath);
050: } catch (Exception e) {
051: LOG.debug("Can't configure logging using " + log4jPath);
052: }
053: } else {
054: // Well, at least switch on debugging
055: System.setProperty(
056: "org.apache.commons.logging.simplelog.defaultlog",
057: "debug");
058: org.apache.log4j.BasicConfigurator.resetConfiguration();
059: org.apache.log4j.BasicConfigurator.configure();
060: }
061:
062: // Load ProxoolDriver class into DriverManager
063: Class.forName(ProxoolDriver.class.getName());
064:
065: // initialise test configuration
066: initTestConstants();
067:
068: // remember we are correctly initialized
069: initialised = true;
070: }
071:
072: /**
073: *
074: * @throws Exception
075: */
076: private static void initTestConstants() throws Exception {
077: String resourceName = System.getProperty("testConfig",
078: defaultConfig);
079: initTestConstants(resourceName);
080: }
081:
082: /**
083: *
084: * @param resourceName
085: */
086: private static void initTestConstants(String resourceName)
087: throws Exception {
088: // locate and read resource file
089: InputStream resourceStream = null;
090: Properties props = new Properties();
091: try {
092: LOG.info("Loading test configuration from " + resourceName);
093: resourceStream = resourceName.getClass()
094: .getResourceAsStream(resourceName);
095: props.load(resourceStream);
096: } catch (Exception e) {
097: LOG.error("Problem while loading test configuration", e);
098: throw new IllegalArgumentException(
099: "Couldn't load resources from " + resourceName, e);
100: } finally {
101: if (resourceStream != null) {
102: resourceStream.close();
103: }
104: }
105:
106: // parse resource file and initialize TestConstants
107: Field[] fields = TestConstants.class.getDeclaredFields();
108: for (int i = 0; i < fields.length; i++) {
109: Field field = fields[i];
110:
111: // locate value in property file
112: String propertyName = field.getName();
113: String value = props.getProperty(propertyName);
114:
115: if (value == null) {
116: LOG.info("Set " + propertyName + " to default value");
117: } else {
118: LOG.info("Set " + propertyName + " to '" + value + "'");
119: field.set(null, value);
120: }
121: }
122: }
123:
124: public static synchronized void globalTeardown(String alias) {
125: ProxoolFacade.shutdown(alias + ":teardown", 0); //, 10000);
126: }
127:
128: /**
129: * Run all tests
130: *
131: * @return a composite test of all Proxool tests.
132: */
133: public static Test suite() {
134: TestSuite suite = new TestSuite();
135: suite.addTest(org.logicalcobwebs.proxool.AllTests.suite());
136: suite.addTest(org.logicalcobwebs.proxool.configuration.AllTests
137: .suite());
138: suite
139: .addTest(org.logicalcobwebs.proxool.admin.AllTests
140: .suite());
141: suite.addTest(org.logicalcobwebs.proxool.admin.jmx.AllTests
142: .suite());
143: suite.addTest(org.logicalcobwebs.proxool.util.AllTests.suite());
144:
145: // create a wrapper for global initialization code.
146: TestSetup wrapper = new TestSetup(suite) {
147: public void setUp() throws Exception {
148: GlobalTest.globalSetup();
149: }
150:
151: protected void tearDown() throws Exception {
152: GlobalTest.globalTeardown("global");
153: }
154: };
155: return wrapper;
156: }
157:
158: }
159:
160: /*
161: Revision history:
162: $Log: GlobalTest.java,v $
163: Revision 1.22 2006/03/23 11:52:28 billhorsman
164: Improve exception message
165:
166: Revision 1.21 2006/01/18 14:40:06 billhorsman
167: Unbundled Jakarta's Commons Logging.
168:
169: Revision 1.20 2004/05/26 22:30:21 brenuart
170: Fix issue where testConfig env property was not correctly handled
171:
172: Revision 1.19 2004/05/26 17:19:09 brenuart
173: Allow JUnit tests to be executed against another database.
174: By default the test configuration will be taken from the 'testconfig-hsqldb.properties' file located in the org.logicalcobwebs.proxool package.
175: This behavior can be overriden by setting the 'testConfig' environment property to another location.
176:
177: Revision 1.18 2003/10/26 16:23:20 billhorsman
178: Fixed up test suites
179:
180: Revision 1.17 2003/09/30 18:58:29 billhorsman
181: Increase shutdown grace time to 10 seconds to make tests more robust.
182:
183: Revision 1.16 2003/09/07 22:10:17 billhorsman
184: Default behaviour of test classes, if Log4J is not configured, is now DEBUG output to console.
185:
186: Revision 1.15 2003/05/06 23:17:59 chr32
187: Added JMX tests to GlobalTest.
188:
189: Revision 1.14 2003/03/11 14:51:47 billhorsman
190: more concurrency fixes relating to snapshots
191:
192: Revision 1.13 2003/03/10 15:31:26 billhorsman
193: fixes
194:
195: Revision 1.12 2003/03/04 10:10:52 billhorsman
196: loads ProxoolDriver
197:
198: Revision 1.11 2003/03/03 17:38:47 billhorsman
199: leave shutdown to AbstractProxoolTest
200:
201: Revision 1.10 2003/03/03 11:12:04 billhorsman
202: fixed licence
203:
204: Revision 1.9 2003/03/01 15:27:24 billhorsman
205: checkstyle
206:
207: Revision 1.8 2003/02/19 23:36:50 billhorsman
208: renamed monitor package to admin
209:
210: Revision 1.7 2003/02/10 00:14:33 chr32
211: Added tests for AbstractListenerContainer.
212:
213: Revision 1.6 2003/02/07 15:10:11 billhorsman
214: add admin tests
215:
216: Revision 1.5 2003/02/06 17:41:03 billhorsman
217: now uses imported logging
218:
219: Revision 1.4 2003/01/31 16:38:05 billhorsman
220: doc
221:
222: Revision 1.3 2002/12/18 03:15:03 chr32
223: Added commented-out code that will make logging level DEBUG.
224:
225: Revision 1.2 2002/12/16 17:15:12 billhorsman
226: fixes
227:
228: Revision 1.1 2002/12/16 17:05:25 billhorsman
229: new test structure
230:
231: */
|