You are viewing a plain text version of this content. The canonical link for it is here.
Posted to derby-commits@db.apache.org by rh...@apache.org on 2012/02/27 16:26:37 UTC
svn commit: r1294183 - in
/db/derby/code/trunk/java/testing/org/apache/derbyTesting:
functionTests/tests/lang/ junit/
Author: rhillegas
Date: Mon Feb 27 15:26:37 2012
New Revision: 1294183
URL: http://svn.apache.org/viewvc?rev=1294183&view=rev
Log:
DERBY-866: Add tests for encrypted credentials db.
Modified:
db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/NativeAuthenticationServiceTest.java
db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/nast1.jar
db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/nast_init.sql
db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/ConnectionPoolDataSourceConnector.java
db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/Connector.java
db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/DataSourceConnector.java
db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/DriverManagerConnector.java
db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/TestConfiguration.java
db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/XADataSourceConnector.java
Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/NativeAuthenticationServiceTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/NativeAuthenticationServiceTest.java?rev=1294183&r1=1294182&r2=1294183&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/NativeAuthenticationServiceTest.java (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/NativeAuthenticationServiceTest.java Mon Feb 27 15:26:37 2012
@@ -58,6 +58,7 @@ public class NativeAuthenticationService
private static final int JAR = 1;
private static final int CLASSPATH = 2;
private static final int NONE = 3;
+ private static final int JAR_ENCRYPTED = 4;
// settings for constructor options
private static final boolean NATIVE = true;
@@ -96,6 +97,8 @@ public class NativeAuthenticationService
private static final String TENTH_DB = "tenthDB";
private static final String ELEVENTH_DB = "eleventhDB";
private static final String TWELTH_DB = "twelthDB";
+ private static final String THIRTEENTH_DB = "thirteenthDB";
+ private static final String FOURTEENTH_DB = "fourteenthDB";
private static final String NAST1_JAR_FILE = "nast1.jar";
private static final String NAST2_JAR_FILE = "nast2.jar";
@@ -125,6 +128,7 @@ public class NativeAuthenticationService
private static final String BAD_PASSWORD_PROPERTY = "4251J";
private static final String BAD_PROPERTY_CHANGE = "XCY02";
private static final String SQL_AUTHORIZATION_NOT_ON = "42Z60";
+ private static final String CANT_BOOT_DATABASE = "XJ040";
///////////////////////////////////////////////////////////////////////////////////
//
@@ -202,7 +206,8 @@ public class NativeAuthenticationService
switch ( _credentialsDBLocation )
{
case JAR:
- _credentialsDBPhysicalName = jarDBName();
+ case JAR_ENCRYPTED:
+ _credentialsDBPhysicalName = jarDBName( _credentialsDBLocation );
break;
case CLASSPATH:
@@ -253,6 +258,7 @@ public class NativeAuthenticationService
switch ( _credentialsDBLocation )
{
case JAR:
+ case JAR_ENCRYPTED:
case CLASSPATH:
return true;
@@ -275,6 +281,10 @@ public class NativeAuthenticationService
dbLocation = "JAR, ";
break;
+ case JAR_ENCRYPTED:
+ dbLocation = "JAR_ENCRYPTED, ";
+ break;
+
case CLASSPATH:
dbLocation = "CLASSPATH, ";
break;
@@ -327,17 +337,21 @@ public class NativeAuthenticationService
TestSuite suite = new TestSuite();
//
- // Special version of the test which disables the security manager in order
- // to use the classpath subprotocol. We may be able to remove this special
- // case after DERBY-5615 is fixed.
+ // Special version of the test which uses an encrypted database for credentials.
+ // Not run on windows until DERBY-5618 is addressed.
+ // Also not run on small devices because encryption is not supported there
+ // by default.
//
- suite.addTest
- (
- (
- new NativeAuthenticationServiceTest
- ( NONE, NO_AUTH, SYSTEM_WIDE, DONT_DISABLE_AUTH, DISABLE_JAVA_SECURITY )
- ).decorate( false )
- );
+ if ( !onWindows() && !JDBC.vmSupportsJSR169() )
+ {
+ suite.addTest
+ (
+ (
+ new NativeAuthenticationServiceTest
+ ( JAR_ENCRYPTED, NATIVE, LOCAL, DONT_DISABLE_AUTH, ENABLE_JAVA_SECURITY )
+ ).decorate( false )
+ );
+ }
suite.addTest( allConfigurations( false ) );
if ( !JDBC.vmSupportsJSR169() ) { suite.addTest( allConfigurations( true ) ); }
@@ -359,7 +373,7 @@ public class NativeAuthenticationService
TestSuite suite = new TestSuite();
//
- // No authentication
+ // No authentication.
//
suite.addTest
(
@@ -536,6 +550,8 @@ public class NativeAuthenticationService
result = TestConfiguration.additionalDatabaseDecoratorNoShutdown( result, TENTH_DB );
result = TestConfiguration.additionalDatabaseDecoratorNoShutdown( result, ELEVENTH_DB );
result = TestConfiguration.additionalDatabaseDecoratorNoShutdown( result, TWELTH_DB );
+ result = TestConfiguration.additionalDatabaseDecoratorNoShutdown( result, THIRTEENTH_DB );
+ result = TestConfiguration.additionalDatabaseDecoratorNoShutdown( result, FOURTEENTH_DB );
result = TestConfiguration.changeUserDecorator( result, DBO, getPassword( DBO ) );
@@ -559,7 +575,11 @@ public class NativeAuthenticationService
println( "Credentials DB physical name = " + _credentialsDBPhysicalName );
println( PROVIDER_PROPERTY + " = " + getSystemProperty( PROVIDER_PROPERTY ) );
- if ( credentialsViaSubprotocol() )
+ if ( _credentialsDBLocation == JAR_ENCRYPTED )
+ {
+ vetEncryptedCredentialsDB();
+ }
+ else if ( credentialsViaSubprotocol() )
{
vetCredentialsViaSubprotocol();
}
@@ -580,6 +600,28 @@ public class NativeAuthenticationService
/**
* <p>
+ * Verify behavior when credentials live in an encrypted db.
+ * </p>
+ */
+ private void vetEncryptedCredentialsDB() throws Exception
+ {
+ // can't create a database if the encrypted credentials db hasn't been booted yet
+ Connection badConn = getConnection( true, true, THIRTEENTH_DB, DBO, CANT_BOOT_DATABASE );
+
+ // now boot the encrypted credentials db
+ Properties props = new Properties();
+ props.setProperty( "bootPassword", "clo760uds2caPe" );
+ Connection dboConn = openConnection( jarDBName( _credentialsDBLocation ), DBO, false, props );
+
+ // credentials db is booted. now we can create databases with good credentials.
+ Connection grapeConn = getConnection( false, true, THIRTEENTH_DB, GRAPE_USER, null );
+
+ // but we can't create a database with bad credentials
+ getConnection( true, true, FOURTEENTH_DB, WALNUT_USER, INVALID_AUTHENTICATION );
+ }
+
+ /**
+ * <p>
* Verify that credentials work when they are stored in a db accessed via
* the jar or classpath subprotocols.
* </p>
@@ -587,7 +629,7 @@ public class NativeAuthenticationService
private void vetCredentialsViaSubprotocol() throws Exception
{
// create a new database
- Connection grapeConn = openConnection( TENTH_DB, GRAPE_USER, true );
+ Connection grapeConn = openConnection( TENTH_DB, GRAPE_USER, true, null );
String[][] legalUsers = _localAuthentication ?
new String[][] { { GRAPE_USER } } : new String[][] {};
assertResults
@@ -615,7 +657,7 @@ public class NativeAuthenticationService
( _nativeAuthentication, true, SECOND_DB, APPLE_USER, CREDENTIALS_DB_DOES_NOT_EXIST );
// create the credentials database
- Connection sysadminConn = openConnection( CREDENTIALS_DB, DBO, true );
+ Connection sysadminConn = openConnection( CREDENTIALS_DB, DBO, true, null );
// add another legal user
addUser( sysadminConn, APPLE_USER );
@@ -726,7 +768,7 @@ public class NativeAuthenticationService
//
///////////////////////////////////////////////////////////////////////////////////
- Connection seventhDBOConn = openConnection( SEVENTH_DB, DBO, true );
+ Connection seventhDBOConn = openConnection( SEVENTH_DB, DBO, true, null );
addUser( seventhDBOConn, APPLE_USER );
goodStatement( seventhDBOConn, "create table t1( a int )" );
@@ -746,7 +788,7 @@ public class NativeAuthenticationService
_seventhDBSetup.getTestConfiguration().shutdownDatabase();
// if NATIVE authentication is set in the database, then sql authorization prevents this legal user from viewing private data
- Connection seventhAppleConn = openConnection( SEVENTH_DB, APPLE_USER, true );
+ Connection seventhAppleConn = openConnection( SEVENTH_DB, APPLE_USER, true, null );
vetStatement( _localAuthentication, seventhAppleConn, "select * from " + DBO + ".t1", NO_COLUMN_PERMISSION );
// if NATIVE authentication is set in the database, then authentication still prevents this user from logging in
@@ -761,7 +803,7 @@ public class NativeAuthenticationService
//
///////////////////////////////////////////////////////////////////////////////////
- Connection eighthDBOConn = openConnection( EIGHTH_DB, DBO, true );
+ Connection eighthDBOConn = openConnection( EIGHTH_DB, DBO, true, null );
addUser( eighthDBOConn, APPLE_USER );
@@ -794,7 +836,7 @@ public class NativeAuthenticationService
if ( !onWindows() )
{
// database accessed via jar subprotocol
- vetProtocol( jarDBName() );
+ vetProtocol( jarDBName( _credentialsDBLocation ) );
//
// We only use the classpath subprotocol if we are not running under a security manager.
@@ -809,11 +851,11 @@ public class NativeAuthenticationService
//
///////////////////////////////////////////////////////////////////////////////////
- Connection twelthDBOConn = openConnection( TWELTH_DB, DBO, true );
+ Connection twelthDBOConn = openConnection( TWELTH_DB, DBO, true, null );
addUser( twelthDBOConn, APPLE_USER );
- Connection twelthAppleConn = openConnection( TWELTH_DB, APPLE_USER, true );
+ Connection twelthAppleConn = openConnection( TWELTH_DB, APPLE_USER, true, null );
// these fail without the appropriate grant
vetStatement( _nativeAuthentication, twelthAppleConn,
@@ -862,9 +904,11 @@ public class NativeAuthenticationService
vetStatement( _nativeAuthentication, twelthAppleConn,
"call syscs_util.syscs_drop_user( 'CORNELIA' )", NO_EXECUTE_PERMISSION );
}
- private static String jarDBName() throws Exception
+ private static String jarDBName( int credentialsDBLocation ) throws Exception
{
- return "jar:(" + SupportFilesSetup.getReadOnlyFileName( NAST1_JAR_FILE ) + ")nast";
+ String dbName = (credentialsDBLocation == JAR_ENCRYPTED) ? "nastEncrypted" : "nast";
+
+ return "jar:(" + SupportFilesSetup.getReadOnlyFileName( NAST1_JAR_FILE ) + ")" + dbName;
}
private static String classpathDBName() { return "classpath:nast"; }
@@ -923,7 +967,7 @@ public class NativeAuthenticationService
private void vetSystemWideOperations() throws Exception
{
// create a database which we will backup and restore
- Connection dboConn = openConnection( SIXTH_DB, DBO, true );
+ Connection dboConn = openConnection( SIXTH_DB, DBO, true, null );
// add another user who can perform restores successfully
addUser( dboConn, BANANA_USER );
@@ -1013,7 +1057,7 @@ public class NativeAuthenticationService
{
// create an empty database without authentication turned on
String dbo = ORANGE_USER;
- Connection dboConn = openConnection( FOURTH_DB, dbo, true );
+ Connection dboConn = openConnection( FOURTH_DB, dbo, true, null );
addUser( dboConn, PEAR_USER );
@@ -1098,7 +1142,7 @@ public class NativeAuthenticationService
Connection appleConn = getConnection( true, true, FOURTH_DB, APPLE_USER, INVALID_AUTHENTICATION );
// ...but these credentials work
- Connection pearConn = openConnection( FOURTH_DB, PEAR_USER, true );
+ Connection pearConn = openConnection( FOURTH_DB, PEAR_USER, true, null );
// should get authorization errors trying to select from a table private to the DBO
// and from trying to view the credentials table
@@ -1115,7 +1159,7 @@ public class NativeAuthenticationService
private void vetPasswordLifetime() throws Exception
{
// create another database
- Connection dboConn = openConnection( FIFTH_DB, DBO, true );
+ Connection dboConn = openConnection( FIFTH_DB, DBO, true, null );
// add another legal user
addUser( dboConn, APPLE_USER );
@@ -1141,7 +1185,7 @@ public class NativeAuthenticationService
_fifthDBSetup.getTestConfiguration().shutdownDatabase();
// the DBO's password does not expire
- dboConn = openConnection( FIFTH_DB, DBO, true );
+ dboConn = openConnection( FIFTH_DB, DBO, true, null );
// but the other user's password has expired
appleConn = getConnection( true, true, FIFTH_DB, APPLE_USER, INVALID_AUTHENTICATION );
@@ -1171,7 +1215,7 @@ public class NativeAuthenticationService
private void vetSQLAuthorizationOn() throws Exception
{
- Connection nonDBOConn = openConnection( CREDENTIALS_DB, APPLE_USER, true );
+ Connection nonDBOConn = openConnection( CREDENTIALS_DB, APPLE_USER, true, null );
String query = "select username from sys.sysusers" ;
try {
@@ -1201,7 +1245,7 @@ public class NativeAuthenticationService
reportConnectionAttempt( dbName, user, isLogicalName );
try {
- conn = openConnection( dbName, user, isLogicalName );
+ conn = openConnection( dbName, user, isLogicalName, null );
if ( shouldFail ) { fail( tagError( "Connection to " + dbName + " should have failed." ) ); }
}
@@ -1226,7 +1270,7 @@ public class NativeAuthenticationService
reportConnectionAttempt( dbName, user, true );
- conn = openConnection( dbName, user, true );
+ conn = openConnection( dbName, user, true, null );
SQLWarning warning = conn.getWarnings();
@@ -1303,7 +1347,7 @@ public class NativeAuthenticationService
///////////////////////////////////////////////////////////////////////////////////
/** Open a connection to a database using the supplied credentials */
- private Connection openConnection( String dbName, String user, boolean isLogicalName )
+ private Connection openConnection( String dbName, String user, boolean isLogicalName, Properties props )
throws SQLException
{
String password = getPassword( user );
@@ -1313,7 +1357,7 @@ public class NativeAuthenticationService
}
else
{
- return getTestConfiguration().openPhysicalConnection( dbName, user, password );
+ return getTestConfiguration().openPhysicalConnection( dbName, user, password, props );
}
}
Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/nast1.jar
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/nast1.jar?rev=1294183&r1=1294182&r2=1294183&view=diff
==============================================================================
Binary files - no diff available.
Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/nast_init.sql
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/nast_init.sql?rev=1294183&r1=1294182&r2=1294183&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/nast_init.sql (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/nast_init.sql Mon Feb 27 15:26:37 2012
@@ -19,8 +19,7 @@
-- use by NativeAuthenticationServiceTest. The build-test-jars target is invoked by
-- a target by the same name in the top build.xml file.
--
-
-connect 'jdbc:derby:nast/nast;create=true;user=kiwi;password=kiwipassword';
+connect 'jdbc:derby:nast/nast;create=true;user=kiwi;password=KIWI_password';
call syscs_util.syscs_create_user( 'KIWI', 'KIWI_password' );
call syscs_util.syscs_create_user( 'GRAPE', 'GRAPE_password' );
@@ -29,6 +28,15 @@ call syscs_util.syscs_set_database_prope
create table t( a int );
insert into t( a ) values ( 100 ), ( 200 );
+--
+-- We use this database to test encryption of the Credentials DB.
+--
+connect 'jdbc:derby:nast/nastEncrypted;create=true;user=kiwi;password=KIWI_password;dataEncryption=true;bootPassword=clo760uds2caPe';
+
+call syscs_util.syscs_create_user( 'KIWI', 'KIWI_password' );
+call syscs_util.syscs_create_user( 'GRAPE', 'GRAPE_password' );
+call syscs_util.syscs_set_database_property( 'derby.authentication.provider', 'NATIVE::LOCAL' );
+
connect 'jdbc:derby:;shutdown=true';
Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/ConnectionPoolDataSourceConnector.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/ConnectionPoolDataSourceConnector.java?rev=1294183&r1=1294182&r2=1294183&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/ConnectionPoolDataSourceConnector.java (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/ConnectionPoolDataSourceConnector.java Mon Feb 27 15:26:37 2012
@@ -22,6 +22,7 @@ package org.apache.derbyTesting.junit;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
+import java.util.Properties;
import javax.sql.ConnectionPoolDataSource;
@@ -78,7 +79,7 @@ public class ConnectionPoolDataSourceCon
// a new DataSource with the createDtabase property set.
if (!expectedState.equals(e.getSQLState()))
throw e;
- return singleUseDS("createDatabase", "create").
+ return singleUseDS( DataSourceConnector.makeCreateDBAttributes( config ) ).
getPooledConnection().getConnection();
}
}
@@ -100,7 +101,7 @@ public class ConnectionPoolDataSourceCon
if (!expectedState.equals(e.getSQLState()))
throw e;
ConnectionPoolDataSource tmpDs =
- singleUseDS("createDatabase", "create");
+ singleUseDS( DataSourceConnector.makeCreateDBAttributes( config ) );
JDBCDataSource.setBeanProperty(tmpDs, "databaseName", databaseName);
return tmpDs.getPooledConnection().getConnection();
}
@@ -116,7 +117,7 @@ public class ConnectionPoolDataSourceCon
// a new DataSource with the createDatabase property set.
if (!"XJ004".equals(e.getSQLState()))
throw e;
- return singleUseDS("createDatabase", "create").
+ return singleUseDS( DataSourceConnector.makeCreateDBAttributes( config ) ).
getPooledConnection(user, password).getConnection();
}
}
@@ -124,7 +125,15 @@ public class ConnectionPoolDataSourceCon
public Connection openConnection(String databaseName,
String user,
String password)
- throws SQLException {
+ throws SQLException
+ {
+ return openConnection( databaseName, user, password, null );
+ }
+
+ public Connection openConnection
+ (String databaseName, String user, String password, Properties connectionProperties)
+ throws SQLException
+ {
JDBCDataSource.setBeanProperty(ds, "databaseName", databaseName);
try {
return ds.getPooledConnection(user, password).getConnection();
@@ -134,21 +143,22 @@ public class ConnectionPoolDataSourceCon
// a new DataSource with the createDatabase property set.
if (!"XJ004".equals(e.getSQLState()))
throw e;
- ConnectionPoolDataSource tmpDs =
- singleUseDS("createDatabase", "create");
+ HashMap hm = DataSourceConnector.makeCreateDBAttributes( config );
+ if ( connectionProperties != null ) { hm.putAll( connectionProperties ); }
+ ConnectionPoolDataSource tmpDs = singleUseDS( hm );
JDBCDataSource.setBeanProperty(tmpDs, "databaseName", databaseName);
return tmpDs.getPooledConnection(user, password).getConnection();
}
}
public void shutDatabase() throws SQLException {
- singleUseDS("shutdownDatabase", "shutdown").
+ singleUseDS( DataSourceConnector.makeShutdownDBAttributes( config ) ).
getPooledConnection().getConnection();
}
public void shutEngine() throws SQLException {
ConnectionPoolDataSource tmpDs =
- singleUseDS("shutdownDatabase", "shutdown");
+ singleUseDS( DataSourceConnector.makeShutdownDBAttributes( config ) );
JDBCDataSource.setBeanProperty(tmpDs, "databaseName", "");
tmpDs.getPooledConnection();
}
@@ -157,10 +167,9 @@ public class ConnectionPoolDataSourceCon
* Get a connection from a single use ConnectionPoolDataSource configured
* from the configuration but with the passed in property set.
*/
- private ConnectionPoolDataSource singleUseDS(String property, String value)
- throws SQLException {
- HashMap hm = JDBCDataSource.getDataSourceProperties(config);
- hm.put(property, value);
+ private ConnectionPoolDataSource singleUseDS( HashMap hm )
+ throws SQLException
+ {
ConnectionPoolDataSource sds =
J2EEDataSource.getConnectionPoolDataSource(config, hm);
return sds;
Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/Connector.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/Connector.java?rev=1294183&r1=1294182&r2=1294183&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/Connector.java (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/Connector.java Mon Feb 27 15:26:37 2012
@@ -19,6 +19,7 @@
*/
package org.apache.derbyTesting.junit;
+import java.util.Properties;
import java.sql.Connection;
import java.sql.SQLException;
@@ -77,6 +78,15 @@ interface Connector {
throws SQLException;
/**
+ * Open a connection to the database
+ * defined by the configuration passed to setConfiguration.
+ * If the database does not exist then it should be created.
+ */
+ abstract Connection openConnection
+ (String databaseName, String user, String password, Properties connectionProperties)
+ throws SQLException;
+
+ /**
* Shutdown the running default database using user and password
* defined by the configuration passed to setConfiguration.
* Return nothing, exception is expected to be thrown with SQLState 08006
Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/DataSourceConnector.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/DataSourceConnector.java?rev=1294183&r1=1294182&r2=1294183&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/DataSourceConnector.java (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/DataSourceConnector.java Mon Feb 27 15:26:37 2012
@@ -22,6 +22,7 @@ package org.apache.derbyTesting.junit;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
+import java.util.Properties;
import javax.sql.DataSource;
@@ -64,7 +65,7 @@ public class DataSourceConnector impleme
// a new DataSource with the createDtabase property set.
if (!expectedState.equals(e.getSQLState()))
throw e;
- return singleUseDS("createDatabase", "create").getConnection();
+ return singleUseDS( makeCreateDBAttributes( config ) ).getConnection();
}
}
@@ -84,7 +85,7 @@ public class DataSourceConnector impleme
// a new DataSource with the createDtabase property set.
if (!expectedState.equals(e.getSQLState()))
throw e;
- DataSource tmpDs = singleUseDS("createDatabase", "create");
+ DataSource tmpDs = singleUseDS( makeCreateDBAttributes( config ) );
JDBCDataSource.setBeanProperty(tmpDs, "databaseName", databaseName);
return tmpDs.getConnection();
}
@@ -100,13 +101,21 @@ public class DataSourceConnector impleme
// a new DataSource with the createDatabase property set.
if (!"XJ004".equals(e.getSQLState()))
throw e;
- return singleUseDS(
- "createDatabase", "create").getConnection(user, password);
+ return singleUseDS( makeCreateDBAttributes( config ) ).getConnection
+ (user, password);
}
}
public Connection openConnection(String databaseName, String user, String password)
- throws SQLException {
+ throws SQLException
+ {
+ return openConnection( databaseName, user, password, null );
+ }
+
+ public Connection openConnection
+ (String databaseName, String user, String password, Properties connectionProperties)
+ throws SQLException
+ {
JDBCDataSource.setBeanProperty(ds, "databaseName", databaseName);
try {
return ds.getConnection(user, password);
@@ -116,18 +125,20 @@ public class DataSourceConnector impleme
// a new DataSource with the createDatabase property set.
if (!"XJ004".equals(e.getSQLState()))
throw e;
- DataSource tmpDs = singleUseDS("createDatabase", "create");
+ HashMap hm = makeCreateDBAttributes( config );
+ if ( connectionProperties != null ) { hm.putAll( connectionProperties ); }
+ DataSource tmpDs = singleUseDS( hm );
JDBCDataSource.setBeanProperty(tmpDs, "databaseName", databaseName);
return tmpDs.getConnection(user, password);
}
}
public void shutDatabase() throws SQLException {
- singleUseDS("shutdownDatabase", "shutdown").getConnection();
+ singleUseDS( makeShutdownDBAttributes( config ) ).getConnection();
}
public void shutEngine() throws SQLException {
- DataSource tmpDs = singleUseDS("shutdownDatabase", "shutdown");
+ DataSource tmpDs = singleUseDS( makeShutdownDBAttributes( config ) );
JDBCDataSource.setBeanProperty(tmpDs, "databaseName", "");
tmpDs.getConnection();
}
@@ -136,12 +147,26 @@ public class DataSourceConnector impleme
* Get a connection from a single use DataSource configured
* from the configuration but with the passed in property set.
*/
- private DataSource singleUseDS(String property, String value)
+ private DataSource singleUseDS( HashMap hm )
throws SQLException {
- HashMap hm = JDBCDataSource.getDataSourceProperties(config);
- hm.put(property, value);
DataSource sds = JDBCDataSource.getDataSource(config, hm);
return sds;
}
+ static HashMap makeCreateDBAttributes( TestConfiguration configuration )
+ {
+ HashMap hm = JDBCDataSource.getDataSourceProperties( configuration );
+ hm.put( "createDatabase", "create" );
+
+ return hm;
+ }
+
+ static HashMap makeShutdownDBAttributes( TestConfiguration configuration )
+ {
+ HashMap hm = JDBCDataSource.getDataSourceProperties( configuration );
+ hm.put( "shutdownDatabase", "shutdown" );
+
+ return hm;
+ }
+
}
Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/DriverManagerConnector.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/DriverManagerConnector.java?rev=1294183&r1=1294182&r2=1294183&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/DriverManagerConnector.java (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/DriverManagerConnector.java Mon Feb 27 15:26:37 2012
@@ -62,8 +62,25 @@ public class DriverManagerConnector impl
* with attributes create=true.
*/
public Connection openConnection(String databaseName, String user, String password)
- throws SQLException {
-
+ throws SQLException
+ {
+ return openConnection( databaseName, user, password, (Properties) null );
+ }
+
+ /**
+ * Open a connection using the DriverManager.
+ * <BR>
+ * The JDBC driver is only loaded if DriverManager.getDriver()
+ * for the JDBC URL throws an exception indicating no driver is loaded.
+ * <BR>
+ * If the connection request fails with SQLState XJ004
+ * (database not found) then the connection is retried
+ * with attributes create=true.
+ */
+ public Connection openConnection
+ (String databaseName, String user, String password, Properties connectionProperties)
+ throws SQLException
+ {
String url = config.getJDBCUrl(databaseName);
try {
@@ -77,6 +94,8 @@ public class DriverManagerConnector impl
connectionAttributes.setProperty("user", user);
connectionAttributes.setProperty("password", password);
+ if ( connectionProperties != null ) { connectionAttributes.putAll( connectionProperties ); }
+
try {
return DriverManager.getConnection(url, connectionAttributes);
} catch (SQLException e) {
Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/TestConfiguration.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/TestConfiguration.java?rev=1294183&r1=1294182&r2=1294183&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/TestConfiguration.java (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/TestConfiguration.java Mon Feb 27 15:26:37 2012
@@ -1698,16 +1698,18 @@ public final class TestConfiguration {
* @param physicalDatabaseName The real database name to use.
* @param user name of user
* @param password password of user
+ * @param props extra properties to pass to the connection
* @return connection to specified database.
*/
- public Connection openPhysicalConnection( String physicalDatabaseName, String user, String password )
+ public Connection openPhysicalConnection( String physicalDatabaseName, String user, String password, Properties props )
throws SQLException
{
return connector.openConnection
(
physicalDatabaseName,
user,
- password
+ password,
+ props
);
}
Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/XADataSourceConnector.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/XADataSourceConnector.java?rev=1294183&r1=1294182&r2=1294183&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/XADataSourceConnector.java (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/XADataSourceConnector.java Mon Feb 27 15:26:37 2012
@@ -22,6 +22,7 @@ package org.apache.derbyTesting.junit;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
+import java.util.Properties;
import javax.sql.XADataSource;
@@ -65,7 +66,7 @@ public class XADataSourceConnector imple
// a new DataSource with the createDtabase property set.
if (!expectedState.equals(e.getSQLState()))
throw e;
- return singleUseDS("createDatabase", "create").
+ return singleUseDS( DataSourceConnector.makeCreateDBAttributes( config ) ).
getXAConnection().getConnection();
}
}
@@ -86,7 +87,7 @@ public class XADataSourceConnector imple
// a new DataSource with the createDtabase property set.
if (!expectedState.equals(e.getSQLState()))
throw e;
- XADataSource tmpDs = singleUseDS("createDatabase", "create");
+ XADataSource tmpDs = singleUseDS( DataSourceConnector.makeCreateDBAttributes( config ) );
JDBCDataSource.setBeanProperty(tmpDs, "databaseName", databaseName);
return tmpDs.getXAConnection().getConnection();
}
@@ -102,13 +103,21 @@ public class XADataSourceConnector imple
// a new DataSource with the createDatabase property set.
if (!"XJ004".equals(e.getSQLState()))
throw e;
- return singleUseDS("createDatabase", "create").
+ return singleUseDS( DataSourceConnector.makeCreateDBAttributes( config ) ).
getXAConnection(user, password).getConnection();
}
}
public Connection openConnection(String databaseName, String user, String password)
- throws SQLException {
+ throws SQLException
+ {
+ return openConnection( databaseName, user, password, null );
+ }
+
+ public Connection openConnection
+ (String databaseName, String user, String password, Properties connectionProperties)
+ throws SQLException
+ {
JDBCDataSource.setBeanProperty(ds, "databaseName", databaseName);
try {
return ds.getXAConnection(user, password).getConnection();
@@ -118,14 +127,17 @@ public class XADataSourceConnector imple
// a new DataSource with the createDatabase property set.
if (!"XJ004".equals(e.getSQLState()))
throw e;
- XADataSource tmpDs = singleUseDS("createDatabase", "create");
+ HashMap hm = DataSourceConnector.makeCreateDBAttributes( config );
+ if ( connectionProperties != null ) { hm.putAll( connectionProperties ); }
+ XADataSource tmpDs = singleUseDS( hm );
JDBCDataSource.setBeanProperty(tmpDs, "databaseName", databaseName);
return tmpDs.getXAConnection(user, password).getConnection();
}
}
public void shutDatabase() throws SQLException {
- singleUseDS("shutdownDatabase", "shutdown").getXAConnection().getConnection();
+ singleUseDS( DataSourceConnector.makeShutdownDBAttributes( config ) )
+ .getXAConnection().getConnection();
}
public void shutEngine() throws SQLException {
@@ -136,10 +148,8 @@ public class XADataSourceConnector imple
* Get a connection from a single use XADataSource configured
* from the configuration but with the passed in property set.
*/
- private XADataSource singleUseDS(String property, String value)
+ private XADataSource singleUseDS( HashMap hm )
throws SQLException {
- HashMap hm = JDBCDataSource.getDataSourceProperties(config);
- hm.put(property, value);
XADataSource sds = J2EEDataSource.getXADataSource(config, hm);
return sds;
}