Search in sources :

Example 1 with JdbcTemplate

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

the class H2SequenceMaxValueIncrementerTests method assertIncrements.

private void assertIncrements(DataSource dataSource) {
    assertThat(new JdbcTemplate(dataSource).queryForObject("values next value for SEQ", int.class)).isEqualTo(1);
    H2SequenceMaxValueIncrementer incrementer = new H2SequenceMaxValueIncrementer(dataSource, "SEQ");
    assertThat(incrementer.nextIntValue()).isEqualTo(2);
    assertThat(incrementer.nextStringValue()).isEqualTo("3");
}
Also used : JdbcTemplate(cn.taketoday.jdbc.core.JdbcTemplate)

Example 2 with JdbcTemplate

use of cn.taketoday.jdbc.core.JdbcTemplate in project today-infrastructure 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 3 with JdbcTemplate

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

the class JdbcNamespaceIntegrationTests method assertCorrectSetupAndCloseContext.

private void assertCorrectSetupAndCloseContext(String file, int count, String... dataSources) {
    try (ConfigurableApplicationContext context = context(file)) {
        for (String dataSourceName : dataSources) {
            DataSource dataSource = context.getBean(dataSourceName, DataSource.class);
            assertNumRowsInTestTable(new JdbcTemplate(dataSource), count);
            assertThat(dataSource instanceof AbstractDriverBasedDataSource).isTrue();
            AbstractDriverBasedDataSource adbDataSource = (AbstractDriverBasedDataSource) dataSource;
            assertThat(adbDataSource.getUrl()).contains(dataSourceName);
        }
    }
}
Also used : ConfigurableApplicationContext(cn.taketoday.context.ConfigurableApplicationContext) AbstractDriverBasedDataSource(cn.taketoday.jdbc.datasource.AbstractDriverBasedDataSource) JdbcTemplate(cn.taketoday.jdbc.core.JdbcTemplate) AbstractDriverBasedDataSource(cn.taketoday.jdbc.datasource.AbstractDriverBasedDataSource) DataSource(javax.sql.DataSource)

Example 4 with JdbcTemplate

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

the class JdbcNamespaceIntegrationTests method assertCorrectSetupForSingleDataSource.

private void assertCorrectSetupForSingleDataSource(String file, Predicate<String> urlPredicate) {
    try (ConfigurableApplicationContext context = context(file)) {
        DataSource dataSource = context.getBean(DataSource.class);
        assertNumRowsInTestTable(new JdbcTemplate(dataSource), 1);
        assertThat(dataSource instanceof AbstractDriverBasedDataSource).isTrue();
        AbstractDriverBasedDataSource adbDataSource = (AbstractDriverBasedDataSource) dataSource;
        assertThat(urlPredicate.test(adbDataSource.getUrl())).isTrue();
    }
}
Also used : ConfigurableApplicationContext(cn.taketoday.context.ConfigurableApplicationContext) AbstractDriverBasedDataSource(cn.taketoday.jdbc.datasource.AbstractDriverBasedDataSource) JdbcTemplate(cn.taketoday.jdbc.core.JdbcTemplate) AbstractDriverBasedDataSource(cn.taketoday.jdbc.datasource.AbstractDriverBasedDataSource) DataSource(javax.sql.DataSource)

Example 5 with JdbcTemplate

use of cn.taketoday.jdbc.core.JdbcTemplate in project today-infrastructure 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)

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