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