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 da...@apache.org on 2013/12/12 00:10:06 UTC
svn commit: r1550308 - in
/db/derby/code/trunk/java/testing/org/apache/derbyTesting:
functionTests/tests/lang/ junit/
Author: dag
Date: Wed Dec 11 23:10:05 2013
New Revision: 1550308
URL: http://svn.apache.org/r1550308
Log:
DERBY-532 Support deferrable constraints
Patch derby-532-test-speedup changes ConstraintCharacteristicsTest to
use a main memory database for some tests for increased speed. It also
changes the way SystemPropertyTestSetup for static properties closes
down the database to not deregister the driver; without this change we
saw a test setup try to connect via the client driver to a Derby server
engine without a registered driver.
Modified:
db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/ConstraintCharacteristicsTest.java
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/SystemPropertyTestSetup.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/ConstraintCharacteristicsTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/ConstraintCharacteristicsTest.java?rev=1550308&r1=1550307&r2=1550308&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/ConstraintCharacteristicsTest.java (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/ConstraintCharacteristicsTest.java Wed Dec 11 23:10:05 2013
@@ -46,6 +46,7 @@ import org.apache.derby.iapi.services.co
import org.apache.derby.iapi.sql.conn.LanguageConnectionContext;
import org.apache.derby.impl.jdbc.EmbedConnection;
import org.apache.derby.impl.sql.GenericPreparedStatement;
+import org.apache.derbyTesting.functionTests.tests.memorydb.MemoryDbManager;
import org.apache.derbyTesting.junit.BaseJDBCTestCase;
import org.apache.derbyTesting.junit.J2EEDataSource;
import org.apache.derbyTesting.junit.JDBC;
@@ -67,6 +68,12 @@ public class ConstraintCharacteristicsTe
static boolean exportFilesCreatedEmbedded = false;
static boolean exportFilesCreatedClient = false;
+ // Use in memory database for speed for some tests
+ private static final MemoryDbManager dbm =
+ MemoryDbManager.getSharedInstance();
+
+ final static int WAIT_TIMEOUT_DURATION = 1;
+
public ConstraintCharacteristicsTest(String name) {
super(name);
}
@@ -89,9 +96,12 @@ public class ConstraintCharacteristicsTe
// Need to set a property to allow non default characteristics until
// feature completed: remove then
Properties systemProperties = new Properties();
- systemProperties.setProperty("derby.constraintsTesting", "true");
- systemProperties.setProperty("derby.locks.waitTimeout", "2");
- TestSuite s = new TestSuite("WithLenientChecking");
+ systemProperties.setProperty(
+ "derby.constraintsTesting", "true");
+ systemProperties.setProperty(
+ "derby.locks.waitTimeout", Integer.toString(WAIT_TIMEOUT_DURATION));
+ TestSuite s = new TestSuite(
+ "WithLenientChecking");
s.addTest(new ConstraintCharacteristicsTest(
"testDropNotNullOnUniqueColumn"));
@@ -128,7 +138,7 @@ public class ConstraintCharacteristicsTe
s.addTest(new ConstraintCharacteristicsTest(
"testAlmostRemovedAllDups"));
- suite.addTest(new SystemPropertyTestSetup(s, systemProperties));
+ suite.addTest(new SystemPropertyTestSetup(s, systemProperties, true));
return new SupportFilesSetup(suite);
}
@@ -182,17 +192,22 @@ public class ConstraintCharacteristicsTe
setAutoCommit(true);
getConnection().createStatement().
executeUpdate("drop table referenced");
+ dbm.cleanUp();
super.tearDown();
}
public void testSyntaxAndBinding() throws SQLException {
- Statement s = getConnection().createStatement();
+ Connection c = dbm.createDatabase("cct");
+ c.setAutoCommit(false);
+ Statement s = c.createStatement();
+ s.executeUpdate("create table referenced(i int primary key)");
+ c.commit();
/*
* T A B L E L E V E L C O N S T R A I N T S
*/
- assertTableLevelDefaultBehaviorAccepted(s);
+ assertTableLevelDefaultBehaviorAccepted(c, s);
assertTableLevelFailTillFeatureImplemented(s);
/*
@@ -227,13 +242,13 @@ public class ConstraintCharacteristicsTe
// Unknown constraint name
assertStatementError( "42X94", s, "set constraints cuckoo deferred");
- rollback();
+ c.rollback();
/*
* C O L U M N L E V E L C O N S T R A I N T S
*/
- assertColumnLevelDefaultBehaviorAccepted(s);
+ assertColumnLevelDefaultBehaviorAccepted(c, s);
assertColumnLevelFailTillFeatureImplemented(s);
// Characteristics are not currently allowed for NOT NULL,
@@ -1088,12 +1103,21 @@ public class ConstraintCharacteristicsTe
final static long SIZE = (1024L * 1024L * 10) / 256;
public void testManySimilarDuplicates() throws SQLException {
- Statement s = createStatement();
+ if (usingDerbyNetClient()) {
+ // skip, too heavy fixture to do twice... we use
+ // in memory db in any case...
+ return;
+ }
+
+ Connection c = dbm.createDatabase("cct");
+ c.setAutoCommit(false);
+
+ Statement s = c.createStatement();
try {
s.executeUpdate(
"create table t (i varchar(256), " +
"constraint c primary key(i) initially deferred)");
- PreparedStatement ps = prepareStatement("insert into t values ?");
+ PreparedStatement ps = c.prepareStatement("insert into t values ?");
char[] value = new char[256];
Arrays.fill(value, 'a');
ps.setString(1, String.valueOf(value));
@@ -1102,18 +1126,10 @@ public class ConstraintCharacteristicsTe
for (long l=0; l < SIZE; l++) {
ps.executeUpdate();
}
- commit();
+ c.commit();
} catch (SQLException e) {
assertSQLState(LANG_DEFERRED_CONSTRAINTS_VIOLATION, e);
s.executeUpdate("call syscs_util.syscs_checkpoint_database()");
- } finally {
- // clean up resources
- try {
- s.executeUpdate("drop table t");
- } catch (SQLException e) {
- // ignore, more interested in original exception
- }
- commit();
}
}
@@ -1172,13 +1188,14 @@ public class ConstraintCharacteristicsTe
}
}
- private void setupTab1() throws SQLException {
- Statement stmt = createStatement();
+ private static void setupTab1(String db) throws SQLException {
+ Connection c = dbm.getConnection(db);
+ Statement stmt = c.createStatement();
stmt.execute(
"create table tab1 (i integer)");
stmt.executeUpdate(
"alter table tab1 add constraint con1 unique (i) deferrable");
- PreparedStatement ps = prepareStatement("insert into tab1 " +
+ PreparedStatement ps = c.prepareStatement("insert into tab1 " +
"values (?)");
for (int i = 0; i < 10; i++) {
@@ -1188,15 +1205,16 @@ public class ConstraintCharacteristicsTe
ps.close();
stmt.close();
- commit();
+ c.commit();
}
- private void dropTab1() throws SQLException {
- Statement stmt = createStatement();
+ private static void dropTab1(String db) throws SQLException {
+ Connection c = dbm.getConnection(db);
+ Statement stmt = c.createStatement();
try {
stmt.execute("drop table tab1");
- commit();
+ c.commit();
} catch (SQLException e) {
// ignore so we get to see original exception if there is one
}
@@ -1223,18 +1241,21 @@ public class ConstraintCharacteristicsTe
* saw a the lock time-out error.
*/
public void testLockingWithCommit () throws Exception {
- setupTab1();
+ final String db = "cct";
+ dbm.createDatabase(db).close();
+ setupTab1(db);
try {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
executeThreads(
- (int)Math.pow(2,i),
- (int)Math.pow(2,j), true);
+ db,
+ (int)Math.pow(2,i),
+ (int)Math.pow(2,j), true);
}
}
} finally {
- dropTab1();
+ dropTab1(db);
}
}
@@ -1245,18 +1266,21 @@ public class ConstraintCharacteristicsTe
* See also comment for {@link #testLockingWithCommit() }.
*/
public void testLockingWithRollback () throws Exception {
- setupTab1();
+ final String db = "cct";
+ dbm.createDatabase(db).close();
+ setupTab1(db);
try {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
executeThreads(
- (int)Math.pow(2,i),
- (int)Math.pow(2,j), false);
+ db,
+ (int)Math.pow(2,i),
+ (int)Math.pow(2,j), false);
}
}
} finally {
- dropTab1();
+ dropTab1(db);
}
}
@@ -1266,6 +1290,7 @@ public class ConstraintCharacteristicsTe
* first transaction is committed or rolled back based on third
* parameter (boolean commit).
*
+ * @param db string of in-memory db to use
* @param isolation1 isolation level for 1st thread
* @param isolation2 isolation level for 2nd thread
* @param commit whether or not to commit
@@ -1273,11 +1298,16 @@ public class ConstraintCharacteristicsTe
* (Lifted from UniqueConstraintMultiThrededTest to test with deferrable
* constraint.)
*/
- private void executeThreads (int isolation1, int isolation2,
- boolean commit) throws Exception {
- Connection con1 = openDefaultConnection();
+ private static void executeThreads (
+ String db,
+ int isolation1,
+ int isolation2,
+ boolean commit) throws Exception {
+
+ Connection con1 = dbm.getConnection(db);
con1.setTransactionIsolation(isolation1);
- Connection con2 = openDefaultConnection();
+ Connection con2 = dbm.getConnection(db);
+
try {
con2.setTransactionIsolation(isolation2);
DBOperations dbo1 = new DBOperations (con1, 5);
@@ -1285,15 +1315,15 @@ public class ConstraintCharacteristicsTe
dbo1.delete();
Thread t = new Thread (dbo2);
t.start();
- //wait for 2 sec should be enough for dbo2 so on wait
- t.sleep(2000);
+
+ t.sleep((WAIT_TIMEOUT_DURATION * 1000) / 2 );
+
if (commit) {
dbo1.rollback();
t.join();
assertSQLState("isolation levels: " + isolation1
+ " " + isolation2, "23505", dbo2.getException());
- }
- else {
+ } else {
dbo1.commit();
t.join();
assertNull("isolation levels: " + isolation1
@@ -1454,7 +1484,7 @@ public class ConstraintCharacteristicsTe
* @throws SQLException
*/
- private void assertTableLevelFailTillFeatureImplemented(
+ private static void assertTableLevelFailTillFeatureImplemented(
Statement s) throws SQLException {
for (String ct : tableConstraintTypes) {
@@ -1476,7 +1506,7 @@ public class ConstraintCharacteristicsTe
* @throws SQLException
*/
- private void assertColumnLevelFailTillFeatureImplemented(
+ private static void assertColumnLevelFailTillFeatureImplemented(
Statement s) throws SQLException {
for (String ct : columnConstraintTypes) {
@@ -1493,18 +1523,20 @@ public class ConstraintCharacteristicsTe
* Assert that we accept characteristics that merely specify the default
* behavior anyway.
*
+ * @param c connection
* @param s statement
*
* @throws SQLException
*/
- private void assertTableLevelDefaultBehaviorAccepted (
+ private static void assertTableLevelDefaultBehaviorAccepted (
+ Connection c,
Statement s) throws SQLException {
for (String ct : tableConstraintTypes) {
for (String[] ch : defaultCharacteristics) {
assertUpdateCount(s, 0,
"create table t(i int, constraint c " + ct + ch[0] + ")");
- rollback();
+ c.rollback();
}
}
}
@@ -1513,18 +1545,20 @@ public class ConstraintCharacteristicsTe
* Assert that we accept characteristics that merely specify the default
* behavior anyway.
*
+ * @param c connection
* @param s statement
*
* @throws SQLException
*/
- private void assertColumnLevelDefaultBehaviorAccepted (
+ private static void assertColumnLevelDefaultBehaviorAccepted (
+ Connection c,
Statement s) throws SQLException {
for (String ct : columnConstraintTypes) {
for (String ch[] : defaultCharacteristics) {
assertUpdateCount(s, 0,
"create table t(i int " + ct + ch[0] + ")");
- rollback();
+ c.rollback();
}
}
}
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=1550308&r1=1550307&r2=1550308&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 Wed Dec 11 23:10:05 2013
@@ -29,6 +29,7 @@ import java.util.Properties;
import javax.sql.ConnectionPoolDataSource;
import junit.framework.AssertionFailedError;
+import org.apache.derby.shared.common.sanity.SanityManager;
/**
* Connection factory using javax.sql.ConnectionPoolDataSource.
@@ -173,7 +174,12 @@ public class ConnectionPoolDataSourceCon
config.waitForShutdownComplete(getDatabaseName());
}
- public void shutEngine() throws SQLException {
+ public void shutEngine(boolean deregisterDriver) throws SQLException {
+ if (SanityManager.DEBUG) {
+ // "false" only used with driver manager
+ SanityManager.ASSERT(deregisterDriver);
+ }
+
ConnectionPoolDataSource tmpDs =
singleUseDS( DataSourceConnector.makeShutdownDBAttributes( config ) );
JDBCDataSource.setBeanProperty(tmpDs, "databaseName", "");
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=1550308&r1=1550307&r2=1550308&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 Wed Dec 11 23:10:05 2013
@@ -104,8 +104,9 @@ interface Connector {
* This method can only be called when the
* engine is running embedded in this JVM.
* Return nothing, exception is expected to be thrown with SQLState XJ015
+ * @param deregisterDriver if true, deregister driver
*/
- abstract void shutEngine() throws SQLException;
+ abstract void shutEngine(boolean deregisterDriver) throws SQLException;
/**
* Set the login timeout for getting connections. Timeout is measured in seconds.
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=1550308&r1=1550307&r2=1550308&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 Wed Dec 11 23:10:05 2013
@@ -26,6 +26,7 @@ import java.util.Map;
import java.util.Properties;
import javax.sql.DataSource;
+import org.apache.derby.shared.common.sanity.SanityManager;
/**
@@ -139,7 +140,12 @@ public class DataSourceConnector impleme
config.waitForShutdownComplete(getDatabaseName());
}
- public void shutEngine() throws SQLException {
+ public void shutEngine(boolean deregisterDriver) throws SQLException {
+ if (SanityManager.DEBUG) {
+ // "false" only used with driver manager
+ SanityManager.ASSERT(deregisterDriver);
+ }
+
DataSource tmpDs = singleUseDS( makeShutdownDBAttributes( config ) );
JDBCDataSource.setBeanProperty(tmpDs, "databaseName", "");
tmpDs.getConnection();
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=1550308&r1=1550307&r2=1550308&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 Wed Dec 11 23:10:05 2013
@@ -22,6 +22,7 @@ package org.apache.derbyTesting.junit;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
+import java.util.Enumeration;
import java.util.Properties;
/**
@@ -154,8 +155,9 @@ public class DriverManagerConnector impl
* with the user and password defined by the configuration.
*/
public void shutDatabase() throws SQLException {
- getConnectionByAttributes(config.getJDBCUrl(),
- "shutdown", "true");
+ Properties p = new Properties();
+ p.setProperty("shutdown", "true");
+ getConnectionByAttributes(config.getJDBCUrl(), p);
config.waitForShutdownComplete(getDatabaseName());
}
@@ -165,10 +167,18 @@ public class DriverManagerConnector impl
* by the configuration.
* Always shutsdown using the embedded URL thus this
* method will not work in a remote testing environment.
+ * @param deregisterDriver
+ * @throws java.sql.SQLException
*/
- public void shutEngine() throws SQLException {
+ public void shutEngine(boolean deregisterDriver) throws SQLException {
+ Properties p = new Properties();
+ p.setProperty("shutdown", "true");
- getConnectionByAttributes("jdbc:derby:", "shutdown", "true");
+ if (!deregisterDriver) {
+ p.setProperty("deregister", "false");
+ }
+
+ getConnectionByAttributes("jdbc:derby:", p);
}
public void setLoginTimeout( int seconds ) throws SQLException
@@ -186,14 +196,20 @@ public class DriverManagerConnector impl
* The attributes user and password are set from the configuration
* and then the passed in attribute is set.
*/
- private Connection getConnectionByAttributes(String url, String key, String value)
+ private Connection getConnectionByAttributes(
+ String url,
+ Properties p)
throws SQLException
{
Properties attributes = new Properties();
attributes.setProperty("user", config.getUserName());
attributes.setProperty("password", config.getUserPassword());
- attributes.setProperty(key, value);
+
+ for (Enumeration e = p.keys(); e.hasMoreElements(); ) {
+ String key = (String)e.nextElement();
+ attributes.setProperty(key, p.getProperty(key));
+ }
try {
DriverManager.getDriver(url);
Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/SystemPropertyTestSetup.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/SystemPropertyTestSetup.java?rev=1550308&r1=1550307&r2=1550308&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/SystemPropertyTestSetup.java (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/SystemPropertyTestSetup.java Wed Dec 11 23:10:05 2013
@@ -109,12 +109,17 @@ public class SystemPropertyTestSetup ext
// during the tearDown of SystemPropertyTestSetup during the
// decorator's reuse.
this.oldValues = new Properties();
- // shutdown engine so static properties take effect
- // shutdown the engine before setting the properties. this
+
+ // Shutdown engine so static properties take effect.
+ // Shutdown the engine before setting the properties. This
// is because the properties may change authentication settings
// to NATIVE authentication and we may be missing a credentials DB.
- if (staticProperties)
- { TestConfiguration.getCurrent().shutdownEngine(); }
+ if (staticProperties) {
+ // Use deregister == false here lest in client server mode
+ // we try to connect to a network server without an embedded
+ // driver registered. Issue seen with ConstraintCharacteristicsTest.
+ TestConfiguration.getCurrent().shutdownEngine(false);
+ }
setProperties(newValues);
}
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=1550308&r1=1550307&r2=1550308&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 Wed Dec 11 23:10:05 2013
@@ -1792,7 +1792,25 @@ public final class TestConfiguration {
public void shutdownEngine()
{
try {
- connector.shutEngine();
+ connector.shutEngine(true);
+ Assert.fail("Engine failed to shut down");
+ } catch (SQLException e) {
+ BaseJDBCTestCase.assertSQLState("Engine shutdown", "XJ015", e);
+ }
+ }
+
+ /**
+ * Shutdown the engine for this configuration
+ * assuming it is booted.
+ * This method can only be called when the engine
+ * is running embedded in this JVM.
+ *
+ * @param deregisterDeriver if true, deregister the driver
+ */
+ public void shutdownEngine(boolean deregisterDeriver)
+ {
+ try {
+ connector.shutEngine(deregisterDeriver);
Assert.fail("Engine failed to shut down");
} catch (SQLException e) {
BaseJDBCTestCase.assertSQLState("Engine shutdown", "XJ015", e);
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=1550308&r1=1550307&r2=1550308&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 Wed Dec 11 23:10:05 2013
@@ -142,7 +142,7 @@ public class XADataSourceConnector imple
config.waitForShutdownComplete(getDatabaseName());
}
- public void shutEngine() throws SQLException {
+ public void shutEngine(boolean deregisterDriver) throws SQLException {
Assert.fail("shutdown engine not implemened");
}