Project

General

Profile

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

    
121
	public static void main(String[] args) {
122
		String userHome = System.getProperty("user.home");
123
		TestConfiguration.logger.error(userHome);
124
	}
125

    
126
}
(6-6/6)