Changeset: 3c0b4215e080 for monetdb-java
URL: https://dev.monetdb.org/hg/monetdb-java/rev/3c0b4215e080
Added Files:
src/test/java/org/monetdb/testinfra/CloseOnFailure.java
src/test/java/org/monetdb/testinfra/CloseOnFailureExtension.java
Modified Files:
src/test/java/JDBC_API_Tester.java
Branch: mvn
Log Message:
Close and reopen connection after api test fails
(if it succeeds the connection is reused by the next test, like it used to)
diffs (278 lines):
diff --git a/src/test/java/JDBC_API_Tester.java
b/src/test/java/JDBC_API_Tester.java
--- a/src/test/java/JDBC_API_Tester.java
+++ b/src/test/java/JDBC_API_Tester.java
@@ -23,8 +23,11 @@ import org.junit.jupiter.api.condition.E
import org.monetdb.jdbc.MonetConnection;
import org.monetdb.jdbc.types.INET;
import org.monetdb.jdbc.types.URL;
+import org.monetdb.testinfra.CloseOnFailure;
import org.monetdb.testinfra.Config;
+import static org.junit.jupiter.api.Assertions.*;
+
/**
* class to test JDBC Driver API methods and behavior of MonetDB server.
*
@@ -50,6 +53,7 @@ public final class JDBC_API_Tester {
// Connection state set in connect()
private String jdbcUrl;
+ @CloseOnFailure
private Connection con; // main connection shared by all tests
private int dbmsMajorVersion;
private int dbmsMinorVersion;
@@ -73,10 +77,12 @@ public final class JDBC_API_Tester {
isPostDec2023 = versionIsAtLeast(11, 50);
}
- @BeforeAll
+ @BeforeEach
public void openConnection() throws SQLException {
+ if (con == null || con.isClosed()) {
String url = Config.getServerURL();
connect(url);
+ }
}
@AfterAll
@@ -84,11 +90,34 @@ public final class JDBC_API_Tester {
closeConx(con);
}
+ @BeforeEach
+ public void clearFoundDifferences() {
+ foundDifferences = false;
+ }
+
@AfterEach
public void checkFoundDifferences() {
assertFalse(foundDifferences);
}
+ @AfterEach
+ public void betweenTests() throws SQLException {
+ if (con == null || con.isClosed())
+ return;
+ assertTrue(con.getAutoCommit());
+ if (versionIsAtLeast(11, 52)) {
+ String query = "SELECT 'query_id=' || query_id ||
'/res_id=' || res_id FROM sys.unclosed_result_sets()";
+ StringBuilder unclosed = new StringBuilder();
+ try (Statement stmt = con.createStatement(); ResultSet
rs = stmt.executeQuery(query)) {
+ while (rs.next()) {
+ if (unclosed.length() > 0)
+ unclosed.append(", ");
+ unclosed.append(rs.getString(1));
+ }
+ }
+ assertEquals("", unclosed.toString(), "this test forgot
to close one or more result sets");
+ }
+ }
/**
* main function
* @param args args[0] should contain the connectionURL string, args[1]
an optional flag: -skipMALoutput
@@ -2482,8 +2511,9 @@ public final class JDBC_API_Tester {
sb.append(" passed\n");
} catch (SQLException e) {
sb.append("FAILED:
").append(e.getMessage()).append("\n");
- }
- closeStmtResSet(pstmt, null);
+ } finally {
+ closeStmtResSet(pstmt, null);
+ }
compareExpectedOutput("Test_PSlargeresponse",
"1. DatabaseMetadata environment retrieval... oke\n" +
@@ -7530,56 +7560,52 @@ public final class JDBC_API_Tester {
private void compareExpectedOutput(String testname, String expected) {
final String produced = sb.toString();
- if (!expected.equals(produced)) {
- foundDifferences = true;
- System.err.print("Test '");
- System.err.print(testname);
- if (!testname.endsWith(")") && !testname.endsWith(";"))
- System.err.print("()");
- System.err.println("' produced different output!");
- int expLen = expected.length();
- int prodLen = produced.length();
- if (expLen > 0 && prodLen > 0) {
- int max_pos = expLen;
- if (prodLen > max_pos)
- max_pos = prodLen;
- int line = 1;
- int rowpos = 0;
- for (int pos = 0; pos < max_pos; pos++) {
- char a = (pos < expLen ?
expected.charAt(pos) : '~');
- char b = (pos < prodLen ?
produced.charAt(pos) : '~');
- if (a == '\n') {
- line++;
- rowpos = 0;
- } else {
- rowpos++;
- }
- if (a != b) {
- if (pos + 40 < expLen)
- expLen = pos + 40;
- if (pos + 40 < prodLen)
- prodLen = pos + 40;
- System.err.println("Difference
found at line " + line + " position " + rowpos
- + ". Expected:\n\"" +
expected.substring(pos < expLen ? pos : expLen-1, expLen-1)
- + "\"\nFound:\n\"" +
produced.substring(pos < prodLen ? pos : prodLen-1, prodLen-1) + "\"");
- pos = max_pos;
- }
+ if (expected.equals(produced)) {
+ return;
+ }
+ foundDifferences = true;
+ System.err.print("Test '");
+ System.err.print(testname);
+ if (!testname.endsWith(")") && !testname.endsWith(";"))
+ System.err.print("()");
+ System.err.println("' produced different output!");
+ int expLen = expected.length();
+ int prodLen = produced.length();
+ if (expLen > 0 && prodLen > 0) {
+ int max_pos = expLen;
+ if (prodLen > max_pos)
+ max_pos = prodLen;
+ int line = 1;
+ int rowpos = 0;
+ for (int pos = 0; pos < max_pos; pos++) {
+ char a = (pos < expLen ? expected.charAt(pos) :
'~');
+ char b = (pos < prodLen ? produced.charAt(pos)
: '~');
+ if (a == '\n') {
+ line++;
+ rowpos = 0;
+ } else {
+ rowpos++;
+ }
+ if (a != b) {
+ if (pos + 40 < expLen)
+ expLen = pos + 40;
+ if (pos + 40 < prodLen)
+ prodLen = pos + 40;
+ System.err.println("Difference found at
line " + line + " position " + rowpos
+ + ". Expected:\n\"" +
expected.substring(pos < expLen ? pos : expLen-1, expLen-1)
+ + "\"\nFound:\n\"" +
produced.substring(pos < prodLen ? pos : prodLen-1, prodLen-1) + "\"");
+ pos = max_pos;
}
}
- System.err.println();
- System.err.println("---- Full Output: ------------");
- System.err.println(sb);
- System.err.println("---- END ---------------------");
- System.err.println("---- Expected Output: --------");
- System.err.println(expected);
- System.err.println("---- END ---------------------");
- System.err.println();
- }
- if (sb.length() > sbInitLen) {
- System.err.println("Test '" + testname
- + "' produced output > " + sbInitLen
- + " chars! Enlarge sbInitLen to: " +
sb.length());
- }
+ }
+ System.err.println();
+ System.err.println("---- Full Output: ------------");
+ System.err.println(sb);
+ System.err.println("---- END ---------------------");
+ System.err.println("---- Expected Output: --------");
+ System.err.println(expected);
+ System.err.println("---- END ---------------------");
+ System.err.println();
}
private void closeConx(Connection cn) {
diff --git a/src/test/java/org/monetdb/testinfra/CloseOnFailure.java
b/src/test/java/org/monetdb/testinfra/CloseOnFailure.java
new file mode 100644
--- /dev/null
+++ b/src/test/java/org/monetdb/testinfra/CloseOnFailure.java
@@ -0,0 +1,27 @@
+/*
+ * SPDX-License-Identifier: MPL-2.0
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ * Copyright 2024, 2025 MonetDB Foundation;
+ * Copyright August 2008 - 2023 MonetDB B.V.;
+ * Copyright 1997 - July 2008 CWI.
+ */
+package org.monetdb.testinfra;
+
+
+import org.junit.jupiter.api.extension.ExtendWith;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+
+@Target({ ElementType.FIELD })
+@Retention(RetentionPolicy.RUNTIME)
+@ExtendWith(CloseOnFailureExtension.class)
+public @interface CloseOnFailure {
+}
diff --git a/src/test/java/org/monetdb/testinfra/CloseOnFailureExtension.java
b/src/test/java/org/monetdb/testinfra/CloseOnFailureExtension.java
new file mode 100644
--- /dev/null
+++ b/src/test/java/org/monetdb/testinfra/CloseOnFailureExtension.java
@@ -0,0 +1,56 @@
+/*
+ * SPDX-License-Identifier: MPL-2.0
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ * Copyright 2024, 2025 MonetDB Foundation;
+ * Copyright August 2008 - 2023 MonetDB B.V.;
+ * Copyright 1997 - July 2008 CWI.
+ */
+package org.monetdb.testinfra;
+
+import org.junit.jupiter.api.extension.ExtensionContext;
+import org.junit.jupiter.api.extension.TestWatcher;
+
+import java.lang.reflect.Field;
+
+public class CloseOnFailureExtension implements TestWatcher {
+
+ @Override
+ public void testAborted(ExtensionContext context, Throwable cause) {
+ closeAnnotatedFields(context);
+ TestWatcher.super.testAborted(context, cause);
+ }
+
+ @Override
+ public void testFailed(ExtensionContext context, Throwable cause) {
+ closeAnnotatedFields(context);
+ TestWatcher.super.testFailed(context, cause);
+ }
+
+ private void closeAnnotatedFields(ExtensionContext context) {
+ Object instance = context.getRequiredTestInstance();
+ for (Field field : instance.getClass().getDeclaredFields()) {
+ try {
+ if
(!field.isAnnotationPresent(CloseOnFailure.class)) {
+ continue;
+ }
+ field.setAccessible(true);
+ Object value = field.get(instance);
+ if (value == null)
+ continue;
+
+ boolean isAutoCloseable = value instanceof
AutoCloseable;
+ if (!isAutoCloseable)
+ continue;
+ ((AutoCloseable) value).close();
+ field.set(instance, null);
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+ }
+
+}
_______________________________________________
checkin-list mailing list -- [email protected]
To unsubscribe send an email to [email protected]