Search in sources :

Example 21 with JdbcTemplate

use of org.springframework.jdbc.core.JdbcTemplate 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);
}
Also used : JdbcOperations(org.springframework.jdbc.core.JdbcOperations) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) DefaultResourceLoader(org.springframework.core.io.DefaultResourceLoader) DataSource(javax.sql.DataSource) Test(org.junit.Test)

Example 22 with JdbcTemplate

use of org.springframework.jdbc.core.JdbcTemplate 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);
}
Also used : JdbcOperations(org.springframework.jdbc.core.JdbcOperations) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) DataSource(javax.sql.DataSource) Test(org.junit.Test)

Example 23 with JdbcTemplate

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);
    }
}
Also used : BadSqlGrammarException(org.springframework.jdbc.BadSqlGrammarException) SQLException(java.sql.SQLException) JdbcOperations(org.springframework.jdbc.core.JdbcOperations) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) DataSource(javax.sql.DataSource) Test(org.junit.Test)

Example 24 with JdbcTemplate

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("ばー");
}
Also used : JdbcOperations(org.springframework.jdbc.core.JdbcOperations) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) DataSource(javax.sql.DataSource) Test(org.junit.Test)

Example 25 with JdbcTemplate

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();
}
Also used : NamedParameterJdbcTemplate(org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) Test(org.junit.Test)

Aggregations

JdbcTemplate (org.springframework.jdbc.core.JdbcTemplate)121 Test (org.junit.Test)45 DataSource (javax.sql.DataSource)36 Before (org.junit.Before)18 SQLException (java.sql.SQLException)11 EmbeddedDatabaseBuilder (org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder)11 BaseDbTest (com.alibaba.otter.node.etl.BaseDbTest)6 DbDialect (com.alibaba.otter.node.etl.common.db.dialect.DbDialect)6 DbMediaSource (com.alibaba.otter.shared.common.model.config.data.db.DbMediaSource)6 Connection (java.sql.Connection)6 JdbcOperations (org.springframework.jdbc.core.JdbcOperations)6 TransactionStatus (org.springframework.transaction.TransactionStatus)6 Test (org.testng.annotations.Test)6 SqlTemplate (com.alibaba.otter.node.etl.common.db.dialect.SqlTemplate)5 PreparedStatement (java.sql.PreparedStatement)5 Table (org.apache.ddlutils.model.Table)5 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)5 DataAccessException (org.springframework.dao.DataAccessException)5 AbstractDriverBasedDataSource (org.springframework.jdbc.datasource.AbstractDriverBasedDataSource)5 TransactionCallback (org.springframework.transaction.support.TransactionCallback)5