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
|
import eu.etaxonomy.dataportal.selenium.WebDriverFactory;
|
21
|
|
22
|
/**
|
23
|
* @author a.kohlbecker
|
24
|
*
|
25
|
*/
|
26
|
public class TestConfiguration {
|
27
|
|
28
|
/**
|
29
|
*
|
30
|
*/
|
31
|
private static final String DATA_PORTAL_TEST_PROPERTIES_FILE = "DataPortalTest.xml";
|
32
|
private static final String DATAPORTAL_TEST_CONF = "dataportal.test.conf";
|
33
|
|
34
|
public final static Logger logger = Logger.getLogger(TestConfiguration.class);
|
35
|
|
36
|
final Properties properties = new Properties();
|
37
|
|
38
|
private URL propertySourceUri;
|
39
|
|
40
|
public Properties getProperties() {
|
41
|
return properties;
|
42
|
}
|
43
|
|
44
|
private static TestConfiguration testConfiguration = null;
|
45
|
|
46
|
private TestConfiguration() {
|
47
|
|
48
|
String userHome = System.getProperty("user.home");
|
49
|
String userDefinedFile = System.getProperty(DATAPORTAL_TEST_CONF);
|
50
|
File propertiesFile = null;
|
51
|
|
52
|
if(userDefinedFile != null){
|
53
|
propertiesFile = new File(userDefinedFile);
|
54
|
} else if (userHome != null) {
|
55
|
propertiesFile = new File(userHome, ".cdmLibrary" + File.separator + DATA_PORTAL_TEST_PROPERTIES_FILE);
|
56
|
}
|
57
|
|
58
|
try {
|
59
|
InputStream in = null;
|
60
|
|
61
|
if (propertiesFile != null) {
|
62
|
if(propertiesFile.exists()) {
|
63
|
propertySourceUri = propertiesFile.toURI().toURL();
|
64
|
in = new FileInputStream(propertiesFile);
|
65
|
} else{
|
66
|
logger.info(propertiesFile + " does not exist");
|
67
|
}
|
68
|
}
|
69
|
|
70
|
if (in == null) {
|
71
|
String resourceName = "/eu/etaxonomy/dataportal/"+ DATA_PORTAL_TEST_PROPERTIES_FILE;
|
72
|
|
73
|
in = this.getClass().getResourceAsStream(resourceName);
|
74
|
propertySourceUri = this.getClass().getResource(resourceName);
|
75
|
}
|
76
|
logger.info("Loading test configuration from " + propertySourceUri);
|
77
|
properties.loadFromXML(in);
|
78
|
in.close();
|
79
|
|
80
|
updateSystemProperties(false);
|
81
|
|
82
|
} catch (FileNotFoundException e) {
|
83
|
logger.error(e);
|
84
|
} catch (IOException e) {
|
85
|
logger.error(e);
|
86
|
}
|
87
|
|
88
|
|
89
|
}
|
90
|
|
91
|
/**
|
92
|
*
|
93
|
*/
|
94
|
private void updateSystemProperties(boolean doOverride) {
|
95
|
for(Object o : properties.keySet()){
|
96
|
String key = (String)o;
|
97
|
|
98
|
// update all webdriver properties and the browser property
|
99
|
if(key.startsWith("webdriver.") || key.equals(WebDriverFactory.SYSTEM_PROPERTY_NAME_BROWSER)) {
|
100
|
if(doOverride || System.getProperty(key) == null){
|
101
|
String oldValue = "";
|
102
|
if(System.getProperty(key) != null) {
|
103
|
oldValue = " ,overriding old value: " + System.getProperty(key) + ")";
|
104
|
}
|
105
|
logger.info("Setting system property: " + key + ": " + properties.getProperty(key) + oldValue);
|
106
|
System.setProperty(key, properties.getProperty(key));
|
107
|
} else {
|
108
|
logger.info("Not overriding system property: " + key + ": " + properties.getProperty(key) + " ,property already exists");
|
109
|
}
|
110
|
}
|
111
|
}
|
112
|
|
113
|
}
|
114
|
|
115
|
public static String getProperty(String key) throws TestConfigurationException{
|
116
|
return getProperty(key, String.class, false);
|
117
|
}
|
118
|
|
119
|
@SuppressWarnings("unchecked")
|
120
|
public static <T> T getProperty(String key, Class<T> type, boolean nonNull) throws TestConfigurationException{
|
121
|
if(testConfiguration == null){
|
122
|
testConfiguration = new TestConfiguration();
|
123
|
}
|
124
|
String value = testConfiguration.getProperties().getProperty(key);
|
125
|
|
126
|
|
127
|
if(value != null){
|
128
|
if(URI.class.isAssignableFrom(type)){
|
129
|
try {
|
130
|
return (T) new URI(value);
|
131
|
} catch (URISyntaxException e) {
|
132
|
throw new TestConfigurationException("Invalid URI " + value + " in property " + key + " of " + testConfiguration.propertySourceUri.toString(), e);
|
133
|
}
|
134
|
} else if(UUID.class.isAssignableFrom(type)){
|
135
|
try {
|
136
|
return (T) UUID.fromString(value);
|
137
|
} catch (IllegalArgumentException e) {
|
138
|
throw new TestConfigurationException("Invalid UUID " + value + " in property " + key + " of " + testConfiguration.propertySourceUri.toString(), e);
|
139
|
}
|
140
|
} else if(String.class.isAssignableFrom(type)){
|
141
|
return (T) value;
|
142
|
} else {
|
143
|
throw new TestConfigurationException("Unsupported type " + type.toString());
|
144
|
}
|
145
|
} else {
|
146
|
if( nonNull) {
|
147
|
throw new TestConfigurationException("Property " + key + " of " + testConfiguration.propertySourceUri.toString() + " must not be null");
|
148
|
}
|
149
|
}
|
150
|
|
151
|
return null;
|
152
|
}
|
153
|
|
154
|
public static void main(String[] args) {
|
155
|
String userHome = System.getProperty("user.home");
|
156
|
TestConfiguration.logger.error(userHome);
|
157
|
}
|
158
|
|
159
|
}
|