From 92a92b79371dbe85cfea9c6b4a923997893ade6d Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Wed, 10 Oct 2012 20:16:55 +0200 Subject: [PATCH] Minor changes along with 3.1.3 backport Issue: SPR-9781 Issue: SPR-9784 --- .../init/ResourceDatabasePopulator.java | 9 +--- .../init/DatabasePopulatorTests.java | 44 ++++++++++++------- 2 files changed, 31 insertions(+), 22 deletions(-) diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/init/ResourceDatabasePopulator.java b/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/init/ResourceDatabasePopulator.java index 974ae380f6..d0a7f3368e 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/init/ResourceDatabasePopulator.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/init/ResourceDatabasePopulator.java @@ -48,9 +48,9 @@ import org.springframework.util.StringUtils; */ public class ResourceDatabasePopulator implements DatabasePopulator { - private static String DEFAULT_COMMENT_PREFIX = "--"; + private static final String DEFAULT_COMMENT_PREFIX = "--"; - private static String DEFAULT_STATEMENT_SEPARATOR = ";"; + private static final String DEFAULT_STATEMENT_SEPARATOR = ";"; private static final Log logger = LogFactory.getLog(ResourceDatabasePopulator.class); @@ -276,27 +276,22 @@ public class ResourceDatabasePopulator implements DatabasePopulator { /** * Return whether the substring of a given source {@link String} starting at the * given index starts with the given delimiter. - * * @param source the source {@link String} to inspect * @param startIndex the index to look for the delimiter * @param delim the delimiter to look for */ private boolean startsWithDelimiter(String source, int startIndex, String delim) { - int endIndex = startIndex + delim.length(); - if (source.length() < endIndex) { // String is too short to contain the delimiter return false; } - return source.substring(startIndex, endIndex).equals(delim); } /** * Split an SQL script into separate statements delimited with the provided delimiter * character. Each individual statement will be added to the provided {@code List}. - * * @param script the SQL script * @param delim character delimiting each statement (typically a ';' character) * @param statements the List that will contain the individual statements diff --git a/spring-jdbc/src/test/java/org/springframework/jdbc/datasource/init/DatabasePopulatorTests.java b/spring-jdbc/src/test/java/org/springframework/jdbc/datasource/init/DatabasePopulatorTests.java index 705b990274..6535eeb426 100644 --- a/spring-jdbc/src/test/java/org/springframework/jdbc/datasource/init/DatabasePopulatorTests.java +++ b/spring-jdbc/src/test/java/org/springframework/jdbc/datasource/init/DatabasePopulatorTests.java @@ -41,11 +41,16 @@ import org.springframework.transaction.support.TransactionSynchronizationManager public class DatabasePopulatorTests { private final EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder(); + private final EmbeddedDatabase db = builder.build(); + private final ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator(); + private final ClassRelativeResourceLoader resourceLoader = new ClassRelativeResourceLoader(getClass()); + private final JdbcTemplate jdbcTemplate = new JdbcTemplate(db); + private void assertTestDatabaseCreated() { assertTestDatabaseCreated("Keith"); } @@ -61,15 +66,14 @@ public class DatabasePopulatorTests { @After public void shutDown() { - if (TransactionSynchronizationManager.isSynchronizationActive()) { TransactionSynchronizationManager.clear(); TransactionSynchronizationManager.unbindResource(db); } - db.shutdown(); } + @Test public void testBuildWithCommentsAndFailedDrop() throws Exception { databasePopulator.addScript(resourceLoader.getResource("db-schema-failed-drop-comments.sql")); @@ -78,7 +82,8 @@ public class DatabasePopulatorTests { Connection connection = db.getConnection(); try { databasePopulator.populate(connection); - } finally { + } + finally { connection.close(); } @@ -92,7 +97,8 @@ public class DatabasePopulatorTests { Connection connection = db.getConnection(); try { databasePopulator.populate(connection); - } finally { + } + finally { connection.close(); } @@ -106,7 +112,8 @@ public class DatabasePopulatorTests { Connection connection = db.getConnection(); try { databasePopulator.populate(connection); - } finally { + } + finally { connection.close(); } @@ -120,7 +127,8 @@ public class DatabasePopulatorTests { Connection connection = db.getConnection(); try { databasePopulator.populate(connection); - } finally { + } + finally { connection.close(); } @@ -136,7 +144,8 @@ public class DatabasePopulatorTests { Connection connection = db.getConnection(); try { databasePopulator.populate(connection); - } finally { + } + finally { connection.close(); } @@ -152,7 +161,8 @@ public class DatabasePopulatorTests { Connection connection = db.getConnection(); try { databasePopulator.populate(connection); - } finally { + } + finally { connection.close(); } @@ -167,7 +177,8 @@ public class DatabasePopulatorTests { Connection connection = db.getConnection(); try { databasePopulator.populate(connection); - } finally { + } + finally { connection.close(); } @@ -183,7 +194,8 @@ public class DatabasePopulatorTests { Connection connection = db.getConnection(); try { databasePopulator.populate(connection); - } finally { + } + finally { connection.close(); } @@ -198,7 +210,8 @@ public class DatabasePopulatorTests { Connection connection = db.getConnection(); try { databasePopulator.populate(connection); - } finally { + } + finally { connection.close(); } @@ -212,7 +225,8 @@ public class DatabasePopulatorTests { Connection connection = db.getConnection(); try { databasePopulator.populate(connection); - } finally { + } + finally { connection.close(); } @@ -225,7 +239,6 @@ public class DatabasePopulatorTests { */ @Test public void usesBoundConnectionIfAvailable() throws SQLException { - TransactionSynchronizationManager.initSynchronization(); Connection connection = DataSourceUtils.getConnection(db); @@ -244,15 +257,16 @@ public class DatabasePopulatorTests { */ @Test(timeout = 1000) public void executesHugeScriptInReasonableTime() throws SQLException { - databasePopulator.addScript(resourceLoader.getResource("db-schema.sql")); databasePopulator.addScript(resourceLoader.getResource("db-test-data-huge.sql")); Connection connection = db.getConnection(); try { databasePopulator.populate(connection); - } finally { + } + finally { connection.close(); } } + }