Changeset: b42ca00599c5 for monetdb-java
URL: https://dev.monetdb.org/hg/monetdb-java/rev/b42ca00599c5
Added Files:
src/test/java/org/monetdb/mcl/net/ScriptRunner.java
src/test/java/org/monetdb/mcl/net/ScriptRunnerSelfTest.java
src/test/java/org/monetdb/mcl/net/TargetTest.java
src/test/java/org/monetdb/mcl/net/UrlTests.java
src/test/resources/org/monetdb/mcl/net/javaspecific.md
src/test/resources/org/monetdb/mcl/net/tests.md
Removed Files:
src/test/resources/javaspecific.md
src/test/resources/tests.md
Modified Files:
pom.xml
src/test/java/UrlTester.java
Branch: mvn
Log Message:
Introduce Junit and convert UrlTester to UrlTests
Original UrlTester is still there because JDBC_API_Tester depends on it
diffs (truncated from 572 to 300 lines):
diff --git a/pom.xml b/pom.xml
--- a/pom.xml
+++ b/pom.xml
@@ -52,6 +52,28 @@
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
+ <!-- with this we can omit the version numbers of junit stuff -->
+ <dependencyManagement>
+ <dependencies>
+ <dependency>
+ <groupId>org.junit</groupId>
+ <artifactId>junit-bom</artifactId>
+ <version>5.11.4</version>
+ <type>pom</type>
+ <scope>import</scope>
+ </dependency>
+ </dependencies>
+ </dependencyManagement>
+
+ <dependencies>
+
+ <dependency>
+ <groupId>org.junit.jupiter</groupId>
+ <artifactId>junit-jupiter</artifactId>
+ <scope>test</scope>
+ </dependency>
+
+ </dependencies>
<build>
<resources>
@@ -122,7 +144,11 @@
<plugins>
- <!-- configure jar builder to set a main class -->
+ <!-- Configure jar builder
+ 1. to set a main class
+ 2. to create a tests jar
+ Note that the tests jar does not contain the dependencies
yet, we will use the assembly plugin for that
+ -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
@@ -133,6 +159,21 @@
</manifest>
</archive>
</configuration>
+ <executions>
+ <!-- There is a default execution that creates the regular
jar, this one creates the test jar -->
+ <execution>
+ <id>make-test-jar</id>
+ <goals>
+ <goal>test-jar</goal>
+ </goals>
+ <configuration>
+ <excludes>
+ <exclude>SQLcopyinto.class</exclude>
+ <exclude>Test_C*.class</exclude>
+ </excludes>
+ </configuration>
+ </execution>
+ </executions>
</plugin>
<plugin>
diff --git a/src/test/java/UrlTester.java b/src/test/java/UrlTester.java
--- a/src/test/java/UrlTester.java
+++ b/src/test/java/UrlTester.java
@@ -102,8 +102,8 @@ public final class UrlTester {
public static void runAllTests() throws IOException, Failure {
runUnitTests();
- UrlTester.forResource("/tests.md", 0).run();
- UrlTester.forResource("/javaspecific.md", 0).run();
+ UrlTester.forResource("/org/monetdb/mcl/net/tests.md", 0).run();
+ UrlTester.forResource("/org/monetdb/mcl/net/javaspecific.md",
0).run();
}
public static void runUnitTests() {
diff --git a/src/test/java/org/monetdb/mcl/net/ScriptRunner.java
b/src/test/java/org/monetdb/mcl/net/ScriptRunner.java
new file mode 100644
--- /dev/null
+++ b/src/test/java/org/monetdb/mcl/net/ScriptRunner.java
@@ -0,0 +1,239 @@
+/*
+ * 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.mcl.net;
+
+import java.net.URISyntaxException;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import static org.junit.jupiter.api.Assertions.fail;
+
+public class ScriptRunner {
+ private final static Pattern pattern =
Pattern.compile("([A-Z]+)\\s+((.*?)(=(.*))?)");
+ private final String source;
+ private final int sourceLineOffset;
+ private final String section;
+ private final String[] lines;
+ int lineno = 0;
+ Target target = null;
+ Target.Validated validated = null;
+
+ public ScriptRunner(String source, int sourceLineOffset, String
section, String[] lines) {
+ this.source = source;
+ this.sourceLineOffset = sourceLineOffset;
+ this.section = section;
+ this.lines = lines;
+ }
+
+ public String origin() {
+ return source + ":" + (sourceLineOffset + 1) + section;
+ }
+
+ public void reset() {
+ lineno = 0;
+ target = new Target();
+ validated = null;
+ }
+
+ public void execute() throws Failure {
+ reset();
+
+ for (String line : lines) {
+ line = line.trim();
+ if (!line.isEmpty()) {
+ Matcher m = pattern.matcher(line);
+ if (!m.matches()) {
+ throw new Failure("Invalid command: " +
line);
+ }
+ String command = m.group(1);
+ String argument = m.group(2);
+ String key = m.group(3);
+ String value = m.group(5);
+ switch (command) {
+ case "ONLY":
+ if (!haveFeature(argument))
+ return;
+ break;
+ case "NOT":
+ if (haveFeature(argument))
+ return;
+ break;
+ case "PARSE":
+ handleParse(argument, null);
+ break;
+ case "ACCEPT":
+ handleParse(argument, true);
+ break;
+ case "REJECT":
+ handleParse(argument, false);
+ break;
+ case "SET":
+ if (value == null)
+ throw new
Failure("expected KEY=VALUE");
+ handleSet(key, value);
+ break;
+ case "EXPECT":
+ if (value == null)
+ throw new
Failure("expected KEY=VALUE");
+ handleExpect(key, value);
+ }
+ }
+
+ lineno++;
+ }
+ }
+
+ private boolean haveFeature(String features) throws Failure {
+ for (String part : features.split("\\s+")) {
+ if (part.equals("jdbc"))
+ return true;
+ }
+ return false;
+ }
+
+ private void handleParse(String url, Boolean shouldSucceed) throws
Failure {
+ URISyntaxException parseError = null;
+ ValidationError validationError = null;
+
+ validated = null;
+ try {
+ target.barrier();
+ MonetUrlParser.parse(target, url);
+ target.validate();
+ } catch (URISyntaxException e) {
+ parseError = e;
+ } catch (ValidationError e) {
+ validationError = e;
+ }
+
+ // REJECT
+ if (shouldSucceed == Boolean.FALSE) {
+ if (parseError != null || validationError != null)
+ return; // happy
+ else
+ throw new Failure("URL unexpectedly parsed and
validated");
+ }
+
+ // PARSE AND ACCEPT
+ if (parseError != null)
+ throw new Failure(parseError.getMessage());
+ // ACCEPT
+ if (shouldSucceed == Boolean.TRUE && validationError != null)
+ throw new Failure(validationError.getMessage());
+ }
+
+ private void handleSet(String key, String value) throws Failure {
+ validated = null;
+
+ try {
+ target.setString(key, value);
+ } catch (ValidationError e) {
+ throw new Failure(e.getMessage());
+ }
+ }
+
+ private void handleExpect(String key, String expectedString) throws
Failure {
+ Object actual;
+ try {
+ actual = extract(key);
+ } catch (ValidationError e) {
+ throw new Failure(e.getMessage());
+ }
+
+ Object expected;
+ try {
+ if (actual instanceof Boolean)
+ expected = ParameterType.Bool.parse(key,
expectedString);
+ else if (actual instanceof Integer)
+ expected = ParameterType.Int.parse(key,
expectedString);
+ else
+ expected = expectedString;
+ } catch (ValidationError e) {
+ String typ = actual.getClass().getName();
+ throw new Failure("Cannot convert expected value <" +
expectedString + "> to " + typ + ": " + e.getMessage());
+ }
+
+ if (actual.equals(expected))
+ return;
+ throw new Failure("Expected " + key + "=<" + expectedString +
">, found <" + actual + ">");
+ }
+
+ private Target.Validated validated() {
+ try {
+ return tryValidate();
+ } catch (ValidationError e) {
+ return fail(e.getMessage());
+ }
+ }
+
+ private Target.Validated tryValidate() throws ValidationError {
+ if (validated == null)
+ validated = target.validate();
+ return validated;
+ }
+
+ private Object extract(String key) throws ValidationError, Failure {
+ switch (key) {
+ case "valid":
+ try {
+ tryValidate();
+ } catch (ValidationError e) {
+ return Boolean.FALSE;
+ }
+ return Boolean.TRUE;
+
+ case "connect_scan":
+ return validated().connectScan();
+ case "connect_port":
+ return validated().connectPort();
+ case "connect_unix":
+ return validated().connectUnix();
+ case "connect_tcp":
+ return validated().connectTcp();
+ case "connect_tls_verify":
+ switch (validated().connectVerify()) {
+ case None:
+ return "";
+ case Cert:
+ return "cert";
+ case Hash:
+ return "hash";
+ case System:
+ return "system";
+ default:
+ throw new
IllegalStateException("unreachable");
+ }
_______________________________________________
checkin-list mailing list -- [email protected]
To unsubscribe send an email to [email protected]