1. About

datasource-assert provides assertion API for DataSource to validate query executions.

The assertion API is used for assertion methods such as assertEquals in JUnit and TestNG. Also, it comes with support for assertThat in AssertJ and Hamcrest.

Github project: datasource-assert

2. Installation

2.1. Dependency

<dependency>
  <groupId>net.ttddyy</groupId>
  <artifactId>datasource-assert</artifactId>
  <version>[LATEST_VERSION]</version>
</dependency>

datasource-proxy is the only dependent library.
AssertJ and Hamcrest are optional. To use those assertion libraries, explicit dependency needs to be specified in your project.

2.1.1. Snapshot (via Sonatype OSSRH)

Snapshot is available via oss sonatype snapshot repository.

To download snapshot jars, enable sonatype snapshot repository:

<repositories>
  <repository>
    <id>sonatype-snapshots-repo</id>
    <url>https://oss.sonatype.org/content/repositories/snapshots</url>
    <releases>
      <enabled>false</enabled>
    </releases>
    <snapshots>
      <enabled>true</enabled>
    </snapshots>
  </repository>
</repositories>

2.1.2. Snapshot (via JitPack)

<repositories>
    <repository>
        <id>jitpack.io</id>
        <url>https://jitpack.io</url>
    </repository>
</repositories>
<dependency>
    <groupId>om.github.ttddyy</groupId>
    <artifactId>datasource-assert</artifactId>
    <version>master-SNAPSHOT</version>
</dependency>

2.2. Supported Java Versions

datasource-assert works with java 1.7+.

3. How to use

datasource-assert provides ProxyTestDataSource class that wraps your actual DataSource and provides assertion APIs.

// Create a DataSource for test
ProxyTestDataSource ds = new ProxyTestDataSource(actualDataSource);

// perform application logic using above "ds" as its DataSource

// verify query interaction with assertEquals, AssertJ, Hamcrest
assertEquals("num of executed queries", 3, ds.getQueryExecutions().size());
assertThat(ds.getFirstStatement(), query(startsWith("SELECT * FROM")));  // with Hamcrest
assertThat(ds.getFirstStatement()).hasQueryType(QueryType.SELECT);  // with AssertJ

3.1. With AssertJ

For AssertJ, DataSourceAssertAssertions defines all assertThat methods and entry point methods.
To use them, static import those methods.

import static net.ttddyy.dsproxy.asserts.assertj.DataSourceAssertAssertions.assertThat;

3.2. With Hamcrest

For Hamcrest, all matchers are defined in DataSourceAssertMatchers class.

4. Assert Query Executions

4.1. assertEquals

This section describes how to assert ProxyTestDataSource with simple assertEquals from JUnit or TestNG.

All assertion examples below use JUnit4 assertEquals.

4.1.1. DataSource

ProxyTestDataSource ds = new ProxyTestDataSource(actualDataSource);

// ... perform application logic with database ...

// execution count
assertEquals(3, ds.getQueryExecutions().size());
assertEquals(3, ds.getStatements().size());
assertEquals(3, ds.getBatchStatements().size());
assertEquals(3, ds.getPrepareds().size());
assertEquals(3, ds.getBatchPrepareds().size());
assertEquals(3, ds.getCallables().size());
assertEquals(3, ds.getBatchCallables().size());

4.1.2. Query Executions

ProxyTestDataSource ds = new ProxyTestDataSource(actualDataSource);

// ... perform application logic with database ...

// each execution
QueryExecution qe = ds.getQueryExecutions().get(0);
assertTrue(qe.isSuccess());
assertTrue(qe.isBatch());
assertTrue("is statement", qe instanceof StatementExecution);
assertTrue("is batch statement", qe instanceof StatementBatchExecution);
assertTrue("is prepared", qe instanceof PreparedExecution);
assertTrue("is batch prepared", qe instanceof PreparedExecution);
assertTrue("is callable", qe instanceof CallableExecution);
assertTrue("is batch callable", qe instanceof CallableBatchExecution);

4.1.3. Statement

ProxyTestDataSource ds = new ProxyTestDataSource(actualDataSource);

// ... perform application logic with database ...

StatementExecution se = ds.getFirstStatement();

assertTrue(se.isSuccess());
assertFalse(se.isSuccess());

assertEquals("FOO", se.getQuery());

4.1.4. Batch Statement

ProxyTestDataSource ds = new ProxyTestDataSource(actualDataSource);

// ... perform application logic with database ...

StatementBatchExecution sbe = ds.getFirstBatchStatement();

assertTrue(sbe.isSuccess());
assertFalse(sbe.isSuccess());

List<String> queries = sbe.getQueries();
assertEquals(3, queries.size());
assertEquals("FOO", queries.get(0));

4.1.5. Prepared Statement

ProxyTestDataSource ds = new ProxyTestDataSource(actualDataSource);

// ... perform application logic with database ...

PreparedExecution pe = ds.getFirstPrepared();

assertTrue(pe.isSuccess());
assertFalse(pe.isSuccess());

assertEquals("FOO", pe.getQuery());

// parameter indexes
List<Integer> indexes = pe.getParamIndexes();
assertEquals(2, indexes.size());
assertEquals(Integer.valueOf(1), indexes.get(0));
assertEquals(Integer.valueOf(2), indexes.get(1));

// parameters
Map<Integer, Object> paramsByIndex = pe.getSetParamsByIndex();
assertEquals(100, paramsByIndex.get(1));
assertEquals("FOO", paramsByIndex.get(2));

// setNull parameters
Map<Integer, Integer> setNullByIndex = pe.getSetNullParamsByIndex();
assertEquals(Integer.valueOf(Types.VARCHAR), setNullByIndex.get(1));
assertTrue(setNullByIndex.containsKey(2));

4.1.6. Batch Prepared Statement

ProxyTestDataSource ds = new ProxyTestDataSource(actualDataSource);

// ... perform application logic with database ...

PreparedBatchExecution pbe = ds.getFirstBatchPrepared();

assertTrue(pbe.isSuccess());
assertFalse(pbe.isSuccess());

assertEquals("FOO", pbe.getQuery());

// check batch executions
List<BatchExecutionEntry> batchEntries = pbe.getBatchExecutionEntries();
assertEquals(3, batchEntries.size());

BatchExecutionEntry entry = batchEntries.get(0);
assertTrue(entry instanceof PreparedBatchExecutionEntry);

PreparedBatchExecutionEntry preparedBatchEntry = (PreparedBatchExecutionEntry) entry;

// parameter indexes
List<Integer> indexes = preparedBatchEntry.getParamIndexes();
assertEquals(3, indexes.size());
assertEquals(Integer.valueOf(10), indexes.get(0));

// parameters
Map<Integer, Object> paramsByIndex = preparedBatchEntry.getSetParamsByIndex();
assertEquals(100, paramsByIndex.get(1));
assertEquals("FOO", paramsByIndex.get(2));

// setNull parameters
Map<Integer, Integer> setNullByIndex = preparedBatchEntry.getSetNullParamsByIndex();
assertEquals(Integer.valueOf(Types.VARCHAR), setNullByIndex.get(1));
assertTrue(setNullByIndex.containsKey(2));

4.1.7. Callable Statement

ProxyTestDataSource ds = new ProxyTestDataSource(actualDataSource);

// ... perform application logic with database ...

CallableExecution ce = ds.getFirstCallable();

assertTrue(ce.isSuccess());
assertFalse(ce.isSuccess());

assertEquals("FOO", ce.getQuery());

// parameter names/indexes
List<Integer> indexes = ce.getParamIndexes();
assertEquals(2, indexes.size());
assertEquals(Integer.valueOf(1), indexes.get(0));
assertEquals(Integer.valueOf(2), indexes.get(1));

List<String> names = ce.getParamNames();
assertEquals(2, names.size());
assertEquals("key1", names.get(0));
assertEquals("key2", names.get(1));


// registerOut names/indexes
List<Integer> outIndexes = ce.getOutParamIndexes();
assertEquals(1, outIndexes.size());
assertEquals(Integer.valueOf(1), outIndexes.get(0));

List<String> outNames = ce.getOutParamNames();
assertEquals(1, outNames.size());
assertEquals("key1", outNames.get(0));


// parameters
Map<Integer, Object> paramsByIndex = ce.getSetParamsByIndex();
assertEquals(100, paramsByIndex.get(1));
assertEquals("FOO", paramsByIndex.get(2));

Map<String, Object> paramsByName = ce.getSetParamsByName();
assertEquals(100, paramsByName.get("key1"));
assertEquals("FOO", paramsByName.get("key2"));


// setNull parameters
Map<Integer, Integer> setNullByIndex = ce.getSetNullParamsByIndex();
assertEquals(Integer.valueOf(Types.VARCHAR), setNullByIndex.get(1));
assertTrue(setNullByIndex.containsKey(2));

Map<String, Integer> setNullByName = ce.getSetNullParamsByName();
assertEquals(Integer.valueOf(Types.VARCHAR), setNullByName.get("key1"));
assertTrue(setNullByName.containsKey("key2"));


// registerOut parameters
Map<Integer, Object> outParamsByIndex = ce.getOutParamsByIndex();
assertEquals(Types.INTEGER, outParamsByIndex.get(0));

Map<String, Object> outParamsByName = ce.getOutParamsByName();
assertEquals(JDBCType.INTEGER, outParamsByName.get("key"));

4.1.8. Batch Callable Statement

ProxyTestDataSource ds = new ProxyTestDataSource(actualDataSource);

// ... perform application logic with database ...

CallableBatchExecution cbe = ds.getFirstBatchCallable();

assertTrue(cbe.isSuccess());
assertFalse(cbe.isSuccess());

assertEquals("FOO", cbe.getQuery());


// check batch executions
List<BatchExecutionEntry> batchEntries = cbe.getBatchExecutionEntries();
assertEquals(3, batchEntries.size());

BatchExecutionEntry entry = batchEntries.get(0);
assertTrue(entry instanceof CallableBatchExecutionEntry);

CallableBatchExecutionEntry callableBatchEntry = (CallableBatchExecutionEntry) entry;


// parameter names/indexes
List<String> names = callableBatchEntry.getParamNames();
assertEquals(3, names.size());
assertEquals("foo", names.get(0));

List<Integer> indexes = callableBatchEntry.getParamIndexes();
assertEquals(3, indexes.size());
assertEquals(Integer.valueOf(10), indexes.get(0));


// registerOut names/indexes
List<Integer> outIndexes = callableBatchEntry.getOutParamIndexes();
assertEquals(1, outIndexes.size());
assertEquals(Integer.valueOf(1), outIndexes.get(0));

List<String> outNames = callableBatchEntry.getOutParamNames();
assertEquals(1, outNames.size());
assertEquals("key1", outNames.get(0));


// parameters
Map<String, Object> paramsByName = callableBatchEntry.getSetParamsByName();
assertEquals(100, paramsByName.get("key1"));
assertEquals("FOO", paramsByName.get("key2"));

Map<Integer, Object> paramsByIndex = callableBatchEntry.getSetParamsByIndex();
assertEquals(100, paramsByIndex.get(1));
assertEquals("FOO", paramsByIndex.get(2));


// setNull parameters
Map<String, Integer> setNullByName = callableBatchEntry.getSetNullParamsByName();
assertEquals(Integer.valueOf(Types.VARCHAR), setNullByName.get("key1"));
assertTrue(setNullByName.containsKey("key2"));

Map<Integer, Integer> setNullByIndex = callableBatchEntry.getSetNullParamsByIndex();
assertEquals(Integer.valueOf(Types.VARCHAR), setNullByIndex.get(1));
assertTrue(setNullByIndex.containsKey(2));


// registerOut parameters
Map<String, Object> outParamByName = callableBatchEntry.getOutParamsByName();
assertEquals(Types.VARCHAR, outParamByName.get("key1"));
assertEquals(JDBCType.INTEGER, outParamByName.get("key2"));

Map<Integer, Object> outParamByIndex = callableBatchEntry.getOutParamsByIndex();
assertEquals(Types.VARCHAR, outParamByIndex.get(1));
assertEquals(JDBCType.INTEGER, outParamByIndex.get(2));

4.2. AssertJ

This section describes how to assert ProxyTestDataSource with AssertJ.

4.2.1. DataSource

ProxyTestDataSource ds = new ProxyTestDataSource(actualDataSource);

// ... perform application logic with database ...

// check num of executions
assertThat(ds).hasExecutionCount(3);
assertThat(ds).hasStatementCount(3);
assertThat(ds).hasBatchStatementCount(3);
assertThat(ds).hasStatementOrBatchStatementCount(3);
assertThat(ds).hasPreparedCount(3);
assertThat(ds).hasBatchPreparedCount(3);
assertThat(ds).hasPreparedOrBatchPreparedCount(3);
assertThat(ds).hasCallableCount(3);
assertThat(ds).hasBatchCallableCount(3);
assertThat(ds).hasCallableOrBatchCallableCount(3);

// check num of queries by type
assertThat(ds).hasTotalQueryCount(3);
assertThat(ds).hasSelectCount(3);
assertThat(ds).hasInsertCount(3);
assertThat(ds).hasUpdateCount(3);
assertThat(ds).hasDeleteCount(3);
assertThat(ds).hasOtherCount(3);

// reset datasource
ds.reset();

4.2.2. Query Executions

ProxyTestDataSource ds = new ProxyTestDataSource(actualDataSource);

// ... perform application logic with database ...

QueryExecution qe = ds.getQueryExecutions().get(0);
assertThat(qe).isSuccess();
assertThat(qe).isFailure();

qe = ds.getFirstBatchPrepared();

assertThat(qe).isBatch();
assertThat(qe).isStatement();
assertThat(qe).isBatchStatement();
assertThat(qe).isStatementOrBatchStatement();
assertThat(qe).isPrepared();
assertThat(qe).isBatchPrepared();
assertThat(qe).isPreparedOrBatchPrepared();
assertThat(qe).isCallable();
assertThat(qe).isBatchCallable();
assertThat(qe).isCallableOrBatchCallable();

assertThat(qe).asStatement();       // follows StatementExecutionAssert
assertThat(qe).asBatchStatement();  // follows StatementBatchExecutionAssert
assertThat(qe).asPrepared();        // follows PreparedExecutionAssert
assertThat(qe).asBatchPrepared();   // follows PreparedBatchExecutionAssert
assertThat(qe).asCallable();        // follows CallableExecutionAssert
assertThat(qe).asBatchCallable();   // follows CallableBatchExecutionAssert

assertThat(ds.getFirstBatchStatement()).queries().contains("ABC", atIndex(3));
assertThat(ds.getFirstBatchStatement().getQueries().get(3)).startsWith("SELECT ");

assertThat(ds.getQueryExecutions().get(0)).isStatement().asStatement().query().startsWith("SELECT");
assertThat(qe).asPrepared().containsParam(1, "value").containsNullParam(1, Types.INTEGER);

4.2.3. Statement

ProxyTestDataSource ds = new ProxyTestDataSource(actualDataSource);

// ... perform application logic with database ...

StatementExecution se = ds.getFirstStatement();

assertThat(se).isSuccess();
assertThat(se).isFailure();

assertThat(se.getQuery()).isEqualTo("...");  // string assertion
assertThat(se).hasQueryType(QueryType.SELECT);

assertThat(ds.getQueryExecutions().get(0)).isStatement().asStatement().query().startsWith("SELECT");

4.2.4. Batch Statement

ProxyTestDataSource ds = new ProxyTestDataSource(actualDataSource);

// ... perform application logic with database ...

StatementBatchExecution sbe = ds.getFirstBatchStatement();

assertThat(sbe).isSuccess();
assertThat(sbe).isFailure();

assertThat(sbe).hasBatchSize(3);

// check batch queries
assertThat(sbe.getQueries().get(0)).isEqualTo("...");  // string assertion
assertThat(sbe).query(atIndex(0)).isEqualTo("...");    // string assertion
assertThat(sbe).query(0).isEqualTo("...");       // string assertion
assertThat(sbe).queries().contains("...");  // list assertion

assertThat(sbe).contains(QueryType.SELECT, atIndex(0));  // list assertion
assertThat(sbe).contains(QueryType.SELECT, 0);    // list assertion

assertThat(ds.getQueryExecutions().get(0)).isBatchStatement().asBatchStatement().queries().hasSize(3);

4.2.5. Prepared Statement

ProxyTestDataSource ds = new ProxyTestDataSource(actualDataSource);

// ... perform application logic with database ...

PreparedExecution pe = ds.getFirstPrepared();

assertThat(pe).isSuccess();
assertThat(pe).isFailure();

assertThat(pe).containsParam(10, "value").containsParam(10, "value");
assertThat(pe).containsParams(param(10, "value"), param(10, "value"), nullParam(12));
assertThat(pe).containsParamIndex(10);
assertThat(pe).containsParamIndexes(10, 11);
assertThat(pe).containsParamValuesExactly("value", 100, null, 12);

assertThat(pe).query().isEqualTo("...");     // string assertion
assertThat(pe.getQuery()).isEqualTo("...");  // string assertion

assertThat(ds.getQueryExecutions().get(0)).isPrepared().asPrepared().query().startsWith("SELECT");

4.2.6. Batch Prepared Statement

ProxyTestDataSource ds = new ProxyTestDataSource(actualDataSource);

// ... perform application logic with database ...

PreparedBatchExecution pbe = ds.getFirstBatchPrepared();

assertThat(pbe).isSuccess();
assertThat(pbe).isFailure();

assertThat(pbe).hasBatchSize(3);

assertThat(pbe).batch(0, containsParamIndexes(1, 2, 3, 4));
assertThat(pbe).batch(0, containsParams(
        param(1, "value"), param(2, 100),
        nullParam(3), nullParam(4, Types.VARCHAR))
);
assertThat(pbe).batch(0, containsParamsExactly(
        param(1, "value"), param(2, 100),
        nullParam(3), nullParam(4, Types.VARCHAR))
);

// assertThat(pbe).batch(0, containsParamValuesExactly("value", 100, null, 12));  // TODO: for values??

assertThat(pbe).query().isEqualTo("...");     // string assertion
assertThat(pbe.getQuery()).isEqualTo("...");  // string assertion

assertThat(ds.getQueryExecutions().get(0)).isBatchPrepared().asBatchPrepared().query().startsWith("SELECT");

4.2.7. Callable Statement

ProxyTestDataSource ds = new ProxyTestDataSource(actualDataSource);

// ... perform application logic with database ...

CallableExecution ce = ds.getFirstCallable();

assertThat(ce).isSuccess();
assertThat(ce).isFailure();

assertThat(ce).query().isEqualTo("...");     // string assertion
assertThat(ce.getQuery()).isEqualTo("...");  // string assertion

assertThat(ce).containsParam("key", "value")
        .containsParam(1, "value")
        .containsNullParam(2)
        .containsNullParam(3, Types.INTEGER)
        .containsOutParam(4, Types.INTEGER)
;

assertThat(ce).containsParamIndex(1)
        .containsParamName("foo")
        .containsParamKey(2)
        .containsParamKey("bar")
;

assertThat(ce).containsParamIndexes(1, 2)
        .containsParamNames("foo", "bar")
        .containsParamKeys("baz", 3)
;

assertThat(ce).containsParams(
        param("key", "value"),
        param(10, "value"),
        param("a", 100),
        outParam("key", Types.INTEGER),
        nullParam("key"),
        nullParam("key", Types.INTEGER))
;

assertThat(ce).containsParamsExactly(
        param("key", "value"),
        param(10, "value"),
        param("a", 100),
        outParam("key", Types.INTEGER),
        nullParam("key"));

assertThat(ce).containsParamKeys("key").containsParamKeys(10).containsParamIndex(11).containsParamName("key");

assertThat(ce).containsParamKeys("key", 10).containsParamIndexes(10, 11).containsParamNames("key1", "key2");

//  assertThat(ce).containsOutParams(param("key", "value"), paramAsString("key", "value"));
//  assertThat(ce).containsNullParams(param("key", "value"), paramAsString("key", "value"));

assertThat(ds.getQueryExecutions().get(0)).asCallable().query().startsWith("SELECT");

4.2.8. Batch Callable Statement

ProxyTestDataSource ds = new ProxyTestDataSource(actualDataSource);

// ... perform application logic with database ...

CallableBatchExecution cbe = ds.getFirstBatchCallable();

assertThat(cbe).isSuccess();
assertThat(cbe).isFailure();

assertThat(cbe).hasBatchSize(3);

assertThat(cbe).batch(0, containsParams(
        param("key", "value"), param("key", "value")
));

assertThat(cbe).batch(0, containsParams(
        param("key", "value"), param(10, "value"),
        param("a", 100),
        outParam("key", Types.INTEGER),
        nullParam("key")
));

assertThat(cbe).batch(0, containsParams(
        param("key", "value"), param(10, "value")
));

assertThat(cbe).batch(0, containsParams(
        outParam("key", Types.INTEGER), outParam(10, Types.INTEGER)
));

assertThat(cbe).batch(0, containsParams(
        nullParam("key", Types.INTEGER), nullParam(10))
);

assertThat(cbe).batch(0, containsParamKeys("key", 10));
assertThat(cbe).batch(0, containsParamIndexes(10, 11));
assertThat(cbe).batch(0, containsParamNames("key", "key2"));

assertThat(cbe.getQuery()).isEqualTo("...");  // string assertion

assertThat(ds.getQueryExecutions().get(0)).isBatchCallable().asBatchCallable().query().startsWith("SELECT");

4.3. Hamcrest

This section describes how to assert ProxyTestDataSource with Hamcrest.

4.3.1. DataSource

ProxyTestDataSource ds = new ProxyTestDataSource(actualDataSource);

// ... perform application logic with database ...

// execution count
assertThat(ds, executionCount(3));
assertThat(ds, statementCount(3));
assertThat(ds, batchStatementCount(3));
assertThat(ds, statementOrBatchStatementCount(3));
assertThat(ds, preparedCount(3));
assertThat(ds, batchPreparedCount(3));
assertThat(ds, preparedOrBatchPreparedCount(3));
assertThat(ds, callableCount(3));
assertThat(ds, batchCallableCount(3));
assertThat(ds, callableOrBatchCallableCount(3));

// each execution
assertThat(ds, executions(0, isBatch()));
assertThat(ds, executions(0, batch()));
assertThat(ds, executions(0, is(batch())));

assertThat(ds, executions(0, isStatement()));
assertThat(ds, executions(0, statement()));
assertThat(ds, executions(0, is(isStatement())));

assertThat(ds, executions(0, isBatchStatement()));
assertThat(ds, executions(0, batchStatement()));
assertThat(ds, executions(0, is(batchStatement())));

assertThat(ds, executions(0, isStatementOrBatchStatement()));
assertThat(ds, executions(0, isPrepared()));
assertThat(ds, executions(0, isBatchPrepared()));
assertThat(ds, executions(0, isPreparedOrBatchPrepared()));
assertThat(ds, executions(0, isCallable()));
assertThat(ds, executions(0, isBatchCallable()));
assertThat(ds, executions(0, isCallableOrBatchCallable()));

assertThat(ds, executions(0, is(success())));

// query count
assertThat(ds, totalQueryCount(5));
assertThat(ds, selectCount(3));
assertThat(ds, insertCount(3));
assertThat(ds, otherCount(3));
assertThat(ds, allOf(updateCount(3), deleteCount(1)));

ds.reset();

assertThat(ds.getFirstStatement(), query(is("abc")));
assertThat(ds.getFirstBatchStatement(), queries(0, is("abc")));
assertThat(ds.getFirstBatchCallable(), query(is("abc")));

4.3.2. Query Executions

ProxyTestDataSource ds = new ProxyTestDataSource(actualDataSource);

// ... perform application logic with database ...

QueryExecution qe = ds.getFirstStatement();

assertThat(qe, success());
assertThat(qe, failure());

// type of execution
assertThat(qe, isBatch());
assertThat(qe, statement());
assertThat(qe, batchStatement());
assertThat(qe, statementOrBatchStatement());
assertThat(qe, prepared());
assertThat(qe, batchPrepared());
assertThat(qe, preparedOrBatchPrepared());
assertThat(qe, callable());
assertThat(qe, batchCallable());
assertThat(qe, callableOrBatchCallable());

4.3.3. Statement

ProxyTestDataSource ds = new ProxyTestDataSource(actualDataSource);

// ... perform application logic with database ...

StatementExecution se = ds.getFirstStatement();

assertThat(se, success());
assertThat(se, failure());

// query with StringMatcher
assertThat(se, query(is("...")));
assertThat(se, query(startsWith("...")));

assertThat(se, queryType(QueryType.SELECT));

4.3.4. Batch Statement

ProxyTestDataSource ds = new ProxyTestDataSource(actualDataSource);

// ... perform application logic with database ...

StatementBatchExecution sbe = ds.getFirstBatchStatement();

assertThat(sbe, success());
assertThat(sbe, failure());

assertThat(sbe, queries(0, is("...")));   // string matcher
assertThat(sbe, queries(hasItems("...", "...")));  // collection matcher
assertThat(sbe, queryTypes(0, is(select())));
assertThat(sbe, queryTypes(0, either(insert()).or(update())));

4.3.5. Prepared Statement

ProxyTestDataSource ds = new ProxyTestDataSource(actualDataSource);

// ... perform application logic with database ...

PreparedExecution pe = ds.getFirstPrepared();

assertThat(pe, success());
assertThat(pe, failure());

assertThat(pe, query(is("FOO")));  // string matcher

// parameter indexes
assertThat(pe, paramIndexes(1, 2, 3));
assertThat(pe, paramIndexes(hasItem(1)));  // integer collection matcher

// parameters
assertThat(pe, param(1, Integer.class, is(100)));
assertThat(pe, param(1, is((Object) 100)));  // Object matcher
assertThat(pe, paramAsInteger(1, is(100)));
assertThat(pe, paramAsLong(1, is(100L)));
assertThat(pe, paramAsDouble(1, is(100.0)));
assertThat(pe, paramAsShort(1, is((short) 1)));
assertThat(pe, paramAsBoolean(1, is(true)));
assertThat(pe, paramAsByte(1, is(new Byte("1"))));
assertThat(pe, paramAsFloat(1, is(Float.valueOf("1"))));
assertThat(pe, paramAsBigDecimal(1, is(BigDecimal.ONE)));
assertThat(pe, paramAsBytes(1, is("100".getBytes())));
assertThat(pe, paramAsDate(1, is(new Date(100))));
assertThat(pe, paramAsTime(1, is(new Time(1000))));
assertThat(pe, paramAsTimestamp(1, is(new Timestamp(1000))));
assertThat(pe, paramAsArray(1, is((Array) new Object())));
assertThat(pe, paramsByIndex(hasEntry(1, (Object) "FOO")));  // map matcher

// setNull parameters
assertThat(pe, nullParam(10, Types.INTEGER));
assertThat(pe, nullParam(10));

assertThat(pe, allOf(
        paramAsInteger(1, is(100)),
        paramAsInteger(2, is(200)),
        nullParam(3)));

4.3.6. Batch Prepared Statement

ProxyTestDataSource ds = new ProxyTestDataSource(actualDataSource);

// ... perform application logic with database ...

PreparedBatchExecution pbe = ds.getFirstBatchPrepared();

assertThat(pbe, success());
assertThat(pbe, failure());

assertThat(pbe, query(is("FOO")));

// check batch executions
assertThat(pbe, batchSize(10));

assertThat(pbe, batch(0, paramIndexes(1, 2, 3)));
assertThat(pbe, batch(0, paramIndexes(hasItem(11))));    // integer collection matcher

assertThat(pbe, batch(0, param(1, Integer.class, is(100))));
assertThat(pbe, batch(0, param(1, is((Object) 100))));  // Object matcher
assertThat(pbe, batch(0, paramAsInteger(1, is(100))));
assertThat(pbe, batch(0, paramsByIndex(hasEntry(11, (Object) "FOO"))));  // map matcher

// setNull parameters
assertThat(pbe, batch(0, nullParam(10, Types.INTEGER)));
assertThat(pbe, batch(0, nullParam(10)));

assertThat(pbe, batch(0, allOf(
        paramAsInteger(1, is(100)),
        paramAsInteger(2, is(200)),
        nullParam(3))));

4.3.7. Callable Statement

ProxyTestDataSource ds = new ProxyTestDataSource(actualDataSource);

// ... perform application logic with database ...

CallableExecution ce = ds.getFirstCallable();

assertThat(ce, success());
assertThat(ce, failure());

assertThat(ce, query(is("FOO")));

// parameter names/indexes
assertThat(ce, paramNames("foo", "bar", "baz"));
assertThat(ce, paramNames(hasItem("foo")));  // string collection matcher
assertThat(ce, paramIndexes(1, 2, 3));
assertThat(ce, paramIndexes(hasItem(11)));    // integer collection matcher

// parameters with map matcher
assertThat(ce, paramsByName(hasEntry("foo", (Object) "FOO")));
assertThat(ce, paramsByIndex(hasEntry(1, (Object) "FOO")));

// parameters
assertThat(ce, param("foo", is((Object) 100)));
assertThat(ce, param("foo", Integer.class, is(100)));
assertThat(ce, paramAsInteger("foo", is(100)));
assertThat(ce, paramAsLong("foo", is(100L)));
assertThat(ce, paramAsDouble("foo", is(100.0)));
assertThat(ce, paramAsShort("foo", is((short) 1)));
assertThat(ce, paramAsBoolean("foo", is(true)));
assertThat(ce, paramAsByte("foo", is(new Byte("1"))));
assertThat(ce, paramAsFloat("foo", is(Float.valueOf("1"))));
assertThat(ce, paramAsBigDecimal("foo", is(BigDecimal.ONE)));
assertThat(ce, paramAsBytes("foo", is("100".getBytes())));
assertThat(ce, paramAsDate("foo", is(new Date(100))));
assertThat(ce, paramAsTime("foo", is(new Time(1000))));
assertThat(ce, paramAsTimestamp("foo", is(new Timestamp(1000))));
assertThat(ce, paramAsArray("foo", is((Array) new Object())));
assertThat(ce, param("foo", is((Object) 100)));
assertThat(ce, param("foo", Integer.class, is(100)));
assertThat(ce, paramAsInteger(1, is(100)));

// setNull parameters
assertThat(ce, nullParam("bar"));
assertThat(ce, nullParam("bar", Types.INTEGER));
assertThat(ce, nullParam(2));
assertThat(ce, nullParam(2, Types.INTEGER));

assertThat(ce, allOf(
        paramAsInteger(1, is(100)),
        paramAsInteger("foo", is(100)),
        nullParam("bar")));

// registerOut parameters
assertThat(ce, outParamNames(hasItem("foo")));
assertThat(ce, outParamIndexes(hasItem(10)));
assertThat(ce, outParam("foo", Types.INTEGER));
assertThat(ce, outParam("foo", JDBCType.INTEGER));
assertThat(ce, outParam(10, Types.INTEGER));
assertThat(ce, outParam(10, JDBCType.INTEGER));
assertThat(ce, allOf(outParam("foo", JDBCType.INTEGER), outParam(10, Types.INTEGER)));

assertThat(ce, allOf(
        paramAsInteger(10, is(100)),
        paramAsInteger("foo", is(100)),
        outParam("bar", JDBCType.INTEGER)));

4.3.8. Batch Callable Statement

ProxyTestDataSource ds = new ProxyTestDataSource(actualDataSource);

// ... perform application logic with database ...

CallableBatchExecution cbe = ds.getFirstBatchCallable();

assertThat(cbe, success());
assertThat(cbe, failure());

assertThat(cbe, query(is("FOO")));

assertThat(cbe, batchSize(10));

// parameter names/indexes
assertThat(cbe, batch(0, paramNames("foo", "bar", "baz")));
assertThat(cbe, batch(0, paramNames(hasItem("foo"))));
assertThat(cbe, batch(0, paramIndexes(1, 2, 3)));
assertThat(cbe, batch(0, paramIndexes(hasItem(1))));

// parameters with map matcher
assertThat(cbe, batch(0, paramsByName(hasEntry("foo", (Object) "FOO"))));
assertThat(cbe, batch(0, paramsByIndex(hasEntry(1, (Object) "FOO"))));

// parameters
assertThat(cbe, batch(0, param("foo", is((Object) 100))));
assertThat(cbe, batch(0, param("foo", Integer.class, is(100))));
assertThat(cbe, batch(0, paramAsInteger("foo", is(100))));
assertThat(cbe, batch(0, param(1, is((Object) 100))));
assertThat(cbe, batch(0, param(1, Integer.class, is(100))));
assertThat(cbe, batch(0, paramAsInteger(1, is(100))));

// setNull parameters
assertThat(cbe, batch(0, nullParam("bar")));
assertThat(cbe, batch(0, nullParam("bar", Types.INTEGER)));
assertThat(cbe, batch(0, nullParam(2)));
assertThat(cbe, batch(0, nullParam(2, Types.INTEGER)));

assertThat(cbe, batch(0, allOf(
        paramAsInteger(1, is(100)),
        paramAsInteger("foo", is(100)),
        nullParam("bar"))));

// registerOut parameters
assertThat(cbe, batch(0, outParamNames(hasItem("foo"))));
assertThat(cbe, batch(0, outParamIndexes(hasItem(10))));
assertThat(cbe, batch(0, outParam("foo", Types.INTEGER)));
assertThat(cbe, batch(0, outParam("foo", JDBCType.INTEGER)));
assertThat(cbe, batch(0, outParam(10, Types.INTEGER)));
assertThat(cbe, batch(0, outParam(10, JDBCType.INTEGER)));
assertThat(cbe, batch(0, allOf(
        outParam("foo", JDBCType.INTEGER),
        outParam(10, Types.INTEGER))));

assertThat(cbe, batch(0, allOf(
        paramAsInteger("foo", is(100)),
        outParam("bar", Types.INTEGER),
        nullParam("baz"))));

5. Development

Source code repository: datasource-assert

5.1. Build Source

> ./mvnw install

5.2. Build Documentation

Generate index.html

> ./mvnw asciidoctor:process-asciidoc@output-html

Http preview

> ./mvnw asciidoctor:http@output-html