Search in sources :

Example 16 with JdbcTemplate

use of cn.taketoday.jdbc.core.JdbcTemplate in project today-framework by TAKETODAY.

the class InitializeDatabaseIntegrationTests method testScriptNameWithPattern.

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

Example 17 with JdbcTemplate

use of cn.taketoday.jdbc.core.JdbcTemplate in project today-framework by TAKETODAY.

the class JdbcNamespaceIntegrationTests method createAndDestroyNestedWithH2.

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

Example 18 with JdbcTemplate

use of cn.taketoday.jdbc.core.JdbcTemplate in project today-framework by TAKETODAY.

the class JdbcNamespaceIntegrationTests method createAndDestroyNestedWithHsql.

@Test
void createAndDestroyNestedWithHsql() throws Exception {
    try (ClassPathXmlApplicationContext context = context("jdbc-destroy-nested-config.xml")) {
        DataSource dataSource = context.getBean(DataSource.class);
        JdbcTemplate template = new JdbcTemplate(dataSource);
        assertNumRowsInTestTable(template, 1);
        context.getBean(EmbeddedDatabaseFactoryBean.class).destroy();
        // Table has been dropped
        assertThatExceptionOfType(BadSqlGrammarException.class).isThrownBy(() -> assertNumRowsInTestTable(template, 1));
    }
}
Also used : BadSqlGrammarException(cn.taketoday.jdbc.BadSqlGrammarException) ClassPathXmlApplicationContext(cn.taketoday.context.support.ClassPathXmlApplicationContext) JdbcTemplate(cn.taketoday.jdbc.core.JdbcTemplate) EmbeddedDatabaseFactoryBean(cn.taketoday.jdbc.datasource.embedded.EmbeddedDatabaseFactoryBean) AbstractDriverBasedDataSource(cn.taketoday.jdbc.datasource.AbstractDriverBasedDataSource) DataSource(javax.sql.DataSource) Test(org.junit.jupiter.api.Test)

Example 19 with JdbcTemplate

use of cn.taketoday.jdbc.core.JdbcTemplate in project today-framework by TAKETODAY.

the class JdbcDaoSupportTests method testJdbcDaoSupportWithJdbcTemplate.

@Test
public void testJdbcDaoSupportWithJdbcTemplate() throws Exception {
    JdbcTemplate template = new JdbcTemplate();
    final List<String> test = new ArrayList<>();
    JdbcDataAccessObjectSupport dao = new JdbcDataAccessObjectSupport() {

        @Override
        protected void initDao() {
            test.add("test");
        }
    };
    dao.setJdbcTemplate(template);
    dao.afterPropertiesSet();
    assertThat(template).as("Correct JdbcTemplate").isEqualTo(dao.getJdbcTemplate());
    assertThat(test.size()).as("initDao called").isEqualTo(1);
}
Also used : ArrayList(java.util.ArrayList) JdbcTemplate(cn.taketoday.jdbc.core.JdbcTemplate) Test(org.junit.jupiter.api.Test)

Example 20 with JdbcTemplate

use of cn.taketoday.jdbc.core.JdbcTemplate in project today-framework by TAKETODAY.

the class EmbeddedDatabaseFactoryBeanTests method testFactoryBeanLifecycle.

@Test
public void testFactoryBeanLifecycle() throws Exception {
    EmbeddedDatabaseFactoryBean bean = new EmbeddedDatabaseFactoryBean();
    ResourceDatabasePopulator populator = new ResourceDatabasePopulator(resource("db-schema.sql"), resource("db-test-data.sql"));
    bean.setDatabasePopulator(populator);
    bean.afterPropertiesSet();
    DataSource ds = bean.getObject();
    JdbcTemplate template = new JdbcTemplate(ds);
    assertThat(template.queryForObject("select NAME from T_TEST", String.class)).isEqualTo("Keith");
    bean.destroy();
}
Also used : ResourceDatabasePopulator(cn.taketoday.jdbc.datasource.init.ResourceDatabasePopulator) JdbcTemplate(cn.taketoday.jdbc.core.JdbcTemplate) DataSource(javax.sql.DataSource) Test(org.junit.jupiter.api.Test)

Aggregations

JdbcTemplate (cn.taketoday.jdbc.core.JdbcTemplate)52 Test (org.junit.jupiter.api.Test)34 DataSource (javax.sql.DataSource)18 AbstractDriverBasedDataSource (cn.taketoday.jdbc.datasource.AbstractDriverBasedDataSource)10 ClassPathXmlApplicationContext (cn.taketoday.context.support.ClassPathXmlApplicationContext)8 BadSqlGrammarException (cn.taketoday.jdbc.BadSqlGrammarException)6 ConfigurableApplicationContext (cn.taketoday.context.ConfigurableApplicationContext)4 EmbeddedDatabaseFactoryBean (cn.taketoday.jdbc.datasource.embedded.EmbeddedDatabaseFactoryBean)4 Transactional (cn.taketoday.transaction.annotation.Transactional)4 List (java.util.List)4 Autowired (cn.taketoday.beans.factory.annotation.Autowired)2 Bean (cn.taketoday.context.annotation.Bean)2 Primary (cn.taketoday.context.annotation.Primary)2 ConditionalOnMissingBean (cn.taketoday.context.condition.ConditionalOnMissingBean)2 ClassRelativeResourceLoader (cn.taketoday.core.io.ClassRelativeResourceLoader)2 CallableStatementCreator (cn.taketoday.jdbc.core.CallableStatementCreator)2 SqlReturnResultSet (cn.taketoday.jdbc.core.SqlReturnResultSet)2 NamedParameterJdbcTemplate (cn.taketoday.jdbc.core.namedparam.NamedParameterJdbcTemplate)2 DriverManagerDataSource (cn.taketoday.jdbc.datasource.DriverManagerDataSource)2 SimpleDriverDataSource (cn.taketoday.jdbc.datasource.SimpleDriverDataSource)2