Search in sources :

Example 46 with JdbcTemplate

use of org.springframework.jdbc.core.JdbcTemplate in project spring-boot by spring-projects.

the class HibernateJpaAutoConfigurationTests method testDataScript.

// This can't succeed because the data SQL is executed immediately after the schema
// and Hibernate hasn't initialized yet at that point
@Test(expected = BeanCreationException.class)
public void testDataScript() throws Exception {
    EnvironmentTestUtils.addEnvironment(this.context, "spring.datasource.data:classpath:/city.sql");
    setupTestConfiguration();
    this.context.refresh();
    assertThat(new JdbcTemplate(this.context.getBean(DataSource.class)).queryForObject("SELECT COUNT(*) from CITY", Integer.class)).isEqualTo(1);
}
Also used : JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) Test(org.junit.Test)

Example 47 with JdbcTemplate

use of org.springframework.jdbc.core.JdbcTemplate in project spring-framework by spring-projects.

the class InitializeDatabaseIntegrationTests method testScriptNameWithPattern.

@Test
public void testScriptNameWithPattern() throws Exception {
    context = new ClassPathXmlApplicationContext("org/springframework/jdbc/config/jdbc-initialize-pattern-config.xml");
    DataSource dataSource = context.getBean("dataSource", DataSource.class);
    assertCorrectSetup(dataSource);
    JdbcTemplate t = new JdbcTemplate(dataSource);
    assertEquals("Dave", t.queryForObject("select name from T_TEST", String.class));
}
Also used : ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) DataSource(javax.sql.DataSource) Test(org.junit.Test)

Example 48 with JdbcTemplate

use of org.springframework.jdbc.core.JdbcTemplate in project spring-framework by spring-projects.

the class InitializeDatabaseIntegrationTests method assertCorrectSetup.

private void assertCorrectSetup(DataSource dataSource) {
    JdbcTemplate jt = new JdbcTemplate(dataSource);
    assertEquals(1, jt.queryForObject("select count(*) from T_TEST", Integer.class).intValue());
}
Also used : JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate)

Example 49 with JdbcTemplate

use of org.springframework.jdbc.core.JdbcTemplate in project spring-framework by spring-projects.

the class JdbcNamespaceIntegrationTests method createAndDestroy.

@Test
public void createAndDestroy() throws Exception {
    ClassPathXmlApplicationContext context = context("jdbc-destroy-config.xml");
    try {
        DataSource dataSource = context.getBean(DataSource.class);
        JdbcTemplate template = new JdbcTemplate(dataSource);
        assertNumRowsInTestTable(template, 1);
        context.getBean(DataSourceInitializer.class).destroy();
        // Table has been dropped
        expected.expect(BadSqlGrammarException.class);
        assertNumRowsInTestTable(template, 1);
    } finally {
        context.close();
    }
}
Also used : ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) DataSourceInitializer(org.springframework.jdbc.datasource.init.DataSourceInitializer) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) AbstractDriverBasedDataSource(org.springframework.jdbc.datasource.AbstractDriverBasedDataSource) DataSource(javax.sql.DataSource) Test(org.junit.Test)

Example 50 with JdbcTemplate

use of org.springframework.jdbc.core.JdbcTemplate in project spring-framework by spring-projects.

the class JdbcNamespaceIntegrationTests method createAndDestroyNestedWithH2.

@Test
public void createAndDestroyNestedWithH2() throws Exception {
    ClassPathXmlApplicationContext context = context("jdbc-destroy-nested-config-h2.xml");
    try {
        DataSource dataSource = context.getBean(DataSource.class);
        JdbcTemplate template = new JdbcTemplate(dataSource);
        assertNumRowsInTestTable(template, 1);
        context.getBean(EmbeddedDatabaseFactoryBean.class).destroy();
        // Table has been dropped
        expected.expect(BadSqlGrammarException.class);
        assertNumRowsInTestTable(template, 1);
    } finally {
        context.close();
    }
}
Also used : ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) EmbeddedDatabaseFactoryBean(org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseFactoryBean) AbstractDriverBasedDataSource(org.springframework.jdbc.datasource.AbstractDriverBasedDataSource) DataSource(javax.sql.DataSource) Test(org.junit.Test)

Aggregations

JdbcTemplate (org.springframework.jdbc.core.JdbcTemplate)124 Test (org.junit.Test)46 DataSource (javax.sql.DataSource)37 Before (org.junit.Before)19 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