use of org.jdbi.v3.core.Jdbi in project tutorials by eugenp.
the class JdbiTest method whenNoResults_thenFindOnlyThrows.
@Test
public void whenNoResults_thenFindOnlyThrows() {
Jdbi jdbi = Jdbi.create("jdbc:hsqldb:mem:testDB", "sa", "");
jdbi.useHandle(handle -> {
handle.execute("create table PROJECT_8 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))");
try {
handle.createQuery("select * from project_8").mapToMap().findOnly();
fail("Exception expected");
} catch (Exception e) {
e.printStackTrace();
}
});
}
use of org.jdbi.v3.core.Jdbi in project tutorials by eugenp.
the class JdbiTest method whenException_thenTransactionIsRolledBack.
@Test
public void whenException_thenTransactionIsRolledBack() {
Jdbi jdbi = Jdbi.create("jdbc:hsqldb:mem:testDB", "sa", "");
jdbi.useHandle(handle -> {
try {
handle.useTransaction(h -> {
h.execute("create table PROJECT_14 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))");
h.execute("INSERT INTO PROJECT_14 (NAME, URL) VALUES ('tutorials', 'https://github.com/eugenp/tutorials')");
List<Map<String, Object>> list = handle.select("SELECT * FROM PROJECT_14").mapToMap().list();
assertTrue(h.isInTransaction());
assertEquals(1, list.size());
throw new Exception("rollback");
});
} catch (Exception ignored) {
}
List<Map<String, Object>> list = handle.select("SELECT * FROM PROJECT_14").mapToMap().list();
assertFalse(handle.isInTransaction());
assertEquals(0, list.size());
});
}
use of org.jdbi.v3.core.Jdbi in project tutorials by eugenp.
the class JdbiTest method whenNoResults_thenFindFirstReturnsNone.
@Test
public void whenNoResults_thenFindFirstReturnsNone() {
Jdbi jdbi = Jdbi.create("jdbc:hsqldb:mem:testDB", "sa", "");
jdbi.useHandle(handle -> {
handle.execute("create table PROJECT_6 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))");
assertFalse(handle.createQuery("select * from project_6").mapToMap().findFirst().isPresent());
});
}
use of org.jdbi.v3.core.Jdbi in project tutorials by eugenp.
the class JdbiTest method whenJdbiCreated_thenSuccess.
@Test
public void whenJdbiCreated_thenSuccess() {
Jdbi jdbi = Jdbi.create("jdbc:hsqldb:mem:testDB", "sa", "");
Jdbi.create("WRONG");
}
use of org.jdbi.v3.core.Jdbi in project tutorials by eugenp.
the class JdbiTest method whenHandle_thenBoh.
@Test
public void whenHandle_thenBoh() {
Jdbi jdbi = Jdbi.create("jdbc:hsqldb:mem:testDB", "sa", "");
final Handle[] handleRef = new Handle[1];
boolean closed = jdbi.withHandle(handle -> {
handleRef[0] = handle;
return handle.isClosed();
});
assertFalse(closed);
assertTrue(handleRef[0].isClosed());
}
Aggregations