From ddc741e71d1c4a199dc11fdb5776155d235d078f Mon Sep 17 00:00:00 2001 From: Julian Hyde Date: Wed, 18 Sep 2024 15:01:50 -0700 Subject: [PATCH] Disable RemotePreparedStatementParametersTest on JDK 23 or higher Add class Version --- .../calcite/jdbc/CalciteRemoteDriverTest.java | 8 +-- ...RemotePreparedStatementParametersTest.java | 20 ++++++ .../org/apache/calcite/util/TestUtil.java | 7 +- .../java/org/apache/calcite/util/Version.java | 65 +++++++++++++++++++ .../org/apache/calcite/util/TestUtilTest.java | 47 ++++++++++++++ 5 files changed, 141 insertions(+), 6 deletions(-) create mode 100644 testkit/src/main/java/org/apache/calcite/util/Version.java diff --git a/core/src/test/java/org/apache/calcite/jdbc/CalciteRemoteDriverTest.java b/core/src/test/java/org/apache/calcite/jdbc/CalciteRemoteDriverTest.java index b0d327b379f4..a7184d7c0124 100644 --- a/core/src/test/java/org/apache/calcite/jdbc/CalciteRemoteDriverTest.java +++ b/core/src/test/java/org/apache/calcite/jdbc/CalciteRemoteDriverTest.java @@ -31,6 +31,7 @@ import org.apache.calcite.test.schemata.hr.Employee; import org.apache.calcite.util.TestUtil; import org.apache.calcite.util.Util; +import org.apache.calcite.util.Version; import com.google.common.collect.ImmutableList; @@ -82,7 +83,7 @@ import static org.hamcrest.CoreMatchers.nullValue; import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assumptions.assumeTrue; +import static org.junit.jupiter.api.Assumptions.assumeFalse; import static java.util.Objects.requireNonNull; @@ -107,9 +108,8 @@ class CalciteRemoteDriverTest { private static @Nullable HttpServer start; @BeforeAll public static void beforeClass() throws Exception { - assumeTrue(TestUtil.getJavaMajorVersion() < 23 - || (TestUtil.AVATICA_VERSION != null - && TestUtil.AVATICA_VERSION.compareTo("1.25.0") > 0), + assumeFalse(TestUtil.getJavaMajorVersion() >= 23 + && TestUtil.AVATICA_VERSION.compareTo(Version.of(1, 25)) > 0, "Cannot run on JDK 23 and higher with Avatica version 1.25 or lower; " + "enable when [CALCITE-6588] is fixed in Avatica"); diff --git a/plus/src/test/java/org/apache/calcite/chinook/RemotePreparedStatementParametersTest.java b/plus/src/test/java/org/apache/calcite/chinook/RemotePreparedStatementParametersTest.java index 77dd923d1cd6..50673152c470 100644 --- a/plus/src/test/java/org/apache/calcite/chinook/RemotePreparedStatementParametersTest.java +++ b/plus/src/test/java/org/apache/calcite/chinook/RemotePreparedStatementParametersTest.java @@ -16,6 +16,9 @@ */ package org.apache.calcite.chinook; +import org.apache.calcite.util.TestUtil; +import org.apache.calcite.util.Version; + import org.junit.jupiter.api.Test; import java.sql.Connection; @@ -23,6 +26,8 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; +import static org.junit.jupiter.api.Assumptions.assumeFalse; + /** * Tests against parameters in prepared statement when using underlying JDBC * sub-schema. @@ -30,6 +35,11 @@ class RemotePreparedStatementParametersTest { @Test void testSimpleStringParameterShouldWorkWithCalcite() throws Exception { + assumeFalse(TestUtil.getJavaMajorVersion() >= 23 + && TestUtil.AVATICA_VERSION.compareTo(Version.of(1, 25)) > 0, + "Cannot run on JDK 23 and higher with Avatica version 1.25 or lower; " + + "enable when [CALCITE-6588] is fixed in Avatica"); + // given ChinookAvaticaServer server = new ChinookAvaticaServer(); server.startWithCalcite(); @@ -44,6 +54,11 @@ class RemotePreparedStatementParametersTest { } @Test void testSeveralParametersShouldWorkWithCalcite() throws Exception { + assumeFalse(TestUtil.getJavaMajorVersion() >= 23 + && TestUtil.AVATICA_VERSION.compareTo(Version.of(1, 25)) > 0, + "Cannot run on JDK 23 and higher with Avatica version 1.25 or lower; " + + "enable when [CALCITE-6588] is fixed in Avatica"); + // given ChinookAvaticaServer server = new ChinookAvaticaServer(); server.startWithCalcite(); @@ -60,6 +75,11 @@ class RemotePreparedStatementParametersTest { } @Test void testParametersShouldWorkWithRaw() throws Exception { + assumeFalse(TestUtil.getJavaMajorVersion() >= 23 + && TestUtil.AVATICA_VERSION.compareTo(Version.of(1, 25)) > 0, + "Cannot run on JDK 23 and higher with Avatica version 1.25 or lower; " + + "enable when [CALCITE-6588] is fixed in Avatica"); + // given ChinookAvaticaServer server = new ChinookAvaticaServer(); server.startWithRaw(); diff --git a/testkit/src/main/java/org/apache/calcite/util/TestUtil.java b/testkit/src/main/java/org/apache/calcite/util/TestUtil.java index 498adae10355..4987235f1923 100644 --- a/testkit/src/main/java/org/apache/calcite/util/TestUtil.java +++ b/testkit/src/main/java/org/apache/calcite/util/TestUtil.java @@ -33,6 +33,8 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; +import static org.apache.calcite.util.Util.first; + import static org.junit.jupiter.api.Assertions.fail; import static java.lang.Integer.parseInt; @@ -55,8 +57,9 @@ public abstract class TestUtil { private static final String JAVA_VERSION = System.getProperties().getProperty("java.version"); - public static final String AVATICA_VERSION = - System.getProperty("calcite.avatica.version"); + public static final Version AVATICA_VERSION = + Version.of(first(System.getProperty("calcite.avatica.version"), "0")); + private static final Supplier GUAVA_MAJOR_VERSION = Suppliers.memoize(TestUtil::computeGuavaMajorVersion); diff --git a/testkit/src/main/java/org/apache/calcite/util/Version.java b/testkit/src/main/java/org/apache/calcite/util/Version.java new file mode 100644 index 000000000000..850950a8f362 --- /dev/null +++ b/testkit/src/main/java/org/apache/calcite/util/Version.java @@ -0,0 +1,65 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to you under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.calcite.util; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.Ordering; + +import java.util.ArrayList; +import java.util.List; + +import static java.lang.Integer.parseInt; + +/** + * Version string parsed into major and minor parts. + */ +public class Version implements Comparable { + private static final Ordering> ORDERING = + Ordering.natural().lexicographical(); + + public final List integers; + public final String string; + + /** Private constructor. */ + private Version(List integers, String string) { + this.integers = ImmutableList.copyOf(integers); + this.string = string; + } + + /** Creates a Version by parsing a string. */ + public static Version of(String s) { + final String[] strings = s.split("[.-]"); + List integers = new ArrayList<>(); + for (String string : strings) { + try { + integers.add(parseInt(string)); + } catch (NumberFormatException e) { + break; + } + } + return new Version(integers, s); + } + + /** Creates a Version from a sequence of integers. */ + public static Version of(int... integers) { + return new Version(ImmutableIntList.copyOf(integers), ""); + } + + @Override public int compareTo(Version version) { + return ORDERING.compare(this.integers, version.integers); + } +} diff --git a/testkit/src/test/java/org/apache/calcite/util/TestUtilTest.java b/testkit/src/test/java/org/apache/calcite/util/TestUtilTest.java index d8028a198f0e..587d3f2a9ae7 100644 --- a/testkit/src/test/java/org/apache/calcite/util/TestUtilTest.java +++ b/testkit/src/test/java/org/apache/calcite/util/TestUtilTest.java @@ -30,6 +30,9 @@ import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.empty; +import static org.hamcrest.Matchers.emptyString; +import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.hasToString; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -101,6 +104,50 @@ private void testJavaVersion(int expectedMajorVersion, String versionString) { versionString); } + /** Unit test for {@link Version}. */ + @SuppressWarnings("EqualsWithItself") + @Test void testVersion() { + Version vEmpty = Version.of(""); + assertThat(vEmpty.integers, empty()); + assertThat(vEmpty.string, emptyString()); + + final Version v1 = Version.of("1"); + assertThat(v1.integers, hasSize(1)); + assertThat(v1.integers, hasToString("[1]")); + + final Version v1_8_3 = Version.of("1.8.3-jre"); // "-jre" is ignored + assertThat(v1_8_3.integers, hasSize(3)); + assertThat(v1_8_3.integers, hasToString("[1, 8, 3]")); + assertThat(v1_8_3.string, is("1.8.3-jre")); + + final Version v1_19 = Version.of("1.19"); + assertThat(v1_19.integers, hasSize(2)); + assertThat(v1_19.integers, hasToString("[1, 19]")); + + final Version v1_23 = Version.of("1.23"); + assertThat(v1_23.integers, hasSize(2)); + assertThat(v1_23.integers, hasToString("[1, 23]")); + + final Version v1_23_0 = Version.of("1.23.0"); + assertThat(v1_23_0.integers, hasSize(3)); + assertThat(v1_23_0.integers, hasToString("[1, 23, 0]")); + + final Version v1_23_1 = Version.of("1.23.1"); + assertThat(v1_23_1.integers, hasSize(3)); + assertThat(v1_23_1.integers, hasToString("[1, 23, 1]")); + + // 1 < 1.8.3 < 1.19 < 1.23 < 1.23.0 < 1.23.1 + assertThat(vEmpty.compareTo(v1), is(-1)); + assertThat(v1.compareTo(v1_23), is(-1)); + assertThat(v1_8_3.compareTo(v1_19), is(-1)); + assertThat(v1_19.compareTo(v1_23), is(-1)); + assertThat(v1_23.compareTo(v1_23_0), is(-1)); + assertThat(v1_23_0.compareTo(v1_23_1), is(-1)); + assertThat(v1_23_1.compareTo(v1), is(1)); + + assertThat(v1.compareTo(v1), is(0)); + } + @Test void testGuavaMajorVersion() { int majorVersion = TestUtil.getGuavaMajorVersion(); assertTrue(majorVersion >= 2,