1 6 package org.logicalcobwebs.proxool; 7 8 import java.io.InputStream ; 9 import java.lang.reflect.Field ; 10 import java.util.Properties ; 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 31 public class GlobalTest { 32 33 private static final String 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 40 { 41 if (initialised) { 43 return; 44 } 45 46 String log4jPath = System.getProperty("log4jPath"); 48 if (log4jPath != null && log4jPath.length() > 0) { 49 try { 50 DOMConfigurator.configureAndWatch(log4jPath); 51 } catch (Exception e) { 52 LOG.debug("Can't configure logging using " + log4jPath); 53 } 54 } else { 55 System.setProperty("org.apache.commons.logging.simplelog.defaultlog", "debug"); 57 org.apache.log4j.BasicConfigurator.resetConfiguration(); 58 org.apache.log4j.BasicConfigurator.configure(); 59 } 60 61 Class.forName(ProxoolDriver.class.getName()); 63 64 initTestConstants(); 66 67 initialised = true; 69 } 70 71 72 76 private static void initTestConstants() throws Exception 77 { 78 String resourceName = System.getProperty("testConfig", defaultConfig); 79 initTestConstants(resourceName); 80 } 81 82 86 private static void initTestConstants(String resourceName) throws Exception 87 { 88 InputStream resourceStream = null; 90 Properties props = new Properties (); 91 try { 92 LOG.info("Loading test configuration from "+resourceName); 93 resourceStream = resourceName.getClass().getResourceAsStream(resourceName); 94 props.load(resourceStream); 95 } 96 catch(Exception e) 97 { 98 LOG.error("Problem while loading test configuration", e); 99 throw new IllegalArgumentException ("Couldn't load resources from " + resourceName, e); 100 } 101 finally { 102 if( resourceStream != null ) { 103 resourceStream.close(); 104 } 105 } 106 107 Field [] fields = TestConstants.class.getDeclaredFields(); 109 for(int i=0; i<fields.length; i++) 110 { 111 Field field = fields[i]; 112 113 String propertyName = field.getName(); 115 String 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 alias) { 131 ProxoolFacade.shutdown(alias + ":teardown", 0); } 133 134 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 TestSetup wrapper = new TestSetup(suite) { 149 public void setUp() throws Exception { 150 GlobalTest.globalSetup(); 151 } 152 153 protected void tearDown() throws Exception { 154 GlobalTest.globalTeardown("global"); 155 } 156 }; 157 return wrapper; 158 } 159 160 } 161 162 163 | Popular Tags |