@Override public ConfigRegistry getConfig() { return handle.getConfig(); }
/** * Create a simple statement context that shares configuration * with the given handle. */ public static StatementContext createContext(Handle handle) { return new StatementContext(handle.getConfig()); } }
@Test public void byteArrayIsTypedAsVarbinary() throws SQLException { Argument nullByteArrayArg = db.getJdbi().withHandle(h -> h.getConfig(Arguments.class).findFor(byte[].class, new byte[] {1})).get(); nullByteArrayArg.apply(0, stmt, null); verify(stmt, never()).setArray(anyInt(), any(Array.class)); verify(stmt).setBytes(anyInt(), any(byte[].class)); }
@Test @SuppressWarnings("unchecked") public void testRegisterByGenericType() throws Exception { ColumnMapper<Iterable<Calendar>> mapper = mock(ColumnMapper.class); GenericType<Iterable<Calendar>> iterableOfCalendarType = new GenericType<Iterable<Calendar>>() {}; h.registerColumnMapper(iterableOfCalendarType, mapper); assertThat(h.getConfig(ColumnMappers.class).findFor(iterableOfCalendarType)) .contains(mapper); } }
@Test public void testDisallowed() { dao.getHandle().getConfig(SqlStatements.class).setUnusedBindingAllowed(true); assertThatThrownBy(() -> dao.disallowed("43")) .isInstanceOf(UnableToCreateStatementException.class) .hasMessageContaining("named parameter"); }
@Test public void testCloseWithOpenTransactionCheckDisabled() { h.getConfig(Handles.class).setForceEndTransactions(false); h.begin(); h.close(); assertThat(h.isClosed()).isTrue(); }
@Test public void testRegisteredDecorator() { handle.getConfig(HandlerDecorators.class).register( (base, sqlObjectType, method) -> (obj, args, handle) -> { invoked("custom"); return base.invoke(obj, args, handle); }); handle.attach(Dao.class).orderedFooBar(); assertThat(INVOCATIONS.get()).containsExactly("custom", "foo", "bar", "method"); }
@Test public void duplicateColumnsWithoutCaseChangeCauseException() { jdbi.useHandle(h -> { h.getConfig(MapMappers.class).setCaseChange(CaseStrategy.NOP); ResultIterable<Map<String, BigDecimal>> query = h.createQuery(QUERY.replace("two", "one")).mapToMap(BigDecimal.class); assertThatThrownBy(query::findOnly) .hasMessageContaining("map key \"one\" (from column \"one\") appears twice"); }); }
@Test public void duplicateKeysAfterCaseChangeCauseException() { jdbi.useHandle(h -> { h.getConfig(MapMappers.class).setCaseChange(CaseStrategy.LOWER); // one and ONE ResultIterable<Map<String, BigDecimal>> query = h.createQuery(QUERY.replace("two", "ONE")).mapToMap(BigDecimal.class); assertThatThrownBy(query::findOnly) .hasMessageContaining("map key \"one\" (from column \"ONE\") appears twice"); }); }
@Test public void enumCanBeAnnotatedAsByName() { h.getConfig(Enums.class).setEnumStrategy(EnumStrategy.BY_ORDINAL); ByName byName = h.createQuery("select :name") .bind("name", ByName.ALPHABETIC.name()) .mapTo(ByName.class) .findOnly(); assertThat(byName) .isEqualTo(ByName.ALPHABETIC); }
@Test public void testCaseLower() { h.getConfig(MapMappers.class).setCaseChange(CaseStrategy.LOCALE_LOWER); Map<String, Object> noOne = h.createQuery("select * from Foo").mapToMap().findOnly(); assertThat(noOne).containsOnlyKeys("id", "firstname"); }
@Test public void testCaseDefaultNop() { h.getConfig(MapMappers.class).setCaseChange(CaseStrategy.NOP); Map<String, Object> noOne = h.createQuery("select * from Foo").mapToMap().findOnly(); assertThat(noOne).containsOnlyKeys("Id", "FirstName"); }
@Test public void useEnumStrategyOrdinalAnnotation() { db.getJdbi().useHandle(h -> { h.getConfig(Enums.class).setEnumStrategy(EnumStrategy.BY_NAME); // dao annotations will override h.execute("create table enums(ordinal int)"); UseEnumStrategyOrdinalDao dao = h.attach(UseEnumStrategyOrdinalDao.class); dao.insert(Foo.BAR); assertThat(h.createQuery("select ordinal from enums").mapTo(Integer.class).findOnly()).isEqualTo(0); Foo value = dao.select(); assertThat(value).isEqualTo(Foo.BAR); }); }
@Test public void testStatementWithOptionalMapResults() { h.getConfig(ResultProducers.class).allowNoResults(true); assertThat(h.createQuery("commit").mapToMap().findFirst()).isEmpty(); }
@Test public void byNameOverridesDefaultInBindingAndMapping() { db.getJdbi().useHandle(h -> { h.getConfig(Enums.class).setEnumStrategy(EnumStrategy.BY_ORDINAL); h.execute("create table enums(name varchar)"); FooByNameDao dao = h.attach(FooByNameDao.class); dao.insert(Foo.BAR); assertThat(h.createQuery("select name from enums").mapTo(String.class).findOnly()).isEqualTo("BAR"); Foo value = dao.select(); assertThat(value).isEqualTo(Foo.BAR); }); }
@Test public void testStatementWithOptionalBeanResults() { h.getConfig(ResultProducers.class).allowNoResults(true); assertThat(h.createQuery("commit").mapToBean(Object.class).findFirst()).isEmpty(); }
@Test public void ordinalsAreMappedCorrectly() { db.getJdbi().useHandle(h -> { h.getConfig(Enums.class).setEnumStrategy(EnumStrategy.BY_ORDINAL); Foobar name = h.createQuery("select :ordinal") .bind("ordinal", Foobar.FOO.ordinal()) .mapTo(Foobar.class) .findOnly(); assertThat(name) .isEqualTo(Foobar.FOO); }); }
@Test public void testTimeout() { h.getConfig(SqlStatements.class).setQueryTimeout(2); assertThatCode(h.createQuery("select pg_sleep(1)").mapTo(String.class)::findOnly) .doesNotThrowAnyException(); assertThatThrownBy(h.createQuery("select pg_sleep(3)").mapTo(String.class)::findOnly) .isInstanceOf(UnableToExecuteStatementException.class) .hasMessageContaining("canceling statement due to user request"); } }
@Test public void methodCallCanBeAnnotatedAsByName() { h.getConfig(Enums.class).setEnumStrategy(EnumStrategy.BY_ORDINAL); Object byName = h.createQuery("select :name") .bind("name", Foobar.FOO.name()) .mapTo(QualifiedType.of(Foobar.class).with(EnumByName.class)) .findOnly(); assertThat(byName) .isEqualTo(Foobar.FOO); }
@Test public void methodCallOverridesClassForName() { h.getConfig(Enums.class).setEnumStrategy(EnumStrategy.BY_ORDINAL); Object byName = h.createQuery("select :name") .bind("name", ByOrdinal.NUMERIC.name()) .mapTo(QualifiedType.of(ByOrdinal.class).with(EnumByName.class)) .findOnly(); assertThat(byName) .isEqualTo(ByOrdinal.NUMERIC); }