Project

General

Profile

Download (3.53 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
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

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

    
35
	final Properties properties = new Properties();
36

    
37
	private URL propertySourceUri;
38

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

    
43
	private static TestConfiguration testConfiguration = null;
44

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

    
50
			try {
51

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

    
64
				updateSystemProperties(false);
65

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

    
72
		}
73
	}
74

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

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

    
90
	}
91

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

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

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

    
123
		return null;
124
	}
125

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

    
131
}
(5-5/5)