Skip to content

Commit

Permalink
First round of unit test refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
chenson42 committed Dec 14, 2007
1 parent 321c3f0 commit faa589a
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 129 deletions.
Expand Up @@ -105,7 +105,7 @@ protected boolean writeConfiguration(Node node, OutputStream out)
if (trigger != null) {
OutgoingBatch batch = dataExtractorService.extractInitialLoadFor(node,
trigger, transport);
// acknowledge right away, because the acknowledgement is not build into the registration
// acknowledge right away, because the acknowledgment is not build into the registration
// protocol.
acknowledgeService.ack(batch.getBatchInfoList());
}
Expand Down
@@ -0,0 +1,85 @@
package org.jumpmind.symmetric;

import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;

import javax.sql.DataSource;

import org.apache.ddlutils.Platform;
import org.apache.ddlutils.io.DatabaseIO;
import org.apache.ddlutils.model.Database;
import org.jumpmind.symmetric.common.Constants;
import org.jumpmind.symmetric.common.TestConstants;
import org.jumpmind.symmetric.db.IDbDialect;
import org.jumpmind.symmetric.db.SqlScript;
import org.jumpmind.symmetric.db.oracle.OracleDbDialect;
import org.jumpmind.symmetric.service.IBootstrapService;

abstract public class AbstractIntegrationTest {

private SymmetricEngine clientEngine;

private SymmetricEngine rootEngine;

abstract File getClientFile();

abstract File getRootFile();

protected SymmetricEngine getClientEngine() {
if (this.clientEngine == null) {
this.clientEngine = createEngine(getClientFile());
dropAndCreateDatabaseTables(clientEngine);
}
return this.clientEngine;
}

protected SymmetricEngine getRootEngine() {
if (this.rootEngine == null) {
this.rootEngine = createEngine(getRootFile());
dropAndCreateDatabaseTables(rootEngine);
((IBootstrapService) this.rootEngine.getApplicationContext().getBean(Constants.BOOTSTRAP_SERVICE)).init();
new SqlScript(getResource(TestConstants.TEST_ROOT_DOMAIN_SETUP_SCRIPT), (DataSource) this.rootEngine
.getApplicationContext().getBean(Constants.DATA_SOURCE), true).execute();
this.rootEngine.start();
}
return this.rootEngine;
}

private SymmetricEngine createEngine(File propertiesFile) {
return new SymmetricEngine("file:" + propertiesFile.getAbsolutePath(), null);
}

private void dropAndCreateDatabaseTables(SymmetricEngine engine) {
DataSource ds = (DataSource) engine.getApplicationContext().getBean(Constants.DATA_SOURCE);
try {
IDbDialect dialect = (IDbDialect) engine.getApplicationContext().getBean(Constants.DB_DIALECT);
Platform platform = dialect.getPlatform();
Database testDb = getTestDatabase();
platform.dropTables(testDb, true);
dialect.purge();

new SqlScript(getResource(TestConstants.TEST_DROP_ALL_SCRIPT), ds, false).execute();

// might want to come up with a slicker way to do db specific drop/create
if (dialect instanceof OracleDbDialect) {
new SqlScript(getResource(TestConstants.TEST_DROP_SEQ_SCRIPT), ds, false).execute();
}

platform.createTables(testDb, false, true);

} catch (Exception e) {
throw new RuntimeException(e);
}
}

private Database getTestDatabase() throws IOException {
return new DatabaseIO().read(new InputStreamReader(getResource("/test-tables-ddl.xml").openStream()));
}

private URL getResource(String resource) {
return SymmetricEngineTestFactory.class.getResource(resource);
}

}
23 changes: 0 additions & 23 deletions symmetric/src/test/java/org/jumpmind/symmetric/DatabaseTest.java

This file was deleted.

7 changes: 0 additions & 7 deletions symmetric/src/test/java/org/jumpmind/symmetric/ITestable.java

This file was deleted.

119 changes: 48 additions & 71 deletions symmetric/src/test/java/org/jumpmind/symmetric/IntegrationTest.java
@@ -1,24 +1,3 @@
/*
* SymmetricDS is an open source database synchronization solution.
*
* Copyright (C) Chris Henson <chenson42@users.sourceforge.net>,
* Eric Long <erilong@users.sourceforge.net>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see
* <http://www.gnu.org/licenses/>.
*/

package org.jumpmind.symmetric;

import java.sql.Timestamp;
Expand All @@ -43,11 +22,7 @@
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class IntegrationTest {

SymmetricEngine rootEngine;

SymmetricEngine clientEngine;
abstract public class IntegrationTest extends AbstractIntegrationTest {

JdbcTemplate rootJdbcTemplate;

Expand All @@ -69,33 +44,34 @@ public class IntegrationTest {
static final String updateTestTriggerTableSql = "update test_triggers_table set string_one_value=?";

static final byte[] BINARY_DATA = new byte[] { 0x01, 0x02, 0x03 };

@BeforeTest(groups = "integration")
@BeforeTest()
public void init() {
SymmetricEngineTestFactory.resetSchemasAndEngines();

// This will start() the root engine.
rootEngine = SymmetricEngineTestFactory.getMySqlTestEngine1(TestConstants.TEST_ROOT_DOMAIN_SETUP_SCRIPT);
BeanFactory rootBeanFactory = rootEngine.getApplicationContext();
BeanFactory rootBeanFactory = getRootEngine().getApplicationContext();
rootJdbcTemplate = new JdbcTemplate((DataSource) rootBeanFactory.getBean(Constants.DATA_SOURCE));
rootEngine.openRegistration(TestConstants.TEST_CLIENT_NODE_GROUP, TestConstants.TEST_CLIENT_EXTERNAL_ID);

clientEngine = SymmetricEngineTestFactory.getMySqlTestEngine2(null);
BeanFactory clientBeanFactory = clientEngine.getApplicationContext();
BeanFactory clientBeanFactory = getClientEngine().getApplicationContext();
clientJdbcTemplate = new JdbcTemplate((DataSource) clientBeanFactory.getBean(Constants.DATA_SOURCE));
Assert.assertTrue(clientEngine.isRegistered(), "The client did not register.");

}

@Test
public void testRegistration() {
getRootEngine().openRegistration(TestConstants.TEST_CLIENT_NODE_GROUP, TestConstants.TEST_CLIENT_EXTERNAL_ID);
getClientEngine().start();
Assert.assertTrue(getClientEngine().isRegistered(), "The client did not register.");
}

@Test(groups = "integration")
@Test(dependsOnMethods="testRegistration")
public void testSyncToClient() {
// test pulling no data
clientEngine.pull();
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 });

clientEngine.pull();
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.queryForObject("select notes from test_customer where customer_id=101",
Expand All @@ -107,37 +83,37 @@ public void testSyncToClient() {

}

@Test(groups = "integration")
@Test
public void testRejectedRegistration() {

}

@Test(groups = "integration", dependsOnMethods = "testSyncToClient")
@Test(dependsOnMethods = "testSyncToClient")
public void testSyncToRootAutoGeneratedPrimaryKey() {
final String NEW_VALUE = "unique new value one value";
clientJdbcTemplate.update(insertTestTriggerTableSql, new Object[] { "value one", "value \" two" });
clientEngine.push();
getClientEngine().push();
clientJdbcTemplate.update(updateTestTriggerTableSql, new Object[] { NEW_VALUE });
clientEngine.push();
getClientEngine().push();
Assert.assertEquals(rootJdbcTemplate.queryForInt("select count(*) from test_triggers_table where string_one_value=?", new Object[] {NEW_VALUE}), 1, "The update on test_triggers_table did not work.");
}

@Test(groups = "integration", dependsOnMethods = "testSyncToClient")
@Test(dependsOnMethods = "testSyncToClient")
public 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 });
clientJdbcTemplate.update(insertOrderDetailSql, new Object[] { "10", 1, "STK", "110000065", 3, 3.33 });
clientEngine.push();
getClientEngine().push();
}

@Test(groups = "integration")
@Test
public void testSyncInsertCondition() throws ParseException {
// Should not sync when status = null
Date date = DateUtils.parseDate("2007-01-02", new String[] { "yyyy-MM-dd" });
rootJdbcTemplate.update(insertOrderHeaderSql, new Object[] { "11", 100, null, date });
clientEngine.pull();
getClientEngine().pull();

IOutgoingBatchService outgoingBatchService = (IOutgoingBatchService) rootEngine.getApplicationContext()
IOutgoingBatchService outgoingBatchService = (IOutgoingBatchService) getRootEngine().getApplicationContext()
.getBean(Constants.OUTGOING_BATCH_SERVICE);
List<OutgoingBatch> batches = outgoingBatchService.getOutgoingBatches(TestConstants.TEST_CLIENT_EXTERNAL_ID);
Assert.assertEquals(batches.size(), 0, "There should be no outgoing batches, yet I found some.");
Expand All @@ -147,22 +123,22 @@ public void testSyncInsertCondition() throws ParseException {

// Should sync when status = C
rootJdbcTemplate.update(insertOrderHeaderSql, new Object[] { "12", 100, "C", date });
clientEngine.pull();
getClientEngine().pull();
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
}

@SuppressWarnings("unchecked")
@Test(groups = "integration")
@Test
public void testSyncUpdateCondition() {
rootJdbcTemplate.update(updateOrderHeaderStatusSql, new Object[] { null, "1" });
clientEngine.pull();
getClientEngine().pull();
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" });
clientEngine.pull();
getClientEngine().pull();
List list = clientJdbcTemplate.queryForList(selectOrderHeaderSql, new Object[] { "1" });
Assert.assertEquals(list.size(), 1, "The order record should exist.");
Map map = (Map) list.get(0);
Expand All @@ -171,27 +147,27 @@ public void testSyncUpdateCondition() {
}

@SuppressWarnings("unchecked")
@Test(groups = "integration", dependsOnMethods = "testSyncUpdateCondition")
@Test(dependsOnMethods = "testSyncUpdateCondition")
public void testIgnoreNodeChannel() {
INodeService nodeService = (INodeService) rootEngine.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 });
clientEngine.pull();
getClientEngine().pull();
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);

}

@Test(groups = "integration", dependsOnMethods = { "testSyncUpdateCondition", "testSyncInsertCondition",
@Test(dependsOnMethods = { "testSyncUpdateCondition", "testSyncInsertCondition",
"testSyncToRoot", "testSyncToClient" })
public void testPurge() throws Exception {
Thread.sleep(1000);
rootEngine.purge();
clientEngine.purge();
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
Expand All @@ -200,53 +176,54 @@ public void testPurge() throws Exception {

}

@Test(groups = "integration")
@Test
public void testHeartbeat() throws Exception {
long ts = System.currentTimeMillis();
Thread.sleep(1000);
clientEngine.heartbeat();
clientEngine.push();
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);
Assert.assertTrue(time != null && time.getTime() > ts,
"The client node was not sync'd to the root as expected.");
}

@Test(groups = "integration")
@Test
public void testMultipleChannels() {
}

@Test(groups = "integration")
@Test
public void testChannelInError() {
}

@Test(groups = "integration")
@Test
public void testTableSyncConfigChangeForRoot() {
}

@Test(groups = "integration")
@Test
public void testTableSyncConfigChangeForClient() {
}

@Test(groups = "integration")
@Test
public void testDataChangeTableChangeDataChangeThenSync() {
}

@Test(groups = "integration")
@Test
public void testTransactionalCommit() {
}

@Test(groups = "integration")
@Test
public void testTransactionalCommitPastBatchBoundary() {
}

@Test(groups = "integration")
@Test
public void testSyncingGlobalParametersFromRoot() {

}

@AfterClass(groups = "integration")
@AfterClass()
public void tearDown() {
SymmetricEngineTestFactory.resetSchemasAndEngines();
}

}

0 comments on commit faa589a

Please sign in to comment.