Search in sources :

Example 31 with Something

use of org.jdbi.v3.core.Something in project jdbi by jdbi.

the class TestVavrValueArgumentFactoryWithDB method createTestData.

@Before
public void createTestData() {
    Handle handle = dbRule.openHandle();
    handle.createUpdate("insert into something (id, name) values (1, 'eric')").execute();
    handle.createUpdate("insert into something (id, name) values (2, 'brian')").execute();
}
Also used : Handle(org.jdbi.v3.core.Handle) Before(org.junit.Before)

Example 32 with Something

use of org.jdbi.v3.core.Something in project jdbi by jdbi.

the class TestOracleReturning method testReturningDmlNamedParams.

@Test
public void testReturningDmlNamedParams() {
    Handle h = dbRule.getSharedHandle();
    List<Integer> ids = h.createUpdate("insert into something(id, name) values (:id, :name) returning id into :result").bindBean(new Something(17, "Brian")).addCustomizer(returnParameters().register("result", OracleTypes.INTEGER)).execute(returningDml()).mapTo(int.class).list();
    assertThat(ids).containsExactly(17);
}
Also used : Something(org.jdbi.v3.core.Something) Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Example 33 with Something

use of org.jdbi.v3.core.Something in project jdbi by jdbi.

the class TestJdbiFactoryBean method testFailsViaException.

@Test
public void testFailsViaException() throws Exception {
    assertThatExceptionOfType(ForceRollback.class).isThrownBy(() -> {
        service.inPropagationRequired(jdbi -> {
            Handle h = JdbiUtil.getHandle(jdbi);
            final int count = h.execute("insert into something (id, name) values (7, 'ignored')");
            if (count == 1) {
                throw new ForceRollback();
            } else {
                throw new RuntimeException("!ZABAK");
            }
        });
    });
    try (final Handle h = Jdbi.open(ds)) {
        int count = h.createQuery("select count(*) from something").mapTo(int.class).findOnly();
        assertThat(count).isEqualTo(0);
    }
}
Also used : Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Example 34 with Something

use of org.jdbi.v3.core.Something in project jdbi by jdbi.

the class TestJdbiFactoryBean method testNested.

@Test
public void testNested() throws Exception {
    assertThatExceptionOfType(ForceRollback.class).isThrownBy(() -> {
        service.inPropagationRequired(outer -> {
            final Handle h = JdbiUtil.getHandle(outer);
            h.execute("insert into something (id, name) values (7, 'ignored')");
            assertThatExceptionOfType(ForceRollback.class).isThrownBy(() -> {
                service.inNested(inner -> {
                    final Handle h1 = JdbiUtil.getHandle(inner);
                    h1.execute("insert into something (id, name) values (8, 'ignored again')");
                    int count = h1.createQuery("select count(*) from something").mapTo(Integer.class).findOnly();
                    assertThat(count).isEqualTo(2);
                    throw new ForceRollback();
                });
            });
            int count = h.createQuery("select count(*) from something").mapTo(Integer.class).findOnly();
            assertThat(count).isEqualTo(1);
            throw new ForceRollback();
        });
    });
    service.inPropagationRequired(jdbi -> {
        final Handle h = JdbiUtil.getHandle(jdbi);
        int count = h.createQuery("select count(*) from something").mapTo(Integer.class).findOnly();
        assertThat(count).isEqualTo(0);
    });
}
Also used : Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Example 35 with Something

use of org.jdbi.v3.core.Something in project jdbi by jdbi.

the class TestJdbiFactoryBean method testRequiresNew.

@Test
public void testRequiresNew() throws Exception {
    service.inPropagationRequired(outer -> {
        final Handle h = JdbiUtil.getHandle(outer);
        h.execute("insert into something (id, name) values (7, 'ignored')");
        assertThatExceptionOfType(ForceRollback.class).isThrownBy(() -> {
            service.inRequiresNewReadUncommitted(inner -> {
                final Handle h1 = JdbiUtil.getHandle(inner);
                int count = h1.createQuery("select count(*) from something").mapTo(Integer.class).findOnly();
                assertThat(count).isEqualTo(1);
                h1.execute("insert into something (id, name) values (8, 'ignored again')");
                throw new ForceRollback();
            });
        });
        int count = h.createQuery("select count(*) from something").mapTo(Integer.class).findOnly();
        assertThat(count).isEqualTo(1);
    });
}
Also used : Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)170 Something (org.jdbi.v3.core.Something)123 Handle (org.jdbi.v3.core.Handle)80 SomethingMapper (org.jdbi.v3.core.mapper.SomethingMapper)17 Before (org.junit.Before)11 ArrayList (java.util.ArrayList)6 Jdbi (org.jdbi.v3.core.Jdbi)6 HashMap (java.util.HashMap)5 Map (java.util.Map)5 GenericType (org.jdbi.v3.core.generic.GenericType)5 List (java.util.List)4 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)4 H2DatabaseRule (org.jdbi.v3.core.rule.H2DatabaseRule)4 Rule (org.junit.Rule)4 Connection (java.sql.Connection)3 LinkedHashMap (java.util.LinkedHashMap)3 Collector (java.util.stream.Collector)3 Collectors.toList (java.util.stream.Collectors.toList)3 JdbcDataSource (org.h2.jdbcx.JdbcDataSource)3 NoSuchMapperException (org.jdbi.v3.core.mapper.NoSuchMapperException)3