use of org.springframework.jdbc.core.JdbcTemplate 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.JdbcTemplate 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.JdbcTemplate in project spring-boot by spring-projects.
the class JdbcTemplateAutoConfigurationTests method testJdbcTemplateExists.
@Test
public void testJdbcTemplateExists() throws Exception {
this.context.register(DataSourceAutoConfiguration.class, JdbcTemplateAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
JdbcTemplate jdbcTemplate = this.context.getBean(JdbcTemplate.class);
assertThat(jdbcTemplate).isNotNull();
assertThat(jdbcTemplate.getDataSource()).isNotNull();
}
use of org.springframework.jdbc.core.JdbcTemplate in project spring-boot by spring-projects.
the class JdbcTemplateAutoConfigurationTests method testJdbcTemplateExistsWithCustomDataSource.
@Test
public void testJdbcTemplateExistsWithCustomDataSource() throws Exception {
this.context.register(TestDataSourceConfiguration.class, DataSourceAutoConfiguration.class, JdbcTemplateAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
JdbcTemplate jdbcTemplate = this.context.getBean(JdbcTemplate.class);
assertThat(jdbcTemplate).isNotNull();
assertThat(jdbcTemplate.getDataSource() instanceof BasicDataSource).isTrue();
}
use of org.springframework.jdbc.core.JdbcTemplate in project spring-framework by spring-projects.
the class AbstractTransactionalAnnotatedConfigClassTests method setDataSource.
@Autowired
public void setDataSource(DataSource dataSource) {
this.dataSourceViaInjection = dataSource;
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
Aggregations