/*
* Copyright 2005 jWic Group (http://www.jwic.de)
* $Id: TestEnvironment.java,v 1.1 2006/01/16 08:31:12 lordsam Exp $
*/
package de.jwic.test;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Properties;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.PropertyConfigurator;
/**
* Holds the environment for tests. It is basically a static pool of objects.
* The test environment settings are loaded from a property file that is loaded
* from the Classpath using the ClassLoader. The default filename is 'testenv.properties'.
* You can start a test using a different environment by using the java VM argument
* <br>
* <code>-Dtestenv.file=<i>filename</i></code><br>
* Sample: <code>-Dtestenv.file=usertest.properties</code><br>
*
* @version $Revision: 1.1 $
* @author Florian Lippisch
*/
public class TestEnvironment {
private static final String SYS_PROPERTY_TESTENVFILE = "testenv.file";
private static final String PROPERTY_LOG4J_PROPERTIES = "log4j.properties";
private static final String TESTENV_PROPERTIES_FILE = "testenv.properties";
private static TestEnvironment environment = null;
protected Log log = null;
private HashMap objects = new HashMap();
private Properties properties = null;
/**
* Private contsructor -> Singleton pattern.
*/
private TestEnvironment() {
super();
String filename = System.getProperty(SYS_PROPERTY_TESTENVFILE, TESTENV_PROPERTIES_FILE);
properties = new Properties();
try {
InputStream inp = getClass().getClassLoader().getResourceAsStream(filename);
if (inp != null) {
properties.load(inp);
} else {
// log-system is not initialized yet!
System.out.println("TestEnvironment configuration file not found (" + filename + ")");
}
} catch (IOException e) {
// logger is not yet available
throw new RuntimeException("Error initializing TestEnvironment: " + e);
}
// setup Log4j
String logProperties = properties.getProperty(PROPERTY_LOG4J_PROPERTIES);
if (logProperties != null) {
PropertyConfigurator.configureAndWatch(new File(logProperties).getAbsolutePath(), 60000L);
} else {
BasicConfigurator.configure();
}
log = LogFactory.getLog(getClass());
log.info("Testenvironment loaded from file " + filename);
}
/**
* Returns the TestEnvironment (singleton).
* @return
*/
public static TestEnvironment getTestEnvironment() {
if (environment == null) {
environment = new TestEnvironment();
}
return environment;
}
/**
* Returns an object to the specified key.
* @param key
* @return
*/
public Object get(Object key) {
return objects.get(key);
}
/**
* Stores an object to the specified key.
* @param key
* @param value
*/
public void put(Object key, Object value) {
objects.put(key, value);
}
/**
* Returns a property setting.
* @param key
* @return
*/
public String getProperty(String key) {
return properties.getProperty(key);
}
}
|