001: /**
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */package com.tc.test;
005:
006: import org.apache.commons.lang.StringUtils;
007:
008: import com.tc.config.Directories;
009: import com.tc.logging.TCLogger;
010: import com.tc.logging.TCLogging;
011: import com.tc.util.Assert;
012: import com.tc.util.runtime.Os;
013: import com.tc.util.runtime.Vm;
014:
015: import java.io.File;
016: import java.io.FileInputStream;
017: import java.io.IOException;
018: import java.io.InputStream;
019: import java.util.Iterator;
020: import java.util.Map;
021: import java.util.Properties;
022:
023: /**
024: * Contains configuration data for tests.
025: * </p>
026: * <p>
027: * This class is a singleton. This is <em>ONLY</em> because this is used all over the place, in JUnit tests.
028: */
029: public class TestConfigObject {
030: public static final String NATIVE_LIB_LINUX_32 = "Linux";
031:
032: public static final String NATIVE_LIB_LINUX_64 = "Linux64";
033:
034: public static final String UNIX_NATIVE_LIB_NAME = "libGetPid.so";
035:
036: public static final String WINDOWS_NATIVE_LIB_NAME = "GetPid.dll";
037:
038: public static final String OSX_NATIVE_LIB_NAME = "libGetPid.jnilib";
039:
040: public static final String TC_BASE_DIR = "tc.base-dir";
041:
042: public static final String SPRING_VARIANT = "spring";
043:
044: public static final String WEBFLOW_VARIANT = "spring-webflow";
045:
046: private static final TCLogger logger = TCLogging
047: .getLogger(TestConfigObject.class);
048:
049: private static final String OS_NAME = "os.name";
050:
051: private static final String DYNAMIC_PROPERTIES_PREFIX = "tc.tests.info.";
052:
053: private static final String STATIC_PROPERTIES_PREFIX = "tc.tests.configuration.";
054:
055: public static final String PROPERTY_FILE_LIST_PROPERTY_NAME = DYNAMIC_PROPERTIES_PREFIX
056: + "property-files";
057:
058: private static final String TEMP_DIRECTORY_ROOT = DYNAMIC_PROPERTIES_PREFIX
059: + "temp-root";
060:
061: private static final String DATA_DIRECTORY_ROOT = DYNAMIC_PROPERTIES_PREFIX
062: + "data-root";
063:
064: private static final String LINKED_CHILD_PROCESS_CLASSPATH = DYNAMIC_PROPERTIES_PREFIX
065: + "linked-child-process-classpath";
066:
067: private static final String JVM_VERSION = DYNAMIC_PROPERTIES_PREFIX
068: + "jvm.version";
069: private static final String JVM_TYPE = DYNAMIC_PROPERTIES_PREFIX
070: + "jvm.type";
071: private static final String JVM_MODE = DYNAMIC_PROPERTIES_PREFIX
072: + "jvm.mode";
073:
074: private static final String BOOT_JAR_NORMAL = DYNAMIC_PROPERTIES_PREFIX
075: + "bootjars.normal";
076:
077: private static final String SESSION_CLASSPATH = DYNAMIC_PROPERTIES_PREFIX
078: + "session.classpath";
079:
080: private static final String AVAILABLE_VARIANTS_PREFIX = DYNAMIC_PROPERTIES_PREFIX
081: + "variants.available.";
082: private static final String VARIANT_LIBRARIES_PREFIX = DYNAMIC_PROPERTIES_PREFIX
083: + "libraries.variants.";
084: private static final String SELECTED_VARIANT_PREFIX = DYNAMIC_PROPERTIES_PREFIX
085: + "variants.selected.";
086: private static final String DEFAULT_VARIANT_PREFIX = STATIC_PROPERTIES_PREFIX
087: + "variants.selected.";
088:
089: private static final String EXECUTABLE_SEARCH_PATH = DYNAMIC_PROPERTIES_PREFIX
090: + "executable-search-path";
091:
092: private static final String JUNIT_TEST_TIMEOUT_INSECONDS = DYNAMIC_PROPERTIES_PREFIX
093: + "junit-test-timeout-inseconds";
094:
095: public static final String APP_SERVER_REPOSITORY_URL_BASE = STATIC_PROPERTIES_PREFIX
096: + "appserver.repository";
097:
098: public static final String APP_SERVER_HOME = STATIC_PROPERTIES_PREFIX
099: + "appserver.home";
100:
101: private static final String APP_SERVER_FACTORY_NAME = STATIC_PROPERTIES_PREFIX
102: + "appserver.factory.name";
103:
104: private static final String APP_SERVER_MAJOR_VERSION = STATIC_PROPERTIES_PREFIX
105: + "appserver.major-version";
106:
107: private static final String APP_SERVER_MINOR_VERSION = STATIC_PROPERTIES_PREFIX
108: + "appserver.minor-version";
109:
110: private static final String TRANSPARENT_TESTS_MODE = STATIC_PROPERTIES_PREFIX
111: + "transparent-tests.mode";
112: private static final String SPRING_TESTS_TIMEOUT = STATIC_PROPERTIES_PREFIX
113: + "spring.tests.timeout";
114:
115: private static final String SYSTEM_PROPERTIES_RESOURCE_NAME = "/test-system-properties.properties";
116:
117: private static final String L2_STARTUP_PREFIX = DYNAMIC_PROPERTIES_PREFIX
118: + "l2.startup.";
119: public static final String L2_STARTUP_MODE = L2_STARTUP_PREFIX
120: + "mode";
121: public static final String L2_STARTUP_JAVA_HOME = L2_STARTUP_PREFIX
122: + "jvm";
123:
124: private static TestConfigObject INSTANCE;
125:
126: private final Properties properties;
127:
128: public static synchronized TestConfigObject getInstance() {
129: if (INSTANCE == null) {
130: try {
131: INSTANCE = new TestConfigObject();
132: } catch (IOException e) {
133: throw new RuntimeException(e);
134: }
135: }
136: return INSTANCE;
137: }
138:
139: private static void loadSystemProperties() throws IOException {
140: InputStream in = null;
141:
142: if (System.getProperty(PROPERTY_FILE_LIST_PROPERTY_NAME) == null) {
143: in = TestConfigObject.class
144: .getResourceAsStream(SYSTEM_PROPERTIES_RESOURCE_NAME);
145: if (in != null) {
146: try {
147: Properties systemProperties = new Properties();
148: systemProperties.load(in);
149: Iterator iter = systemProperties.entrySet()
150: .iterator();
151:
152: while (iter.hasNext()) {
153: Map.Entry entry = (Map.Entry) iter.next();
154: System.setProperty((String) entry.getKey(),
155: (String) entry.getValue());
156: }
157:
158: logger.info("Set " + systemProperties.size()
159: + " system properties from resource '"
160: + SYSTEM_PROPERTIES_RESOURCE_NAME + "'.");
161: } finally {
162: in.close();
163: }
164: }
165: }
166: }
167:
168: private static void loadEnv() throws IOException {
169: initBaseDir();
170:
171: if (!StringUtils
172: .isBlank(System
173: .getProperty(Directories.TC_INSTALL_ROOT_PROPERTY_NAME))) {
174: throw new RuntimeException("Don't set '"
175: + Directories.TC_INSTALL_ROOT_PROPERTY_NAME
176: + "' in tests.");
177: }
178: System
179: .setProperty(
180: Directories.TC_INSTALL_ROOT_IGNORE_CHECKS_PROPERTY_NAME,
181: "true");
182: System.setProperty(
183: Directories.TC_LICENSE_LOCATION_PROPERTY_NAME, baseDir
184: .getCanonicalPath());
185: }
186:
187: private static void initBaseDir() {
188: String baseDirProp = System.getProperty(TC_BASE_DIR);
189: if (baseDirProp == null || baseDirProp.trim().equals(""))
190: invalidBaseDir();
191: baseDir = new File(baseDirProp);
192: if (!baseDir.isDirectory())
193: invalidBaseDir();
194: }
195:
196: private static void invalidBaseDir() {
197: baseDir = null;
198: String value = System.getProperty(TC_BASE_DIR);
199: StringBuffer buf = new StringBuffer();
200: buf.append("The value of the system property " + TC_BASE_DIR
201: + " is not valid.");
202: buf.append(" The value is: \"").append(value).append("\"");
203: throw new RuntimeException(buf.toString());
204: }
205:
206: private TestConfigObject() throws IOException {
207: this .properties = new Properties();
208: StringBuffer loadedFrom = new StringBuffer();
209:
210: loadEnv();
211: loadSystemProperties();
212:
213: int filesRead = 0;
214:
215: // *DO NOT* hardcode system properties in here just to make tests easier
216: // to run in Eclipse.
217: // Doing so makes it a *great* deal harder to modify the build system.
218: // All you need to do
219: // to run tests in Eclipse is to run 'tcbuild check_prep <module-name>
220: // <test-type>' before
221: // you run your tests.
222: //
223: // If these things are too hard for you to do, please come talk to the
224: // build team. Hardcoding
225: // properties here can make our lives very difficult.
226:
227: String[] components = {};
228: if (System.getProperty(PROPERTY_FILE_LIST_PROPERTY_NAME) != null) {
229: components = System.getProperty(
230: PROPERTY_FILE_LIST_PROPERTY_NAME).split(
231: File.pathSeparator);
232: }
233:
234: for (int i = components.length - 1; i >= 0; --i) {
235: File this File = new File(components[i]);
236: if (this File.exists()) {
237: Properties theseProperties = new Properties();
238: theseProperties.load(new FileInputStream(this File));
239: this .properties.putAll(theseProperties);
240: if (filesRead > 0)
241: loadedFrom.append(", ");
242: loadedFrom.append("'" + this File.getAbsolutePath()
243: + "'");
244: ++filesRead;
245: }
246: }
247:
248: if (filesRead > 0)
249: loadedFrom.append(", ");
250: loadedFrom.append("system properties");
251:
252: this .properties.putAll(System.getProperties());
253:
254: logger.info("Loaded test configuration from "
255: + loadedFrom.toString());
256: }
257:
258: private String getProperty(String key, String defaultValue) {
259: String result = this .properties.getProperty(key);
260: if (result == null) {
261: result = defaultValue;
262: }
263: return result;
264: }
265:
266: private String getProperty(String key) {
267: return getProperty(key, null);
268: }
269:
270: public String getL2StartupMode() {
271: return this .properties.getProperty(L2_STARTUP_MODE);
272: }
273:
274: public boolean isL2StartupModeExternal() {
275: return "external".equalsIgnoreCase(getL2StartupMode());
276: }
277:
278: public String getL2StartupJavaHome() {
279: String result = this .properties
280: .getProperty(L2_STARTUP_JAVA_HOME);
281: if (result == null && Vm.isJDK15Compliant()) {
282: result = System.getProperty("java.home");
283: }
284: return result;
285: }
286:
287: public String[] availableVariantsFor(String variantName) {
288: String out = this .properties
289: .getProperty(AVAILABLE_VARIANTS_PREFIX + variantName);
290: if (StringUtils.isBlank(out))
291: return new String[0];
292: return out.split(",");
293: }
294:
295: public String variantLibraryClasspathFor(String variantName,
296: String variantValue) {
297: return this .properties.getProperty(VARIANT_LIBRARIES_PREFIX
298: + variantName + "." + variantValue, "");
299: }
300:
301: public String selectedVariantFor(String variantName) {
302: String selected = this .properties
303: .getProperty(SELECTED_VARIANT_PREFIX + variantName);
304: if (null == selected) {
305: selected = this .properties
306: .getProperty(DEFAULT_VARIANT_PREFIX + variantName);
307: }
308:
309: return selected;
310: }
311:
312: /**
313: * Returns the version string for the current JVM. Equivalent to
314: * <code>System.getProperty("java.runtime.version")</code>.
315: */
316: public String jvmVersion() {
317: return System.getProperty("java.runtime.version");
318: }
319:
320: /**
321: * Returns the type of the current JVM. Equivalent to <code>System.getProperty("java.vm.name")</code>.
322: */
323: public String jvmName() {
324: return System.getProperty("java.vm.name");
325: }
326:
327: public String osName() {
328: return getProperty(OS_NAME);
329: }
330:
331: public String platform() {
332: String osname = osName();
333: if (osname.startsWith("Windows")) {
334: return "windows";
335: } else if (osname.startsWith("Linux")) {
336: return "linux";
337: } else if (osname.startsWith("SunOS")) {
338: return "solaris";
339: } else
340: return osname;
341: }
342:
343: public String nativeLibName() {
344: String osname = osName();
345: if (osname.startsWith("Windows")) {
346: return WINDOWS_NATIVE_LIB_NAME;
347: } else if (osname.startsWith("Darwin")) {
348: return OSX_NATIVE_LIB_NAME;
349: } else {
350: return UNIX_NATIVE_LIB_NAME;
351: }
352: }
353:
354: public String dataDirectoryRoot() {
355: return getProperty(DATA_DIRECTORY_ROOT, new File(baseDir,
356: "test-data").getAbsolutePath());
357: }
358:
359: public String tempDirectoryRoot() {
360: return getProperty(TEMP_DIRECTORY_ROOT, new File(baseDir,
361: "temp").getAbsolutePath());
362: }
363:
364: public String appserverHome() {
365: return this .properties.getProperty(APP_SERVER_HOME);
366: }
367:
368: public String appserverFactoryName() {
369: String out = this .properties
370: .getProperty(APP_SERVER_FACTORY_NAME);
371: Assert.assertNotBlank(out);
372: return out;
373: }
374:
375: public String appserverMajorVersion() {
376: String out = this .properties
377: .getProperty(APP_SERVER_MAJOR_VERSION);
378: Assert.assertNotBlank(out);
379: return out;
380: }
381:
382: public String appserverMinorVersion() {
383: String out = this .properties
384: .getProperty(APP_SERVER_MINOR_VERSION);
385: Assert.assertNotBlank(out);
386: return out;
387: }
388:
389: public String springTestsTimeout() {
390: return this .properties.getProperty(SPRING_TESTS_TIMEOUT);
391: }
392:
393: public String executableSearchPath() {
394: String nativeLibDirPath = this .properties
395: .getProperty(EXECUTABLE_SEARCH_PATH);
396: if (nativeLibDirPath.endsWith(NATIVE_LIB_LINUX_32)
397: || nativeLibDirPath.endsWith(NATIVE_LIB_LINUX_64)) {
398: int lastSeparator = nativeLibDirPath
399: .lastIndexOf(File.separator);
400: String vmType = System.getProperty("sun.arch.data.model");
401: if (vmType.equals("32")) {
402: nativeLibDirPath = nativeLibDirPath.substring(0,
403: lastSeparator)
404: + File.separator + NATIVE_LIB_LINUX_32;
405: } else if (vmType.equals("64")) {
406: nativeLibDirPath = nativeLibDirPath.substring(0,
407: lastSeparator)
408: + File.separator + NATIVE_LIB_LINUX_64;
409: }
410: }
411: return nativeLibDirPath;
412: }
413:
414: public File cacheDir() {
415: String root = System.getProperty("user.home");
416: if (Os.isWindows()) {
417: File temp = new File("c:/temp");
418: if (!temp.exists()) {
419: temp = new File(root.substring(0, 2) + "/temp");
420: }
421: return temp;
422: }
423: return new File(root, ".tc");
424: }
425:
426: public File appserverServerInstallDir() {
427: File installDir = new File(cacheDir(), "appservers");
428: if (!installDir.exists())
429: installDir.mkdirs();
430: return installDir;
431: }
432:
433: public String normalBootJar() {
434: String out = this .properties.getProperty(BOOT_JAR_NORMAL);
435: Assert.assertNotBlank(out);
436: assertFileExists(out);
437: return out;
438: }
439:
440: public String linkedChildProcessClasspath() {
441: String out = this .properties
442: .getProperty(LINKED_CHILD_PROCESS_CLASSPATH);
443: Assert.assertNotBlank(out);
444: assertValidClasspath(out);
445: return out;
446: }
447:
448: public String sessionClasspath() {
449: String out = this .properties.getProperty(SESSION_CLASSPATH);
450: Assert.assertNotBlank(out);
451: assertValidClasspath(out);
452: return out;
453: }
454:
455: public int getJunitTimeoutInSeconds() {
456: return Integer.parseInt(getProperty(
457: JUNIT_TEST_TIMEOUT_INSECONDS, "900"));
458: }
459:
460: public static final String TRANSPARENT_TESTS_MODE_NORMAL = "normal";
461: public static final String TRANSPARENT_TESTS_MODE_CRASH = "crash";
462: public static final String TRANSPARENT_TESTS_MODE_ACTIVE_PASSIVE = "active-passive";
463:
464: private static final String[] ALL_TRANSPARENT_TESTS_MODES = {
465: TRANSPARENT_TESTS_MODE_NORMAL,
466: TRANSPARENT_TESTS_MODE_CRASH,
467: TRANSPARENT_TESTS_MODE_ACTIVE_PASSIVE };
468:
469: private static File baseDir;
470:
471: public String transparentTestsMode() {
472: return getProperty(TRANSPARENT_TESTS_MODE,
473: TRANSPARENT_TESTS_MODE_NORMAL);
474: }
475:
476: private void assertValidClasspath(String out) {
477: String[] pathElements = out.split(File.pathSeparator);
478: for (int i = 0; i < pathElements.length; i++) {
479: String pathElement = pathElements[i];
480: Assert.assertTrue("Path element is non-existent: "
481: + pathElement, new File(pathElement).exists());
482:
483: }
484: }
485:
486: private void assertFileExists(String out) {
487: File file = new File(out);
488: Assert.assertTrue("not a file: " + out, file.isFile());
489: }
490: }
|