1   package net.sourceforge.jpotpourri;
2   
3   import java.util.Collections;
4   import java.util.Enumeration;
5   import java.util.HashMap;
6   import java.util.Map;
7   import java.util.ResourceBundle;
8   
9   import org.apache.commons.logging.Log;
10  import org.apache.commons.logging.LogFactory;
11  
12  /**
13   * @author christoph_pickl@users.sourceforge.net
14   */
15  public final class TestProperties {
16  
17  	private static final Log LOG = LogFactory.getLog(TestProperties.class);
18  	
19  	private static final String PROPERTIES_FILE_NAME = "test_config";
20  	
21  	private static final Map<String, TestKey> MAIN_KEYS;
22  	static {
23  		final Map<String, TestKey> tmp = new HashMap<String, TestKey>();
24  		tmp.put(TestKey.FOLDER_TESTROOT.getPropKey(), TestKey.FOLDER_TESTROOT);
25  		MAIN_KEYS = Collections.unmodifiableMap(tmp);
26  	}
27  	
28  	private static final TestProperties INSTANCE = new TestProperties();
29  	
30  	private final Map<TestKey, String> values = new HashMap<TestKey, String>();
31  	
32  	
33  	
34  	private TestProperties() {
35  		final ResourceBundle properties = ResourceBundle.getBundle(PROPERTIES_FILE_NAME);
36  		
37  		for (final Enumeration<String> keys = properties.getKeys(); keys.hasMoreElements();) {
38  			final String propKey = keys.nextElement();
39  			final TestKey key = MAIN_KEYS.get(propKey);
40  			if(key == null) {
41  				throw new IllegalArgumentException("Invalid property key [" + propKey + "]!");
42  			}
43  			final String value = properties.getString(propKey);
44  			LOG.debug("Setting main property [" + key.getPropKey() + "] = [" + value + "]");
45  			this.values.put(key, value);
46  		}
47  	}
48  	
49  	public static TestProperties getInstance() {
50  		return INSTANCE;
51  	}
52  
53  	public String getTestRootPath() {
54  		return this.values.get(TestKey.FOLDER_TESTROOT);
55  	}
56  
57  	
58  	
59  	
60  	
61  	/**
62  	 * @author christoph.pickl@bmi.gv.at
63  	 */
64  	private static enum TestKey {
65  		FOLDER_TESTROOT("folder.testroot");
66  		
67  		private final String propKey;
68  		
69  		private TestKey(final String propKey) {
70  			this.propKey = propKey;
71  		}
72  		
73  		String getPropKey() {
74  			return this.propKey;
75  		}
76  	}
77  
78  }