diff --git a/.github/workflows/service_test_redis.yml b/.github/workflows/service_test_redis.yml index 1f738eac3144..8bb9aecc10f6 100644 --- a/.github/workflows/service_test_redis.yml +++ b/.github/workflows/service_test_redis.yml @@ -210,9 +210,9 @@ jobs: - name: Test shell: bash working-directory: bindings/java - run: ./mvnw test -Dgroups="services_redis" -Dcargo-build.features=services-redis + run: ./mvnw test -Dtest=ServiceBehaviorTest -Dcargo-build.features=services-redis env: - OPENDAL_REDIS_TEST: on + OPENDAL_TEST: redis OPENDAL_REDIS_ENDPOINT: tcp://127.0.0.1:6379 OPENDAL_REDIS_ROOT: / OPENDAL_REDIS_DB: 0 diff --git a/.github/workflows/service_test_s3.yml b/.github/workflows/service_test_s3.yml index fcceeed7ce01..d97a36bb4bad 100644 --- a/.github/workflows/service_test_s3.yml +++ b/.github/workflows/service_test_s3.yml @@ -238,9 +238,9 @@ jobs: - name: Test shell: bash working-directory: bindings/java - run: ./mvnw test -Dgroups="services_s3" + run: ./mvnw test -Dtest=ServiceBehaviorTest env: - OPENDAL_S3_TEST: on + OPENDAL_TEST: s3 OPENDAL_S3_BUCKET: test OPENDAL_S3_ENDPOINT: "http://127.0.0.1:9000" OPENDAL_S3_ACCESS_KEY_ID: minioadmin diff --git a/bindings/java/README.md b/bindings/java/README.md index 5a7596e0345d..72b73302d4fb 100644 --- a/bindings/java/README.md +++ b/bindings/java/README.md @@ -116,30 +116,22 @@ You can copy [.env.example](/.env.example) to `${project.rootdir}/.env` and chan Take `fs` for example, we need to enable bench on `fs` on `/tmp`: ```properties -OPENDAL_FS_TEST=on -OPENDAL_FS_ROOT=/opendal +OPENDAL_TEST=fs +OPENDAL_FS_ROOT=/tmp ``` You can run service tests of enabled with the following command: ```shell -./mvnw test -Dtest=org.apache.opendal.behavior.FsTest # replace with the certain service tests -``` - -Or: - -```shell -./mvnw test -Dgroups="services_fs" # replace with the certain service tests +./mvnw test -Dtest=ServiceBehaviorTest ``` Remember to enable the necessary features via `-Dcargo-build.features=services-xxx` when running specific service test: ```shell -./mvnw test -Dtest=org.apache.opendal.behavior.RedisTest -Dcargo-build.features=services-redis -``` - -Or: - -```shell -./mvnw test -Dgroups="services_redis" -Dcargo-build.features=services-redis +export OPENDAL_TEST=redis +export OPENDAL_REDIS_ENDPOINT=tcp://127.0.0.1:6379 +export OPENDAL_REDIS_ROOT=/ +export OPENDAL_REDIS_DB=0 +./mvnw test -Dtest=ServiceBehaviorTest -Dcargo-build.features=services-redis ``` diff --git a/bindings/java/src/test/java/org/apache/opendal/test/behavior/AbstractBehaviorTest.java b/bindings/java/src/test/java/org/apache/opendal/test/behavior/AbstractBehaviorTest.java index b8bbcdb2ccbe..6b407167ed41 100644 --- a/bindings/java/src/test/java/org/apache/opendal/test/behavior/AbstractBehaviorTest.java +++ b/bindings/java/src/test/java/org/apache/opendal/test/behavior/AbstractBehaviorTest.java @@ -23,10 +23,7 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assumptions.assumeTrue; -import io.github.cdimascio.dotenv.Dotenv; -import io.github.cdimascio.dotenv.DotenvEntry; import java.util.Arrays; -import java.util.HashMap; import java.util.Map; import java.util.Random; import java.util.UUID; @@ -41,18 +38,17 @@ import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestInstance; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; @TestInstance(TestInstance.Lifecycle.PER_CLASS) public abstract class AbstractBehaviorTest { + protected final Logger log = LoggerFactory.getLogger(getClass()); protected final String scheme; protected final Map config; protected Operator operator; protected BlockingOperator blockingOperator; - protected AbstractBehaviorTest(String scheme) { - this(scheme, createSchemeConfig(scheme)); - } - protected AbstractBehaviorTest(String scheme, Map config) { this.scheme = scheme; this.config = config; @@ -60,9 +56,12 @@ protected AbstractBehaviorTest(String scheme, Map config) { @BeforeAll public void setup() { - assertThat(isSchemeEnabled(config)) - .describedAs("service test for " + scheme + " is not enabled.") - .isTrue(); + log.info( + "\n================================================================================" + + "\nTest {} is running with scheme {}." + + "\n--------------------------------------------------------------------------------", + getClass().getCanonicalName(), + scheme); this.operator = Operator.of(scheme, config); this.blockingOperator = BlockingOperator.of(scheme, config); } @@ -194,7 +193,7 @@ public void precondition() { */ @Test public void testCreateDir() { - final String path = String.format("%s/", UUID.randomUUID().toString()); + final String path = UUID.randomUUID() + "/"; operator.createDir(path).join(); final Metadata meta = operator.stat(path).join(); @@ -208,7 +207,7 @@ public void testCreateDir() { */ @Test public void testCreateDirExisting() { - final String path = String.format("%s/", UUID.randomUUID().toString()); + final String path = UUID.randomUUID() + "/"; operator.createDir(path).join(); operator.createDir(path).join(); @@ -283,7 +282,7 @@ public void testCopyNonExistingSource() { */ @Test public void testCopySourceDir() { - final String sourcePath = String.format("%s/", UUID.randomUUID().toString()); + final String sourcePath = UUID.randomUUID() + "/"; final String targetPath = UUID.randomUUID().toString(); assertThatThrownBy(() -> operator.copy(sourcePath, targetPath).join()) @@ -300,7 +299,7 @@ public void testCopyTargetDir() { operator.write(sourcePath, content).join(); - final String targetPath = String.format("%s/", UUID.randomUUID().toString()); + final String targetPath = UUID.randomUUID() + "/"; operator.createDir(targetPath).join(); assertThatThrownBy(() -> operator.copy(sourcePath, targetPath).join()) @@ -336,11 +335,8 @@ public void testCopyNested() { operator.write(sourcePath, content).join(); - final String targetPath = String.format( - "%s/%s/%s", - UUID.randomUUID().toString(), - UUID.randomUUID().toString(), - UUID.randomUUID().toString()); + final String targetPath = + String.format("%s/%s/%s", UUID.randomUUID(), UUID.randomUUID(), UUID.randomUUID()); operator.copy(sourcePath, targetPath).join(); @@ -351,7 +347,7 @@ public void testCopyNested() { } /** - * Copy to a exist path should overwrite successfully. + * Copy to an existing path should overwrite successfully. */ @Test public void testCopyOverwrite() { @@ -424,7 +420,7 @@ public void testRenameNonExistingSource() { */ @Test public void testRenameSourceDir() { - final String sourcePath = String.format("%s/", UUID.randomUUID().toString()); + final String sourcePath = UUID.randomUUID() + "/"; final String targetPath = UUID.randomUUID().toString(); operator.createDir(sourcePath).join(); @@ -445,7 +441,7 @@ public void testRenameTargetDir() { operator.write(sourcePath, content).join(); - final String targetPath = String.format("%s/", UUID.randomUUID().toString()); + final String targetPath = UUID.randomUUID() + "/"; operator.createDir(targetPath).join(); @@ -482,11 +478,8 @@ public void testRenameNested() { operator.write(sourcePath, content).join(); - final String targetPath = String.format( - "%s/%s/%s", - UUID.randomUUID().toString(), - UUID.randomUUID().toString(), - UUID.randomUUID().toString()); + final String targetPath = + String.format("%s/%s/%s", UUID.randomUUID(), UUID.randomUUID(), UUID.randomUUID()); operator.rename(sourcePath, targetPath).join(); @@ -500,7 +493,7 @@ public void testRenameNested() { } /** - * Rename to a exist path should overwrite successfully. + * Rename to an existing path should overwrite successfully. */ @Test public void testRenameOverwrite() { @@ -589,7 +582,7 @@ public void precondition() { */ @Test public void testBlockingCreateDir() { - final String path = String.format("%s/", UUID.randomUUID().toString()); + final String path = UUID.randomUUID() + "/"; blockingOperator.createDir(path); final Metadata meta = blockingOperator.stat(path); @@ -603,7 +596,7 @@ public void testBlockingCreateDir() { */ @Test public void testBlockingDirExisting() { - final String path = String.format("%s/", UUID.randomUUID().toString()); + final String path = UUID.randomUUID() + "/"; blockingOperator.createDir(path); blockingOperator.createDir(path); @@ -660,7 +653,7 @@ public void testBlockingCopyNonExistingSource() { */ @Test public void testBlockingCopySourceDir() { - final String sourcePath = String.format("%s/", UUID.randomUUID().toString()); + final String sourcePath = UUID.randomUUID() + "/"; final String targetPath = UUID.randomUUID().toString(); blockingOperator.createDir(sourcePath); @@ -681,7 +674,7 @@ public void testBlockingCopyTargetDir() { blockingOperator.write(sourcePath, sourceContent); - final String targetPath = String.format("%s/", UUID.randomUUID().toString()); + final String targetPath = UUID.randomUUID() + "/"; blockingOperator.createDir(targetPath); @@ -718,11 +711,8 @@ public void testBlockingCopyNested() { blockingOperator.write(sourcePath, content); - final String targetPath = String.format( - "%s/%s/%s", - UUID.randomUUID().toString(), - UUID.randomUUID().toString(), - UUID.randomUUID().toString()); + final String targetPath = + String.format("%s/%s/%s", UUID.randomUUID(), UUID.randomUUID(), UUID.randomUUID()); blockingOperator.copy(sourcePath, targetPath); @@ -733,7 +723,7 @@ public void testBlockingCopyNested() { } /** - * Copy to a exist path should overwrite successfully. + * Copy to an existing path should overwrite successfully. */ @Test public void testBlockingCopyOverwrite() { @@ -806,7 +796,7 @@ public void testBlockingRenameNonExistingSource() { */ @Test public void testBlockingRenameSourceDir() { - final String sourcePath = String.format("%s/", UUID.randomUUID().toString()); + final String sourcePath = UUID.randomUUID() + "/"; final String targetPath = UUID.randomUUID().toString(); blockingOperator.createDir(sourcePath); @@ -825,7 +815,7 @@ public void testBlockingRenameTargetDir() { blockingOperator.write(sourcePath, sourceContent); - final String targetPath = String.format("%s/", UUID.randomUUID().toString()); + final String targetPath = UUID.randomUUID() + "/"; blockingOperator.createDir(targetPath); @@ -862,11 +852,8 @@ public void testBlockingRenameNested() { blockingOperator.write(sourcePath, sourceContent); - final String targetPath = String.format( - "%s/%s/%s", - UUID.randomUUID().toString(), - UUID.randomUUID().toString(), - UUID.randomUUID().toString()); + final String targetPath = + String.format("%s/%s/%s", UUID.randomUUID(), UUID.randomUUID(), UUID.randomUUID()); blockingOperator.rename(sourcePath, targetPath); @@ -880,7 +867,7 @@ public void testBlockingRenameNested() { } /** - * Rename to a exist path should overwrite successfully. + * Rename to an existing path should overwrite successfully. */ @Test public void testBlockingRenameOverwrite() { @@ -918,22 +905,4 @@ public static byte[] generateBytes() { random.nextBytes(content); return content; } - - protected static boolean isSchemeEnabled(Map config) { - final String turnOn = config.getOrDefault("test", "").toLowerCase(); - return turnOn.equals("on") || turnOn.equals("true"); - } - - protected static Map createSchemeConfig(String scheme) { - final Dotenv dotenv = Dotenv.configure().ignoreIfMissing().load(); - final Map config = new HashMap<>(); - final String prefix = "opendal_" + scheme.toLowerCase() + "_"; - for (DotenvEntry entry : dotenv.entries()) { - final String key = entry.getKey().toLowerCase(); - if (key.startsWith(prefix)) { - config.put(key.substring(prefix.length()), entry.getValue()); - } - } - return config; - } } diff --git a/bindings/java/src/test/java/org/apache/opendal/test/behavior/FsBehaviorTest.java b/bindings/java/src/test/java/org/apache/opendal/test/behavior/FsBehaviorTest.java new file mode 100644 index 000000000000..6acf1d28acc2 --- /dev/null +++ b/bindings/java/src/test/java/org/apache/opendal/test/behavior/FsBehaviorTest.java @@ -0,0 +1,37 @@ +/* + * 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.opendal.test.behavior; + +import java.io.File; +import java.util.Collections; +import java.util.Map; +import org.assertj.core.util.Files; + +class FsBehaviorTest extends AbstractBehaviorTest { + protected FsBehaviorTest() { + super("fs", createSchemeConfig()); + } + + private static Map createSchemeConfig() { + final File tempDir = Files.newTemporaryFolder(); + tempDir.deleteOnExit(); + return Collections.singletonMap("root", tempDir.getAbsolutePath()); + } +} diff --git a/bindings/java/src/test/java/org/apache/opendal/test/behavior/MemoryBehaviorTest.java b/bindings/java/src/test/java/org/apache/opendal/test/behavior/MemoryBehaviorTest.java new file mode 100644 index 000000000000..23ece54d3d9c --- /dev/null +++ b/bindings/java/src/test/java/org/apache/opendal/test/behavior/MemoryBehaviorTest.java @@ -0,0 +1,33 @@ +/* + * 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.opendal.test.behavior; + +import java.util.Collections; +import java.util.Map; + +class MemoryBehaviorTest extends AbstractBehaviorTest { + protected MemoryBehaviorTest() { + super("memory", createSchemeConfig()); + } + + private static Map createSchemeConfig() { + return Collections.singletonMap("root", "/tmp"); + } +} diff --git a/bindings/java/src/test/java/org/apache/opendal/test/behavior/ServiceBehaviorTest.java b/bindings/java/src/test/java/org/apache/opendal/test/behavior/ServiceBehaviorTest.java new file mode 100644 index 000000000000..8341f83510b6 --- /dev/null +++ b/bindings/java/src/test/java/org/apache/opendal/test/behavior/ServiceBehaviorTest.java @@ -0,0 +1,58 @@ +/* + * 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.opendal.test.behavior; + +import io.github.cdimascio.dotenv.Dotenv; +import io.github.cdimascio.dotenv.DotenvEntry; +import java.util.HashMap; +import java.util.Map; +import org.junit.jupiter.api.condition.EnabledIf; + +@EnabledIf("enabled") +class ServiceBehaviorTest extends AbstractBehaviorTest { + protected ServiceBehaviorTest() { + super(lookupScheme(), createSchemeConfig()); + } + + private static boolean enabled() { + return lookupScheme() != null; + } + + private static String lookupScheme() { + final Dotenv dotenv = Dotenv.configure().ignoreIfMissing().load(); + return dotenv.get("OPENDAL_TEST"); + } + + private static Map createSchemeConfig() { + final String scheme = lookupScheme(); + final Map config = new HashMap<>(); + if (scheme != null) { + final String prefix = "opendal_" + scheme.toLowerCase() + "_"; + final Dotenv dotenv = Dotenv.configure().ignoreIfMissing().load(); + for (DotenvEntry entry : dotenv.entries()) { + final String key = entry.getKey().toLowerCase(); + if (key.startsWith(prefix)) { + config.put(key.substring(prefix.length()), entry.getValue()); + } + } + } + return config; + } +} diff --git a/bindings/java/src/test/java/org/apache/opendal/test/behavior/ServiceBehaviorTests.java b/bindings/java/src/test/java/org/apache/opendal/test/behavior/ServiceBehaviorTests.java deleted file mode 100644 index c8154419a32f..000000000000 --- a/bindings/java/src/test/java/org/apache/opendal/test/behavior/ServiceBehaviorTests.java +++ /dev/null @@ -1,92 +0,0 @@ -/* - * 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.opendal.test.behavior; - -import java.io.File; -import java.util.Map; -import lombok.extern.slf4j.Slf4j; -import org.assertj.core.util.Files; -import org.junit.jupiter.api.Tag; -import org.junit.jupiter.api.condition.EnabledIf; - -@Tag("services_memory") -@Slf4j -class MemoryTest extends AbstractBehaviorTest { - public MemoryTest() { - super("memory", defaultSchemeConfig()); - } - - private static Map defaultSchemeConfig() { - final Map config = createSchemeConfig("memory"); - if (!isSchemeEnabled(config)) { - log.info("Running MemoryTest with default config."); - config.clear(); - config.put("test", "on"); - config.put("root", "/tmp"); - } - return config; - } -} - -@Tag("services_fs") -@Slf4j -class FsTest extends AbstractBehaviorTest { - public FsTest() { - super("fs", schemeConfig()); - } - - private static Map schemeConfig() { - final Map config = createSchemeConfig("fs"); - if (!isSchemeEnabled(config)) { - log.info("Running FsTest with default config."); - config.clear(); - - final File tempDir = Files.newTemporaryFolder(); - tempDir.deleteOnExit(); - config.put("test", "on"); - config.put("root", tempDir.getAbsolutePath()); - } - return config; - } -} - -@Tag("services_redis") -@EnabledIf("enabled") -class RedisTest extends AbstractBehaviorTest { - public RedisTest() { - super("redis"); - } - - private static boolean enabled() { - return isSchemeEnabled(createSchemeConfig("redis")); - } -} - -@Tag("services_s3") -@EnabledIf("enabled") -class S3Test extends AbstractBehaviorTest { - public S3Test() { - super("s3"); - } - - private static boolean enabled() { - return isSchemeEnabled(createSchemeConfig("s3")); - } -}