1 package org.ogf.saga;
2
3
4 import org.apache.log4j.Logger;
5 import org.apache.log4j.Priority;
6 import org.junit.After;
7 import org.junit.Assert;
8 import org.junit.BeforeClass;
9 import org.junit.Rule;
10 import org.junit.rules.TestRule;
11 import org.junit.rules.TestWatcher;
12 import org.junit.runner.Description;
13 import org.ogf.saga.error.*;
14 import org.ogf.saga.url.URL;
15 import org.ogf.saga.url.URLFactory;
16
17 import java.io.*;
18 import java.util.*;
19
20
21
22
23
24
25
26
27
28
29
30
31
32 public abstract class JSAGABaseTest extends Assert {
33
34 protected static final String CONFIG_JOBSERVICE_URL = "jobservice.url";
35
36 protected static final String CONFIG_BASE_URL = "base.url";
37
38 protected static final String CONFIG_BASE2_URL = "base2.url";
39
40 protected static final String CONFIG_PHYSICAL_PROTOCOL = "physical.protocol";
41
42 private Properties m_properties;
43 private long startTime;
44 private Logger logger = Logger.getLogger(this.getClass());
45
46 public JSAGABaseTest() throws Exception {
47
48 if (System.getProperty("jsaga.default.contexts") == null) {
49 java.net.URL defaultContexts = this.getResource("etc/jsaga-default-contexts.xml");
50 if (defaultContexts != null) {
51 System.setProperty("jsaga.default.contexts", defaultContexts.toString());
52 }
53 }
54 if (System.getProperty("jsaga.timeout") == null) {
55 java.net.URL timeout = this.getResource("etc/jsaga-timeout.properties");
56 if (timeout != null) {
57 System.setProperty("jsaga.timeout", timeout.toString());
58 }
59 }
60 if (System.getProperty("log4j.configuration") == null) {
61 java.net.URL log4j = this.getResource("etc/log4j.properties");
62 if (log4j != null) {
63 System.setProperty("log4j.configuration", log4j.toString());
64 }
65 }
66
67
68 java.net.URL test = this.getResource("saga-test.properties");
69 if (test == null) {
70 throw new Exception("Resource not found: saga-test.properties");
71 }
72 m_properties = new Properties();
73 m_properties.load(test.openStream());
74
75 File developerTestProps = new File(new File(System.getProperty("user.home"), ".jsaga"), "saga-test.properties");
76 if (developerTestProps.exists()) {
77 Properties developerProps = new Properties();
78 developerProps.load(new FileInputStream(developerTestProps));
79 m_properties.putAll(developerProps);
80 }
81
82 System.getProperties().putAll(m_properties);
83 }
84
85 @Rule
86 public TestRule watcher = new TestWatcher() {
87 protected void starting(Description description) {
88 if(logger.isInfoEnabled())
89 logger.info(description.getMethodName()+" running...");
90 startTime = new Date().getTime();
91 }
92 protected void finished(Description description) {
93 if(logger.isDebugEnabled())
94 logger.debug(description.getMethodName()+" - Duration: "+ (new Date().getTime() - startTime)+" ms");
95 }
96
97 };
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118 protected String getOptionalProperty(String protocol, String name) {
119 return m_properties.getProperty(protocol+"."+name);
120 }
121
122 protected String getOptionalProperty(String protocol, String name, String defaultValue) {
123 return m_properties.getProperty(protocol+"."+name, defaultValue);
124 }
125
126 protected String getRequiredProperty(String protocol, String name) throws Exception {
127 String value = m_properties.getProperty(protocol+"."+name);
128 if (value != null) {
129 return value;
130 } else {
131 throw new Exception("Test properties file is missing required property: "+protocol+"."+name);
132 }
133 }
134
135 protected static URL createURL(URL base, String name) throws NotImplementedException, NoSuccessException, BadParameterException {
136 String basePath = base.getPath();
137 String path = (basePath.endsWith("/") ? basePath+name : basePath+"/"+name);
138 URL url = URLFactory.createURL(base.toString());
139 url.setPath(path);
140 return url;
141 }
142
143 private java.net.URL getResource(String path) throws IOException {
144 ClassLoader loader = this.getClass().getClassLoader();
145
146 String classPath = this.getClass().getName().replaceAll("\\.", "/") + ".class";
147 java.net.URL classResource = loader.getResource(classPath);
148 String classJar = getJar(classResource, classPath);
149
150 for (Enumeration<java.net.URL> e=loader.getResources(path); e.hasMoreElements(); ) {
151
152 java.net.URL resource = e.nextElement();
153 String jar = getJar(resource, path);
154
155 if (classJar.equals(jar)) {
156 return resource;
157 }
158 }
159 return null;
160 }
161 private static String getJar(java.net.URL resource, String path) {
162 int index = resource.toString().indexOf(path);
163 if (index > -1) {
164 return resource.toString().substring(0, index);
165 } else {
166 return null;
167 }
168 }
169 }