tidy up a bunch of jdbc test cases to ensure embedded derby is shutdown

This commit is contained in:
gtully 2015-04-23 13:12:49 +01:00
parent 971ff0dd9e
commit 165959e250
23 changed files with 80 additions and 129 deletions

View File

@ -18,6 +18,7 @@ package org.apache.activemq.store.jdbc;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import javax.sql.DataSource;
@ -36,6 +37,7 @@ abstract public class DataSourceServiceSupport extends LockableServiceSupport {
private String dataDirectory = IOHelper.getDefaultDataDirectory();
private File dataDirectoryFile;
private DataSource dataSource;
private DataSource createdDefaultDataSource;
public DataSourceServiceSupport() {
}
@ -68,11 +70,19 @@ abstract public class DataSourceServiceSupport extends LockableServiceSupport {
dataSource = createDataSource(getDataDirectoryFile().getCanonicalPath());
if (dataSource == null) {
throw new IllegalArgumentException("No dataSource property has been configured");
} else {
createdDefaultDataSource = dataSource;
}
}
return dataSource;
}
public void closeDataSource(DataSource dataSource) {
if (createdDefaultDataSource != null && createdDefaultDataSource.equals(dataSource)) {
shutdownDefaultDataSource(dataSource);
}
}
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
@ -90,6 +100,15 @@ abstract public class DataSourceServiceSupport extends LockableServiceSupport {
return ds;
}
public static void shutdownDefaultDataSource(DataSource dataSource) {
final EmbeddedDataSource ds = (EmbeddedDataSource) dataSource;
ds.setShutdownDatabase("shutdown");
try {
ds.getConnection();
} catch (SQLException expectedAndIgnored) {
}
}
public String toString() {
return "" + dataSource;
}

View File

@ -143,7 +143,7 @@ public class DefaultDatabaseLocker extends AbstractJDBCLocker {
try {
connection.rollback();
} catch (SQLException sqle) {
LOG.warn("Exception while rollbacking the connection on shutdown. This exception is ignored.", sqle);
LOG.debug("Exception while rollbacking the connection on shutdown. This exception is ignored.", sqle);
} finally {
try {
connection.close();

View File

@ -336,6 +336,7 @@ public class JDBCPersistenceAdapter extends DataSourceServiceSupport implements
cleanupTicket.cancel(true);
cleanupTicket = null;
}
closeDataSource(getDataSource());
}
public void cleanup() {
@ -408,15 +409,14 @@ public class JDBCPersistenceAdapter extends DataSourceServiceSupport implements
throw new IllegalArgumentException(
"No dataSource property has been configured");
}
} else {
LOG.info("Using a separate dataSource for locking: "
+ lockDataSource);
}
return lockDataSource;
}
public void setLockDataSource(DataSource dataSource) {
this.lockDataSource = dataSource;
LOG.info("Using a separate dataSource for locking: "
+ lockDataSource);
}
@Override

View File

@ -63,11 +63,6 @@ public class AMQ4351Test extends BrokerTestSupport {
System.setProperty("derby.system.home", new File(IOHelper.getDefaultDataDirectory()).getCanonicalPath());
JDBCPersistenceAdapter jdbc = new JDBCPersistenceAdapter();
EmbeddedDataSource dataSource = new EmbeddedDataSource();
dataSource.setDatabaseName("derbyDb");
dataSource.setCreateDatabase("create");
jdbc.setDataSource(dataSource);
jdbc.deleteAllMessages();
broker.setPersistenceAdapter(jdbc);
return broker;

View File

@ -33,6 +33,7 @@ import org.apache.activemq.store.jdbc.JDBCPersistenceAdapter;
import org.apache.activemq.util.DefaultIOExceptionHandler;
import org.apache.activemq.util.IOHelper;
import org.apache.derby.jdbc.EmbeddedDataSource;
import org.junit.After;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -79,6 +80,10 @@ public class DbRestartJDBCQueueTest extends JmsTopicSendReceiveWithTwoConnection
broker.stop();
}
@After
public void shutDownDerby() {
DataSourceServiceSupport.shutdownDefaultDataSource(sharedDs);
}
protected Session createSendSession(Connection sendConnection) throws Exception {
if (transactedSends) {

View File

@ -35,7 +35,7 @@ import org.apache.activemq.util.IOHelper;
import org.apache.derby.jdbc.EmbeddedDataSource;
public class JDBCQueueMasterSlaveTest extends QueueMasterSlaveTestSupport {
protected DataSource sharedDs;
protected SyncCreateDataSource sharedDs;
protected String MASTER_URL = "tcp://localhost:62001";
protected String SLAVE_URL = "tcp://localhost:62002";
@ -45,6 +45,11 @@ public class JDBCQueueMasterSlaveTest extends QueueMasterSlaveTestSupport {
super.setUp();
}
protected void tearDown() throws Exception {
super.tearDown();
DataSourceServiceSupport.shutdownDefaultDataSource(sharedDs.delegate);
}
protected void createMaster() throws Exception {
master = new BrokerService();
master.setBrokerName("master");

View File

@ -78,15 +78,8 @@ public class AMQ4636Test {
broker.stop();
broker.waitUntilStopped();
}
try {
if (embeddedDataSource != null) {
// ref http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/JDBCDataSource.java?view=markup
embeddedDataSource.setShutdownDatabase("shutdown");
embeddedDataSource.getConnection();
}
} catch (Exception ignored) {
} finally {
embeddedDataSource.setShutdownDatabase(null);
DataSourceServiceSupport.shutdownDefaultDataSource(embeddedDataSource);
}
}

View File

@ -55,6 +55,7 @@ import org.apache.activemq.command.ActiveMQDestination;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.network.ConditionalNetworkBridgeFilterFactory;
import org.apache.activemq.network.NetworkConnector;
import org.apache.activemq.store.jdbc.DataSourceServiceSupport;
import org.apache.activemq.store.jdbc.JDBCPersistenceAdapter;
import org.apache.activemq.util.IOHelper;
import org.apache.activemq.util.IntrospectionSupport;
@ -275,13 +276,19 @@ public class AMQ4952Test {
protected void doTearDown() throws Exception {
DataSource dataSource = ((JDBCPersistenceAdapter)producerBroker.getPersistenceAdapter()).getDataSource();
try {
producerBroker.stop();
} catch (Exception ex) {
} finally {
DataSourceServiceSupport.shutdownDefaultDataSource(dataSource);
}
dataSource = ((JDBCPersistenceAdapter)consumerBroker.getPersistenceAdapter()).getDataSource();
try {
consumerBroker.stop();
} catch (Exception ex) {
} finally {
DataSourceServiceSupport.shutdownDefaultDataSource(dataSource);
}
}

View File

@ -73,7 +73,6 @@ public class TrapMessageInJDBCStoreTest extends TestCase {
broker.setUseJmx(withJMX);
EmbeddedDataSource embeddedDataSource = (EmbeddedDataSource) DataSourceServiceSupport.createDataSource(IOHelper.getDefaultDataDirectory());
embeddedDataSource.setCreateDatabase("create");
//wire in a TestTransactionContext (wrapper to TransactionContext) that has an executeBatch()
// method that can be configured to throw a SQL exception on demand

View File

@ -563,7 +563,7 @@ abstract public class MessagePriorityTest extends CombinationTestSupport {
}
public void testQueueBacklog() throws Exception {
final int backlog = 180000;
final int backlog = 18000;
ActiveMQQueue queue = (ActiveMQQueue)sess.createQueue("TEST");
ProducerThread lowPri = new ProducerThread(queue, backlog, LOW_PRI);

View File

@ -47,8 +47,6 @@ public class JDBCCommitExceptionTest extends TestCase {
protected ActiveMQConnectionFactory factory;
protected BrokerService broker;
protected String connectionUri;
protected EmbeddedDataSource dataSource;
protected java.sql.Connection dbConnection;
protected BrokenPersistenceAdapter jdbc;
@Override
@ -155,11 +153,6 @@ public class JDBCCommitExceptionTest extends TestCase {
BrokerService broker = new BrokerService();
jdbc = new BrokenPersistenceAdapter();
dataSource = new EmbeddedDataSource();
dataSource.setDatabaseName("target/derbyDb");
dataSource.setCreateDatabase("create");
jdbc.setDataSource(dataSource);
jdbc.setUseLock(false);
jdbc.deleteAllMessages();

View File

@ -31,6 +31,7 @@ import org.apache.activemq.util.IOHelper;
import org.apache.activemq.util.LeaseLockerIOExceptionHandler;
import org.apache.activemq.util.Wait;
import org.apache.derby.jdbc.EmbeddedDataSource;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
@ -51,14 +52,15 @@ public class JDBCIOExceptionHandlerTest {
private static final Logger LOG = LoggerFactory.getLogger(JDBCIOExceptionHandlerTest.class);
private static final String TRANSPORT_URL = "tcp://0.0.0.0:0";
private static final String DATABASE_NAME = "DERBY_OVERRIDE";
private ActiveMQConnectionFactory factory;
private ReconnectingEmbeddedDataSource dataSource;
private BrokerService broker;
@Before
public void dbHomeSysProp() throws Exception {
System.setProperty("derby.system.home", new File(IOHelper.getDefaultDataDirectory()).getCanonicalPath());
@After
public void stopDB() {
if (dataSource != null) {
dataSource.stopDB();
}
}
protected BrokerService createBroker(boolean withJMX) throws Exception {
@ -71,15 +73,11 @@ public class JDBCIOExceptionHandlerTest {
broker.setUseJmx(withJMX);
EmbeddedDataSource embeddedDataSource = new EmbeddedDataSource();
embeddedDataSource.setDatabaseName(DATABASE_NAME);
embeddedDataSource.setCreateDatabase("create");
JDBCPersistenceAdapter jdbc = new JDBCPersistenceAdapter();
EmbeddedDataSource embeddedDataSource = (EmbeddedDataSource) jdbc.getDataSource();
// create a wrapper to EmbeddedDataSource to allow the connection be
// reestablished to derby db
dataSource = new ReconnectingEmbeddedDataSource(embeddedDataSource);
JDBCPersistenceAdapter jdbc = new JDBCPersistenceAdapter();
jdbc.setDataSource(dataSource);
jdbc.setLockKeepAlivePeriod(1000l);
@ -310,23 +308,17 @@ public class JDBCIOExceptionHandlerTest {
*
* @throws SQLException
*/
public void restartDB() throws SQLException {
EmbeddedDataSource newDatasource = new EmbeddedDataSource();
newDatasource.setDatabaseName(DATABASE_NAME);
public void restartDB() throws Exception {
EmbeddedDataSource newDatasource =
(EmbeddedDataSource) DataSourceServiceSupport.createDataSource(broker.getDataDirectoryFile().getCanonicalPath());
newDatasource.getConnection();
LOG.info("*** DB restarted now...");
this.realDatasource = newDatasource;
}
public void stopDB() {
try {
realDatasource.setShutdownDatabase("shutdown");
LOG.info("***DB is being shutdown...");
dataSource.getConnection();
fail("should have thrown a db closed exception");
} catch (Exception ex) {
ex.printStackTrace(System.out);
}
DataSourceServiceSupport.shutdownDefaultDataSource(realDatasource);
}
public java.util.logging.Logger getParentLogger() throws SQLFeatureNotSupportedException {

View File

@ -34,6 +34,7 @@ import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.TopicSubscriber;
import javax.sql.DataSource;
import junit.framework.Test;
import org.apache.activemq.command.ActiveMQMessage;
@ -48,39 +49,19 @@ import org.slf4j.LoggerFactory;
public class JDBCMessagePriorityTest extends MessagePriorityTest {
private static final Logger LOG = LoggerFactory.getLogger(JDBCMessagePriorityTest.class);
EmbeddedDataSource dataSource;
DataSource dataSource;
JDBCPersistenceAdapter jdbc;
@Override
protected PersistenceAdapter createPersistenceAdapter(boolean delete) throws Exception {
jdbc = new JDBCPersistenceAdapter();
dataSource = new EmbeddedDataSource();
dataSource.setDatabaseName("derbyDb");
dataSource.setCreateDatabase("create");
dataSource.setShutdownDatabase(null);
jdbc.setDataSource(dataSource);
dataSource = jdbc.getDataSource();
jdbc.deleteAllMessages();
jdbc.setCleanupPeriod(2000);
return jdbc;
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
try {
if (dataSource != null) {
// ref http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/JDBCDataSource.java?view=markup
dataSource.setShutdownDatabase("shutdown");
dataSource.getConnection();
}
} catch (Exception ignored) {
} finally {
dataSource.setShutdownDatabase(null);
}
}
// this cannot be a general test as kahaDB just has support for 3 priority levels
public void testDurableSubsReconnectWithFourLevels() throws Exception {
ActiveMQTopic topic = (ActiveMQTopic) sess.createTopic("TEST");

View File

@ -35,7 +35,6 @@ public class JDBCNegativeQueueTest extends NegativeQueueTest {
protected void configureBroker(BrokerService answer) throws Exception {
super.configureBroker(answer);
JDBCPersistenceAdapter jdbc = new JDBCPersistenceAdapter();
jdbc.setDataSource(dataSource);
answer.setPersistenceAdapter(jdbc);
dataSource = jdbc.getDataSource();
}

View File

@ -24,10 +24,8 @@ public class JDBCNetworkBrokerDetachTest extends NetworkBrokerDetachTest {
protected void configureBroker(BrokerService broker) throws Exception {
JDBCPersistenceAdapter jdbc = new JDBCPersistenceAdapter();
EmbeddedDataSource dataSource = new EmbeddedDataSource();
EmbeddedDataSource dataSource = (EmbeddedDataSource) jdbc.getDataSource();
dataSource.setDatabaseName(broker.getBrokerName());
dataSource.setCreateDatabase("create");
jdbc.setDataSource(dataSource);
jdbc.deleteAllMessages();
broker.setPersistenceAdapter(jdbc);
broker.setUseVirtualTopics(false);

View File

@ -22,7 +22,6 @@ import junit.framework.AssertionFailedError;
import org.apache.activemq.store.PersistenceAdapter;
import org.apache.activemq.store.PersistenceAdapterTestSupport;
import org.apache.derby.jdbc.EmbeddedDataSource;
public class JDBCPersistenceAdapterTest extends PersistenceAdapterTestSupport {
@ -35,11 +34,6 @@ public class JDBCPersistenceAdapterTest extends PersistenceAdapterTestSupport {
brokerService.setSchedulerSupport(false);
brokerService.setPersistenceAdapter(jdbc);
jdbc.setBrokerService(brokerService);
EmbeddedDataSource dataSource = new EmbeddedDataSource();
dataSource.setDatabaseName("derbyDb");
dataSource.setCreateDatabase("create");
jdbc.setDataSource(dataSource);
if( delete ) {
jdbc.deleteAllMessages();
}

View File

@ -47,6 +47,7 @@ import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.sql.DataSource;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.broker.BrokerService;
@ -137,22 +138,21 @@ public class JDBCStoreAutoCommitTest {
c1.close();
broker.stop();
broker.waitUntilStopped();
if (realDataSource != null) {
DataSourceServiceSupport.shutdownDefaultDataSource(realDataSource);
}
}
}
DataSource realDataSource;
private BrokerService createBrokerService() throws IOException {
BrokerService broker = new BrokerService();
broker.setBrokerName(BROKER_NAME);
broker.setUseJmx(false);
JDBCPersistenceAdapter jdbc = new JDBCPersistenceAdapter();
EmbeddedDataSource embeddedDataSource = new EmbeddedDataSource();
embeddedDataSource.setDatabaseName("derbyDb");
embeddedDataSource.setCreateDatabase("create");
javax.sql.DataSource wrappedDataSource = new TestDataSource(embeddedDataSource);
jdbc.setDataSource(wrappedDataSource);
realDataSource = jdbc.getDataSource();
jdbc.setDataSource(new TestDataSource(realDataSource));
broker.setPersistenceAdapter(jdbc);
return broker;

View File

@ -27,24 +27,14 @@ public class JDBCStoreBrokerTest extends BrokerTest {
protected BrokerService createBroker() throws Exception {
BrokerService broker = new BrokerService();
JDBCPersistenceAdapter jdbc = new JDBCPersistenceAdapter();
EmbeddedDataSource dataSource = new EmbeddedDataSource();
dataSource.setDatabaseName("derbyDb");
dataSource.setCreateDatabase("create");
jdbc.setDataSource(dataSource);
jdbc.deleteAllMessages();
broker.setPersistenceAdapter(jdbc);
return broker;
}
protected BrokerService createRestartedBroker() throws Exception {
protected BrokerService x_createRestartedBroker() throws Exception {
BrokerService broker = new BrokerService();
JDBCPersistenceAdapter jdbc = new JDBCPersistenceAdapter();
EmbeddedDataSource dataSource = new EmbeddedDataSource();
dataSource.setDatabaseName("derbyDb");
dataSource.setCreateDatabase("create");
jdbc.setDataSource(dataSource);
broker.setPersistenceAdapter(jdbc);
broker.setPersistenceAdapter(new JDBCPersistenceAdapter());
return broker;
}

View File

@ -52,12 +52,7 @@ public class JDBCStoreOrderTest extends StoreOrderTest {
@Override
protected void setPersistentAdapter(BrokerService brokerService)
throws Exception {
JDBCPersistenceAdapter jdbc = new JDBCPersistenceAdapter();
EmbeddedDataSource dataSource = new EmbeddedDataSource();
dataSource.setDatabaseName("derbyDb");
dataSource.setCreateDatabase("create");
jdbc.setDataSource(dataSource);
brokerService.setPersistenceAdapter(jdbc);
brokerService.setPersistenceAdapter(new JDBCPersistenceAdapter());
}
}

View File

@ -112,10 +112,6 @@ public class JDBCTablePrefixAssignedTest {
protected BrokerService createBroker() throws Exception {
BrokerService broker = new BrokerService();
JDBCPersistenceAdapter jdbc = new JDBCPersistenceAdapter();
EmbeddedDataSource dataSource = new EmbeddedDataSource();
dataSource.setDatabaseName("derbyDb");
dataSource.setCreateDatabase("create");
DefaultJDBCAdapter adapter = new DefaultJDBCAdapter();
jdbc.setAdapter(adapter);
@ -124,7 +120,6 @@ public class JDBCTablePrefixAssignedTest {
jdbc.setStatements(statements);
jdbc.setUseLock(false);
jdbc.setDataSource(dataSource);
jdbc.deleteAllMessages();
broker.setPersistenceAdapter(jdbc);
return broker;

View File

@ -53,11 +53,6 @@ public class JDBCTestMemory extends TestCase {
BrokerService broker = new BrokerService();
broker.setUseJmx(true);
JDBCPersistenceAdapter jdbc = new JDBCPersistenceAdapter();
EmbeddedDataSource dataSource = new EmbeddedDataSource();
dataSource.setDatabaseName("derbyDb");
dataSource.setCreateDatabase("create");
jdbc.setDataSource(dataSource);
jdbc.deleteAllMessages();
broker.setPersistenceAdapter(jdbc);
broker.addConnector("tcp://0.0.0.0:61616");
@ -68,10 +63,6 @@ public class JDBCTestMemory extends TestCase {
BrokerService broker = new BrokerService();
broker.setUseJmx(true);
JDBCPersistenceAdapter jdbc = new JDBCPersistenceAdapter();
EmbeddedDataSource dataSource = new EmbeddedDataSource();
dataSource.setDatabaseName("derbyDb");
dataSource.setCreateDatabase("create");
jdbc.setDataSource(dataSource);
broker.setPersistenceAdapter(jdbc);
broker.addConnector("tcp://0.0.0.0:61616");
return broker;

View File

@ -27,6 +27,7 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.sql.DataSource;
import org.apache.activemq.broker.AbstractLocker;
import org.apache.activemq.broker.BrokerService;
import org.apache.activemq.store.jdbc.adapter.DefaultJDBCAdapter;
@ -35,6 +36,7 @@ import org.apache.derby.jdbc.EmbeddedDataSource;
import org.jmock.Expectations;
import org.jmock.Mockery;
import org.jmock.lib.legacy.ClassImposteriser;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
@ -51,20 +53,22 @@ public class LeaseDatabaseLockerTest {
JDBCPersistenceAdapter jdbc;
BrokerService brokerService;
EmbeddedDataSource dataSource;
DataSource dataSource;
@Before
public void setUpStore() throws Exception {
dataSource = new EmbeddedDataSource();
dataSource.setDatabaseName("derbyDb");
dataSource.setCreateDatabase("create");
jdbc = new JDBCPersistenceAdapter();
jdbc.setDataSource(dataSource);
dataSource = jdbc.getDataSource();
brokerService = new BrokerService();
jdbc.setBrokerService(brokerService);
jdbc.getAdapter().doCreateTables(jdbc.getTransactionContext());
}
@After
public void stopDerby() {
DataSourceServiceSupport.shutdownDefaultDataSource(dataSource);
}
@Test
public void testLockInterleave() throws Exception {

View File

@ -29,10 +29,6 @@ public class JDBCDurableSubscriptionTest extends DurableSubscriptionTestSupport
protected PersistenceAdapter createPersistenceAdapter() throws IOException {
JDBCPersistenceAdapter jdbc = new JDBCPersistenceAdapter();
EmbeddedDataSource dataSource = new EmbeddedDataSource();
dataSource.setDatabaseName("derbyDb");
dataSource.setCreateDatabase("create");
jdbc.setDataSource(dataSource);
jdbc.setCleanupPeriod(1000); // set up small cleanup period
return jdbc;
}