Skip to content

Commit

Permalink
Clean up FBPreparedStatementTest and use new assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
mrotteveel committed Mar 14, 2024
1 parent 74221b2 commit 41dec4b
Show file tree
Hide file tree
Showing 2 changed files with 240 additions and 101 deletions.
152 changes: 143 additions & 9 deletions src/test/org/firebirdsql/common/assertions/ResultSetAssertions.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@
*/
package org.firebirdsql.common.assertions;

import org.junit.jupiter.api.function.ThrowingSupplier;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.Arrays;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.*;

/**
* Various assertions for result set.
Expand All @@ -44,6 +47,7 @@ private ResultSetAssertions() {
*
* @param rs
* result set
* @see #assertNextRow(ResultSet, String)
*/
public static void assertNextRow(ResultSet rs) {
assertNextRow(rs, "Expected a row");
Expand All @@ -62,28 +66,158 @@ public static void assertNextRow(ResultSet rs, String message) {
}

/**
* Asserts that {@code rs} has no next row by checking if {@link ResultSet#next()} returned {@code false}.
* <p>
* Equivalent to using {@link #assertNoNextRow(ResultSet, String)} with message {@code "Expected no more rows"}.
* </p>
* Asserts that {@code rs} has no next row ({@link ResultSet#next()} returned {@code false}).
*
* @param rs
* result set
* @see #assertNoNextRow(ResultSet, String)
*/
public static void assertNoNextRow(ResultSet rs) {
assertNoNextRow(rs, "Expected no more rows");
}

/**
* Asserts that {@code rs} has no next row by checking if {@link ResultSet#next()} returned {@code false}.
* Asserts that {@code rs} has no next row ({@link ResultSet#next()} returned {@code false}).
*
* @param rs
* result set
* @param message
* message to use for the assertion if {@link ResultSet#next()} returned {@code true}
*/
public static void assertNoNextRow(ResultSet rs, String message) {
assertFalse(assertDoesNotThrow(rs::next, "No exception expected for ResultSet.next()"), message);
assertFalse(assertNoException(rs::next, "ResultSet.next()"), message);
}

/**
* Asserts that {@code rs} is open ({@link ResultSet#isClosed()} returned {@code false})
*
* @param rs
* result set
* @see #assertResultSetOpen(ResultSet, String)
*/
public static void assertResultSetOpen(ResultSet rs) {
assertResultSetOpen(rs, "Expected open result set, was closed");
}

/**
* Asserts that {@code rs} is open ({@link ResultSet#isClosed()} returned {@code false})
*
* @param rs
* result set
* @param message
* message to use for the assertion if {@link ResultSet#isClosed()} returned {@code true}
*/
public static void assertResultSetOpen(ResultSet rs, String message) {
assertFalse(assertNoException(rs::isClosed, "ResultSet.isClosed()"), message);
}

/**
* Asserts that {@code rs} is closed ({@link ResultSet#isClosed()} returned {@code true})
*
* @param rs
* result set
* @see #assertResultSetClosed(ResultSet, String)
*/
public static void assertResultSetClosed(ResultSet rs) {
assertResultSetClosed(rs, "Expected closed result set, was open");
}

/**
* Asserts that {@code rs} is closed ({@link ResultSet#isClosed()} returned {@code true})
*
* @param rs
* result set
* @param message
* message to use for the assertion if {@link ResultSet#isClosed()} returned {@code false}
*/
public static void assertResultSetClosed(ResultSet rs, String message) {
assertTrue(assertNoException(rs::isClosed, "ResultSet.isClosed()"), message);
}

/**
* Asserts that the current row of the result set matches in length and values.
*
* @param rs
* result set
* @param expectedValues
* expected values
* @see #assertRowEquals(String, ResultSet, List)
*/
public static void assertRowEquals(ResultSet rs, Object... expectedValues) {
assertRowEquals(rs, Arrays.asList(expectedValues));
}

/**
* Asserts that the current row of the result set matches in length and values.
*
* @param message
* message to use for assertion failures
* @param rs
* result set
* @param expectedValues
* expected values
* @see #assertRowEquals(String, ResultSet, List)
*/
public static void assertRowEquals(String message, ResultSet rs, Object... expectedValues) {
assertRowEquals(message, rs, Arrays.asList(expectedValues));
}

/**
* Asserts that the current row of the result set matches in length and values.
*
* @param rs
* result set
* @param expectedValues
* expected values
* @see #assertRowEquals(String, ResultSet, List)
*/
public static void assertRowEquals(ResultSet rs, List<Object> expectedValues) {
assertRowEquals("Row mismatch", rs, expectedValues);
}

/**
* Asserts that the current row of the result set matches in length and values.
* <p>
* For each non-null value in {@code expectedValue}, its class is used to call
* {@link ResultSet#getObject(int, Class)}, for {@code byte[]}, {@link ResultSet#getBytes(int)}. For {@code null}
* values, {@link ResultSet#getObject(int)} is called. The 0-based index of {@code expectedValues} is transformed to
* the 1-based index of JDBC. Assertions report the 1-based index.
* </p>
* <p>
* Assertion stops at the first mismatch.
* </p>
*
* @param message
* message to use for assertion failures
* @param rs
* result set
* @param expectedValues
* expected values
*/
public static void assertRowEquals(String message, ResultSet rs, List<Object> expectedValues) {
ResultSetMetaData rsmd = assertNoException(rs::getMetaData, "ResultSet.getMetaData()");
assertEquals(expectedValues.size(), assertNoException(rsmd::getColumnCount, "ResultSet.getColumnCount()"),
message + ": column count differs");
for (int idx = 0; idx < expectedValues.size(); idx++) {
Object expectedValue = expectedValues.get(idx);
int colIdx = idx + 1;
if (expectedValue == null) {
assertNull(assertNoException(() -> rs.getObject(colIdx), "ResultSet.getObject(int)"),
message + " at column " + colIdx + " (1-based)");
} else if (expectedValue instanceof byte[] expectedBytes) {
assertArrayEquals(expectedBytes,
assertNoException(() -> rs.getBytes(colIdx), "ResultSet.getBytes(int)"),
message + " at column " + colIdx + " (1-based)");
} else {
assertEquals(expectedValue,
assertNoException(() -> rs.getObject(colIdx, expectedValue.getClass()), "ResultSet.getObject(int, Class)"),
message + " at column " + colIdx + " (1-based)");
}
}
}

private static <T> T assertNoException(ThrowingSupplier<T> supplier, String methodName) {
return assertDoesNotThrow(supplier, "No exception expected for " + methodName);
}

}
Loading

0 comments on commit 41dec4b

Please sign in to comment.