001: /*
002: * Copyright 2005 jWic Group (http://www.jwic.de)
003: * $Id: TestEnvironment.java,v 1.1 2006/01/16 08:31:12 lordsam Exp $
004: */
005: package de.jwic.test;
006:
007: import java.io.File;
008: import java.io.IOException;
009: import java.io.InputStream;
010: import java.util.HashMap;
011: import java.util.Properties;
012:
013: import org.apache.commons.logging.Log;
014: import org.apache.commons.logging.LogFactory;
015: import org.apache.log4j.BasicConfigurator;
016: import org.apache.log4j.PropertyConfigurator;
017:
018: /**
019: * Holds the environment for tests. It is basically a static pool of objects.
020: * The test environment settings are loaded from a property file that is loaded
021: * from the Classpath using the ClassLoader. The default filename is 'testenv.properties'.
022: * You can start a test using a different environment by using the java VM argument
023: * <br>
024: * <code>-Dtestenv.file=<i>filename</i></code><br>
025: * Sample: <code>-Dtestenv.file=usertest.properties</code><br>
026: *
027: * @version $Revision: 1.1 $
028: * @author Florian Lippisch
029: */
030: public class TestEnvironment {
031:
032: private static final String SYS_PROPERTY_TESTENVFILE = "testenv.file";
033: private static final String PROPERTY_LOG4J_PROPERTIES = "log4j.properties";
034: private static final String TESTENV_PROPERTIES_FILE = "testenv.properties";
035: private static TestEnvironment environment = null;
036:
037: protected Log log = null;
038:
039: private HashMap objects = new HashMap();
040: private Properties properties = null;
041:
042: /**
043: * Private contsructor -> Singleton pattern.
044: */
045: private TestEnvironment() {
046: super ();
047:
048: String filename = System.getProperty(SYS_PROPERTY_TESTENVFILE,
049: TESTENV_PROPERTIES_FILE);
050: properties = new Properties();
051: try {
052: InputStream inp = getClass().getClassLoader()
053: .getResourceAsStream(filename);
054: if (inp != null) {
055: properties.load(inp);
056: } else {
057: // log-system is not initialized yet!
058: System.out
059: .println("TestEnvironment configuration file not found ("
060: + filename + ")");
061: }
062: } catch (IOException e) {
063: // logger is not yet available
064: throw new RuntimeException(
065: "Error initializing TestEnvironment: " + e);
066: }
067:
068: // setup Log4j
069: String logProperties = properties
070: .getProperty(PROPERTY_LOG4J_PROPERTIES);
071: if (logProperties != null) {
072: PropertyConfigurator.configureAndWatch(new File(
073: logProperties).getAbsolutePath(), 60000L);
074: } else {
075: BasicConfigurator.configure();
076: }
077: log = LogFactory.getLog(getClass());
078: log.info("Testenvironment loaded from file " + filename);
079: }
080:
081: /**
082: * Returns the TestEnvironment (singleton).
083: * @return
084: */
085: public static TestEnvironment getTestEnvironment() {
086: if (environment == null) {
087: environment = new TestEnvironment();
088: }
089: return environment;
090: }
091:
092: /**
093: * Returns an object to the specified key.
094: * @param key
095: * @return
096: */
097: public Object get(Object key) {
098: return objects.get(key);
099: }
100:
101: /**
102: * Stores an object to the specified key.
103: * @param key
104: * @param value
105: */
106: public void put(Object key, Object value) {
107: objects.put(key, value);
108: }
109:
110: /**
111: * Returns a property setting.
112: * @param key
113: * @return
114: */
115: public String getProperty(String key) {
116: return properties.getProperty(key);
117: }
118: }
|