@SuppressWarnings("resource") @Test public void testIsClosed() { Handle h = dbRule.openHandle(); assertThat(h.isClosed()).isFalse(); h.close(); assertThat(h.isClosed()).isTrue(); }
@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 testInsert() { Handle h = dbRule.openHandle(); Update insert = h.createUpdate("insert into something (id, name) values (:id, :name)"); insert.bind("id", 1); insert.bind("name", "Brian"); int count = insert.execute(); assertThat(count).isEqualTo(1); }
@Test public void testExecuteSomeStatements() { try (Handle h = dbRule.openHandle()) { h.execute("insert into something (id, name) values (?, ?)", 3, "Patrick"); List<Map<String, Object>> rs = h.select("select id, name from something").mapToMap().list(); assertThat(rs).containsExactlyElementsOf(ImmutableList.of(ImmutableMap.of("id", 3L, "name", "Patrick"))); } }
@Test public void unknownTransactionLevelIsOk() { Handle h = dbRule.openHandle(); assertThatThrownBy(() -> h.setTransactionIsolation(Integer.MIN_VALUE)) .isInstanceOf(UnableToManipulateTransactionIsolationLevelException.class); assertThatCode(() -> h.setTransactionIsolation(TransactionIsolationLevel.UNKNOWN)) .doesNotThrowAnyException(); }
@Test public void testNoRootRegistrations() { try (Handle h = dbRule.openHandle()) { h.execute("insert into something (id, name) values (1, 'Henning')"); assertThatThrownBy(() -> h.createQuery("select id, name from something where id = 1") .mapTo(Something.class) .findFirst()).isInstanceOf(NoSuchMapperException.class); } }
@Test public void testFiveMinuteFluentApi() { try (Handle h = dbRule.openHandle()) { h.execute("insert into something (id, name) values (?, ?)", 1, "Brian"); String name = h.createQuery("select name from something where id = :id") .bind("id", 1) .mapTo(String.class) .findOnly(); assertThat(name).isEqualTo("Brian"); } }
@Before public void createTable() { h = dbRule.openHandle(); h.createUpdate("create table testBean (valueType varchar(50))").execute(); dao = h.attach(TestDao.class); }
@Test public void testInTransaction() { Handle h = dbRule.openHandle(); String value = h.inTransaction(handle -> { handle.execute("insert into something (id, name) values (1, 'Brian')"); return handle.createQuery("select name from something where id = 1").mapToBean(Something.class).findOnly().getName(); }); assertThat(value).isEqualTo("Brian"); }
@Test public void testScriptAsSetOfSeparateStatements() { assertThatExceptionOfType(StatementException.class) .isThrownBy(() -> { Handle h = dbRule.openHandle(); Script script = h.createScript(getResourceOnClasspath("script/malformed-sql-script.sql")); script.executeAsSeparateStatements(); }) .satisfies(e -> assertThat(e.getStatementContext().getRawSql().trim()) .isEqualTo("insert into something(id, name) values (2, eric)")); } }
@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"); }
@Before public void addData() { Handle handle = dbRule.openHandle(); handle.createUpdate("insert into something (id, name) values (1, 'eric')").execute(); handle.createUpdate("insert into something (id) values (2)").execute(); }
@Test public void testNamedParamsInExternal() { Handle h = dbRule.openHandle(); h.createUpdate(ClasspathSqlLocator.findSqlOnClasspath("insert-id-name")) .bind("id", 1) .bind("name", "Tip") .execute(); assertThat(h.select("select name from something").mapTo(String.class).list()).hasSize(1); }
@Test public void testScriptWithStringSemicolon() { Handle h = dbRule.openHandle(); Script script = h.createScript(getResourceOnClasspath("script/insert-with-string-semicolons.sql")); script.execute(); assertThat(h.select("select * from something").mapToMap()).hasSize(3); }
@Test public void testMappingExampleChainedIterator3() { try (Handle h = dbRule.openHandle()) { h.execute("insert into something (id, name) values (1, 'Brian')"); h.execute("insert into something (id, name) values (2, 'Keith')"); ResultIterable<String> names = h.createQuery("select name from something order by id").mapTo(String.class); assertThat(names.iterator()).containsExactly("Brian", "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); }
@Test public void testFluentUpdate() { try (Handle h = dbRule.openHandle()) { h.createUpdate("insert into something(id, name) values (:id, :name)") .bind("id", 4) .bind("name", "Martin") .execute(); } }
@Before public void addData() { Handle handle = dbRule.openHandle(); handle.createUpdate("insert into something (id, name, integerValue, intValue) values (1, 'eric', 99, 100)").execute(); handle.createUpdate("insert into something (id, name, integerValue, intValue) values (2, 'brian', 101, 102)").execute(); }
@Test public void testUri() { Handle h = dbRule.openHandle(); h.createUpdate("insert into something (id, name) values (1, :uri)") .bind("uri", TEST_URI).execute(); assertThat(h.createQuery("SELECT name FROM something") .mapTo(URI.class) .findOnly()).isEqualTo(TEST_URI); } }
@Test public void testMapEnumValues() { Handle h = dbRule.openHandle(); h.createUpdate("insert into something (id, name) values (1, 'eric')").execute(); h.createUpdate("insert into something (id, name) values (2, 'brian')").execute(); List<SomethingElse> results = h.createQuery("select * from something order by id") .mapToBean(SomethingElse.class) .list(); assertThat(results).extracting(se -> se.name).containsExactly(SomethingElse.Name.eric, SomethingElse.Name.brian); }