|
|
|
@ -16,13 +16,24 @@ |
|
|
|
|
|
|
|
|
|
|
|
package org.springframework.jdbc.datasource.init; |
|
|
|
package org.springframework.jdbc.datasource.init; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.nio.charset.StandardCharsets; |
|
|
|
import java.sql.SQLException; |
|
|
|
import java.sql.SQLException; |
|
|
|
|
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
|
|
|
|
import org.junit.jupiter.api.BeforeEach; |
|
|
|
import org.junit.jupiter.api.BeforeEach; |
|
|
|
import org.junit.jupiter.api.Test; |
|
|
|
import org.junit.jupiter.api.Test; |
|
|
|
|
|
|
|
import org.junit.jupiter.params.Parameter; |
|
|
|
|
|
|
|
import org.junit.jupiter.params.ParameterizedClass; |
|
|
|
|
|
|
|
import org.junit.jupiter.params.provider.EnumSource; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import org.springframework.core.io.ByteArrayResource; |
|
|
|
|
|
|
|
import org.springframework.core.io.Resource; |
|
|
|
|
|
|
|
import org.springframework.core.io.support.EncodedResource; |
|
|
|
|
|
|
|
import org.springframework.jdbc.core.DataClassRowMapper; |
|
|
|
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; |
|
|
|
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat; |
|
|
|
|
|
|
|
import static org.assertj.core.api.Assumptions.assumeThat; |
|
|
|
import static org.springframework.jdbc.datasource.init.ScriptUtils.executeSqlScript; |
|
|
|
import static org.springframework.jdbc.datasource.init.ScriptUtils.executeSqlScript; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
@ -32,16 +43,22 @@ import static org.springframework.jdbc.datasource.init.ScriptUtils.executeSqlScr |
|
|
|
* @since 4.0.3 |
|
|
|
* @since 4.0.3 |
|
|
|
* @see ScriptUtilsTests |
|
|
|
* @see ScriptUtilsTests |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
|
|
|
|
@ParameterizedClass |
|
|
|
|
|
|
|
@EnumSource(EmbeddedDatabaseType.class) |
|
|
|
class ScriptUtilsIntegrationTests extends AbstractDatabaseInitializationTests { |
|
|
|
class ScriptUtilsIntegrationTests extends AbstractDatabaseInitializationTests { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Parameter |
|
|
|
|
|
|
|
EmbeddedDatabaseType databaseType; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
protected EmbeddedDatabaseType getEmbeddedDatabaseType() { |
|
|
|
protected EmbeddedDatabaseType getEmbeddedDatabaseType() { |
|
|
|
return EmbeddedDatabaseType.HSQL; |
|
|
|
return this.databaseType; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@BeforeEach |
|
|
|
@BeforeEach |
|
|
|
void setUpSchema() throws SQLException { |
|
|
|
void setUpSchema() throws SQLException { |
|
|
|
executeSqlScript(db.getConnection(), usersSchema()); |
|
|
|
executeSqlScript(db.getConnection(), encodedResource(usersSchema()), false, true, "--", null, "/*", "*/"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
@ -59,4 +76,52 @@ class ScriptUtilsIntegrationTests extends AbstractDatabaseInitializationTests { |
|
|
|
assertUsersDatabaseCreated("Hoeller", "Brannen"); |
|
|
|
assertUsersDatabaseCreated("Hoeller", "Brannen"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
@SuppressWarnings("unchecked") |
|
|
|
|
|
|
|
void statementWithMultipleResultSets() throws SQLException { |
|
|
|
|
|
|
|
// Derby does not support multiple statements/ResultSets within a single Statement.
|
|
|
|
|
|
|
|
assumeThat(this.databaseType).isNotSameAs(EmbeddedDatabaseType.DERBY); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
EncodedResource resource = encodedResource(resource("users-data.sql")); |
|
|
|
|
|
|
|
executeSqlScript(db.getConnection(), resource, false, true, "--", null, "/*", "*/"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assertUsersInDatabase(user("Sam", "Brannen")); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
resource = encodedResource(inlineResource(""" |
|
|
|
|
|
|
|
SELECT last_name FROM users WHERE id = 0; |
|
|
|
|
|
|
|
UPDATE users SET first_name = 'Jane' WHERE id = 0; |
|
|
|
|
|
|
|
UPDATE users SET last_name = 'Smith' WHERE id = 0; |
|
|
|
|
|
|
|
SELECT last_name FROM users WHERE id = 0; |
|
|
|
|
|
|
|
GO |
|
|
|
|
|
|
|
""")); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String separator = "GO\n"; |
|
|
|
|
|
|
|
executeSqlScript(db.getConnection(), resource, false, true, "--", separator, "/*", "*/"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assertUsersInDatabase(user("Jane", "Smith")); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void assertUsersInDatabase(User... expectedUsers) { |
|
|
|
|
|
|
|
List<User> users = jdbcTemplate.query("SELECT * FROM users WHERE id = 0", |
|
|
|
|
|
|
|
new DataClassRowMapper<>(User.class)); |
|
|
|
|
|
|
|
assertThat(users).containsExactly(expectedUsers); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static EncodedResource encodedResource(Resource resource) { |
|
|
|
|
|
|
|
return new EncodedResource(resource); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static Resource inlineResource(String sql) { |
|
|
|
|
|
|
|
byte[] bytes = sql.getBytes(StandardCharsets.UTF_8); |
|
|
|
|
|
|
|
return new ByteArrayResource(bytes, "inline SQL"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static User user(String firstName, String lastName) { |
|
|
|
|
|
|
|
return new User(0, firstName, lastName); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
record User(int id, String firstName, String lastName) { |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|