1 20 package org.apache.derbyTesting.junit; 21 22 import java.io.File ; 23 import java.lang.reflect.Method ; 24 import java.security.*; 25 import java.sql.Connection ; 26 import java.sql.DriverManager ; 27 import java.sql.SQLException ; 28 import java.util.Properties ; 29 30 import junit.framework.Test; 31 import junit.framework.TestCase; 32 33 import org.apache.derbyTesting.functionTests.util.TestDataSourceFactory; 34 35 38 public class TestConfiguration { 39 42 private final static String DEFAULT_DBNAME = "wombat"; 43 private final static String DEFAULT_USER_NAME = "APP"; 44 private final static String DEFAULT_USER_PASSWORD = "APP"; 45 private final static int DEFAULT_PORT = 1527; 46 private final static String DEFAULT_FRAMEWORK = "embedded"; 47 private final static String DEFAULT_HOSTNAME = "localhost"; 48 49 52 private final static String KEY_DBNAME = "databaseName"; 53 private final static String KEY_FRAMEWORK = "framework"; 54 private final static String KEY_USER_PASSWORD = "password"; 55 private final static String KEY_USER_NAME = "user"; 56 private final static String KEY_HOSTNAME = "hostName"; 57 private final static String KEY_PORT = "port"; 58 private final static String KEY_VERBOSE = "derby.tests.debug"; 59 private final static String KEY_SINGLE_LEG_XA = "derbyTesting.xa.single"; 60 61 64 private final static String UNUSED = "file://unused/"; 65 66 67 71 private static final TestConfiguration DERBY_HARNESS_CONFIG = 72 new TestConfiguration(getSystemProperties()); 73 74 78 private static final TestConfiguration JUNIT_CONFIG 79 = new TestConfiguration(); 80 81 84 private static final TestConfiguration DEFAULT_CONFIG; 85 86 90 private static final boolean runningInDerbyHarness; 91 92 static { 93 boolean assumeHarness = false; 94 95 if (!DERBY_HARNESS_CONFIG.getJDBCClient().isEmbedded()) 98 assumeHarness = true; 99 100 if (!DERBY_HARNESS_CONFIG.getDatabaseName().equals(DEFAULT_DBNAME)) 102 assumeHarness = true; 103 104 if (!DERBY_HARNESS_CONFIG.getUserName().equals(DEFAULT_USER_NAME)) 106 assumeHarness = true; 107 108 if (BaseTestCase.getSystemProperty("derby.system.home") != null) 111 assumeHarness = true; 112 113 assumeHarness = true; 115 116 DEFAULT_CONFIG = assumeHarness ? DERBY_HARNESS_CONFIG : JUNIT_CONFIG; 117 runningInDerbyHarness = assumeHarness; 118 119 if (!assumeHarness) { 120 File dsh = new File ("system"); 121 122 BaseTestCase.setSystemProperty("derby.system.home", 123 dsh.getAbsolutePath()); 124 } 125 } 126 127 132 private static final ThreadLocal CURRENT_CONFIG = new ThreadLocal () { 133 protected Object initialValue() { 134 return DEFAULT_CONFIG; 135 } 136 }; 137 138 143 public static TestConfiguration getCurrent() { 144 return (TestConfiguration) CURRENT_CONFIG.get(); 145 } 146 147 152 static void setCurrent(TestConfiguration config) 153 { 154 CURRENT_CONFIG.set(config); 155 } 156 170 public static Test derbyClientServerDecorator(Class suite) throws Exception  171 { 172 TestConfiguration config = TestConfiguration.getCurrent(); 173 174 TestConfiguration derbyClientConfig = 175 new TestConfiguration(config, JDBCClient.DERBYNETCLIENT, 176 DEFAULT_HOSTNAME, DEFAULT_PORT); 177 178 TestConfiguration.setCurrent(derbyClientConfig); 179 180 Test test = addSuiteByReflection(suite); 181 182 TestConfiguration.setCurrent(config); 183 184 test = new NetworkServerTestSetup(test); 185 186 return new ChangeConfigurationSetup(derbyClientConfig, test); 187 188 } 189 private static Test addSuiteByReflection(Class clz) throws Exception  190 { 191 Method sm = clz.getMethod("suite", null); 192 193 return (Test) sm.invoke(null, null); 194 } 195 196 public static Test changeUserDecorator(Test test, String user, String password) 197 { 198 return new ChangeUserSetup(test, user, password); 199 } 200 204 private TestConfiguration() { 205 this.dbName = DEFAULT_DBNAME; 206 this.userName = DEFAULT_USER_NAME; 207 this.userPassword = DEFAULT_USER_PASSWORD; 208 this.hostName = null; 209 this.port = -1; 210 this.singleLegXA = false; 211 212 this.jdbcClient = JDBCClient.EMBEDDED; 213 url = createJDBCUrlWithDatabaseName(dbName); 214 215 } 216 217 private TestConfiguration(TestConfiguration copy, JDBCClient client, 218 String hostName, int port) 219 { 220 this.dbName = copy.dbName; 221 this.userName = copy.userName; 222 this.userPassword = copy.userPassword; 223 224 this.isVerbose = copy.isVerbose; 225 this.singleLegXA = copy.singleLegXA; 226 this.port = port; 227 228 this.jdbcClient = client; 229 this.hostName = hostName; 230 231 this.url = createJDBCUrlWithDatabaseName(dbName); 232 } 233 234 235 242 TestConfiguration(TestConfiguration copy, String user, String password) 243 { 244 this.dbName = copy.dbName; 245 this.userName = user; 246 this.userPassword = password; 247 248 this.isVerbose = copy.isVerbose; 249 this.singleLegXA = copy.singleLegXA; 250 this.port = copy.port; 251 252 this.jdbcClient = copy.jdbcClient; 253 this.hostName = copy.hostName; 254 255 this.url = copy.url; 256 } 257 258 263 private TestConfiguration(Properties props) 264 throws NumberFormatException { 265 266 dbName = props.getProperty(KEY_DBNAME, DEFAULT_DBNAME); 267 userName = props.getProperty(KEY_USER_NAME, DEFAULT_USER_NAME); 268 userPassword = props.getProperty(KEY_USER_PASSWORD, 269 DEFAULT_USER_PASSWORD); 270 hostName = props.getProperty(KEY_HOSTNAME, DEFAULT_HOSTNAME); 271 isVerbose = Boolean.valueOf(props.getProperty(KEY_VERBOSE)).booleanValue(); 272 String portStr = props.getProperty(KEY_PORT); 273 singleLegXA = Boolean.valueOf(props.getProperty(KEY_SINGLE_LEG_XA) 274 ).booleanValue(); 275 if (portStr != null) { 276 try { 277 port = Integer.parseInt(portStr); 278 } catch (NumberFormatException nfe) { 279 throw new NumberFormatException ( 281 "Port number must be an integer. Value: " + portStr); 282 } 283 } else { 284 port = DEFAULT_PORT; 285 } 286 287 String framework = props.getProperty(KEY_FRAMEWORK, DEFAULT_FRAMEWORK); 288 289 if ("DerbyNetClient".equals(framework)) { 290 jdbcClient = JDBCClient.DERBYNETCLIENT; 291 } else if ("DerbyNet".equals(framework)) { 292 jdbcClient = JDBCClient.DB2CLIENT; 293 } else { 294 jdbcClient = JDBCClient.EMBEDDED; 295 } 296 url = createJDBCUrlWithDatabaseName(dbName); 297 } 298 299 304 private static final Properties getSystemProperties() { 305 Properties sysProps = (Properties )AccessController.doPrivileged( 307 new PrivilegedAction() { 308 public Object run() { 309 return System.getProperties(); 310 } 311 }); 312 return sysProps; 313 } 314 315 320 private String createJDBCUrlWithDatabaseName(String name) { 321 if (jdbcClient == JDBCClient.EMBEDDED) { 322 return jdbcClient.getUrlBase() + name; 323 } else { 324 return jdbcClient.getUrlBase() + hostName + ":" + port + "/" + name; 325 } 326 } 327 328 333 public JDBCClient getJDBCClient() { 334 return jdbcClient; 335 } 336 337 338 343 public String getJDBCUrl() { 344 return url; 345 } 346 347 353 public String getJDBCUrl(String databaseName) { 354 return createJDBCUrlWithDatabaseName(databaseName); 355 } 356 357 362 public String getDatabaseName() { 363 return dbName; 364 } 365 366 371 public String getUserName() { 372 return userName; 373 } 374 375 380 public String getUserPassword() { 381 return userPassword; 382 } 383 384 389 public String getHostName() { 390 return hostName; 391 } 392 393 398 public int getPort() { 399 return port; 400 } 401 402 409 public Connection openDefaultConnection() 410 throws SQLException { 411 return getDefaultConnection("create=true"); 412 } 413 414 423 public Connection openConnection (String databaseName) throws SQLException { 424 return getConnection(databaseName, "create=true"); 425 } 426 427 435 public Connection getDefaultConnection(String connAttrs) 436 throws SQLException { 437 return getConnection(getDatabaseName(), connAttrs); 438 } 439 440 449 public Connection getConnection (String databaseName, String connAttrs) 450 throws SQLException { 451 Connection con = null; 452 JDBCClient client =getJDBCClient(); 453 if (JDBC.vmSupportsJDBC2()) { 454 loadJDBCDriver(client.getJDBCDriverName()); 455 if (!isSingleLegXA()) { 456 con = DriverManager.getConnection( 457 getJDBCUrl(databaseName) + ";" + connAttrs, 458 getUserName(), 459 getUserPassword()); 460 } 461 else { 462 Properties attrs = 463 getDataSourcePropertiesForDatabase(databaseName, connAttrs); 464 con = TestDataSourceFactory.getXADataSource(attrs). 465 getXAConnection (getUserName(), 466 getUserPassword()).getConnection(); 467 } 468 } else { 469 Properties attrs = getDataSourcePropertiesForDatabase(databaseName, connAttrs); 471 con = TestDataSourceFactory.getDataSource(attrs).getConnection(); 472 } 473 return con; 474 } 475 476 479 public void setVerbosity( boolean isChatty ) { isVerbose = isChatty; } 480 481 486 public boolean isVerbose() { 487 return isVerbose; 488 } 489 490 497 public static boolean loadingFromJars() 498 { 499 return SecurityManagerSetup.isJars; 500 } 501 502 509 public static boolean runningInDerbyHarness() 510 { 511 return runningInDerbyHarness; 512 } 513 514 518 public boolean isSingleLegXA () { 519 return singleLegXA; 520 } 521 522 536 File getFailureFolder(TestCase test){ 537 538 StringBuffer sb = new StringBuffer (); 539 540 sb.append("fail"); 541 sb.append(File.separatorChar); 542 sb.append(getJDBCClient().getName()); 543 sb.append(File.separatorChar); 544 545 String className = test.getClass().getName(); 546 int lastDot = className.lastIndexOf('.'); 547 if (lastDot != -1) 548 className = className.substring(lastDot+1, className.length()); 549 550 sb.append(className); 551 sb.append(File.separatorChar); 552 sb.append(test.getName()); 553 554 String base = sb.toString().intern(); 555 final File folder = new File (base); 556 557 synchronized (base) { 560 561 AccessController.doPrivileged 562 (new java.security.PrivilegedAction (){ 563 public Object run(){ 564 if (folder.exists()) { 565 } 567 return new Boolean (folder.mkdirs()); 568 } 569 } 570 ); 571 } 572 573 return folder; 574 575 } 576 577 580 private final String dbName; 581 private final String url; 582 private final String userName; 583 private final String userPassword; 584 private final int port; 585 private final String hostName; 586 private final JDBCClient jdbcClient; 587 private boolean isVerbose; 588 private final boolean singleLegXA; 589 590 591 600 public static Properties getDefaultDataSourceProperties() { 601 return getDataSourcePropertiesForDatabase( 602 getCurrent().getDatabaseName(), "create=true"); 603 } 604 605 614 public static Properties getDataSourcePropertiesForDatabase 615 (String databaseName, String connAttrs) 616 { 617 Properties attrs = new Properties (); 618 if (!(getCurrent().getJDBCClient() == JDBCClient.EMBEDDED)) { 619 attrs.setProperty("serverName", getCurrent().getHostName()); 620 attrs.setProperty("portNumber", Integer.toString(getCurrent().getPort())); 621 } 622 attrs.setProperty("databaseName", databaseName); 623 attrs.setProperty("connectionAttributes", connAttrs); 624 return attrs; 625 } 626 627 633 private static void loadJDBCDriver(String driverClass) 634 throws SQLException { 635 try { 636 Class.forName(driverClass).newInstance(); 637 } catch (ClassNotFoundException cnfe) { 638 throw new SQLException ("Failed to load JDBC driver '" + 639 driverClass + "': " + cnfe.getMessage()); 640 } catch (IllegalAccessException iae) { 641 throw new SQLException ("Failed to load JDBC driver '" + 642 driverClass + "': " + iae.getMessage()); 643 } catch (InstantiationException ie) { 644 throw new SQLException ("Failed to load JDBC driver '" + 645 driverClass + "': " + ie.getMessage()); 646 } 647 } 648 649 652 653 658 boolean defaultSecurityManagerSetup() throws PrivilegedActionException { 659 660 if (jdbcClient.isDB2Client()) { 664 SecurityManagerSetup.noSecurityManager(); 665 return false; 666 } else { 667 SecurityManagerSetup.installSecurityManager(); 668 return true; 669 } 670 } 671 672 673 } 674
| Popular Tags
|