@Test public void testUuidArrayList() { assertThat( h.createUpdate(U_INSERT) .bindByType("u", new ArrayList<>(Arrays.asList(testUuids)), UUID_LIST) .execute()) .isEqualTo(1); assertThat( h.createQuery(U_SELECT) .mapTo(UUID_ARRAYLIST) .findOnly()) .containsExactly(testUuids); }
@Test public void testUuidList() { assertThat( h.createUpdate(U_INSERT) .bindByType("u", Arrays.asList(testUuids), UUID_LIST) .execute()) .isEqualTo(1); assertThat( h.createQuery(U_SELECT) .mapTo(UUID_LIST) .findOnly()) .containsExactly(testUuids); }
@Test public void testUuidHashSet() { assertThat( h.createUpdate(U_INSERT) .bindByType("u", new HashSet<>(Arrays.asList(testUuids)), UUID_SET) .execute()) .isEqualTo(1); assertThat( h.createQuery(U_SELECT) .mapTo(UUID_HASHSET) .findOnly()) .containsExactlyInAnyOrder(testUuids); }
@Test public void testInsertNullUrlUsingBindByType() { handle.createUpdate("INSERT INTO foo VALUES (:url)") .bindByType("url", null, URL.class) .execute(); assertThatThrownBy(() -> handle.createQuery("SELECT url FROM foo").mapTo(URL.class).findOnly()) .isInstanceOf(ResultSetException.class); }
@Test public void testInsertUrlUsingBindByType() throws MalformedURLException { URL githubUrl = new URL("http://www.github.com"); handle.createUpdate("INSERT INTO foo VALUES (:url)") .bindByType("url", githubUrl, URL.class) .execute(); URL dbUrl = handle.createQuery("SELECT * FROM foo").mapTo(URL.class).findOnly(); Assertions.assertThat(dbUrl).hasToString(githubUrl.toString()); }
@Test public void testUuidLinkedHashSet() { assertThat( h.createUpdate(U_INSERT) .bindByType("u", new LinkedHashSet<>(Arrays.asList(testUuids)), UUID_SET) .execute()) .isEqualTo(1); assertThat( h.createQuery(U_SELECT) .mapTo(UUID_LINKEDHASHSET) .findOnly()) .isInstanceOf(LinkedHashSet.class) .containsExactly(testUuids); } }
@Test public void factoryChainWorks() { Object instance = new Foo(); String json = "foo"; when(jsonMapper.toJson(eq(Foo.class), eq(instance), any(ConfigRegistry.class))).thenReturn(json); when(jsonMapper.fromJson(eq(Foo.class), eq(json), any(ConfigRegistry.class))).thenReturn(instance); Object result = db.getJdbi().withHandle(h -> { h.createUpdate("insert into foo(bar) values(:foo)") .bindByType("foo", instance, QualifiedType.of(Foo.class).with(Json.class)) .execute(); assertThat(h.createQuery("select bar from foo").mapTo(String.class).findOnly()) .isEqualTo(json); return h.createQuery("select bar from foo") .mapTo(QualifiedType.of(Foo.class).with(Json.class)) .findOnly(); }); assertThat(result).isSameAs(instance); verify(jsonMapper).fromJson(eq(Foo.class), eq(json), any(ConfigRegistry.class)); verify(jsonMapper).toJson(eq(Foo.class), eq(instance), any(ConfigRegistry.class)); }
@Test public void methodCallCanBeAnnotatedAsByOrdinal() { db.getJdbi().useHandle(h -> { h.createUpdate("create table enums(id int, ordinal int)").execute(); h.createUpdate("insert into enums (id, ordinal) values (1, :ordinal)") .bindByType("ordinal", Foobar.FOO, QualifiedType.of(Foobar.class).with(EnumByOrdinal.class)) .execute(); Integer inserted = h.createQuery("select ordinal from enums") .mapTo(Integer.class) .findOnly(); assertThat(inserted).isEqualTo(Foobar.FOO.ordinal()); }); }
@Test public void registerArgumentFactory() { dbRule.getJdbi() .registerArgument(new ReversedStringArgumentFactory()) .useHandle(handle -> { handle.createUpdate("INSERT INTO something (id, name) VALUES (1, :name)") .bindByType("name", "abc", QualifiedType.of(String.class).with(Reversed.class)) .execute(); assertThat( handle.select("SELECT name FROM something") .mapTo(String.class) .findOnly()) .isEqualTo("cba"); }); }
@Test public void methodCallOverridesClassForOrdinal() { db.getJdbi().useHandle(h -> { h.createUpdate("create table enums(id int, ordinal int)").execute(); h.createUpdate("insert into enums(id, ordinal) values(1, :ordinal)") .bindByType("ordinal", ByName.ALPHABETIC, QualifiedType.of(ByName.class).with(EnumByOrdinal.class)) .execute(); Integer inserted = h.createQuery("select ordinal from enums") .mapTo(Integer.class) .findOnly(); assertThat(inserted).isEqualTo(ByName.ALPHABETIC.ordinal()); }); }
@Test public void testFluentApi() { jdbi.useHandle(h -> { h.execute("create table subjects (id serial primary key, subject json not null)"); JsonBean in = new JsonBean("nom", 10); h.createUpdate("insert into subjects(id, subject) values(1, :bean)") .bindByType("bean", in, QualifiedType.of(JsonBean.class).with(Json.class)) .execute(); JsonBean out = h.createQuery("select subject from subjects") .mapTo(QualifiedType.of(JsonBean.class).with(Json.class)) .findOnly(); assertThat(out).isEqualTo(in); }); }
@Test public void configArgumentsRegister() { dbRule.getJdbi() .configure(Arguments.class, config -> config.register(new ReversedStringArgumentFactory())) .useHandle(handle -> { handle.createUpdate("INSERT INTO something (id, name) VALUES (1, :name)") .bindByType("name", "abc", QualifiedType.of(String.class).with(Reversed.class)) .execute(); assertThat( handle.select("SELECT name FROM something") .mapTo(String.class) .findOnly()) .isEqualTo("cba"); }); }
@Test public void methodCallCanBeAnnotatedAsByName() { db.getJdbi().useHandle(h -> { h.getConfig(Enums.class).setEnumStrategy(EnumStrategy.BY_ORDINAL); h.createUpdate("create table enums(id int, name varchar)").execute(); h.createUpdate("insert into enums (id, name) values (1, :name)") .bindByType("name", Foobar.FOO, QualifiedType.of(Foobar.class).with(EnumByName.class)) .execute(); String inserted = h.createQuery("select name from enums") .mapTo(String.class) .findOnly(); assertThat(inserted).isEqualTo(Foobar.FOO.name()); }); }
@Test public void methodCallOverridesClassForName() { db.getJdbi().useHandle(h -> { h.getConfig(Enums.class).setEnumStrategy(EnumStrategy.BY_ORDINAL); h.createUpdate("create table enums(id int, name varchar)").execute(); h.createUpdate("insert into enums(id, name) values(1, :name)") .bindByType("name", ByOrdinal.NUMERIC, QualifiedType.of(ByOrdinal.class).with(EnumByName.class)) .execute(); String inserted = h.createQuery("select name from enums") .mapTo(String.class) .findOnly(); assertThat(inserted).isEqualTo(ByOrdinal.NUMERIC.name()); }); }
@Test public void testWritesViaFluentApi() { handle.createUpdate("insert into campaigns(id, caps) values (:id, :caps)") .bind("id", 3) .bindByType("caps", caps, QualifiedType.of(STRING_MAP).with(HStore.class)) .execute(); Map<String, String> newCaps = handle.createQuery("select caps from campaigns where id=?") .bind(0, 3) .mapTo(QualifiedType.of(STRING_MAP).with(HStore.class)) .findOnly(); assertThat(newCaps).isEqualTo(caps); }
@Test public void testNull() { db.getJdbi().useHandle(h -> { h.createUpdate("create table enums(value varchar)").execute(); h.createUpdate("insert into enums(value) values(:enum)") .bindByType("enum", null, Foobar.class) .execute(); String inserted = h.createQuery("select value from enums") .mapTo(String.class) .findOnly(); assertThat(inserted).isNull(); Foobar mapped = h.createQuery("select value from enums") .mapTo(Foobar.class) .findOnly(); assertThat(mapped).isNull(); }); }
@Test public void bindMultipleQualifiers() { dbRule.getJdbi() // should use this one - register first so it's consulted last .registerArgument(new ReversedUpperCaseStringArgumentFactory()) .registerArgument(new ReversedStringArgumentFactory()) .registerArgument(new UpperCaseArgumentFactory()) .useHandle(handle -> { handle.createUpdate("INSERT INTO something (id, name) VALUES (1, :name)") .bindByType("name", "abc", QualifiedType.of(String.class).with(Reversed.class, UpperCase.class)) .execute(); assertThat(handle.select("SELECT name FROM something") .mapTo(String.class) .findOnly()) .isEqualTo("CBA"); }); }
.bindByType(1, "baz", NVARCHAR_STRING) .execute(); .bindByType("name", "qux", NVARCHAR_STRING) .execute();
@Test public void factoryChainWorks() { Object instance = new Foo(); String json = "foo"; when(jsonMapper.toJson(eq(Foo.class), eq(instance), any(StatementContext.class))).thenReturn(json); when(jsonMapper.fromJson(eq(Foo.class), eq(json), any(StatementContext.class))).thenReturn(instance); Object result = db.getJdbi().withHandle(h -> { h.createUpdate("insert into foo(bar) values(:foo)") .bindByType("foo", instance, QualifiedType.of(Foo.class).with(Json.class)) .execute(); assertThat(h.createQuery("select bar from foo").mapTo(String.class).findOnly()) .isEqualTo(json); return h.createQuery("select bar from foo") .mapTo(QualifiedType.of(Foo.class).with(Json.class)) .findOnly(); }); assertThat(result).isSameAs(instance); verify(jsonMapper).fromJson(eq(Foo.class), eq(json), any(StatementContext.class)); verify(jsonMapper).toJson(eq(Foo.class), eq(instance), any(StatementContext.class)); }
@Test public void testFluentApi() { jdbi.useHandle(h -> { h.execute("create table subjects (id serial primary key, subject json not null)"); JsonBean in = new JsonBean("nom", 10); h.createUpdate("insert into subjects(id, subject) values(1, :bean)") .bindByType("bean", in, QualifiedType.of(JsonBean.class).with(Json.class)) .execute(); JsonBean out = h.createQuery("select subject from subjects") .mapTo(QualifiedType.of(JsonBean.class).with(Json.class)) .findOnly(); assertThat(out).isEqualTo(in); }); }