use of com.github.fabriciofx.cactoos.jdbc.source.SourceH2 in project cactoos-jdbc by fabriciofx.
the class LoggedTest method loggedUpdate.
@Test
public void loggedUpdate() throws Exception {
final Logger logger = new LoggerFake();
final String sql = "CREATE TABLE t012 (id INT AUTO_INCREMENT, name VARCHAR(50))";
final Session session = new Logged(new SessionNoAuth(new SourceH2("testdb")), "cactoos-jdbc", logger);
new StatementUpdate(session, new QuerySimple(sql)).result();
MatcherAssert.assertThat("Can't connection from cactoos-jdbc", logger.toString(), Matchers.allOf(Matchers.containsString("Connection[#0] has been opened with properties"), Matchers.containsString(String.format("PreparedStatement[#0] created using SQL '%s'", sql)), Matchers.containsString("PreparedStatement[#0] updated a source and returned '0' in"), Matchers.containsString("PreparedStatement[#0] closed"), Matchers.containsString("Connection[#0] closed")));
}
use of com.github.fabriciofx.cactoos.jdbc.source.SourceH2 in project cactoos-jdbc by fabriciofx.
the class StatementTransactionTest method rollback.
@Test
public void rollback() throws Exception {
final Transacted transacted = new Transacted(new SessionNoAuth(new SourceH2("unsafedb")));
new ScriptSql(new ResourceOf("com/github/fabriciofx/cactoos/jdbc/phonebook/phonebook-h2.sql")).run(transacted);
final Phonebook phonebook = new PhonebookSql(transacted);
final String name = "Frank Miller";
try {
new StatementTransaction<>(transacted, () -> {
final Contact contact = phonebook.contact(new MapOf<String, String>(new MapEntry<>("name", name)));
contact.phones().add(new MapOf<String, String>(new MapEntry<>("number", "99991234"), new MapEntry<>("carrier", "TIM")));
throw new IllegalStateException("Rollback");
}).result();
} catch (final IllegalStateException ex) {
}
MatcherAssert.assertThat("Can't perform a transaction rollback", StreamSupport.stream(phonebook.filter(name).spliterator(), false).count(), Matchers.equalTo(0L));
}
use of com.github.fabriciofx.cactoos.jdbc.source.SourceH2 in project cactoos-jdbc by fabriciofx.
the class StatementTransactionTest method commit.
@Test
public void commit() throws Exception {
final Transacted transacted = new Transacted(new SessionNoAuth(new SourceH2("safedb")));
new ScriptSql(new ResourceOf("com/github/fabriciofx/cactoos/jdbc/phonebook/phonebook-h2.sql")).run(transacted);
MatcherAssert.assertThat("Can't perform a transaction commit", XhtmlMatchers.xhtml(new ResultAsValue<>(new StatementTransaction<>(transacted, () -> {
final Phonebook phonebook = new PhonebookSql(transacted);
final Contact contact = phonebook.contact(new MapOf<String, String>(new MapEntry<>("name", "Albert Einstein")));
contact.phones().add(new MapOf<String, String>(new MapEntry<>("number", "99991234"), new MapEntry<>("carrier", "TIM")));
contact.phones().add(new MapOf<String, String>(new MapEntry<>("number", "98812564"), new MapEntry<>("carrier", "Oi")));
return contact.about();
})).value()), XhtmlMatchers.hasXPaths("/contact/name[text()='Albert Einstein']", "/contact/phones/phone/number[text()='99991234']", "/contact/phones/phone/carrier[text()='TIM']", "/contact/phones/phone/number[text()='98812564']", "/contact/phones/phone/carrier[text()='Oi']"));
}
Aggregations