use of org.springframework.jdbc.core.JdbcOperations in project spring-boot by spring-projects.
the class DataSourceInitializerTests method multipleScriptsAppliedInLexicalOrder.
@Test
public void multipleScriptsAppliedInLexicalOrder() throws Exception {
EnvironmentTestUtils.addEnvironment(this.context, "spring.datasource.initialize:true", "spring.datasource.schema:" + ClassUtils.addResourcePathToPackagePath(getClass(), "lexical-schema-*.sql"), "spring.datasource.data:" + ClassUtils.addResourcePathToPackagePath(getClass(), "data.sql"));
this.context.register(DataSourceAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class);
ReverseOrderResourceLoader resourceLoader = new ReverseOrderResourceLoader(new DefaultResourceLoader());
this.context.setResourceLoader(resourceLoader);
this.context.refresh();
DataSource dataSource = this.context.getBean(DataSource.class);
assertThat(dataSource instanceof org.apache.tomcat.jdbc.pool.DataSource).isTrue();
assertThat(dataSource).isNotNull();
JdbcOperations template = new JdbcTemplate(dataSource);
assertThat(template.queryForObject("SELECT COUNT(*) from FOO", Integer.class)).isEqualTo(1);
}
use of org.springframework.jdbc.core.JdbcOperations in project spring-boot by spring-projects.
the class DataSourceInitializerTests method testDataSourceInitializedWithMultipleScripts.
@Test
public void testDataSourceInitializedWithMultipleScripts() throws Exception {
EnvironmentTestUtils.addEnvironment(this.context, "spring.datasource.initialize:true", "spring.datasource.schema:" + ClassUtils.addResourcePathToPackagePath(getClass(), "schema.sql") + "," + ClassUtils.addResourcePathToPackagePath(getClass(), "another.sql"), "spring.datasource.data:" + ClassUtils.addResourcePathToPackagePath(getClass(), "data.sql"));
this.context.register(DataSourceAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
DataSource dataSource = this.context.getBean(DataSource.class);
assertThat(dataSource instanceof org.apache.tomcat.jdbc.pool.DataSource).isTrue();
assertThat(dataSource).isNotNull();
JdbcOperations template = new JdbcTemplate(dataSource);
assertThat(template.queryForObject("SELECT COUNT(*) from FOO", Integer.class)).isEqualTo(1);
assertThat(template.queryForObject("SELECT COUNT(*) from SPAM", Integer.class)).isEqualTo(0);
}
use of org.springframework.jdbc.core.JdbcOperations in project spring-boot by spring-projects.
the class DataSourceInitializerTests method testInitializationDisabled.
@Test
public void testInitializationDisabled() throws Exception {
this.context.register(DataSourceAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
DataSource dataSource = this.context.getBean(DataSource.class);
this.context.publishEvent(new DataSourceInitializedEvent(dataSource));
assertThat(dataSource instanceof org.apache.tomcat.jdbc.pool.DataSource).isTrue();
assertThat(dataSource).isNotNull();
JdbcOperations template = new JdbcTemplate(dataSource);
try {
template.queryForObject("SELECT COUNT(*) from BAR", Integer.class);
fail("Query should have failed as BAR table does not exist");
} catch (BadSqlGrammarException ex) {
SQLException sqlException = ex.getSQLException();
// user lacks privilege or object not found
int expectedCode = -5501;
assertThat(sqlException.getErrorCode()).isEqualTo(expectedCode);
}
}
use of org.springframework.jdbc.core.JdbcOperations in project spring-boot by spring-projects.
the class DataSourceInitializerTests method testDataSourceInitializedWithExplicitSqlScriptEncoding.
@Test
public void testDataSourceInitializedWithExplicitSqlScriptEncoding() throws Exception {
this.context.register(DataSourceAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class);
EnvironmentTestUtils.addEnvironment(this.context, "spring.datasource.initialize:true", "spring.datasource.sqlScriptEncoding:UTF-8", "spring.datasource.schema:" + ClassUtils.addResourcePathToPackagePath(getClass(), "encoding-schema.sql"), "spring.datasource.data:" + ClassUtils.addResourcePathToPackagePath(getClass(), "encoding-data.sql"));
this.context.refresh();
DataSource dataSource = this.context.getBean(DataSource.class);
assertThat(dataSource instanceof org.apache.tomcat.jdbc.pool.DataSource).isTrue();
assertThat(dataSource).isNotNull();
JdbcOperations template = new JdbcTemplate(dataSource);
assertThat(template.queryForObject("SELECT COUNT(*) from BAR", Integer.class)).isEqualTo(2);
assertThat(template.queryForObject("SELECT name from BAR WHERE id=1", String.class)).isEqualTo("bar");
assertThat(template.queryForObject("SELECT name from BAR WHERE id=2", String.class)).isEqualTo("ばー");
}
use of org.springframework.jdbc.core.JdbcOperations in project spring-boot by spring-projects.
the class DataSourceInitializerTests method testDataSourceInitialized.
@Test
public void testDataSourceInitialized() throws Exception {
EnvironmentTestUtils.addEnvironment(this.context, "spring.datasource.initialize:true");
this.context.register(DataSourceAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
DataSource dataSource = this.context.getBean(DataSource.class);
assertThat(dataSource instanceof org.apache.tomcat.jdbc.pool.DataSource).isTrue();
assertThat(dataSource).isNotNull();
JdbcOperations template = new JdbcTemplate(dataSource);
assertThat(template.queryForObject("SELECT COUNT(*) from BAR", Integer.class)).isEqualTo(1);
}
Aggregations