@Before public void before() { jdbi = db.getJdbi().setTemplateEngine(TemplateEngine.NOP); }
default <C extends JdbiConfig<C>> Self withConfig(Class<C> configClass, Consumer<C> configurer) { return withPlugin(ConfiguringPlugin.of(configClass, configurer)); } }
@Before public void before() { jdbi = db.getJdbi().setTemplateEngine(TemplateEngine.NOP); }
@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 public void nullByteArrayIsTypedAsVarbinary() throws SQLException { Argument nullByteArrayArg = db.getJdbi().withHandle(h -> h.getConfig(Arguments.class).findFor(byte[].class, null)).get(); nullByteArrayArg.apply(0, stmt, null); verify(stmt, never()).setNull(anyInt(), eq(Types.ARRAY)); verify(stmt).setNull(anyInt(), eq(Types.VARBINARY)); } }
@After public void tearDown() { dbRule.getJdbi().useHandle(handle -> handle.execute("drop table nvarchars")); }
@Before public void createTable() { dbRule.getJdbi().useHandle(h -> { h.execute("CREATE TABLE test (id BIGINT PRIMARY KEY, value TEXT)"); }); }
private void insert(String binding, Object bean) { dbRule.getJdbi().useHandle(h -> { String insert = String.format("INSERT INTO test VALUES(:id, :%s)", binding); h.createUpdate(insert).bindBean(bean).execute(); }); }
private Optional<IdValue> select() { return dbRule.getJdbi().withHandle( h -> h.createQuery("SELECT id, value FROM test") .map((rs, ctx) -> new IdValue(rs.getLong("id"), rs.getString("value"))) .findFirst()); }
@Before public void setUp() { dbRule.getJdbi().useHandle(handle -> handle.execute("create table nvarchars (id int primary key, name nvarchar not null)")); }
@Test public void findNVarcharMapper() throws Exception { dbRule.getJdbi().useHandle(handle -> { ResultSet rs = mock(ResultSet.class); when(rs.getNString(anyInt())).thenReturn("value"); assertThat( handle.getConfig(Mappers.class) .findFor(NVARCHAR_STRING) .orElseThrow(IllegalStateException::new) .map(rs, null)) .isEqualTo("value"); assertThat( handle.getConfig(ColumnMappers.class) .findFor(NVARCHAR_STRING) .orElseThrow(IllegalStateException::new) .map(rs, 1, null)) .isEqualTo("value"); }); } }
@Before public void before() { handle = db.getJdbi().open(); handle.execute("create table foo(bar binary)"); handle.setSqlLogger(new SqlLogger() { @Override public void logBeforeExecution(StatementContext context) { context.getBinding().findForPosition(0).ifPresent(value -> positional = Objects.toString(value)); context.getBinding().findForName(NAME, context).ifPresent(value -> named = Objects.toString(value)); } }); }
@Test public void findNVarcharArgument() throws Exception { dbRule.getJdbi().useHandle(handle -> { String value = "foo"; PreparedStatement stmt = mock(PreparedStatement.class); handle.getConfig(Arguments.class) .findFor(NVARCHAR_STRING, value) .orElseThrow(IllegalStateException::new) .apply(1, stmt, null); verify(stmt).setNString(1, value); handle.createQuery("no execute") .getContext() .findArgumentFor(NVARCHAR_STRING, value) .orElseThrow(IllegalStateException::new) .apply(2, stmt, null); verify(stmt).setNString(2, value); }); }
@Test public void registerColumnMapperFactory() { dbRule.getJdbi() .registerColumnMapper(new ReversedStringMapperFactory()) .useHandle(handle -> { handle.execute("insert into something (id, name) values (1, 'xyz')"); assertThat( handle.select("SELECT name FROM something") .mapTo(QualifiedType.of(String.class).with(Reversed.class)) .findOnly()) .isEqualTo("zyx"); }); }
@Test public void registerColumnMapper() { dbRule.getJdbi() .registerColumnMapper(new ReversedStringMapper()) .useHandle(handle -> { handle.execute("insert into something (id, name) values (1, 'abc')"); assertThat( handle.select("SELECT name FROM something") .mapTo(QualifiedType.of(String.class).with(Reversed.class)) .findOnly()) .isEqualTo("cba"); }); }
@Test public void registerColumnMapperByQualifiedType() { dbRule.getJdbi() .registerColumnMapper( QualifiedType.of(String.class).with(Reversed.class), (r, c, ctx) -> reverse(r.getString(c))) .useHandle(handle -> { handle.execute("insert into something (id, name) values (1, 'abcdef')"); assertThat( handle.select("SELECT name FROM something") .mapTo(QualifiedType.of(String.class).with(Reversed.class)) .findOnly()) .isEqualTo("fedcba"); }); }
@Test public void bindBeanQualifiedSetterParam() { dbRule.getJdbi() .registerArgument(new ReversedStringArgumentFactory()) .useHandle(handle -> { handle.createUpdate("INSERT INTO something (id, name) VALUES (:id, :name)") .bindBean(new QualifiedSetterParamThing(1, "abc")) .execute(); assertThat(handle.select("SELECT name FROM something") .mapTo(String.class) .findOnly()) .isEqualTo("cba"); }); }
@Test public void bindBeanQualifiedGetter() { dbRule.getJdbi() .registerArgument(new ReversedStringArgumentFactory()) .useHandle(handle -> { handle.createUpdate("INSERT INTO something (id, name) VALUES (:id, :name)") .bindBean(new QualifiedGetterThing(1, "abc")) .execute(); assertThat(handle.select("SELECT name FROM something") .mapTo(String.class) .findOnly()) .isEqualTo("cba"); }); }
@Test public void bindFieldsQualified() { dbRule.getJdbi() .registerArgument(new ReversedStringArgumentFactory()) .useHandle(handle -> { handle.createUpdate("INSERT INTO something (id, name) VALUES (:id, :name)") .bindFields(new QualifiedFieldThing(1, "abc")) .execute(); assertThat(handle.select("SELECT name FROM something") .mapTo(String.class) .findOnly()) .isEqualTo("cba"); }); }
@Test public void mapBeanQualifiedGetter() { dbRule.getJdbi() .registerColumnMapper(new ReversedStringMapper()) .registerRowMapper(BeanMapper.factory(QualifiedGetterThing.class)) .useHandle(handle -> { handle.execute("INSERT INTO something (id, name) VALUES (1, 'abc')"); assertThat(handle.select("SELECT * FROM something") .mapTo(QualifiedGetterThing.class) .findOnly()) .isEqualTo(new QualifiedGetterThing(1, "cba")); }); }