Project

General

Profile

Download (3.87 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
					String resourceName = "/eu/etaxonomy/dataportal/"+ DATA_PORTAL_TEST_PROPERTIES_FILE;
58

    
59
					in =  this.getClass().getResourceAsStream(resourceName);
60
					propertySourceUri = this.getClass().getResource(resourceName);
61
				}
62
				logger.info("Loading test configuration from " + propertySourceUri);
63
				properties.loadFromXML(in);
64
				in.close();
65

    
66
				updateSystemProperties(false);
67

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

    
74
		}
75
	}
76

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

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

    
92
	}
93

    
94
	public static String getProperty(String key) throws TestConfigurationException{
95
		return getProperty(key, String.class, false);
96
	}
97

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

    
105

    
106
		if(value != null){
107
			if(URI.class.isAssignableFrom(type)){
108
				try {
109
					return (T) new URI(value);
110
				} catch (URISyntaxException e) {
111
					throw new TestConfigurationException("Invalid URI " + value + " in property " + key + " of " + testConfiguration.propertySourceUri.toString(), e);
112
				}
113
			} else if(UUID.class.isAssignableFrom(type)){
114
				try {
115
					return (T) UUID.fromString(value);
116
				} catch (IllegalArgumentException e) {
117
					throw new TestConfigurationException("Invalid UUID " + value + " in property " + key + " of " + testConfiguration.propertySourceUri.toString(), e);
118
				}
119
			} else if(String.class.isAssignableFrom(type)){
120
				return (T) value;
121
			} else {
122
				throw new TestConfigurationException("Unsupported type " + type.toString());
123
			}
124
		} else {
125
			if( nonNull) {
126
				throw new TestConfigurationException("Property " + key + " of " + testConfiguration.propertySourceUri.toString() + " must not be null");
127
			}
128
		}
129

    
130
		return null;
131
	}
132

    
133
	public static void main(String[] args) {
134
		String userHome = System.getProperty("user.home");
135
		TestConfiguration.logger.error(userHome);
136
	}
137

    
138
}
(5-5/6)