Browse Source
This commit continues the work in the previous commit as follows: - Introduced an exception hierarchy for exceptions related to SQL scripts, with ScriptException as the base. - CannotReadScriptException and ScriptStatementFailedException now extend ScriptException. - Introduced ScriptParseException, used by ScriptUtils.splitSqlScript(). - DatabasePopulatorUtils.execute() now explicitly throws a DataAccessException. - Polished Javadoc in ResourceDatabasePopulator. - Overhauled Javadoc in ScriptUtils and documented all constants. - Added missing @author tags for original authors in ScriptUtils and ScriptUtilsTests. - ScriptUtils.splitSqlScript() now asserts preconditions. - Deleted superfluous methods in ScriptUtils and changed method visibility to private or package private as appropriate. - Deleted the ScriptStatementExecutor introduced in the previous commit; ScriptUtils.executeSqlScript() now accepts a JDBC Connection; JdbcTestUtils, AbstractTransactionalJUnit4SpringContextTests, and AbstractTransactionalTestNGSpringContextTests now use DatabasePopulatorUtils to execute a ResourceDatabasePopulator instead of executing a script directly via ScriptUtils. - Introduced JdbcTestUtilsIntegrationTests. Issue: SPR-9531pull/448/merge
17 changed files with 475 additions and 354 deletions
@ -0,0 +1,45 @@
@@ -0,0 +1,45 @@
|
||||
/* |
||||
* Copyright 2002-2014 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.jdbc.datasource.init; |
||||
|
||||
/** |
||||
* Root of the hierarchy of SQL script exceptions. |
||||
* |
||||
* @author Sam Brannen |
||||
* @since 4.0.3 |
||||
*/ |
||||
@SuppressWarnings("serial") |
||||
public abstract class ScriptException extends RuntimeException { |
||||
|
||||
/** |
||||
* Constructor for {@code ScriptException}. |
||||
* @param message the detail message |
||||
*/ |
||||
public ScriptException(String message) { |
||||
super(message); |
||||
} |
||||
|
||||
/** |
||||
* Constructor for {@code ScriptException}. |
||||
* @param message the detail message |
||||
* @param cause the root cause |
||||
*/ |
||||
public ScriptException(String message, Throwable cause) { |
||||
super(message, cause); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,54 @@
@@ -0,0 +1,54 @@
|
||||
/* |
||||
* Copyright 2002-2014 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.jdbc.datasource.init; |
||||
|
||||
import org.springframework.core.io.support.EncodedResource; |
||||
|
||||
/** |
||||
* Thrown by {@link ScriptUtils} if an SQL script cannot be properly parsed. |
||||
* |
||||
* @author Sam Brannen |
||||
* @since 4.0.3 |
||||
*/ |
||||
@SuppressWarnings("serial") |
||||
public class ScriptParseException extends ScriptException { |
||||
|
||||
/** |
||||
* Construct a new {@code ScriptParseException}. |
||||
* @param message detailed message |
||||
* @param resource the resource from which the SQL script was read |
||||
*/ |
||||
public ScriptParseException(String message, EncodedResource resource) { |
||||
super(buildMessage(message, resource)); |
||||
} |
||||
|
||||
/** |
||||
* Construct a new {@code ScriptParseException}. |
||||
* @param message detailed message |
||||
* @param resource the resource from which the SQL script was read |
||||
* @param cause the underlying cause of the failure |
||||
*/ |
||||
public ScriptParseException(String message, EncodedResource resource, Throwable cause) { |
||||
super(buildMessage(message, resource), cause); |
||||
} |
||||
|
||||
private static String buildMessage(String message, EncodedResource resource) { |
||||
return String.format("Failed to parse SQL script from resource [%s]: %s", (resource == null ? "<unknown>" |
||||
: resource), message); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,63 @@
@@ -0,0 +1,63 @@
|
||||
/* |
||||
* Copyright 2002-2014 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.test.jdbc; |
||||
|
||||
import java.util.Arrays; |
||||
|
||||
import org.junit.After; |
||||
import org.junit.Test; |
||||
import org.springframework.core.io.ClassPathResource; |
||||
import org.springframework.core.io.Resource; |
||||
import org.springframework.core.io.support.EncodedResource; |
||||
import org.springframework.jdbc.core.JdbcTemplate; |
||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase; |
||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; |
||||
|
||||
import static org.junit.Assert.*; |
||||
|
||||
/** |
||||
* Integration tests for {@link JdbcTestUtils}. |
||||
* |
||||
* @author Sam Brannen |
||||
* @since 4.0.3 |
||||
* @see JdbcTestUtilsTests |
||||
*/ |
||||
public class JdbcTestUtilsIntegrationTests { |
||||
|
||||
private final EmbeddedDatabase db = new EmbeddedDatabaseBuilder().build(); |
||||
|
||||
private JdbcTemplate jdbcTemplate = new JdbcTemplate(db); |
||||
|
||||
|
||||
@After |
||||
public void shutdown() { |
||||
db.shutdown(); |
||||
} |
||||
|
||||
@Test |
||||
@SuppressWarnings("deprecation") |
||||
public void executeSqlScriptsAndcountRowsInTableWhere() throws Exception { |
||||
|
||||
for (String script : Arrays.asList("schema.sql", "data.sql")) { |
||||
Resource resource = new ClassPathResource(script, getClass()); |
||||
JdbcTestUtils.executeSqlScript(this.jdbcTemplate, new EncodedResource(resource), false); |
||||
} |
||||
|
||||
assertEquals(1, JdbcTestUtils.countRowsInTableWhere(jdbcTemplate, "person", "name = 'bob'")); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1 @@
@@ -0,0 +1 @@
|
||||
INSERT INTO person VALUES('bob'); |
||||
Loading…
Reference in new issue