Search in sources :

Example 1 with SourceH2

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")));
}
Also used : SourceH2(com.github.fabriciofx.cactoos.jdbc.source.SourceH2) StatementUpdate(com.github.fabriciofx.cactoos.jdbc.statement.StatementUpdate) QuerySimple(com.github.fabriciofx.cactoos.jdbc.query.QuerySimple) Logger(java.util.logging.Logger) SessionNoAuth(com.github.fabriciofx.cactoos.jdbc.session.SessionNoAuth) Logged(com.github.fabriciofx.cactoos.jdbc.session.Logged) Session(com.github.fabriciofx.cactoos.jdbc.Session) Test(org.junit.Test)

Example 2 with SourceH2

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));
}
Also used : PhonebookSql(com.github.fabriciofx.cactoos.jdbc.phonebook.sql.PhonebookSql) Contact(com.github.fabriciofx.cactoos.jdbc.phonebook.Contact) SourceH2(com.github.fabriciofx.cactoos.jdbc.source.SourceH2) ScriptSql(com.github.fabriciofx.cactoos.jdbc.script.ScriptSql) Phonebook(com.github.fabriciofx.cactoos.jdbc.phonebook.Phonebook) ResourceOf(org.cactoos.io.ResourceOf) MapOf(org.cactoos.map.MapOf) Transacted(com.github.fabriciofx.cactoos.jdbc.session.Transacted) SessionNoAuth(com.github.fabriciofx.cactoos.jdbc.session.SessionNoAuth) Test(org.junit.Test)

Example 3 with SourceH2

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']"));
}
Also used : MapEntry(org.cactoos.map.MapEntry) PhonebookSql(com.github.fabriciofx.cactoos.jdbc.phonebook.sql.PhonebookSql) Contact(com.github.fabriciofx.cactoos.jdbc.phonebook.Contact) SourceH2(com.github.fabriciofx.cactoos.jdbc.source.SourceH2) ScriptSql(com.github.fabriciofx.cactoos.jdbc.script.ScriptSql) Phonebook(com.github.fabriciofx.cactoos.jdbc.phonebook.Phonebook) ResourceOf(org.cactoos.io.ResourceOf) Transacted(com.github.fabriciofx.cactoos.jdbc.session.Transacted) SessionNoAuth(com.github.fabriciofx.cactoos.jdbc.session.SessionNoAuth) Test(org.junit.Test)

Aggregations

SessionNoAuth (com.github.fabriciofx.cactoos.jdbc.session.SessionNoAuth)3 SourceH2 (com.github.fabriciofx.cactoos.jdbc.source.SourceH2)3 Test (org.junit.Test)3 Contact (com.github.fabriciofx.cactoos.jdbc.phonebook.Contact)2 Phonebook (com.github.fabriciofx.cactoos.jdbc.phonebook.Phonebook)2 PhonebookSql (com.github.fabriciofx.cactoos.jdbc.phonebook.sql.PhonebookSql)2 ScriptSql (com.github.fabriciofx.cactoos.jdbc.script.ScriptSql)2 Transacted (com.github.fabriciofx.cactoos.jdbc.session.Transacted)2 ResourceOf (org.cactoos.io.ResourceOf)2 Session (com.github.fabriciofx.cactoos.jdbc.Session)1 QuerySimple (com.github.fabriciofx.cactoos.jdbc.query.QuerySimple)1 Logged (com.github.fabriciofx.cactoos.jdbc.session.Logged)1 StatementUpdate (com.github.fabriciofx.cactoos.jdbc.statement.StatementUpdate)1 Logger (java.util.logging.Logger)1 MapEntry (org.cactoos.map.MapEntry)1 MapOf (org.cactoos.map.MapOf)1