1 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 ; 16 import java.io.FileInputStream ; 17 import java.io.IOException ; 18 import java.io.InputStream ; 19 import java.util.Iterator ; 20 import java.util.Map ; 21 import java.util.Properties ; 22 23 29 public class TestConfigObject { 30 31 private static final String APP_SERVER_WORKING = "sandbox"; 32 33 public static final String TC_BASE_DIR = "tc.base-dir"; 34 35 public static final String SPRING_VARIANT = "spring"; 36 37 public static final String WEBFLOW_VARIANT = "spring-webflow"; 38 39 private static final TCLogger logger = TCLogging.getLogger(TestConfigObject.class); 40 41 private static final String OS_NAME = "os.name"; 42 43 private static final String DYNAMIC_PROPERTIES_PREFIX = "tc.tests.info."; 44 45 private static final String STATIC_PROPERTIES_PREFIX = "tc.tests.configuration."; 46 47 public static final String PROPERTY_FILE_LIST_PROPERTY_NAME = DYNAMIC_PROPERTIES_PREFIX + "property-files"; 48 49 private static final String TEMP_DIRECTORY_ROOT = DYNAMIC_PROPERTIES_PREFIX + "temp-root"; 50 51 private static final String DATA_DIRECTORY_ROOT = DYNAMIC_PROPERTIES_PREFIX + "data-root"; 52 53 private static final String LINKED_CHILD_PROCESS_CLASSPATH = DYNAMIC_PROPERTIES_PREFIX 54 + "linked-child-process-classpath"; 55 56 private static final String JVM_VERSION = DYNAMIC_PROPERTIES_PREFIX + "jvm.version"; 57 private static final String JVM_TYPE = DYNAMIC_PROPERTIES_PREFIX + "jvm.type"; 58 private static final String JVM_MODE = DYNAMIC_PROPERTIES_PREFIX + "jvm.mode"; 59 60 private static final String BOOT_JAR_NORMAL = DYNAMIC_PROPERTIES_PREFIX + "bootjars.normal"; 61 62 private static final String SESSION_CLASSPATH = DYNAMIC_PROPERTIES_PREFIX + "session.classpath"; 63 64 private static final String SHORT_PATH_TEMP_DIR = DYNAMIC_PROPERTIES_PREFIX + "short-path-tempdir"; 65 66 private static final String AVAILABLE_VARIANTS_PREFIX = DYNAMIC_PROPERTIES_PREFIX + "variants.available."; 67 private static final String VARIANT_LIBRARIES_PREFIX = DYNAMIC_PROPERTIES_PREFIX + "libraries.variants."; 68 private static final String SELECTED_VARIANT_PREFIX = DYNAMIC_PROPERTIES_PREFIX + "variants.selected."; 69 private static final String DEFAULT_VARIANT_PREFIX = STATIC_PROPERTIES_PREFIX + "variants.selected."; 70 71 private static final String EXECUTABLE_SEARCH_PATH = DYNAMIC_PROPERTIES_PREFIX 72 + "executable-search-path"; 73 74 private static final String JUNIT_TEST_TIMEOUT_INSECONDS = DYNAMIC_PROPERTIES_PREFIX 75 + "junit-test-timeout-inseconds"; 76 77 public static final String APP_SERVER_REPOSITORY_URL_BASE = STATIC_PROPERTIES_PREFIX + "appserver.repository"; 78 79 public static final String APP_SERVER_HOME = DYNAMIC_PROPERTIES_PREFIX + "appserver.home"; 80 81 private static final String APP_SERVER_FACTORY_NAME = STATIC_PROPERTIES_PREFIX 82 + "appserver.factory.name"; 83 84 private static final String APP_SERVER_MAJOR_VERSION = STATIC_PROPERTIES_PREFIX 85 + "appserver.major-version"; 86 87 private static final String APP_SERVER_MINOR_VERSION = STATIC_PROPERTIES_PREFIX 88 + "appserver.minor-version"; 89 90 private static final String TRANSPARENT_TESTS_MODE = STATIC_PROPERTIES_PREFIX 91 + "transparent-tests.mode"; 92 private static final String SPRING_TESTS_TIMEOUT = STATIC_PROPERTIES_PREFIX + "spring.tests.timeout"; 93 94 private static final String SYSTEM_PROPERTIES_RESOURCE_NAME = "/test-system-properties.properties"; 95 96 97 98 private static TestConfigObject INSTANCE; 99 100 private final Properties properties; 101 102 public static synchronized TestConfigObject getInstance() throws IOException { 103 if (INSTANCE == null) { 104 INSTANCE = new TestConfigObject(); 105 } 106 return INSTANCE; 107 } 108 109 private static void loadSystemProperties() throws IOException { 110 InputStream 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 systemProperties = new Properties (); 117 systemProperties.load(in); 118 Iterator iter = systemProperties.entrySet().iterator(); 119 120 while (iter.hasNext()) { 121 Map.Entry entry = (Map.Entry ) iter.next(); 122 System.setProperty((String ) entry.getKey(), (String ) 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 private static void loadEnv() { 136 File userDir = new File (System.getProperty("user.dir")); 137 String baseDirProp = System.getProperty(TC_BASE_DIR); 138 if (baseDirProp == null || baseDirProp.trim().equals("")) invalidBaseDir(); 139 String [] baseDirParts = baseDirProp.split("[/\\\\]"); 140 String baseDir = null; 141 int count = baseDirParts.length - 1; 142 File 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 value = System.getProperty(TC_BASE_DIR); 164 StringBuffer buf = new StringBuffer (); 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 (buf.toString()); 168 } 169 170 private TestConfigObject() throws IOException { 171 this.properties = new Properties (); 172 StringBuffer loadedFrom = new StringBuffer (); 173 174 loadEnv(); 175 loadSystemProperties(); 176 177 int filesRead = 0; 178 179 191 if (System.getProperty(PROPERTY_FILE_LIST_PROPERTY_NAME) == null) { throw new IOException ( 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 [] components = System.getProperty(PROPERTY_FILE_LIST_PROPERTY_NAME).split(File.pathSeparator); 200 201 for (int i = components.length - 1; i >= 0; --i) { 202 File thisFile = new File (components[i]); 203 if (thisFile.exists()) { 204 Properties theseProperties = new Properties (); 205 theseProperties.load(new FileInputStream (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 [] availableVariantsFor(String variantName) { 222 String out = this.properties.getProperty(AVAILABLE_VARIANTS_PREFIX + variantName); 223 if (StringUtils.isBlank(out)) return new String [0]; 224 return out.split(","); 225 } 226 227 public String variantLibraryClasspathFor(String variantName, String variantValue) { 228 return this.properties.getProperty(VARIANT_LIBRARIES_PREFIX + variantName + "." + variantValue, ""); 229 } 230 231 public String selectedVariantFor(String variantName) { 232 String 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 jvmVersion() { 241 String out = this.properties.getProperty(JVM_VERSION); 242 Assert.assertNotBlank(out); 243 return out; 244 } 245 246 public String jvmType() { 247 String out = this.properties.getProperty(JVM_TYPE); 248 Assert.assertNotBlank(out); 249 return out; 250 } 251 252 public String jvmMode() { 253 String out = this.properties.getProperty(JVM_MODE); 254 Assert.assertNotBlank(out); 255 return out; 256 } 257 258 public String osName() { 259 return this.properties.getProperty(OS_NAME); 260 } 261 262 public String platform() { 263 String 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 dataDirectoryRoot() { 277 String out = this.properties.getProperty(DATA_DIRECTORY_ROOT); 278 Assert.assertNotBlank(out); 279 return out; 280 } 281 282 public String tempDirectoryRoot() { 283 String out = this.properties.getProperty(TEMP_DIRECTORY_ROOT); 284 Assert.assertNotBlank(out); 285 return out; 286 } 287 288 public String appserverURLBase() { 289 return this.properties.getProperty(APP_SERVER_REPOSITORY_URL_BASE); 290 } 291 292 public String appserverHome() { 293 return this.properties.getProperty(APP_SERVER_HOME); 294 } 295 296 public String appserverFactoryName() { 297 String out = this.properties.getProperty(APP_SERVER_FACTORY_NAME); 298 Assert.assertNotBlank(out); 299 return out; 300 } 301 302 public String appserverMajorVersion() { 303 String out = this.properties.getProperty(APP_SERVER_MAJOR_VERSION); 304 Assert.assertNotBlank(out); 305 return out; 306 } 307 308 public String appserverMinorVersion() { 309 String out = this.properties.getProperty(APP_SERVER_MINOR_VERSION); 310 Assert.assertNotBlank(out); 311 return out; 312 } 313 314 public String springTestsTimeout() { 315 return this.properties.getProperty(SPRING_TESTS_TIMEOUT); 316 } 317 318 private String shortPathNameTempDirectory() { 319 return this.properties.getProperty(SHORT_PATH_TEMP_DIR); 320 } 321 322 public String executableSearchPath() { 323 return this.properties.getProperty(EXECUTABLE_SEARCH_PATH); 324 } 325 326 private File effectiveShortPathNameTempDirectory() { 327 if (shortPathNameTempDirectory() != null) { 328 return new File (shortPathNameTempDirectory()); 329 } else { 330 return new File (tempDirectoryRoot(), "short-names"); 331 } 332 } 333 334 public String appserverServerInstallDir() { 335 if (Os.isWindows()) { return new File (effectiveShortPathNameTempDirectory(), "AS").getAbsolutePath(); } 338 339 File buildDir = new File (System.getProperty(TC_BASE_DIR), ".tc-build-cache"); 340 return new File (buildDir, "app-server-install").getAbsolutePath(); 341 } 342 343 public String appserverWorkingDir() { 344 return new File (effectiveShortPathNameTempDirectory(), APP_SERVER_WORKING).getAbsolutePath(); 345 } 346 347 public String normalBootJar() { 348 String out = this.properties.getProperty(BOOT_JAR_NORMAL); 349 Assert.assertNotBlank(out); 350 assertFileExists(out); 351 return out; 352 } 353 354 public String linkedChildProcessClasspath() { 355 String out = this.properties.getProperty(LINKED_CHILD_PROCESS_CLASSPATH); 356 Assert.assertNotBlank(out); 357 assertValidClasspath(out); 358 return out; 359 } 360 361 public String sessionClasspath() { 362 String out = this.properties.getProperty(SESSION_CLASSPATH); 363 Assert.assertNotBlank(out); 364 assertValidClasspath(out); 365 return out; 366 } 367 368 public int getJunitTimeoutInSeconds() { 369 String seconds = this.properties.getProperty(JUNIT_TEST_TIMEOUT_INSECONDS); 370 Assert.assertNotBlank(seconds); 371 return Integer.parseInt(seconds); 372 } 373 374 public static final String TRANSPARENT_TESTS_MODE_NORMAL = "normal"; 375 public static final String TRANSPARENT_TESTS_MODE_RESTART = "restart"; 376 public static final String TRANSPARENT_TESTS_MODE_CRASH = "crash"; 377 378 private static final String [] ALL_TRANSPARENT_TESTS_MODES = { TRANSPARENT_TESTS_MODE_NORMAL, 379 TRANSPARENT_TESTS_MODE_RESTART, TRANSPARENT_TESTS_MODE_CRASH }; 380 381 public String transparentTestsMode() { 382 String 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 out) { 396 String [] pathElements = out.split(File.pathSeparator); 397 for (int i = 0; i < pathElements.length; i++) { 398 String pathElement = pathElements[i]; 399 Assert.assertTrue("Path element is non-existent: " + pathElement, new File (pathElement).exists()); 400 401 } 402 } 403 404 private void assertFileExists(String out) { 405 File file = new File (out); 406 Assert.assertTrue("not a file: " + out, file.isFile()); 407 } 408 } 409 | Popular Tags |