From f00a93e28d69a6c8471d599b55f9d53930921a62 Mon Sep 17 00:00:00 2001 From: brinn <brinn> Date: Wed, 20 May 2009 14:32:19 +0000 Subject: [PATCH] add: support for Java migration steps who need the DatabaseConfigurationContext fix: wrong test expectations SVN: 11095 --- .../cisd/dbmigration/h2/H2DAOFactory.java | 4 +- .../java/MigrationStepExecutor.java | 31 ++++++---- .../postgresql/PostgreSQLDAOFactory.java | 4 +- .../dbmigration/DBMigrationEngineTest.java | 57 ++++++++++++++++++- .../java/MigrationStepExecutorTest.java | 24 ++++---- .../java/MigrationStepFrom002To003.java | 9 ++- 6 files changed, 97 insertions(+), 32 deletions(-) diff --git a/dbmigration/source/java/ch/systemsx/cisd/dbmigration/h2/H2DAOFactory.java b/dbmigration/source/java/ch/systemsx/cisd/dbmigration/h2/H2DAOFactory.java index 546b2282b88..5b8f95da3c5 100644 --- a/dbmigration/source/java/ch/systemsx/cisd/dbmigration/h2/H2DAOFactory.java +++ b/dbmigration/source/java/ch/systemsx/cisd/dbmigration/h2/H2DAOFactory.java @@ -58,8 +58,8 @@ public class H2DAOFactory implements IDAOFactory { final DataSource dataSource = context.getDataSource(); sqlScriptExecutor = new SqlScriptExecutor(dataSource, context.isScriptSingleStepMode()); - migrationStepExecutor = new MigrationStepExecutor(dataSource, false); - migrationStepExecutorAdmin = new MigrationStepExecutor(context.getAdminDataSource(), true); + migrationStepExecutor = new MigrationStepExecutor(context, false); + migrationStepExecutorAdmin = new MigrationStepExecutor(context, true); databaseVersionLogDAO = new DatabaseVersionLogDAO(dataSource, context.getLobHandler()); try { diff --git a/dbmigration/source/java/ch/systemsx/cisd/dbmigration/java/MigrationStepExecutor.java b/dbmigration/source/java/ch/systemsx/cisd/dbmigration/java/MigrationStepExecutor.java index eac57a47cf1..9625198bc76 100644 --- a/dbmigration/source/java/ch/systemsx/cisd/dbmigration/java/MigrationStepExecutor.java +++ b/dbmigration/source/java/ch/systemsx/cisd/dbmigration/java/MigrationStepExecutor.java @@ -16,8 +16,6 @@ package ch.systemsx.cisd.dbmigration.java; -import javax.sql.DataSource; - import org.apache.commons.lang.StringUtils; import org.apache.log4j.Logger; import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport; @@ -30,6 +28,7 @@ import ch.systemsx.cisd.common.parser.Line; import ch.systemsx.cisd.common.parser.ParserUtilities; import ch.systemsx.cisd.common.parser.filter.NonEmptyLineFilter; import ch.systemsx.cisd.common.utilities.ClassUtils; +import ch.systemsx.cisd.dbmigration.DatabaseConfigurationContext; /** * Allows to extract {@link IMigrationStep} class from migration script and run the <i>pre</i>- and @@ -53,18 +52,22 @@ public class MigrationStepExecutor extends SimpleJdbcDaoSupport implements IMigr private final boolean isAdmin; + private final DatabaseConfigurationContext dbConfigurationContext; + private IMigrationStep migrationStep; private boolean inited; - public MigrationStepExecutor(final DataSource dataSource, boolean isAdmin) + public MigrationStepExecutor(final DatabaseConfigurationContext dbConfigurationContext, + boolean isAdmin) { - setDataSource(dataSource); + this.dbConfigurationContext = dbConfigurationContext; this.isAdmin = isAdmin; + setDataSource(isAdmin ? dbConfigurationContext.getAdminDataSource() + : dbConfigurationContext.getDataSource()); } - private final static IMigrationStep tryExtractMigrationStep(final Script sqlScript, - final boolean isAdmin) + private final IMigrationStep tryExtractMigrationStep(final Script sqlScript) { assert sqlScript != null : "SQL script not provided"; @@ -77,13 +80,12 @@ public class MigrationStepExecutor extends SimpleJdbcDaoSupport implements IMigr ParserUtilities.tryGetFirstAcceptedLine(content, NonEmptyLineFilter.INSTANCE); if (firstNonEmptyLineOrNull != null) { - return tryExtractMigrationStepFromLine(firstNonEmptyLineOrNull.getText(), isAdmin); + return tryExtractMigrationStepFromLine(firstNonEmptyLineOrNull.getText()); } return null; } - private final static IMigrationStep tryExtractMigrationStepFromLine(final String lineToProcess, - final boolean isAdmin) + private final IMigrationStep tryExtractMigrationStepFromLine(final String lineToProcess) { final String line = StringUtils.deleteWhitespace(lineToProcess); final String prefix = @@ -93,7 +95,14 @@ public class MigrationStepExecutor extends SimpleJdbcDaoSupport implements IMigr final String className = StringUtils.removeStart(line, prefix); try { - return (IMigrationStep) ClassUtils.createInstance(Class.forName(className)); + if (ClassUtils.hasConstructor(Class.forName(className), dbConfigurationContext)) + { + return ClassUtils.create(IMigrationStep.class, Class.forName(className), + dbConfigurationContext); + } else + { + return (IMigrationStep) ClassUtils.createInstance(Class.forName(className)); + } } catch (final ClassNotFoundException ex) { throw CheckedExceptionTunnel.wrapIfNecessary(ex); @@ -110,7 +119,7 @@ public class MigrationStepExecutor extends SimpleJdbcDaoSupport implements IMigr public final void init(final Script migrationScript) { - migrationStep = tryExtractMigrationStep(migrationScript, isAdmin); + migrationStep = tryExtractMigrationStep(migrationScript); if (migrationStep != null) { operationLog.info(String.format( diff --git a/dbmigration/source/java/ch/systemsx/cisd/dbmigration/postgresql/PostgreSQLDAOFactory.java b/dbmigration/source/java/ch/systemsx/cisd/dbmigration/postgresql/PostgreSQLDAOFactory.java index 337a31b369f..71597100f9a 100644 --- a/dbmigration/source/java/ch/systemsx/cisd/dbmigration/postgresql/PostgreSQLDAOFactory.java +++ b/dbmigration/source/java/ch/systemsx/cisd/dbmigration/postgresql/PostgreSQLDAOFactory.java @@ -59,8 +59,8 @@ public class PostgreSQLDAOFactory implements IDAOFactory { final DataSource dataSource = context.getDataSource(); sqlScriptExecutor = new SqlScriptExecutor(dataSource, context.isScriptSingleStepMode()); - migrationStepExecutor = new MigrationStepExecutor(dataSource, false); - migrationStepExecutorAdmin = new MigrationStepExecutor(context.getAdminDataSource(), true); + migrationStepExecutor = new MigrationStepExecutor(context, false); + migrationStepExecutorAdmin = new MigrationStepExecutor(context, true); databaseVersionLogDAO = new DatabaseVersionLogDAO(dataSource, context.getLobHandler()); try { diff --git a/dbmigration/sourceTest/java/ch/systemsx/cisd/dbmigration/DBMigrationEngineTest.java b/dbmigration/sourceTest/java/ch/systemsx/cisd/dbmigration/DBMigrationEngineTest.java index 7258ce24800..15e482d83b4 100644 --- a/dbmigration/sourceTest/java/ch/systemsx/cisd/dbmigration/DBMigrationEngineTest.java +++ b/dbmigration/sourceTest/java/ch/systemsx/cisd/dbmigration/DBMigrationEngineTest.java @@ -66,11 +66,15 @@ public class DBMigrationEngineTest final boolean honorSingleStepMode) { will(returnValue(script)); + one(migrationStepExecutorAdmin).init(script); one(migrationStepExecutor).init(script); + one(migrationStepExecutorAdmin).performPreMigration(); one(migrationStepExecutor).performPreMigration(); one(scriptExecutor).execute(script, honorSingleStepMode, logDAO); one(migrationStepExecutor).performPostMigration(); + one(migrationStepExecutorAdmin).performPostMigration(); one(migrationStepExecutor).finish(); + one(migrationStepExecutorAdmin).finish(); } } @@ -88,6 +92,8 @@ public class DBMigrationEngineTest private IMigrationStepExecutor migrationStepExecutor; + private IMigrationStepExecutor migrationStepExecutorAdmin; + private BufferedAppender logRecorder; @BeforeMethod @@ -99,7 +105,8 @@ public class DBMigrationEngineTest adminDAO = context.mock(IDatabaseAdminDAO.class); logDAO = context.mock(IDatabaseVersionLogDAO.class); scriptExecutor = context.mock(ISqlScriptExecutor.class); - migrationStepExecutor = context.mock(IMigrationStepExecutor.class); + migrationStepExecutor = context.mock(IMigrationStepExecutor.class, "migrationStepExecutor"); + migrationStepExecutorAdmin = context.mock(IMigrationStepExecutor.class, "migrationStepExecutorAdmin"); logRecorder = new BufferedAppender("%-5p %c - %m%n", Level.DEBUG); } @@ -136,6 +143,8 @@ public class DBMigrationEngineTest will(returnValue(scriptExecutor)); one(daoFactory).getMigrationStepExecutor(); will(returnValue(migrationStepExecutor)); + one(daoFactory).getMigrationStepExecutorAdmin(); + will(returnValue(migrationStepExecutorAdmin)); one(adminDAO).dropDatabase(); one(logDAO).canConnectToDatabase(); @@ -143,6 +152,7 @@ public class DBMigrationEngineTest one(adminDAO).getDatabaseName(); will(returnValue("my 1. database")); one(adminDAO).createOwner(); + one(adminDAO).createReadOnlyGroup(); one(scriptProvider).isDumpRestore(version); will(returnValue(false)); one(adminDAO).createDatabase(); @@ -186,6 +196,8 @@ public class DBMigrationEngineTest will(returnValue(scriptExecutor)); one(daoFactory).getMigrationStepExecutor(); will(returnValue(migrationStepExecutor)); + one(daoFactory).getMigrationStepExecutorAdmin(); + will(returnValue(migrationStepExecutorAdmin)); one(adminDAO).dropDatabase(); one(logDAO).canConnectToDatabase(); @@ -193,6 +205,7 @@ public class DBMigrationEngineTest one(adminDAO).getDatabaseName(); will(returnValue("my 1. database")); one(adminDAO).createOwner(); + one(adminDAO).createReadOnlyGroup(); one(scriptProvider).isDumpRestore(version); will(returnValue(true)); @@ -235,6 +248,8 @@ public class DBMigrationEngineTest one(daoFactory).getMigrationStepExecutor(); will(returnValue(migrationStepExecutor)); + one(daoFactory).getMigrationStepExecutorAdmin(); + will(returnValue(migrationStepExecutorAdmin)); one(adminDAO).dropDatabase(); one(logDAO).canConnectToDatabase(); @@ -277,12 +292,15 @@ public class DBMigrationEngineTest will(returnValue(scriptExecutor)); one(daoFactory).getMigrationStepExecutor(); will(returnValue(migrationStepExecutor)); + one(daoFactory).getMigrationStepExecutorAdmin(); + will(returnValue(migrationStepExecutorAdmin)); one(logDAO).canConnectToDatabase(); will(returnValue(false)); one(adminDAO).getDatabaseName(); will(returnValue("my 1. database")); one(adminDAO).createOwner(); + one(adminDAO).createReadOnlyGroup(); one(scriptProvider).isDumpRestore(version); will(returnValue(false)); one(adminDAO).createDatabase(); @@ -324,12 +342,15 @@ public class DBMigrationEngineTest one(daoFactory).getMigrationStepExecutor(); will(returnValue(migrationStepExecutor)); + one(daoFactory).getMigrationStepExecutorAdmin(); + will(returnValue(migrationStepExecutorAdmin)); one(logDAO).canConnectToDatabase(); will(returnValue(false)); one(adminDAO).getDatabaseName(); will(returnValue("my 1. database")); one(adminDAO).createOwner(); + one(adminDAO).createReadOnlyGroup(); one(scriptProvider).isDumpRestore(version); will(returnValue(false)); one(adminDAO).createDatabase(); @@ -370,14 +391,18 @@ public class DBMigrationEngineTest will(returnValue(logDAO)); one(daoFactory).getSqlScriptExecutor(); will(returnValue(scriptExecutor)); + one(daoFactory).getMigrationStepExecutor(); will(returnValue(migrationStepExecutor)); + one(daoFactory).getMigrationStepExecutorAdmin(); + will(returnValue(migrationStepExecutorAdmin)); one(logDAO).canConnectToDatabase(); will(returnValue(false)); one(adminDAO).getDatabaseName(); will(returnValue("my 1. database")); one(adminDAO).createOwner(); + one(adminDAO).createReadOnlyGroup(); one(scriptProvider).isDumpRestore(version); will(returnValue(false)); one(adminDAO).createDatabase(); @@ -418,6 +443,8 @@ public class DBMigrationEngineTest will(returnValue(scriptExecutor)); one(daoFactory).getMigrationStepExecutor(); will(returnValue(migrationStepExecutor)); + one(daoFactory).getMigrationStepExecutorAdmin(); + will(returnValue(migrationStepExecutorAdmin)); one(logDAO).canConnectToDatabase(); will(returnValue(true)); @@ -461,6 +488,8 @@ public class DBMigrationEngineTest will(returnValue(scriptExecutor)); one(daoFactory).getMigrationStepExecutor(); will(returnValue(migrationStepExecutor)); + one(daoFactory).getMigrationStepExecutorAdmin(); + will(returnValue(migrationStepExecutorAdmin)); one(logDAO).canConnectToDatabase(); will(returnValue(true)); @@ -522,6 +551,8 @@ public class DBMigrationEngineTest will(returnValue(scriptExecutor)); one(daoFactory).getMigrationStepExecutor(); will(returnValue(migrationStepExecutor)); + one(daoFactory).getMigrationStepExecutorAdmin(); + will(returnValue(migrationStepExecutorAdmin)); one(logDAO).canConnectToDatabase(); will(returnValue(true)); @@ -537,6 +568,8 @@ public class DBMigrationEngineTest will(returnValue(script)); + one(migrationStepExecutorAdmin).init(script); + one(migrationStepExecutorAdmin).performPreMigration(); one(migrationStepExecutor).init(script); one(migrationStepExecutor).performPreMigration(); one(scriptExecutor).execute(script, true, logDAO); @@ -579,6 +612,8 @@ public class DBMigrationEngineTest will(returnValue(scriptExecutor)); one(daoFactory).getMigrationStepExecutor(); will(returnValue(migrationStepExecutor)); + one(daoFactory).getMigrationStepExecutorAdmin(); + will(returnValue(migrationStepExecutorAdmin)); one(logDAO).canConnectToDatabase(); will(returnValue(true)); @@ -593,6 +628,9 @@ public class DBMigrationEngineTest final Script script = new Script("m-099-100", "code 099 100", toVersion); will(returnValue(script)); + one(migrationStepExecutorAdmin).init(script); + one(migrationStepExecutorAdmin).performPreMigration(); + one(migrationStepExecutor).init(script); one(migrationStepExecutor).performPreMigration(); will(throwException(new EmptyResultDataAccessException(1))); @@ -633,6 +671,8 @@ public class DBMigrationEngineTest will(returnValue(scriptExecutor)); one(daoFactory).getMigrationStepExecutor(); will(returnValue(migrationStepExecutor)); + one(daoFactory).getMigrationStepExecutorAdmin(); + will(returnValue(migrationStepExecutorAdmin)); one(logDAO).canConnectToDatabase(); will(returnValue(true)); @@ -686,6 +726,8 @@ public class DBMigrationEngineTest will(returnValue(scriptExecutor)); one(daoFactory).getMigrationStepExecutor(); will(returnValue(migrationStepExecutor)); + one(daoFactory).getMigrationStepExecutorAdmin(); + will(returnValue(migrationStepExecutorAdmin)); one(logDAO).canConnectToDatabase(); will(returnValue(true)); @@ -731,6 +773,8 @@ public class DBMigrationEngineTest will(returnValue(scriptExecutor)); one(daoFactory).getMigrationStepExecutor(); will(returnValue(migrationStepExecutor)); + one(daoFactory).getMigrationStepExecutorAdmin(); + will(returnValue(migrationStepExecutorAdmin)); one(adminDAO).dropDatabase(); one(logDAO).canConnectToDatabase(); @@ -776,6 +820,8 @@ public class DBMigrationEngineTest will(returnValue(scriptExecutor)); one(daoFactory).getMigrationStepExecutor(); will(returnValue(migrationStepExecutor)); + one(daoFactory).getMigrationStepExecutorAdmin(); + will(returnValue(migrationStepExecutorAdmin)); one(adminDAO).dropDatabase(); one(logDAO).canConnectToDatabase(); @@ -825,6 +871,8 @@ public class DBMigrationEngineTest will(returnValue(scriptExecutor)); one(daoFactory).getMigrationStepExecutor(); will(returnValue(migrationStepExecutor)); + one(daoFactory).getMigrationStepExecutorAdmin(); + will(returnValue(migrationStepExecutorAdmin)); one(adminDAO).dropDatabase(); one(logDAO).canConnectToDatabase(); @@ -871,6 +919,8 @@ public class DBMigrationEngineTest will(returnValue(scriptExecutor)); one(daoFactory).getMigrationStepExecutor(); will(returnValue(migrationStepExecutor)); + one(daoFactory).getMigrationStepExecutorAdmin(); + will(returnValue(migrationStepExecutorAdmin)); one(logDAO).canConnectToDatabase(); will(returnValue(true)); @@ -889,7 +939,10 @@ public class DBMigrationEngineTest one(migrationStepExecutor).performPreMigration(); one(scriptExecutor).execute(script, true, logDAO); will(throwException(exception)); - } + + one(migrationStepExecutorAdmin).init(script); + one(migrationStepExecutorAdmin).performPreMigration(); +} }); final DBMigrationEngine migrationEngine = new DBMigrationEngine(daoFactory, scriptProvider, false); diff --git a/dbmigration/sourceTest/java/ch/systemsx/cisd/dbmigration/java/MigrationStepExecutorTest.java b/dbmigration/sourceTest/java/ch/systemsx/cisd/dbmigration/java/MigrationStepExecutorTest.java index 5b8c01711b0..75d143779f9 100644 --- a/dbmigration/sourceTest/java/ch/systemsx/cisd/dbmigration/java/MigrationStepExecutorTest.java +++ b/dbmigration/sourceTest/java/ch/systemsx/cisd/dbmigration/java/MigrationStepExecutorTest.java @@ -20,10 +20,7 @@ import static org.testng.AssertJUnit.assertEquals; import static org.testng.AssertJUnit.assertFalse; import static org.testng.AssertJUnit.fail; -import javax.sql.DataSource; - import org.apache.log4j.Level; -import org.jmock.Mockery; import org.springframework.dao.DataIntegrityViolationException; import org.springframework.dao.EmptyResultDataAccessException; import org.testng.annotations.BeforeClass; @@ -34,6 +31,7 @@ import ch.systemsx.cisd.base.exceptions.CheckedExceptionTunnel; import ch.systemsx.cisd.common.Script; import ch.systemsx.cisd.common.logging.BufferedAppender; import ch.systemsx.cisd.common.logging.LogInitializer; +import ch.systemsx.cisd.dbmigration.DatabaseConfigurationContext; /** * Test cases for the {@link MigrationStepExecutor}. @@ -42,17 +40,15 @@ import ch.systemsx.cisd.common.logging.LogInitializer; */ public final class MigrationStepExecutorTest { - private Mockery context; - - private DataSource dataSource; - + private DatabaseConfigurationContext dbContext; + private BufferedAppender logRecorder; @BeforeMethod public final void setUp() { - context = new Mockery(); - dataSource = context.mock(DataSource.class); + dbContext = new DatabaseConfigurationContext(); + dbContext.setDatabaseEngineCode("postgresql"); logRecorder = new BufferedAppender("%m%n", Level.DEBUG); } @@ -66,7 +62,7 @@ public final class MigrationStepExecutorTest public final void testHappyCase() { final MigrationStepExecutor migrationStepExecutor = - new MigrationStepExecutor(dataSource, false); + new MigrationStepExecutor(dbContext, false); Script script = new Script("001To002.sql", "-- JAVA ch.systemsx.cisd.dbmigration.java.MigrationStepFrom001To002"); @@ -89,7 +85,7 @@ public final class MigrationStepExecutorTest public final void testHappyCaseAdmin() { final MigrationStepExecutor migrationStepExecutor = - new MigrationStepExecutor(dataSource, true); + new MigrationStepExecutor(dbContext, true); Script script = new Script("001To002.sql", "-- JAVA_ADMIN ch.systemsx.cisd.dbmigration.java.MigrationStepFrom001To002"); @@ -112,7 +108,7 @@ public final class MigrationStepExecutorTest public final void testFinish() { final MigrationStepExecutor migrationStepExecutor = - new MigrationStepExecutor(dataSource, false); + new MigrationStepExecutor(dbContext, false); final Script script = new Script("001To002.sql", "-- JAVA ch.systemsx.cisd.dbmigration.java.MigrationStepFrom001To002"); @@ -135,7 +131,7 @@ public final class MigrationStepExecutorTest public final void testUnhappyCase() { final MigrationStepExecutor migrationStepExecutor = - new MigrationStepExecutor(dataSource, false); + new MigrationStepExecutor(dbContext, false); Script script = new Script("002To003.sql", "\n-- This is a comment\n" + "-- JAVA ch.systemsx.cisd.dbmigration.java.MigrationStepFrom002To003"); @@ -171,7 +167,7 @@ public final class MigrationStepExecutorTest public final void testClassNotFound() { final MigrationStepExecutor migrationStepExecutor = - new MigrationStepExecutor(dataSource, false); + new MigrationStepExecutor(dbContext, false); final Script script = new Script("003To004.sql", "-- JAVA ch.systemsx.cisd.dbmigration.java.MigrationStepFrom003To003"); diff --git a/dbmigration/sourceTest/java/ch/systemsx/cisd/dbmigration/java/MigrationStepFrom002To003.java b/dbmigration/sourceTest/java/ch/systemsx/cisd/dbmigration/java/MigrationStepFrom002To003.java index 27cfe28b346..640d3eb50b2 100644 --- a/dbmigration/sourceTest/java/ch/systemsx/cisd/dbmigration/java/MigrationStepFrom002To003.java +++ b/dbmigration/sourceTest/java/ch/systemsx/cisd/dbmigration/java/MigrationStepFrom002To003.java @@ -21,6 +21,9 @@ import org.springframework.dao.DataAccessException; import org.springframework.dao.DataIntegrityViolationException; import org.springframework.dao.EmptyResultDataAccessException; import org.springframework.jdbc.core.simple.SimpleJdbcTemplate; +import org.testng.AssertJUnit; + +import ch.systemsx.cisd.dbmigration.DatabaseConfigurationContext; /** * A <code>IMigrationStep</code> implementation for test. @@ -29,7 +32,11 @@ import org.springframework.jdbc.core.simple.SimpleJdbcTemplate; */ public final class MigrationStepFrom002To003 implements IMigrationStep { - + public MigrationStepFrom002To003(DatabaseConfigurationContext context) + { + AssertJUnit.assertNotNull(context); + } + // // IMigrationStep // -- GitLab