@Before public void setUp() { final Handle handle = dbRule.getSharedHandle(); dao = handle.attach(SomethingDao.class); }
@Test public void immutableSortedSetWithComparator() { Comparator<Integer> comparator = Comparator.<Integer>naturalOrder().reversed(); ImmutableSortedSet<Integer> set = dbRule.getSharedHandle().createQuery("select intValue from something") .mapTo(int.class) .collect(ImmutableSortedSet.toImmutableSortedSet(comparator)); assertThat(set).containsExactlyElementsOf(expected.stream() .sorted(comparator) .collect(Collectors.toList())); }
@Test public void testReduceBiFunction() { assertThat( dbRule.getSharedHandle().createQuery("SELECT * FROM reduce") .mapTo(Integer.class) .reduce(0, TestResultBearing::add)) .isEqualTo(10); }
@Before public void setUp() { dao = dbRule.getSharedHandle().attach(Dao.class); dbRule.getSharedHandle().useTransaction(h -> { h.execute("insert into something (id, name) values (1, 'Alice')"); h.execute("insert into something (id, name) values (2, 'Bob')"); h.execute("insert into something (id, name) values (3, 'Charles')"); }); }
@Before public void setUp() { handle = dbRule.getSharedHandle(); handle.registerRowMapper(new SomethingMapper()); }
@Before public void setUp() { Handle h = dbRule.getSharedHandle(); h.execute("CREATE TABLE reduce (u INT)"); for (int u = 0; u < 5; u++) { h.execute("INSERT INTO reduce VALUES (?)", u); } }
@Before public void setup() { jdbi = dbRule.getJdbi(); h = dbRule.getSharedHandle(); h.execute("create table immutables (t int, x varchar)"); }
@Before public void setUp() { dao = dbRule.getSharedHandle().attach(PrimitiveDao.class); dao.insert(1, "foo"); }
@Test public void immutableSortedSet() { ImmutableSortedSet<Integer> set = dbRule.getSharedHandle().createQuery("select intValue from something") .collectInto(new GenericType<ImmutableSortedSet<Integer>>(){}); assertThat(set).containsExactlyElementsOf(expected); }
@Before public void setUp() throws Exception { assumeFalse(BindDao.class.getMethod("getByIdImplicitBindPositional", int.class).getParameters()[0].isNamePresent()); h = dbRule.getSharedHandle(); h.registerRowMapper(BeanMapper.factory(Something.class)); h.execute("insert into something (id, name) values (1, 'Elsie Hughes')"); }
@Test public void testDefineStrings() { assertThat( db.getSharedHandle().createQuery("select <a> from values(:a) union all select <b> from values(:b)") .defineNamedBindings() .bindBean(new DefinedBean()) .mapTo(boolean.class) .list()) .isEqualTo(Arrays.asList(true, false)); }
@Test public void optionalPresent() { Optional<Integer> shouldBePresent = dbRule.getSharedHandle().createQuery("select intValue from something where intValue = 1") .collectInto(new GenericType<Optional<Integer>>(){}); assertThat(shouldBePresent).contains(1); }
@Test public void testNestedHalfPresent() { Handle handle = dbRule.getSharedHandle(); assertThat(handle .registerRowMapper(FieldMapper.factory(NullableNestedThing.class)) .select("SELECT 42 as testValue, '3' as s") .mapTo(NullableNestedThing.class) .findOnly()) .extracting("testValue", "nested.i", "nested.s") .containsExactly(42, null, "3"); }
@Test public void nestedParameters() { assertThat(dbRule.getSharedHandle() .registerRowMapper(ConstructorMapper.factory(NestedBean.class)) .select("select s, i from bean") .mapTo(NestedBean.class) .findOnly()) .extracting("nested.s", "nested.i") .containsExactly("3", 2); }
@Before public void setUp() { handle = dbRule.getSharedHandle(); handle.execute("create table thing (id identity primary key, foo varchar(50), bar varchar(50), baz varchar(50))"); handle.execute("insert into thing (id, foo, bar, baz) values (?, ?, ?, ?)", 1, "foo1", "bar1", "baz1"); handle.execute("insert into thing (id, foo, bar, baz) values (?, ?, ?, ?)", 2, "foo2", "bar2", "baz2"); handle.registerRowMapper(FieldMapper.factory(Thing.class)); }
@Before public void setUp() { Handle h = dbRule.getSharedHandle(); h.execute("CREATE TABLE something_location (id int, location varchar)"); h.execute("INSERT INTO something (id, name) VALUES (1, 'tree')"); h.execute("INSERT INTO something (id, name) VALUES (2, 'apple')"); h.execute("INSERT INTO something_location (id, location) VALUES (1, 'outside')"); h.execute("INSERT INTO something_location (id, location) VALUES (2, 'tree')"); h.execute("INSERT INTO something_location (id, location) VALUES (2, 'pie')"); h.registerRowMapper(new SomethingMapper()); }
@Test public void testEntityNoColumnAnnotations() { EntityThing brian = new EntityThing(1, "Brian"); EntityThing keith = new EntityThing(2, "Keith"); EntityThingDao dao = dbRule.getSharedHandle().attach(EntityThingDao.class); dao.insert(brian); dao.insert(keith); List<EntityThing> rs = dao.list(); assertThatThing(rs).containsOnlyOnce(brian, keith); }
@Test public void testField() { FieldThing brian = new FieldThing(1, "Brian"); FieldThing keith = new FieldThing(2, "Keith"); FieldThingDao dao = dbRule.getSharedHandle().attach(FieldThingDao.class); dao.insert(brian); dao.insert(keith); List<FieldThing> rs = dao.list(); assertThatThing(rs).containsOnlyOnce(brian, keith); }
@Test public void testAnnotationPriority() { // fields before getters before setters AnnotationPriorityThing brian = new AnnotationPriorityThing(1, "Brian"); AnnotationPriorityThing keith = new AnnotationPriorityThing(2, "Keith"); AnnotationPriorityThingDao dao = dbRule.getSharedHandle().attach(AnnotationPriorityThingDao.class); dao.insert(brian); dao.insert(keith); List<AnnotationPriorityThing> rs = dao.list(); assertThatThing(rs).containsOnlyOnce(brian, keith); }
@Test public void subclassAnnotationOverridesSuperclass() { // Annotated takes precedence over no annotation, even if annotated in superclass // Annotated member in subclass takes precedence over annotated member in superclass OverridingSubclassThing brian = new OverridingSubclassThing(1, "Brian"); OverridingSubclassThing keith = new OverridingSubclassThing(2, "Keith"); OverridingSubclassThingDao dao = dbRule.getSharedHandle().attach(OverridingSubclassThingDao.class); dao.insert(brian); dao.insert(keith); List<OverridingSubclassThing> rs = dao.list(); assertThatThing(rs).containsOnlyOnce(brian, keith); }