[jOOQ/jOOQ#8621] [jOOQ/jOOQ#9209] Support for exceptions in mocks

[jOOQ/jOOQ#8621] Add support for exception syntax in MockFileDatabase
[jOOQ/jOOQ#9209] Add support for exceptions in MockResult
This commit is contained in:
Lukas Eder 2019-09-13 17:22:29 +02:00
parent dbb1aba882
commit e37d77087a
3 changed files with 38 additions and 5 deletions

View File

@ -300,13 +300,19 @@ public class MockFileDatabase implements MockDataProvider {
private MockResult parse(String rowString) {
int rows = 0;
SQLException exception = null;
if (rowString.startsWith("@ rows:"))
rows = Integer.parseInt(rowString.substring(7).trim());
if (rowString.startsWith("@ exception:"))
exception = new SQLException(rowString.substring(12).trim());
String resultText = currentResult.toString();
String trimmed = resultText.trim();
MockResult result =
resultText.isEmpty()
exception != null
? new MockResult(exception)
: resultText.isEmpty()
? new MockResult(rows)
: trimmed.startsWith("<")
? new MockResult(rows, create.fetchFromXML(resultText))

View File

@ -37,6 +37,7 @@
*/
package org.jooq.tools.jdbc;
import java.sql.SQLException;
import java.sql.Statement;
import org.jooq.DSLContext;
@ -72,7 +73,7 @@ public class MockResult {
* <li>-1: the row count is not applicable</li>
* </ul>
*/
public final int rows;
public final int rows;
/**
* The result data associated with this execution result.
@ -88,7 +89,14 @@ public class MockResult {
* Note, that this can also be used to provide a result for
* {@link Statement#getGeneratedKeys()}
*/
public final Result<?> data;
public final Result<?> data;
/**
* The exception associated with this execution result.
* <p>
* If present, the current result produces an exception.
*/
public final SQLException exception;
/**
* Create a new <code>MockResult</code>.
@ -144,10 +152,21 @@ public class MockResult {
public MockResult(int rows, Result<?> data) {
this.rows = rows;
this.data = data;
this.exception = null;
}
public MockResult(SQLException exception) {
this.rows = -1;
this.data = null;
this.exception = exception;
}
@Override
public String toString() {
return (data != null) ? data.toString() : ("" + rows);
return (exception != null)
? "Exception : " + exception.getMessage()
: (data != null)
? data.toString()
: ("" + rows);
}
}

View File

@ -151,6 +151,13 @@ public class MockStatement extends JDBC41Statement implements CallableStatement
throw new SQLException("Connection is already closed");
}
private boolean checkException() throws SQLException {
if (result[resultIndex].exception != null)
throw result[resultIndex].exception;
else
return true;
}
@Override
public Connection getConnection() throws SQLException {
return connection;
@ -184,6 +191,7 @@ public class MockStatement extends JDBC41Statement implements CallableStatement
result = data.execute(context);
return result != null
&& result.length > 0
&& checkException()
&& result[resultIndex].data != null
// [#8113] The first result may be the generated keys
@ -213,7 +221,7 @@ public class MockStatement extends JDBC41Statement implements CallableStatement
@Override
public boolean getMoreResults(int current) throws SQLException {
return (result != null && ++resultIndex < result.length);
return (result != null && ++resultIndex < result.length) && checkException();
}
@Override