@Test public void testAutocommitFailDoesntLeak() { final BoomHandler handler = new BoomHandler(); dbRule.getJdbi().setTransactionHandler(handler); final Handle h = dbRule.openHandle(); assertThat(h.isClosed()).isFalse(); handler.failTest = true; assertThatThrownBy(h::close) .isInstanceOf(CloseException.class); assertThat(h.isClosed()).isTrue(); }
@Test public void testDefaultMethod() { Spiffy dao = dbRule.getJdbi().onDemand(Spiffy.class); Something test = dao.insertAndReturn(3, "test"); assertThat(test).isEqualTo(new Something(3, "test")); }
@Test public void testSimple() { FooDao fooDao = dbRule.getJdbi().onDemand(FooDao.class); List<Foo> foos = fooDao.select(); assertThat(foos).isEmpty(); fooDao.insert(1, "John Doe"); fooDao.insert(2, "Jane Doe"); List<Foo> foos2 = fooDao.select(); assertThat(foos2).hasSize(2); }
@Before public void setupDbi() { db = dbRule.getJdbi(); h = db.open(); h.execute("CREATE TABLE foo (bar UUID)"); }
@Before public void setup() { jdbi = dbRule.getJdbi(); h = dbRule.getSharedHandle(); h.execute("create table immutables (t int, x varchar)"); }
@Test public void testRegisterRowMapperAnnotationWorks() { Kabob bob = dbRule.getJdbi().onDemand(Kabob.class); bob.insert(1, "Henning"); Something henning = bob.find(1); assertThat(henning).isEqualTo(new Something(1, "Henning")); }
@Test public void testSimpleTransactionsSucceed() { SomethingDao dao = dbRule.getJdbi().onDemand(SomethingDao.class); dao.insertInSingleTransaction(10, "Linda"); }
@Test public void testSillyNumberOfCallbacks() throws Exception { try (Handle h = dbRule.openHandle()) { h.execute("insert into something (id, name) values (1, 'Keith')"); } // strangely enough, the compiler can't infer this and thinks the throws is redundant String value = dbRule.getJdbi().<String, Exception>withHandle(handle -> handle.inTransaction(handle1 -> handle1.createQuery("select name from something where id = 1").mapTo(String.class).findOnly())); assertThat(value).isEqualTo("Keith"); }
@Test public void testGlobalStatementCustomizers() throws Exception { dbRule.getJdbi().addCustomizer(StatementCustomizers.maxRows(1)); handle = dbRule.openHandle(); handle.execute("insert into something (id, name) values (?, ?)", 1, "hello"); handle.execute("insert into something (id, name) values (?, ?)", 2, "world"); List<Something> rs = handle.createQuery("select id, name from something") .mapToBean(Something.class) .list(); assertThat(rs).hasSize(1); }
@Before public void setUp() { db = dbRule.getJdbi(); db.useHandle(h -> h.execute("CREATE ALIAS custom_insert FOR " + "\"org.jdbi.v3.sqlobject.TestTimingCollector.customInsert\";")); db.setTimingCollector(timingCollector); dao = db.onDemand(DAO.class); }
@Test public void testIterator() { handle.execute("insert into something (id, name) values (7, 'Tim')"); handle.execute("insert into something (id, name) values (3, 'Diego')"); dbRule.getJdbi().useExtension(Spiffy.class, spiffy -> { Iterator<Something> itty = spiffy.findByIdRange(2, 10); assertThat(itty).containsOnlyOnce(new Something(7, "Tim"), new Something(3, "Diego")); }); }
@Test public void testList() { handle.execute("insert into something (id, name) values (7, 'Tim')"); handle.execute("insert into something (id, name) values (3, 'Diego')"); dbRule.getJdbi().useExtension(Spiffy.class, spiffy -> { List<Something> all = spiffy.findTwoByIds(3, 7); assertThat(all).containsOnlyOnce(new Something(7, "Tim"), new Something(3, "Diego")); }); }
@Test public void testWithSqlObjectSetReturnValue() { Dao dao = dbRule.getJdbi().onDemand(Dao.class); dao.insert(new Something(1, "Coda")); dao.insert(new Something(2, "Brian")); SortedSet<String> rs = dao.findAllAsSet(); assertThat(rs).containsExactly("Brian", "Coda"); }
@Before public void before() { handle = h2.getJdbi() .registerRowMapper(new SomethingMapper()) .open(); handle.execute("insert into something(id, name) values(1, '1')"); handle.execute("insert into something(id, name) values(2, '2')"); // "control group" element that should *not* be returned by the queries handle.execute("insert into something(id, name) values(3, '3')"); expectedSomethings = Arrays.asList(new Something(1, "1"), new Something(2, "2")); }
@Before public void before() { final Jdbi db = dbRule.getJdbi(); db.installPlugin(new SqlObjectPlugin()); db.registerRowMapper(new SomethingMapper()); handle = db.open(); handle.execute("insert into something(id, name) values(1, '1')"); handle.execute("insert into something(id, name) values(2, '2')"); // "control group" element that should *not* be returned by the queries handle.execute("insert into something(id, name) values(3, '3')"); expectedSomethings = Arrays.asList(new Something(1, "1"), new Something(2, "2")); }