KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > test > TestConfigObject


1 /**
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
3  * notice. All rights reserved.
4  */

5 package com.tc.test;
6
7 import org.apache.commons.lang.StringUtils;
8
9 import com.tc.config.Directories;
10 import com.tc.logging.TCLogger;
11 import com.tc.logging.TCLogging;
12 import com.tc.util.Assert;
13 import com.tc.util.runtime.Os;
14
15 import java.io.File JavaDoc;
16 import java.io.FileInputStream JavaDoc;
17 import java.io.IOException JavaDoc;
18 import java.io.InputStream JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.Map JavaDoc;
21 import java.util.Properties JavaDoc;
22
23 /**
24  * Contains configuration data for tests.
25  * </p>
26  * <p>
27  * This class is a singleton. This is <em>ONLY</em> because this is used all over the place, in JUnit tests.
28  */

29 public class TestConfigObject {
30
31   private static final String JavaDoc APP_SERVER_WORKING = "sandbox";
32
33   public static final String JavaDoc TC_BASE_DIR = "tc.base-dir";
34
35   public static final String JavaDoc SPRING_VARIANT = "spring";
36
37   public static final String JavaDoc WEBFLOW_VARIANT = "spring-webflow";
38
39   private static final TCLogger logger = TCLogging.getLogger(TestConfigObject.class);
40
41   private static final String JavaDoc OS_NAME = "os.name";
42
43   private static final String JavaDoc DYNAMIC_PROPERTIES_PREFIX = "tc.tests.info.";
44
45   private static final String JavaDoc STATIC_PROPERTIES_PREFIX = "tc.tests.configuration.";
46
47   public static final String JavaDoc PROPERTY_FILE_LIST_PROPERTY_NAME = DYNAMIC_PROPERTIES_PREFIX + "property-files";
48
49   private static final String JavaDoc TEMP_DIRECTORY_ROOT = DYNAMIC_PROPERTIES_PREFIX + "temp-root";
50
51   private static final String JavaDoc DATA_DIRECTORY_ROOT = DYNAMIC_PROPERTIES_PREFIX + "data-root";
52
53   private static final String JavaDoc LINKED_CHILD_PROCESS_CLASSPATH = DYNAMIC_PROPERTIES_PREFIX
54                                                                      + "linked-child-process-classpath";
55
56   private static final String JavaDoc JVM_VERSION = DYNAMIC_PROPERTIES_PREFIX + "jvm.version";
57   private static final String JavaDoc JVM_TYPE = DYNAMIC_PROPERTIES_PREFIX + "jvm.type";
58   private static final String JavaDoc JVM_MODE = DYNAMIC_PROPERTIES_PREFIX + "jvm.mode";
59
60   private static final String JavaDoc BOOT_JAR_NORMAL = DYNAMIC_PROPERTIES_PREFIX + "bootjars.normal";
61
62   private static final String JavaDoc SESSION_CLASSPATH = DYNAMIC_PROPERTIES_PREFIX + "session.classpath";
63
64   private static final String JavaDoc SHORT_PATH_TEMP_DIR = DYNAMIC_PROPERTIES_PREFIX + "short-path-tempdir";
65
66   private static final String JavaDoc AVAILABLE_VARIANTS_PREFIX = DYNAMIC_PROPERTIES_PREFIX + "variants.available.";
67   private static final String JavaDoc VARIANT_LIBRARIES_PREFIX = DYNAMIC_PROPERTIES_PREFIX + "libraries.variants.";
68   private static final String JavaDoc SELECTED_VARIANT_PREFIX = DYNAMIC_PROPERTIES_PREFIX + "variants.selected.";
69   private static final String JavaDoc DEFAULT_VARIANT_PREFIX = STATIC_PROPERTIES_PREFIX + "variants.selected.";
70
71   private static final String JavaDoc EXECUTABLE_SEARCH_PATH = DYNAMIC_PROPERTIES_PREFIX
72                                                                      + "executable-search-path";
73
74   private static final String JavaDoc JUNIT_TEST_TIMEOUT_INSECONDS = DYNAMIC_PROPERTIES_PREFIX
75                                                                      + "junit-test-timeout-inseconds";
76
77   public static final String JavaDoc APP_SERVER_REPOSITORY_URL_BASE = STATIC_PROPERTIES_PREFIX + "appserver.repository";
78
79   public static final String JavaDoc APP_SERVER_HOME = DYNAMIC_PROPERTIES_PREFIX + "appserver.home";
80
81   private static final String JavaDoc APP_SERVER_FACTORY_NAME = STATIC_PROPERTIES_PREFIX
82                                                                      + "appserver.factory.name";
83
84   private static final String JavaDoc APP_SERVER_MAJOR_VERSION = STATIC_PROPERTIES_PREFIX
85                                                                      + "appserver.major-version";
86
87   private static final String JavaDoc APP_SERVER_MINOR_VERSION = STATIC_PROPERTIES_PREFIX
88                                                                      + "appserver.minor-version";
89
90   private static final String JavaDoc TRANSPARENT_TESTS_MODE = STATIC_PROPERTIES_PREFIX
91                                                                      + "transparent-tests.mode";
92   private static final String JavaDoc SPRING_TESTS_TIMEOUT = STATIC_PROPERTIES_PREFIX + "spring.tests.timeout";
93
94   private static final String JavaDoc SYSTEM_PROPERTIES_RESOURCE_NAME = "/test-system-properties.properties";
95
96
97
98   private static TestConfigObject INSTANCE;
99
100   private final Properties JavaDoc properties;
101
102   public static synchronized TestConfigObject getInstance() throws IOException JavaDoc {
103     if (INSTANCE == null) {
104       INSTANCE = new TestConfigObject();
105     }
106     return INSTANCE;
107   }
108
109   private static void loadSystemProperties() throws IOException JavaDoc {
110     InputStream JavaDoc in = null;
111
112     if (System.getProperty(PROPERTY_FILE_LIST_PROPERTY_NAME) == null) {
113       in = TestConfigObject.class.getResourceAsStream(SYSTEM_PROPERTIES_RESOURCE_NAME);
114       if (in != null) {
115         try {
116           Properties JavaDoc systemProperties = new Properties JavaDoc();
117           systemProperties.load(in);
118           Iterator JavaDoc iter = systemProperties.entrySet().iterator();
119
120           while (iter.hasNext()) {
121             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iter.next();
122             System.setProperty((String JavaDoc) entry.getKey(), (String JavaDoc) entry.getValue());
123           }
124
125           logger.info("Set " + systemProperties.size() + " system properties from resource '"
126                       + SYSTEM_PROPERTIES_RESOURCE_NAME + "'.");
127         } finally {
128           in.close();
129         }
130       }
131     }
132   }
133
134   // this is a hack
135
private static void loadEnv() {
136     File JavaDoc userDir = new File JavaDoc(System.getProperty("user.dir"));
137     String JavaDoc baseDirProp = System.getProperty(TC_BASE_DIR);
138     if (baseDirProp == null || baseDirProp.trim().equals("")) invalidBaseDir();
139     String JavaDoc[] baseDirParts = baseDirProp.split("[/\\\\]");
140     String JavaDoc baseDir = null;
141     int count = baseDirParts.length - 1;
142     File JavaDoc parent = null;
143
144     while (true) {
145       if (userDir.getName().equals(baseDirParts[count])) {
146         if (count == baseDirParts.length - 1) baseDir = userDir.getPath();
147         if (--count == -1) break;
148       }
149       if ((parent = userDir.getParentFile()) != null) userDir = parent;
150       else break;
151     }
152
153     if (baseDir == null || baseDir.trim().equals("")) invalidBaseDir();
154
155     if (StringUtils.isBlank(System.getProperty(Directories.TC_INSTALL_ROOT_PROPERTY_NAME))) {
156       System.setProperty(Directories.TC_INSTALL_ROOT_PROPERTY_NAME, baseDir);
157       System.setProperty(Directories.TC_INSTALL_ROOT_IGNORE_CHECKS_PROPERTY_NAME, "true");
158     }
159     System.setProperty(Directories.TC_LICENSE_LOCATION_PROPERTY_NAME, baseDir);
160   }
161
162   private static void invalidBaseDir() {
163     String JavaDoc value = System.getProperty(TC_BASE_DIR);
164     StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
165     buf.append("The value of the system property " + TC_BASE_DIR + " is not valid.");
166     buf.append(" The value is: \"").append(value).append("\"");
167     throw new RuntimeException JavaDoc(buf.toString());
168   }
169
170   private TestConfigObject() throws IOException JavaDoc {
171     this.properties = new Properties JavaDoc();
172     StringBuffer JavaDoc loadedFrom = new StringBuffer JavaDoc();
173
174     loadEnv();
175     loadSystemProperties();
176
177     int filesRead = 0;
178
179     // *DO NOT* hardcode system properties in here just to make tests easier
180
// to run in Eclipse.
181
// Doing so makes it a *great* deal harder to modify the build system.
182
// All you need to do
183
// to run tests in Eclipse is to run 'tcbuild check_prep <module-name>
184
// <test-type>' before
185
// you run your tests.
186
//
187
// If these things are too hard for you to do, please come talk to the
188
// build team. Hardcoding
189
// properties here can make our lives very difficult.
190

191     if (System.getProperty(PROPERTY_FILE_LIST_PROPERTY_NAME) == null) { throw new IOException JavaDoc(
192                                                                                               "You must set the system property '"
193                                                                                                   + PROPERTY_FILE_LIST_PROPERTY_NAME
194                                                                                                   + "' to point to the appropriate test properties file for this set of tests. If you're running "
195                                                                                                   + "from Eclipse, you can do this by running 'ant test.setup', which will generate a properties "
196                                                                                                   + "file in build/<module>/tests.<type>.configuration, and then setting the above system "
197                                                                                                   + "property to point to it."); }
198
199     String JavaDoc[] components = System.getProperty(PROPERTY_FILE_LIST_PROPERTY_NAME).split(File.pathSeparator);
200
201     for (int i = components.length - 1; i >= 0; --i) {
202       File JavaDoc thisFile = new File JavaDoc(components[i]);
203       if (thisFile.exists()) {
204         Properties JavaDoc theseProperties = new Properties JavaDoc();
205         theseProperties.load(new FileInputStream JavaDoc(thisFile));
206         this.properties.putAll(theseProperties);
207         if (filesRead > 0) loadedFrom.append(", ");
208         loadedFrom.append("'" + thisFile.getAbsolutePath() + "'");
209         ++filesRead;
210       }
211     }
212
213     if (filesRead > 0) loadedFrom.append(", ");
214     loadedFrom.append("system properties");
215
216     this.properties.putAll(System.getProperties());
217
218     logger.info("Loaded test configuration from " + loadedFrom.toString());
219   }
220
221   public String JavaDoc[] availableVariantsFor(String JavaDoc variantName) {
222     String JavaDoc out = this.properties.getProperty(AVAILABLE_VARIANTS_PREFIX + variantName);
223     if (StringUtils.isBlank(out)) return new String JavaDoc[0];
224     return out.split(",");
225   }
226
227   public String JavaDoc variantLibraryClasspathFor(String JavaDoc variantName, String JavaDoc variantValue) {
228     return this.properties.getProperty(VARIANT_LIBRARIES_PREFIX + variantName + "." + variantValue, "");
229   }
230
231   public String JavaDoc selectedVariantFor(String JavaDoc variantName) {
232     String JavaDoc selected = this.properties.getProperty(SELECTED_VARIANT_PREFIX + variantName);
233     if (null == selected) {
234       selected = this.properties.getProperty(DEFAULT_VARIANT_PREFIX + variantName);
235     }
236
237     return selected;
238   }
239
240   public String JavaDoc jvmVersion() {
241     String JavaDoc out = this.properties.getProperty(JVM_VERSION);
242     Assert.assertNotBlank(out);
243     return out;
244   }
245
246   public String JavaDoc jvmType() {
247     String JavaDoc out = this.properties.getProperty(JVM_TYPE);
248     Assert.assertNotBlank(out);
249     return out;
250   }
251
252   public String JavaDoc jvmMode() {
253     String JavaDoc out = this.properties.getProperty(JVM_MODE);
254     Assert.assertNotBlank(out);
255     return out;
256   }
257
258   public String JavaDoc osName() {
259     return this.properties.getProperty(OS_NAME);
260   }
261
262   public String JavaDoc platform() {
263     String JavaDoc osname = osName();
264     if (osname.startsWith("Windows")) {
265       return "windows";
266     }
267     else if (osname.startsWith("Linux")) {
268       return "linux";
269     }
270     else if (osname.startsWith("SunOS")) {
271       return "solaris";
272     }
273     else return osname;
274   }
275
276   public String JavaDoc dataDirectoryRoot() {
277     String JavaDoc out = this.properties.getProperty(DATA_DIRECTORY_ROOT);
278     Assert.assertNotBlank(out);
279     return out;
280   }
281
282   public String JavaDoc tempDirectoryRoot() {
283     String JavaDoc out = this.properties.getProperty(TEMP_DIRECTORY_ROOT);
284     Assert.assertNotBlank(out);
285     return out;
286   }
287
288   public String JavaDoc appserverURLBase() {
289     return this.properties.getProperty(APP_SERVER_REPOSITORY_URL_BASE);
290   }
291
292   public String JavaDoc appserverHome() {
293     return this.properties.getProperty(APP_SERVER_HOME);
294   }
295
296   public String JavaDoc appserverFactoryName() {
297     String JavaDoc out = this.properties.getProperty(APP_SERVER_FACTORY_NAME);
298     Assert.assertNotBlank(out);
299     return out;
300   }
301
302   public String JavaDoc appserverMajorVersion() {
303     String JavaDoc out = this.properties.getProperty(APP_SERVER_MAJOR_VERSION);
304     Assert.assertNotBlank(out);
305     return out;
306   }
307
308   public String JavaDoc appserverMinorVersion() {
309     String JavaDoc out = this.properties.getProperty(APP_SERVER_MINOR_VERSION);
310     Assert.assertNotBlank(out);
311     return out;
312   }
313
314   public String JavaDoc springTestsTimeout() {
315     return this.properties.getProperty(SPRING_TESTS_TIMEOUT);
316   }
317
318   private String JavaDoc shortPathNameTempDirectory() {
319     return this.properties.getProperty(SHORT_PATH_TEMP_DIR);
320   }
321
322   public String JavaDoc executableSearchPath() {
323     return this.properties.getProperty(EXECUTABLE_SEARCH_PATH);
324   }
325
326   private File JavaDoc effectiveShortPathNameTempDirectory() {
327     if (shortPathNameTempDirectory() != null) {
328       return new File JavaDoc(shortPathNameTempDirectory());
329     } else {
330       return new File JavaDoc(tempDirectoryRoot(), "short-names");
331     }
332   }
333
334   public String JavaDoc appserverServerInstallDir() {
335     // Glassfish gets mad (imqbrokersvc.exe crashes) if the install path is long on windows,
336
// maybe someday this hack won't be needed
337
if (Os.isWindows()) { return new File JavaDoc(effectiveShortPathNameTempDirectory(), "AS").getAbsolutePath(); }
338
339     File JavaDoc buildDir = new File JavaDoc(System.getProperty(TC_BASE_DIR), ".tc-build-cache");
340     return new File JavaDoc(buildDir, "app-server-install").getAbsolutePath();
341   }
342
343   public String JavaDoc appserverWorkingDir() {
344     return new File JavaDoc(effectiveShortPathNameTempDirectory(), APP_SERVER_WORKING).getAbsolutePath();
345   }
346
347   public String JavaDoc normalBootJar() {
348     String JavaDoc out = this.properties.getProperty(BOOT_JAR_NORMAL);
349     Assert.assertNotBlank(out);
350     assertFileExists(out);
351     return out;
352   }
353
354   public String JavaDoc linkedChildProcessClasspath() {
355     String JavaDoc out = this.properties.getProperty(LINKED_CHILD_PROCESS_CLASSPATH);
356     Assert.assertNotBlank(out);
357     assertValidClasspath(out);
358     return out;
359   }
360
361   public String JavaDoc sessionClasspath() {
362     String JavaDoc out = this.properties.getProperty(SESSION_CLASSPATH);
363     Assert.assertNotBlank(out);
364     assertValidClasspath(out);
365     return out;
366   }
367
368   public int getJunitTimeoutInSeconds() {
369     String JavaDoc seconds = this.properties.getProperty(JUNIT_TEST_TIMEOUT_INSECONDS);
370     Assert.assertNotBlank(seconds);
371     return Integer.parseInt(seconds);
372   }
373
374   public static final String JavaDoc TRANSPARENT_TESTS_MODE_NORMAL = "normal";
375   public static final String JavaDoc TRANSPARENT_TESTS_MODE_RESTART = "restart";
376   public static final String JavaDoc TRANSPARENT_TESTS_MODE_CRASH = "crash";
377
378   private static final String JavaDoc[] ALL_TRANSPARENT_TESTS_MODES = { TRANSPARENT_TESTS_MODE_NORMAL,
379       TRANSPARENT_TESTS_MODE_RESTART, TRANSPARENT_TESTS_MODE_CRASH };
380
381   public String JavaDoc transparentTestsMode() {
382     String JavaDoc out = this.properties.getProperty(TRANSPARENT_TESTS_MODE);
383     Assert.assertNotBlank(out);
384
385     boolean foundIt = false;
386     for (int i = 0; i < ALL_TRANSPARENT_TESTS_MODES.length; ++i) {
387       foundIt = foundIt || ALL_TRANSPARENT_TESTS_MODES[i].equals(out);
388     }
389
390     Assert.eval(foundIt);
391
392     return out;
393   }
394
395   private void assertValidClasspath(String JavaDoc out) {
396     String JavaDoc[] pathElements = out.split(File.pathSeparator);
397     for (int i = 0; i < pathElements.length; i++) {
398       String JavaDoc pathElement = pathElements[i];
399       Assert.assertTrue("Path element is non-existent: " + pathElement, new File JavaDoc(pathElement).exists());
400
401     }
402   }
403
404   private void assertFileExists(String JavaDoc out) {
405     File JavaDoc file = new File JavaDoc(out);
406     Assert.assertTrue("not a file: " + out, file.isFile());
407   }
408 }
409
Popular Tags