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.InvalidPropertiesFormatException;
|
15
|
import java.util.Properties;
|
16
|
import java.util.UUID;
|
17
|
|
18
|
import org.apache.log4j.Logger;
|
19
|
|
20
|
import eu.etaxonomy.dataportal.junit.CdmDataPortalTestBase;
|
21
|
import eu.etaxonomy.dataportal.selenium.WebDriverFactory;
|
22
|
|
23
|
/**
|
24
|
* @author a.kohlbecker
|
25
|
*
|
26
|
*/
|
27
|
public class TestConfiguration {
|
28
|
|
29
|
/**
|
30
|
*
|
31
|
*/
|
32
|
private static final String DATA_PORTAL_TEST_PROPERTIES_FILE = "DataPortalTest.xml";
|
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
|
InputStream in = null;
|
48
|
|
49
|
in = readFromUserHome();
|
50
|
if(in == null){
|
51
|
in = readFromClassPath();
|
52
|
}
|
53
|
if(in == null){
|
54
|
String message = "Test configuration file " + DATA_PORTAL_TEST_PROPERTIES_FILE + " not found!";
|
55
|
logger.error(message);
|
56
|
System.exit(-1);
|
57
|
}
|
58
|
|
59
|
logger.info("Loading test configuration from " + propertySourceUri);
|
60
|
try {
|
61
|
properties.loadFromXML(in);
|
62
|
} catch (InvalidPropertiesFormatException e) {
|
63
|
logger.error(e);
|
64
|
} catch (IOException e) {
|
65
|
logger.error(e);
|
66
|
} finally {
|
67
|
try {
|
68
|
in.close();
|
69
|
} catch (IOException e) {
|
70
|
/* IGNORE */
|
71
|
}
|
72
|
}
|
73
|
}
|
74
|
|
75
|
/**
|
76
|
* @return
|
77
|
*/
|
78
|
private InputStream readFromClassPath() {
|
79
|
ClassLoader cl = getClass().getClassLoader();
|
80
|
return cl.getResourceAsStream("eu/etaxonomy/dataportal/"+DATA_PORTAL_TEST_PROPERTIES_FILE);
|
81
|
}
|
82
|
|
83
|
/**
|
84
|
* @param userHome
|
85
|
* @param in
|
86
|
* @return
|
87
|
*/
|
88
|
public InputStream readFromUserHome() {
|
89
|
|
90
|
InputStream in = null;
|
91
|
String userHome = System.getProperty("user.home");
|
92
|
if (userHome != null) {
|
93
|
|
94
|
File propertiesFile = new File(userHome, ".cdmLibrary" + File.separator + DATA_PORTAL_TEST_PROPERTIES_FILE);
|
95
|
|
96
|
try {
|
97
|
|
98
|
if (propertiesFile.exists()) {
|
99
|
propertySourceUri = propertiesFile.toURI().toURL();
|
100
|
in = new FileInputStream(propertiesFile);
|
101
|
} else {
|
102
|
in = this.getClass().getResourceAsStream("/eu/etaxonomy/dataportal/DataPortalTest.properties");
|
103
|
propertySourceUri = this.getClass().getResource("/eu/etaxonomy/dataportal/DataPortalTest.properties");
|
104
|
}
|
105
|
|
106
|
updateSystemProperties(false);
|
107
|
|
108
|
} catch (FileNotFoundException e) {
|
109
|
logger.error(e);
|
110
|
} catch (IOException e) {
|
111
|
logger.error(e);
|
112
|
}
|
113
|
}
|
114
|
return in;
|
115
|
}
|
116
|
|
117
|
/**
|
118
|
*
|
119
|
*/
|
120
|
private void updateSystemProperties(boolean doOverride) {
|
121
|
for(Object o : properties.keySet()){
|
122
|
String key = (String)o;
|
123
|
|
124
|
// update all webdriver properties and the browser property
|
125
|
if(key.startsWith("webdriver.") || key.equals(WebDriverFactory.SYSTEM_PROPERTY_NAME_BROWSER)){
|
126
|
if(doOverride || System.getProperty(key) == null){
|
127
|
System.setProperty(key, properties.getProperty(key));
|
128
|
}
|
129
|
}
|
130
|
}
|
131
|
|
132
|
}
|
133
|
|
134
|
public static String getProperty(String key){
|
135
|
return getProperty(key, String.class);
|
136
|
}
|
137
|
|
138
|
@SuppressWarnings("unchecked")
|
139
|
public static <T> T getProperty(String key, Class<T> type){
|
140
|
if(testConfiguration == null){
|
141
|
testConfiguration = new TestConfiguration();
|
142
|
}
|
143
|
String value = testConfiguration.getProperties().getProperty(key);
|
144
|
|
145
|
if(value != null){
|
146
|
if(URI.class.isAssignableFrom(type)){
|
147
|
try {
|
148
|
return (T) new URI(value);
|
149
|
} catch (URISyntaxException e) {
|
150
|
logger.error("Invalid URI " + value + " in property " + key + " of " + testConfiguration.propertySourceUri.toString());
|
151
|
}
|
152
|
} else if(UUID.class.isAssignableFrom(type)){
|
153
|
try {
|
154
|
return (T) UUID.fromString(value);
|
155
|
} catch (IllegalArgumentException e) {
|
156
|
logger.error("Invalid UUID " + value + " in property " + key + " of " + testConfiguration.propertySourceUri.toString());
|
157
|
}
|
158
|
} else if(String.class.isAssignableFrom(type)){
|
159
|
return (T) value;
|
160
|
} else {
|
161
|
throw new RuntimeException("Unsupported type " + type.toString());
|
162
|
}
|
163
|
}
|
164
|
|
165
|
return null;
|
166
|
}
|
167
|
|
168
|
public static void main(String[] args) {
|
169
|
String userHome = System.getProperty("user.home");
|
170
|
TestConfiguration.logger.error(userHome);
|
171
|
}
|
172
|
|
173
|
}
|