From de85ea2b2e4fb7ff2a6deb8e7884ae5ec0404610 Mon Sep 17 00:00:00 2001 From: chenson42 Date: Sat, 26 Apr 2008 20:55:38 +0000 Subject: [PATCH] try to cleanup up after an integration test. --- .../jumpmind/symmetric/SymmetricEngine.java | 91 +++++++++--- .../jumpmind/symmetric/IntegrationTest.java | 136 +++++++++++------- 2 files changed, 150 insertions(+), 77 deletions(-) diff --git a/symmetric/src/main/java/org/jumpmind/symmetric/SymmetricEngine.java b/symmetric/src/main/java/org/jumpmind/symmetric/SymmetricEngine.java index 3aff113bae..924b653cc1 100644 --- a/symmetric/src/main/java/org/jumpmind/symmetric/SymmetricEngine.java +++ b/symmetric/src/main/java/org/jumpmind/symmetric/SymmetricEngine.java @@ -20,10 +20,15 @@ package org.jumpmind.symmetric; +import java.sql.SQLException; import java.util.HashMap; import java.util.Map; import java.util.Properties; +import java.util.Set; +import javax.sql.DataSource; + +import org.apache.commons.dbcp.BasicDataSource; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.jumpmind.symmetric.common.Constants; @@ -96,14 +101,48 @@ public SymmetricEngine(String overridePropertiesResource1, String overrideProper // Setting system properties is probably not the best way to accomplish this setup. // Synchronizing on the class so creating multiple engines is thread safe. synchronized (SymmetricEngine.class) { - System.setProperty("symmetric.override.properties.file.1", overridePropertiesResource1 == null ? "" - : overridePropertiesResource1); - System.setProperty("symmetric.override.properties.file.2", overridePropertiesResource2 == null ? "" - : overridePropertiesResource2); + System.setProperty("symmetric.override.properties.file.1", + overridePropertiesResource1 == null ? "" : overridePropertiesResource1); + System.setProperty("symmetric.override.properties.file.2", + overridePropertiesResource2 == null ? "" : overridePropertiesResource2); this.init(createContext(), null); } } + public void close() { + logger.info("Closing SymmetricDS externalId=" + runtimeConfig.getExternalId() + " version=" + + Version.version() + " database=" + dbDialect.getName()); + removeMeFromMap(registeredEnginesByName); + removeMeFromMap(registeredEnginesByUrl); + DataSource ds = dbDialect.getJdbcTemplate().getDataSource(); + if (ds instanceof BasicDataSource) { + try { + ((BasicDataSource) ds).close(); + } catch (SQLException ex) { + logger.error(ex, ex); + } + } + applicationContext = null; + properties = null; + bootstrapService = null; + runtimeConfig = null; + nodeService = null; + registrationService = null; + purgeService = null; + dataService = null; + dbDialect = null; + + } + + private void removeMeFromMap(Map map) { + Set keys = map.keySet(); + for (String key : keys) { + if (map.get(key).equals(this)) { + map.remove(key); + } + } + } + /** * Create a symmetric node */ @@ -133,7 +172,8 @@ private void init(ApplicationContext applicationContext, IActivityListener activ bootstrapService = (IBootstrapService) applicationContext.getBean(Constants.BOOTSTRAP_SERVICE); runtimeConfig = (IRuntimeConfig) applicationContext.getBean(Constants.RUNTIME_CONFIG); nodeService = (INodeService) applicationContext.getBean(Constants.NODE_SERVICE); - registrationService = (IRegistrationService) applicationContext.getBean(Constants.REGISTRATION_SERVICE); + registrationService = (IRegistrationService) applicationContext + .getBean(Constants.REGISTRATION_SERVICE); purgeService = (IPurgeService) applicationContext.getBean(Constants.PURGE_SERVICE); dataService = (IDataService) applicationContext.getBean(Constants.DATA_SERVICE); dbDialect = (IDbDialect) applicationContext.getBean(Constants.DB_DIALECT); @@ -162,31 +202,35 @@ private void registerEngine() { * Start the jobs if they are configured to be started in symmetric.properties */ private void startJobs() { - if (Boolean.TRUE.toString().equalsIgnoreCase(properties.getProperty(PropertiesConstants.START_PUSH_JOB))) { + if (Boolean.TRUE.toString().equalsIgnoreCase( + properties.getProperty(PropertiesConstants.START_PUSH_JOB))) { applicationContext.getBean(Constants.PUSH_JOB_TIMER); } - if (Boolean.TRUE.toString().equalsIgnoreCase(properties.getProperty(PropertiesConstants.START_PULL_JOB))) { + if (Boolean.TRUE.toString().equalsIgnoreCase( + properties.getProperty(PropertiesConstants.START_PULL_JOB))) { applicationContext.getBean(Constants.PULL_JOB_TIMER); } - if (Boolean.TRUE.toString().equalsIgnoreCase(properties.getProperty(PropertiesConstants.START_PURGE_JOB))) { + if (Boolean.TRUE.toString().equalsIgnoreCase( + properties.getProperty(PropertiesConstants.START_PURGE_JOB))) { applicationContext.getBean(Constants.PURGE_JOB_TIMER); } - if (Boolean.TRUE.toString().equalsIgnoreCase(properties.getProperty(PropertiesConstants.START_HEARTBEAT_JOB))) { + if (Boolean.TRUE.toString().equalsIgnoreCase( + properties.getProperty(PropertiesConstants.START_HEARTBEAT_JOB))) { applicationContext.getBean(Constants.HEARTBEAT_JOB_TIMER); } - if (Boolean.TRUE.toString() - .equalsIgnoreCase(properties.getProperty(PropertiesConstants.START_SYNCTRIGGERS_JOB))) { + if (Boolean.TRUE.toString().equalsIgnoreCase( + properties.getProperty(PropertiesConstants.START_SYNCTRIGGERS_JOB))) { applicationContext.getBean(Constants.SYNC_TRIGGERS_JOB_TIMER); } - - if (Boolean.TRUE.toString() - .equalsIgnoreCase(properties.getProperty(PropertiesConstants.START_STATISTIC_FLUSH_JOB))) { + + if (Boolean.TRUE.toString().equalsIgnoreCase( + properties.getProperty(PropertiesConstants.START_STATISTIC_FLUSH_JOB))) { applicationContext.getBean(Constants.STATISTIC_FLUSH_JOB_TIMER); } - + } /** @@ -219,11 +263,11 @@ public synchronized void start() { setup(); Node node = nodeService.findIdentity(); if (node != null) { - logger.info("Starting registered node [group=" + node.getNodeGroupId() + ", id=" + node.getNodeId() - + ", externalId=" + node.getExternalId() + "]"); + logger.info("Starting registered node [group=" + node.getNodeGroupId() + ", id=" + + node.getNodeId() + ", externalId=" + node.getExternalId() + "]"); } else { - logger.info("Starting unregistered node [group=" + runtimeConfig.getNodeGroupId() + ", externalId=" - + runtimeConfig.getExternalId() + "]"); + logger.info("Starting unregistered node [group=" + runtimeConfig.getNodeGroupId() + + ", externalId=" + runtimeConfig.getExternalId() + "]"); } bootstrapService.register(); bootstrapService.syncTriggers(); @@ -245,7 +289,8 @@ public void reloadNode(String nodeId) { * @see IPushService#pushData() */ public void push() { - if (!Boolean.TRUE.toString().equalsIgnoreCase(properties.getProperty(PropertiesConstants.START_PUSH_JOB))) { + if (!Boolean.TRUE.toString().equalsIgnoreCase( + properties.getProperty(PropertiesConstants.START_PUSH_JOB))) { ((IPushService) applicationContext.getBean(Constants.PUSH_SERVICE)).pushData(); } else { throw new UnsupportedOperationException("Cannot actuate a push if it is already scheduled."); @@ -266,7 +311,8 @@ public void syncTriggers() { * @see IPullService#pullData() */ public void pull() { - if (!Boolean.TRUE.toString().equalsIgnoreCase(properties.getProperty(PropertiesConstants.START_PULL_JOB))) { + if (!Boolean.TRUE.toString().equalsIgnoreCase( + properties.getProperty(PropertiesConstants.START_PULL_JOB))) { ((IPullService) applicationContext.getBean(Constants.PULL_SERVICE)).pullData(); } else { throw new UnsupportedOperationException("Cannot actuate a push if it is already scheduled."); @@ -278,7 +324,8 @@ public void pull() { * @see IPurgeService#purge() */ public void purge() { - if (!Boolean.TRUE.toString().equalsIgnoreCase(properties.getProperty(PropertiesConstants.START_PURGE_JOB))) { + if (!Boolean.TRUE.toString().equalsIgnoreCase( + properties.getProperty(PropertiesConstants.START_PURGE_JOB))) { purgeService.purge(); } else { throw new UnsupportedOperationException("Cannot actuate a purge if it is already scheduled."); diff --git a/symmetric/src/test/java/org/jumpmind/symmetric/IntegrationTest.java b/symmetric/src/test/java/org/jumpmind/symmetric/IntegrationTest.java index 1a0e2f220d..11a5eb24f0 100644 --- a/symmetric/src/test/java/org/jumpmind/symmetric/IntegrationTest.java +++ b/symmetric/src/test/java/org/jumpmind/symmetric/IntegrationTest.java @@ -84,6 +84,8 @@ public void testLifecycle() { } catch (Exception ex) { logger.error(ex, ex); Assert.fail(); + } finally { + teardown(); } } @@ -96,35 +98,51 @@ protected void init() { clientJdbcTemplate = new JdbcTemplate((DataSource) clientBeanFactory.getBean(Constants.DATA_SOURCE)); } + protected void teardown() { + rootJdbcTemplate = null; + clientJdbcTemplate = null; + getRootEngine().close(); + getClientEngine().close(); + } + protected void register() { - getRootEngine().openRegistration(TestConstants.TEST_CLIENT_NODE_GROUP, TestConstants.TEST_CLIENT_EXTERNAL_ID); + getRootEngine().openRegistration(TestConstants.TEST_CLIENT_NODE_GROUP, + TestConstants.TEST_CLIENT_EXTERNAL_ID); getClientEngine().start(); Assert.assertTrue(getClientEngine().isRegistered(), "The client did not register."); - IStatisticManager statMgr = (IStatisticManager)getClientEngine().getApplicationContext().getBean(Constants.STATISTIC_MANAGER); + IStatisticManager statMgr = (IStatisticManager) getClientEngine().getApplicationContext().getBean( + Constants.STATISTIC_MANAGER); statMgr.flush(); } - protected void initialLoad() { + protected void initialLoad() { IDbDialect rootDialect = getRootDbDialect(); rootJdbcTemplate.update(insertCustomerSql, new Object[] { 301, "Linus", "1", "42 Blanket Street", "Santa Claus", "IN", 90009, new Date(), "This is a test", BINARY_DATA }); - insertIntoTestTriggerTable(rootDialect, new Object[] {1, "wow", "mom"}); - insertIntoTestTriggerTable(rootDialect, new Object[] {2, "mom", "wow"}); + insertIntoTestTriggerTable(rootDialect, new Object[] { 1, "wow", "mom" }); + insertIntoTestTriggerTable(rootDialect, new Object[] { 2, "mom", "wow" }); INodeService nodeService = (INodeService) getRootEngine().getApplicationContext().getBean( Constants.NODE_SERVICE); String nodeId = nodeService.findNodeByExternalId(TestConstants.TEST_CLIENT_NODE_GROUP, TestConstants.TEST_CLIENT_EXTERNAL_ID).getNodeId(); getRootEngine().reloadNode(nodeId); - IOutgoingBatchService outgoingBatchService = (IOutgoingBatchService)getRootEngine().getApplicationContext().getBean(Constants.OUTGOING_BATCH_SERVICE); + IOutgoingBatchService outgoingBatchService = (IOutgoingBatchService) getRootEngine() + .getApplicationContext().getBean(Constants.OUTGOING_BATCH_SERVICE); Assert.assertFalse(outgoingBatchService.isInitialLoadComplete(nodeId)); getClientEngine().pull(); Assert.assertTrue(outgoingBatchService.isInitialLoadComplete(nodeId)); - Assert.assertEquals(clientJdbcTemplate.queryForInt("select count(*) from test_triggers_table"), 2, "test_triggers_table on the client did not contain the expected number of rows"); - Assert.assertEquals(clientJdbcTemplate.queryForInt("select count(*) from test_customer"), 2, "test_customer on the client did not contain the expected number of rows"); - Assert.assertEquals(clientJdbcTemplate.queryForInt("select count(*) from sym_node_security where initial_load_enabled=1"), 0, "Initial load was not successful according to the client"); - Assert.assertEquals(rootJdbcTemplate.queryForInt("select count(*) from sym_node_security where initial_load_enabled=1"), 0, "Initial load was not successful accordign to the root"); + Assert.assertEquals(clientJdbcTemplate.queryForInt("select count(*) from test_triggers_table"), 2, + "test_triggers_table on the client did not contain the expected number of rows"); + Assert.assertEquals(clientJdbcTemplate.queryForInt("select count(*) from test_customer"), 2, + "test_customer on the client did not contain the expected number of rows"); + Assert.assertEquals(clientJdbcTemplate + .queryForInt("select count(*) from sym_node_security where initial_load_enabled=1"), 0, + "Initial load was not successful according to the client"); + Assert.assertEquals(rootJdbcTemplate + .queryForInt("select count(*) from sym_node_security where initial_load_enabled=1"), 0, + "Initial load was not successful accordign to the root"); } - + private void insertIntoTestTriggerTable(IDbDialect dialect, Object[] values) { Table testTriggerTable = dialect.getMetaDataFor(null, null, "test_triggers_table", true); try { @@ -140,12 +158,13 @@ protected void testSyncToClient() { getClientEngine().pull(); // now change some data that should be sync'd - rootJdbcTemplate.update(insertCustomerSql, new Object[] { 101, "Charlie Brown", "1", "300 Grub Street", - "New Yorl", "NY", 90009, new Date(), "This is a test", BINARY_DATA }); + rootJdbcTemplate.update(insertCustomerSql, new Object[] { 101, "Charlie Brown", "1", + "300 Grub Street", "New Yorl", "NY", 90009, new Date(), "This is a test", BINARY_DATA }); getClientEngine().pull(); - Assert.assertEquals(clientJdbcTemplate.queryForInt("select count(*) from test_customer where customer_id=101"), - 1, "The customer was not sync'd to the client."); + Assert.assertEquals(clientJdbcTemplate + .queryForInt("select count(*) from test_customer where customer_id=101"), 1, + "The customer was not sync'd to the client."); if (getRootDbDialect().isClobSyncSupported()) { Assert.assertEquals(clientJdbcTemplate.queryForObject( @@ -165,21 +184,22 @@ protected void testSyncToClient() { protected void testSyncToRootAutoGeneratedPrimaryKey() { final String NEW_VALUE = "unique new value one value"; IDbDialect clientDialect = getClientDbDialect(); - insertIntoTestTriggerTable(clientDialect, new Object[] { 3, "value one", "value \" two"}); + insertIntoTestTriggerTable(clientDialect, new Object[] { 3, "value one", "value \" two" }); getClientEngine().push(); clientJdbcTemplate.update(updateTestTriggerTableSql, new Object[] { NEW_VALUE }); getClientEngine().push(); int syncCount = rootJdbcTemplate.queryForInt( - "select count(*) from test_triggers_table where string_one_value=?", new Object[] { NEW_VALUE }); - Assert.assertEquals(syncCount, 3, - syncCount + " of the rows were updated"); + "select count(*) from test_triggers_table where string_one_value=?", + new Object[] { NEW_VALUE }); + Assert.assertEquals(syncCount, 3, syncCount + " of the rows were updated"); } protected void testSyncToRoot() throws ParseException { Date date = DateUtils.parseDate("2007-01-03", new String[] { "yyyy-MM-dd" }); clientJdbcTemplate.update(insertOrderHeaderSql, new Object[] { "10", 100, null, date }, new int[] { Types.VARCHAR, Types.INTEGER, Types.CHAR, Types.DATE }); - clientJdbcTemplate.update(insertOrderDetailSql, new Object[] { "10", 1, "STK", "110000065", 3, 3.33 }); + clientJdbcTemplate + .update(insertOrderDetailSql, new Object[] { "10", 1, "STK", "110000065", 3, 3.33 }); getClientEngine().push(); } @@ -190,20 +210,21 @@ protected void testSyncInsertCondition() throws ParseException { Types.VARCHAR, Types.INTEGER, Types.CHAR, Types.DATE }); getClientEngine().pull(); - IOutgoingBatchService outgoingBatchService = (IOutgoingBatchService) getRootEngine().getApplicationContext() - .getBean(Constants.OUTGOING_BATCH_SERVICE); - List batches = outgoingBatchService.getOutgoingBatches(TestConstants.TEST_CLIENT_EXTERNAL_ID); + IOutgoingBatchService outgoingBatchService = (IOutgoingBatchService) getRootEngine() + .getApplicationContext().getBean(Constants.OUTGOING_BATCH_SERVICE); + List batches = outgoingBatchService + .getOutgoingBatches(TestConstants.TEST_CLIENT_EXTERNAL_ID); Assert.assertEquals(batches.size(), 0, "There should be no outgoing batches, yet I found some."); - Assert.assertEquals(clientJdbcTemplate.queryForList(selectOrderHeaderSql, new Object[] { "11" }).size(), 0, - "The order record was sync'd when it should not have been."); + Assert.assertEquals(clientJdbcTemplate.queryForList(selectOrderHeaderSql, new Object[] { "11" }) + .size(), 0, "The order record was sync'd when it should not have been."); // Should sync when status = C - rootJdbcTemplate.update(insertOrderHeaderSql, new Object[] { "12", 100, "C", date }, new int[] { Types.VARCHAR, - Types.INTEGER, Types.CHAR, Types.DATE }); + rootJdbcTemplate.update(insertOrderHeaderSql, new Object[] { "12", 100, "C", date }, new int[] { + Types.VARCHAR, Types.INTEGER, Types.CHAR, Types.DATE }); getClientEngine().pull(); - Assert.assertEquals(clientJdbcTemplate.queryForList(selectOrderHeaderSql, new Object[] { "12" }).size(), 1, - "The order record was not sync'd when it should have been."); + Assert.assertEquals(clientJdbcTemplate.queryForList(selectOrderHeaderSql, new Object[] { "12" }) + .size(), 1, "The order record was not sync'd when it should have been."); // TODO: make sure event did not fire } @@ -211,8 +232,8 @@ protected void testSyncInsertCondition() throws ParseException { protected void testSyncUpdateCondition() { rootJdbcTemplate.update(updateOrderHeaderStatusSql, new Object[] { "I", "1" }); getClientEngine().pull(); - Assert.assertEquals(clientJdbcTemplate.queryForList(selectOrderHeaderSql, new Object[] { "1" }).size(), 0, - "The order record was sync'd when it should not have been."); + Assert.assertEquals(clientJdbcTemplate.queryForList(selectOrderHeaderSql, new Object[] { "1" }) + .size(), 0, "The order record was sync'd when it should not have been."); rootJdbcTemplate.update(updateOrderHeaderStatusSql, new Object[] { "C", "1" }); getClientEngine().pull(); @@ -225,14 +246,16 @@ protected void testSyncUpdateCondition() { @SuppressWarnings("unchecked") protected void testIgnoreNodeChannel() { - INodeService nodeService = (INodeService) getRootEngine().getApplicationContext().getBean("nodeService"); + INodeService nodeService = (INodeService) getRootEngine().getApplicationContext().getBean( + "nodeService"); nodeService.ignoreNodeChannelForExternalId(true, TestConstants.TEST_CHANNEL_ID, TestConstants.TEST_ROOT_NODE_GROUP, TestConstants.TEST_ROOT_EXTERNAL_ID); - rootJdbcTemplate.update(insertCustomerSql, new Object[] { 201, "Charlie Dude", "1", "300 Grub Street", - "New Yorl", "NY", 90009, new Date(), "This is a test", BINARY_DATA }); + rootJdbcTemplate.update(insertCustomerSql, new Object[] { 201, "Charlie Dude", "1", + "300 Grub Street", "New Yorl", "NY", 90009, new Date(), "This is a test", BINARY_DATA }); getClientEngine().pull(); - Assert.assertEquals(clientJdbcTemplate.queryForInt("select count(*) from test_customer where customer_id=201"), - 0, "The customer was sync'd to the client."); + Assert.assertEquals(clientJdbcTemplate + .queryForInt("select count(*) from test_customer where customer_id=201"), 0, + "The customer was sync'd to the client."); nodeService.ignoreNodeChannelForExternalId(false, TestConstants.TEST_CHANNEL_ID, TestConstants.TEST_ROOT_NODE_GROUP, TestConstants.TEST_ROOT_EXTERNAL_ID); @@ -248,7 +271,7 @@ protected void testSyncUpdateWithEmptyKey() { clientJdbcTemplate.update(updateStoreStatusSql, new Object[] { 2, "00001", "" }); getClientEngine().push(); - + int status = rootJdbcTemplate.queryForInt(selectStoreStatusSql, new Object[] { "00001", " " }); Assert.assertEquals(status, 2, "Wrong store status"); } @@ -257,11 +280,10 @@ protected void testPurge() throws Exception { Thread.sleep(1000); getRootEngine().purge(); getClientEngine().purge(); - Assert.assertEquals(rootJdbcTemplate.queryForInt("select count(*) from " + TestConstants.TEST_PREFIX + "data"), - 0, "Expected all data rows to have been purged."); - Assert.assertEquals(clientJdbcTemplate - .queryForInt("select count(*) from " + TestConstants.TEST_PREFIX + "data"), 0, - "Expected all data rows to have been purged."); + Assert.assertEquals(rootJdbcTemplate.queryForInt("select count(*) from " + TestConstants.TEST_PREFIX + + "data"), 0, "Expected all data rows to have been purged."); + Assert.assertEquals(clientJdbcTemplate.queryForInt("select count(*) from " + + TestConstants.TEST_PREFIX + "data"), 0, "Expected all data rows to have been purged."); } @@ -270,13 +292,14 @@ protected void testHeartbeat() throws Exception { Thread.sleep(1000); getClientEngine().heartbeat(); getClientEngine().push(); - Date time = (Date) rootJdbcTemplate.queryForObject("select heartbeat_time from " + TestConstants.TEST_PREFIX - + "node where external_id='" + TestConstants.TEST_CLIENT_EXTERNAL_ID + "'", Timestamp.class); + Date time = (Date) rootJdbcTemplate.queryForObject("select heartbeat_time from " + + TestConstants.TEST_PREFIX + "node where external_id='" + + TestConstants.TEST_CLIENT_EXTERNAL_ID + "'", Timestamp.class); Assert.assertTrue(time != null && time.getTime() > ts, "The client node was not sync'd to the root as expected."); } - - protected void testVirtualTransactionId() { + + protected void testVirtualTransactionId() { rootJdbcTemplate.update("insert into test_very_long_table_name_1234 values('42')"); if (getRootDbDialect().isTransactionIdOverrideSupported()) { Assert @@ -297,22 +320,25 @@ protected void testVirtualTransactionId() { "The hardcoded transaction id was not found."); } } - + protected void testCaseSensitiveTableNames() { rootJdbcTemplate.update("insert into TEST_ALL_CAPS values(1, 'HELLO')"); getClientEngine().pull(); - Assert.assertEquals(clientJdbcTemplate.queryForInt("select count(*) from TEST_ALL_CAPS where ALL_CAPS_ID = 1"), - 1, "Table name in all caps was not synced"); + Assert.assertEquals(clientJdbcTemplate + .queryForInt("select count(*) from TEST_ALL_CAPS where ALL_CAPS_ID = 1"), 1, + "Table name in all caps was not synced"); rootJdbcTemplate.update("insert into Test_Mixed_Case values(1, 'Hello')"); getClientEngine().pull(); - Assert.assertEquals(clientJdbcTemplate.queryForInt("select count(*) from Test_Mixed_Case where Mixed_Case_Id = 1"), - 1, "Table name in mixed case was not synced"); + Assert.assertEquals(clientJdbcTemplate + .queryForInt("select count(*) from Test_Mixed_Case where Mixed_Case_Id = 1"), 1, + "Table name in mixed case was not synced"); + } + + protected void testDeletes() { } - - protected void testDeletes() { + + protected void testMultiRowInsert() { } - - protected void testMultiRowInsert() {} protected void testMultipleChannels() { }