Project

General

Profile

Download (3.48 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
 *
3
 */
4
package eu.etaxonomy.dataportal;
5

    
6
import java.io.File;
7
import java.io.FileInputStream;
8
import java.io.FileNotFoundException;
9
import java.io.IOException;
10
import java.io.InputStream;
11
import java.net.URI;
12
import java.net.URISyntaxException;
13
import java.net.URL;
14
import java.util.Properties;
15
import java.util.UUID;
16

    
17
import org.apache.log4j.Logger;
18

    
19
import eu.etaxonomy.dataportal.junit.CdmDataPortalTestBase;
20

    
21
/**
22
 * @author a.kohlbecker
23
 *
24
 */
25
public class TestConfiguration {
26

    
27
	/**
28
	 *
29
	 */
30
	private static final String DATA_PORTAL_TEST_PROPERTIES_FILE = "DataPortalTest.properties";
31

    
32
	public final static Logger logger = Logger.getLogger(TestConfiguration.class);
33

    
34
	final Properties properties = new Properties();
35

    
36
	private URL propertySourceUri;
37

    
38
	public Properties getProperties() {
39
		return properties;
40
	}
41

    
42
	private static TestConfiguration testConfiguration = null;
43

    
44
	private TestConfiguration() {
45
		String userHome = System.getProperty("user.home");
46
		if (userHome != null) {
47
			File propertiesFile = new File(userHome, ".cdmLibrary" + File.separator + DATA_PORTAL_TEST_PROPERTIES_FILE);
48

    
49
			try {
50

    
51
				InputStream in;
52
				if (propertiesFile.exists()) {
53
					propertySourceUri = propertiesFile.toURI().toURL();
54
					in = new FileInputStream(propertiesFile);
55
				} else {
56
					in =  this.getClass().getResourceAsStream("/eu/etaxonomy/dataportal/DataPortalTest.properties");
57
					propertySourceUri = this.getClass().getResource("/eu/etaxonomy/dataportal/DataPortalTest.properties");
58
				}
59
				logger.info("Loading test configuration from " + propertySourceUri);
60
				properties.load(in);
61
				in.close();
62

    
63
				updateSystemProperties(false);
64

    
65
			} catch (FileNotFoundException e) {
66
				logger.error(e);
67
			} catch (IOException e) {
68
				logger.error(e);
69
			}
70

    
71
		}
72
	}
73

    
74
	/**
75
	 *
76
	 */
77
	private void updateSystemProperties(boolean doOverride) {
78
		for(Object o : properties.keySet()){
79
			String key = (String)o;
80

    
81
			// update all webdriver properties and the browser property
82
			if(key.startsWith("webdriver.") || key.equals(CdmDataPortalTestBase.SYSTEM_PROPERTY_NAME_BROWSER)){
83
				if(doOverride || System.getProperty(key) == null){
84
					System.setProperty(key, properties.getProperty(key));
85
				}
86
			}
87
		}
88

    
89
	}
90

    
91
	public static String getProperty(String key){
92
		return getProperty(key, String.class);
93
	}
94

    
95
	@SuppressWarnings("unchecked")
96
	public static <T> T getProperty(String key, Class<T> type){
97
		if(testConfiguration == null){
98
			testConfiguration = new TestConfiguration();
99
		}
100
		String value = testConfiguration.getProperties().getProperty(key);
101

    
102
		if(value != null){
103
			if(URI.class.isAssignableFrom(type)){
104
				try {
105
					return (T) new URI(value);
106
				} catch (URISyntaxException e) {
107
					logger.error("Invalid URI " + value + " in property " + key + " of " + testConfiguration.propertySourceUri.toString());
108
				}
109
			} else if(UUID.class.isAssignableFrom(type)){
110
				try {
111
					return (T) UUID.fromString(value);
112
				} catch (IllegalArgumentException e) {
113
					logger.error("Invalid UUID " + value + " in property " + key + " of " + testConfiguration.propertySourceUri.toString());
114
				}
115
			} else if(String.class.isAssignableFrom(type)){
116
				return (T) value;
117
			} else {
118
				throw new RuntimeException("Unsupported type " + type.toString());
119
			}
120
		}
121

    
122
		return null;
123
	}
124

    
125
	public static void main(String[] args) {
126
		String userHome = System.getProperty("user.home");
127
		TestConfiguration.logger.error(userHome);
128
	}
129

    
130
}
(5-5/5)