[#2202] Add Mock JDBC objects for unit testing with jOOQ - Use
jOOQ-style getters, not JavaBeans style
This commit is contained in:
parent
4db89119b5
commit
1502c0f330
@ -470,11 +470,11 @@ public abstract class jOOQAbstractTest<
|
||||
public MockResult[] execute(MockExecuteContext context) throws SQLException {
|
||||
Executor executor = new Executor(c, getDialect());
|
||||
|
||||
if (context.isSingleBatch()) {
|
||||
Query query = executor.query(context.getSQL(), new Object[context.getBatchBindings()[0].length]);
|
||||
if (context.batchSingle()) {
|
||||
Query query = executor.query(context.sql(), new Object[context.batchBindings()[0].length]);
|
||||
int[] result =
|
||||
executor.batch(query)
|
||||
.bind(context.getBatchBindings())
|
||||
.bind(context.batchBindings())
|
||||
.execute();
|
||||
|
||||
MockResult[] r = new MockResult[result.length];
|
||||
@ -484,10 +484,10 @@ public abstract class jOOQAbstractTest<
|
||||
|
||||
return r;
|
||||
}
|
||||
else if (context.isMultiBatch()) {
|
||||
else if (context.batchMultiple()) {
|
||||
List<Query> queries = new ArrayList<Query>();
|
||||
|
||||
for (String sql : context.getBatchSQL()) {
|
||||
for (String sql : context.batchSQL()) {
|
||||
queries.add(executor.query(sql));
|
||||
}
|
||||
|
||||
@ -502,8 +502,8 @@ public abstract class jOOQAbstractTest<
|
||||
|
||||
return r;
|
||||
}
|
||||
else if (context.getSQL().toLowerCase().matches("(?s:\\W*(select|with).*)")) {
|
||||
List<Result<Record>> result = executor.fetchMany(context.getSQL(), context.getBindings());
|
||||
else if (context.sql().toLowerCase().matches("(?s:\\W*(select|with).*)")) {
|
||||
List<Result<Record>> result = executor.fetchMany(context.sql(), context.bindings());
|
||||
MockResult[] r = new MockResult[result.size()];
|
||||
|
||||
for (int i = 0; i < result.size(); i++) {
|
||||
@ -513,7 +513,7 @@ public abstract class jOOQAbstractTest<
|
||||
return r;
|
||||
}
|
||||
else {
|
||||
int result = executor.execute(context.getSQL(), context.getBindings());
|
||||
int result = executor.execute(context.sql(), context.bindings());
|
||||
|
||||
MockResult[] r = new MockResult[1];
|
||||
r[0] = new MockResult(result, null);
|
||||
|
||||
@ -84,15 +84,15 @@ public interface MockDataProvider {
|
||||
* <ul>
|
||||
* <li><code>MockStatement</code> does not distinguish between "static" and
|
||||
* "prepared" statements. However, a non-empty
|
||||
* {@link MockExecuteContext#getBindings()} is a strong indicator for a
|
||||
* {@link MockExecuteContext#bindings()} is a strong indicator for a
|
||||
* {@link PreparedStatement}.</li>
|
||||
* <li><code>MockStatement</code> does not distinguish between "batch" and
|
||||
* "single" statements. However...
|
||||
* <ul>
|
||||
* <li>A {@link MockExecuteContext#getBatchSQL()} with more than one SQL
|
||||
* <li>A {@link MockExecuteContext#batchSQL()} with more than one SQL
|
||||
* string is a strong indicator for a "multi-batch statement", as understood
|
||||
* by jOOQ's {@link Executor#batch(Query...)}.</li>
|
||||
* <li>A {@link MockExecuteContext#getBatchBindings()} with more than one
|
||||
* <li>A {@link MockExecuteContext#batchBindings()} with more than one
|
||||
* bind variable array is a strong indicator for a "single-batch statement",
|
||||
* as understood by jOOQ's {@link Executor#batch(Query)}.</li>
|
||||
* </ul>
|
||||
@ -123,9 +123,9 @@ public interface MockDataProvider {
|
||||
* relevant flag is passed from <code>MockStatement</code> to any of these
|
||||
* properties:
|
||||
* <ul>
|
||||
* <li> {@link MockExecuteContext#getAutoGeneratedKeys()}</li>
|
||||
* <li> {@link MockExecuteContext#getColumnIndexes()}</li>
|
||||
* <li> {@link MockExecuteContext#getColumnNames()}</li>
|
||||
* <li> {@link MockExecuteContext#autoGeneratedKeys()}</li>
|
||||
* <li> {@link MockExecuteContext#columnIndexes()}</li>
|
||||
* <li> {@link MockExecuteContext#columnNames()}</li>
|
||||
* </ul>
|
||||
* </li>
|
||||
* </ul>
|
||||
|
||||
@ -121,8 +121,8 @@ public class MockExecuteContext {
|
||||
*
|
||||
* @return Whether this execution context is a from a batch statement.
|
||||
*/
|
||||
public boolean isBatch() {
|
||||
return isSingleBatch() || isMultiBatch();
|
||||
public boolean batch() {
|
||||
return batchSingle() || batchMultiple();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -132,7 +132,7 @@ public class MockExecuteContext {
|
||||
* statement.
|
||||
* @see Executor#batch(Query)
|
||||
*/
|
||||
public boolean isSingleBatch() {
|
||||
public boolean batchSingle() {
|
||||
return bindings.length > 1;
|
||||
}
|
||||
|
||||
@ -142,7 +142,7 @@ public class MockExecuteContext {
|
||||
* @return Whether this execution context is from a "multi batch" statement.
|
||||
* @see Executor#batch(Query...)
|
||||
*/
|
||||
public boolean isMultiBatch() {
|
||||
public boolean batchMultiple() {
|
||||
return sql.length > 1;
|
||||
}
|
||||
|
||||
@ -152,7 +152,7 @@ public class MockExecuteContext {
|
||||
* @return All batch SQL statements of a "multi batch" statement, or an
|
||||
* array of length <code>1</code> with the single statement.
|
||||
*/
|
||||
public String[] getBatchSQL() {
|
||||
public String[] batchSQL() {
|
||||
return sql;
|
||||
}
|
||||
|
||||
@ -163,7 +163,7 @@ public class MockExecuteContext {
|
||||
* length <code>1</code> with the bind variables of a single
|
||||
* statement.
|
||||
*/
|
||||
public Object[][] getBatchBindings() {
|
||||
public Object[][] batchBindings() {
|
||||
return bindings;
|
||||
}
|
||||
|
||||
@ -172,7 +172,7 @@ public class MockExecuteContext {
|
||||
*
|
||||
* @return The single (or first "multi batch") SQL statement.
|
||||
*/
|
||||
public String getSQL() {
|
||||
public String sql() {
|
||||
return sql[0];
|
||||
}
|
||||
|
||||
@ -181,7 +181,7 @@ public class MockExecuteContext {
|
||||
*
|
||||
* @return The single (or first "single batch") set of bind variables.
|
||||
*/
|
||||
public Object[] getBindings() {
|
||||
public Object[] bindings() {
|
||||
return (bindings != null && bindings.length > 0) ? bindings[0] : new Object[0];
|
||||
}
|
||||
|
||||
@ -190,7 +190,7 @@ public class MockExecuteContext {
|
||||
*
|
||||
* @return The corresponding value from <code>MockStatement</code>.
|
||||
*/
|
||||
public int getAutoGeneratedKeys() {
|
||||
public int autoGeneratedKeys() {
|
||||
return autoGeneratedKeys;
|
||||
}
|
||||
|
||||
@ -199,7 +199,7 @@ public class MockExecuteContext {
|
||||
*
|
||||
* @return The corresponding value from <code>MockStatement</code>.
|
||||
*/
|
||||
public int[] getColumnIndexes() {
|
||||
public int[] columnIndexes() {
|
||||
return columnIndexes;
|
||||
}
|
||||
|
||||
@ -208,7 +208,7 @@ public class MockExecuteContext {
|
||||
*
|
||||
* @return The corresponding value from <code>MockStatement</code>.
|
||||
*/
|
||||
public String[] getColumnNames() {
|
||||
public String[] columnNames() {
|
||||
return columnNames;
|
||||
}
|
||||
}
|
||||
|
||||
@ -165,13 +165,13 @@ public class MockTest extends AbstractTest {
|
||||
|
||||
abstract class AbstractResult implements MockDataProvider {
|
||||
public void execute0(MockExecuteContext ctx) {
|
||||
assertEquals(1, ctx.getBatchSQL().length);
|
||||
assertEquals("select ?, ? from dual", ctx.getBatchSQL()[0]);
|
||||
assertEquals("select ?, ? from dual", ctx.getSQL());
|
||||
assertEquals(1, ctx.batchSQL().length);
|
||||
assertEquals("select ?, ? from dual", ctx.batchSQL()[0]);
|
||||
assertEquals("select ?, ? from dual", ctx.sql());
|
||||
|
||||
assertEquals(1, ctx.getBatchBindings().length);
|
||||
assertEquals(asList(1, 2), asList(ctx.getBatchBindings()[0]));
|
||||
assertEquals(asList(1, 2), asList(ctx.getBindings()));
|
||||
assertEquals(1, ctx.batchBindings().length);
|
||||
assertEquals(asList(1, 2), asList(ctx.batchBindings()[0]));
|
||||
assertEquals(asList(1, 2), asList(ctx.bindings()));
|
||||
}
|
||||
}
|
||||
|
||||
@ -194,13 +194,13 @@ public class MockTest extends AbstractTest {
|
||||
|
||||
@Override
|
||||
public MockResult[] execute(MockExecuteContext ctx) throws SQLException {
|
||||
assertEquals(2, ctx.getBatchSQL().length);
|
||||
assertEquals("insert into x values(1)", ctx.getBatchSQL()[0]);
|
||||
assertEquals("insert into x values(2)", ctx.getBatchSQL()[1]);
|
||||
assertEquals("insert into x values(1)", ctx.getSQL());
|
||||
assertEquals(2, ctx.batchSQL().length);
|
||||
assertEquals("insert into x values(1)", ctx.batchSQL()[0]);
|
||||
assertEquals("insert into x values(2)", ctx.batchSQL()[1]);
|
||||
assertEquals("insert into x values(1)", ctx.sql());
|
||||
|
||||
assertEquals(0, ctx.getBatchBindings().length);
|
||||
assertEquals(asList(), asList(ctx.getBindings()));
|
||||
assertEquals(0, ctx.batchBindings().length);
|
||||
assertEquals(asList(), asList(ctx.bindings()));
|
||||
|
||||
return new MockResult[] {
|
||||
new MockResult(0, null),
|
||||
@ -230,14 +230,14 @@ public class MockTest extends AbstractTest {
|
||||
|
||||
@Override
|
||||
public MockResult[] execute(MockExecuteContext ctx) throws SQLException {
|
||||
assertEquals(1, ctx.getBatchSQL().length);
|
||||
assertEquals("insert into x values(?, ?)", ctx.getBatchSQL()[0]);
|
||||
assertEquals("insert into x values(?, ?)", ctx.getSQL());
|
||||
assertEquals(1, ctx.batchSQL().length);
|
||||
assertEquals("insert into x values(?, ?)", ctx.batchSQL()[0]);
|
||||
assertEquals("insert into x values(?, ?)", ctx.sql());
|
||||
|
||||
assertEquals(2, ctx.getBatchBindings().length);
|
||||
assertEquals(asList(1, 2), asList(ctx.getBatchBindings()[0]));
|
||||
assertEquals(asList(3, 4), asList(ctx.getBatchBindings()[1]));
|
||||
assertEquals(asList(1, 2), asList(ctx.getBindings()));
|
||||
assertEquals(2, ctx.batchBindings().length);
|
||||
assertEquals(asList(1, 2), asList(ctx.batchBindings()[0]));
|
||||
assertEquals(asList(3, 4), asList(ctx.batchBindings()[1]));
|
||||
assertEquals(asList(1, 2), asList(ctx.bindings()));
|
||||
|
||||
return new MockResult[] {
|
||||
new MockResult(0, null),
|
||||
@ -291,10 +291,10 @@ public class MockTest extends AbstractTest {
|
||||
|
||||
@Override
|
||||
public MockResult[] execute(MockExecuteContext ctx) throws SQLException {
|
||||
assertEquals(1, ctx.getBatchSQL().length);
|
||||
assertEquals(1, ctx.getBatchBindings().length);
|
||||
assertEquals(asList(1), asList(ctx.getBatchBindings()[0]));
|
||||
assertEquals(asList(1), asList(ctx.getBindings()));
|
||||
assertEquals(1, ctx.batchSQL().length);
|
||||
assertEquals(1, ctx.batchBindings().length);
|
||||
assertEquals(asList(1), asList(ctx.batchBindings()[0]));
|
||||
assertEquals(asList(1), asList(ctx.bindings()));
|
||||
|
||||
return new MockResult[] {
|
||||
new MockResult(1, resultOne)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user