Project

General

Profile

Download (4.59 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.WebDriverFactory;
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.xml";
31
	private static final String DATAPORTAL_TEST_CONF = "dataportal.test.conf";
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

    
47
		String userHome = System.getProperty("user.home");
48
		String userDefinedFile = System.getProperty(DATAPORTAL_TEST_CONF);
49
		File propertiesFile = null;
50

    
51
		if(userDefinedFile != null){
52
			propertiesFile = new File(userDefinedFile);
53
		} else if (userHome != null) {
54
			propertiesFile = new File(userHome, ".cdmLibrary" + File.separator + DATA_PORTAL_TEST_PROPERTIES_FILE);
55
		}
56

    
57
		try {
58
			InputStream in = null;
59

    
60
			if (propertiesFile != null) {
61
				if(propertiesFile.exists()) {
62
					propertySourceUri = propertiesFile.toURI().toURL();
63
					in = new FileInputStream(propertiesFile);
64
				} else{
65
					logger.info(propertiesFile +  " does not exist");
66
				}
67
			}
68

    
69
			if (in == null) {
70
				String resourceName = "/eu/etaxonomy/dataportal/"+ DATA_PORTAL_TEST_PROPERTIES_FILE;
71

    
72
				in =  this.getClass().getResourceAsStream(resourceName);
73
				propertySourceUri = this.getClass().getResource(resourceName);
74
			}
75
			logger.info("Loading test configuration from " + propertySourceUri);
76
			properties.loadFromXML(in);
77
			in.close();
78

    
79
			updateSystemProperties(false);
80

    
81
		} catch (FileNotFoundException e) {
82
			logger.error(e);
83
		} catch (IOException e) {
84
			logger.error(e);
85
		}
86

    
87

    
88
	}
89

    
90
	/**
91
	 *
92
	 */
93
	private void updateSystemProperties(boolean doOverride) {
94
		for(Object o : properties.keySet()){
95
			String key = (String)o;
96

    
97
			// update all webdriver properties and the browser property
98
			if(key.startsWith("webdriver.") || key.equals(WebDriverFactory.SYSTEM_PROPERTY_NAME_BROWSER)) {
99
				if(doOverride || System.getProperty(key) == null){
100
					String oldValue = "";
101
					if(System.getProperty(key) != null) {
102
						oldValue = " ,overriding old value: " + System.getProperty(key) + ")";
103
					}
104
					logger.info("Setting system property: " + key + ": " + properties.getProperty(key) + oldValue);
105
					System.setProperty(key, properties.getProperty(key));
106
				} else {
107
					logger.info("Not overriding system property: " + key + ": " + properties.getProperty(key) + " ,property already exists");
108
				}
109
			}
110
		}
111

    
112
	}
113

    
114
	public static String getProperty(String key) throws TestConfigurationException{
115
		return getProperty(key, String.class, false);
116
	}
117

    
118
	   public static String getProperty(String key, boolean nonNull) throws TestConfigurationException{
119
	        return getProperty(key, String.class, nonNull);
120
	    }
121

    
122
	@SuppressWarnings("unchecked")
123
	public static <T> T getProperty(String key, Class<T> type, boolean nonNull) throws TestConfigurationException{
124
		if(testConfiguration == null){
125
			testConfiguration = new TestConfiguration();
126
		}
127
		String value = testConfiguration.getProperties().getProperty(key);
128

    
129

    
130
		if(value != null){
131
			if(URI.class.isAssignableFrom(type)){
132
				try {
133
					return (T) new URI(value);
134
				} catch (URISyntaxException e) {
135
					throw new TestConfigurationException("Invalid URI " + value + " in property " + key + " of " + testConfiguration.propertySourceUri.toString(), e);
136
				}
137
			} else if(UUID.class.isAssignableFrom(type)){
138
				try {
139
					return (T) UUID.fromString(value);
140
				} catch (IllegalArgumentException e) {
141
					throw new TestConfigurationException("Invalid UUID " + value + " in property " + key + " of " + testConfiguration.propertySourceUri.toString(), e);
142
				}
143
			} else if(String.class.isAssignableFrom(type)){
144
				return (T) value;
145
			} else {
146
				throw new TestConfigurationException("Unsupported type " + type.toString());
147
			}
148
		} else {
149
			if( nonNull) {
150
				throw new TestConfigurationException("Property " + key + " of " + testConfiguration.propertySourceUri.toString() + " must not be null");
151
			}
152
		}
153

    
154
		return null;
155
	}
156

    
157
	public static void main(String[] args) {
158
		String userHome = System.getProperty("user.home");
159
		TestConfiguration.logger.error(userHome);
160
	}
161

    
162
}
(13-13/14)