You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2025/12/21 13:37:42 UTC

(shardingsphere) branch master updated: Refactor BootstrapArgumentsTest (#37456)

This is an automated email from the ASF dual-hosted git repository.

zhangliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new 92b85ddfa97 Refactor BootstrapArgumentsTest (#37456)
92b85ddfa97 is described below

commit 92b85ddfa975c2ee381a6272c33a1cd5e67d59c8
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Sun Dec 21 21:37:35 2025 +0800

    Refactor BootstrapArgumentsTest (#37456)
---
 .../proxy/arguments/BootstrapArgumentsTest.java    | 133 ++++++++++-----------
 1 file changed, 66 insertions(+), 67 deletions(-)

diff --git a/proxy/bootstrap/src/test/java/org/apache/shardingsphere/proxy/arguments/BootstrapArgumentsTest.java b/proxy/bootstrap/src/test/java/org/apache/shardingsphere/proxy/arguments/BootstrapArgumentsTest.java
index 5b63c987477..652f865a8ea 100644
--- a/proxy/bootstrap/src/test/java/org/apache/shardingsphere/proxy/arguments/BootstrapArgumentsTest.java
+++ b/proxy/bootstrap/src/test/java/org/apache/shardingsphere/proxy/arguments/BootstrapArgumentsTest.java
@@ -17,11 +17,17 @@
 
 package org.apache.shardingsphere.proxy.arguments;
 
-import org.junit.jupiter.api.Test;
+import lombok.SneakyThrows;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
 import org.mockito.internal.configuration.plugins.Plugins;
 
+import java.util.Arrays;
 import java.util.Collections;
+import java.util.List;
 import java.util.Optional;
+import java.util.stream.Stream;
 
 import static org.hamcrest.CoreMatchers.is;
 import static org.hamcrest.MatcherAssert.assertThat;
@@ -31,75 +37,68 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
 
 class BootstrapArgumentsTest {
     
-    @Test
-    void assertGetPortWithoutArguments() {
-        assertFalse(new BootstrapArguments(new String[]{}).getPort().isPresent());
+    @SuppressWarnings("OptionalUsedAsFieldOrParameterType")
+    @ParameterizedTest(name = "{0}")
+    @MethodSource("provideBootstrapArguments")
+    void assertBootstrapArguments(final String scenario, final String[] args, final Optional<Integer> expectedPort, final Optional<String> expectedPortErrorMessage,
+                                  final String expectedConfigPath, final List<String> expectedAddresses, final Optional<String> expectedSocketPath,
+                                  final Optional<String> expectedSocketPathErrorMessage) {
+        BootstrapArguments arguments = new BootstrapArguments(args);
+        if (expectedPortErrorMessage.isPresent()) {
+            IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, arguments::getPort);
+            assertThat(scenario + ": port error message", ex.getMessage(), is(expectedPortErrorMessage.get()));
+        } else {
+            Optional<Integer> actualPort = arguments.getPort();
+            if (expectedPort.isPresent()) {
+                assertTrue(actualPort.isPresent());
+                assertThat(scenario + ": port value", actualPort.get(), is(expectedPort.get()));
+            } else {
+                assertFalse(actualPort.isPresent());
+            }
+        }
+        assertThat(scenario + ": configuration path", arguments.getConfigurationPath(), is(expectedConfigPath));
+        assertThat(scenario + ": addresses", arguments.getAddresses(), is(expectedAddresses));
+        if (expectedSocketPathErrorMessage.isPresent()) {
+            IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, arguments::getSocketPath);
+            assertThat(scenario + ": socket path error message", ex.getMessage(), is(expectedSocketPathErrorMessage.get()));
+        } else {
+            Optional<String> actualSocketPath = arguments.getSocketPath();
+            if (expectedSocketPath.isPresent()) {
+                assertTrue(actualSocketPath.isPresent());
+                assertThat(scenario + ": socket path", actualSocketPath.get(), is(expectedSocketPath.get()));
+            } else {
+                assertFalse(actualSocketPath.isPresent());
+            }
+        }
     }
     
-    @Test
-    void assertGetPortWithNegativeArgument() {
-        assertFalse(new BootstrapArguments(new String[]{"-1"}).getPort().isPresent());
+    private static Stream<Arguments> provideBootstrapArguments() {
+        String defaultConfigPath = getDefaultConfigPath();
+        return Stream.of(
+                Arguments.of("without arguments", new String[]{}, Optional.empty(), Optional.empty(), defaultConfigPath,
+                        Collections.singletonList("0.0.0.0"), Optional.empty(), Optional.empty()),
+                Arguments.of("negative port", new String[]{"-1"}, Optional.empty(), Optional.empty(), defaultConfigPath,
+                        Collections.singletonList("0.0.0.0"), Optional.empty(), Optional.empty()),
+                Arguments.of("non numeric port", new String[]{"WrongArgument"}, Optional.empty(), Optional.of("Invalid port `WrongArgument`."),
+                        defaultConfigPath, Collections.singletonList("0.0.0.0"), Optional.empty(), Optional.empty()),
+                Arguments.of("valid port uses default configuration path", new String[]{"3306"}, Optional.of(3306), Optional.empty(),
+                        defaultConfigPath, Collections.singletonList("0.0.0.0"), Optional.empty(), Optional.empty()),
+                Arguments.of("pad leading slash", new String[]{"0", "test/"}, Optional.of(0), Optional.empty(), "/test/",
+                        Collections.singletonList("0.0.0.0"), Optional.empty(), Optional.empty()),
+                Arguments.of("pad trailing slash", new String[]{"0", "/test"}, Optional.of(0), Optional.empty(), "/test/",
+                        Collections.singletonList("0.0.0.0"), Optional.empty(), Optional.empty()),
+                Arguments.of("default addresses when arguments less than three", new String[]{"0", "conf"}, Optional.of(0), Optional.empty(),
+                        "/conf/", Collections.singletonList("0.0.0.0"), Optional.empty(), Optional.empty()),
+                Arguments.of("filter non inet address", new String[]{"0", "conf", "127.0.0.1,/tmp/shardingsphere.sock"}, Optional.of(0), Optional.empty(),
+                        "/conf/", Collections.singletonList("127.0.0.1"), Optional.of("/tmp/shardingsphere.sock"), Optional.empty()),
+                Arguments.of("socket path empty when only inet addresses", new String[]{"0", "conf", "127.0.0.1,1.1.1.1"}, Optional.of(0), Optional.empty(),
+                        "/conf/", Arrays.asList("127.0.0.1", "1.1.1.1"), Optional.empty(), Optional.empty()),
+                Arguments.of("invalid socket path", new String[]{"0", "conf", "127.0.0.1,\0"}, Optional.of(0), Optional.empty(),
+                        "/conf/", Collections.singletonList("127.0.0.1"), Optional.empty(), Optional.of("Invalid path `\0`.")));
     }
     
-    @Test
-    void assertGetPortWithNonNumericArgument() {
-        IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> new BootstrapArguments(new String[]{"WrongArgument"}).getPort());
-        assertThat(ex.getMessage(), is("Invalid port `WrongArgument`."));
-    }
-    
-    @Test
-    void assertGetPortWithValidArgument() {
-        Optional<Integer> actualPort = new BootstrapArguments(new String[]{"3306"}).getPort();
-        assertTrue(actualPort.isPresent());
-        assertThat(actualPort.get(), is(3306));
-    }
-    
-    @Test
-    void assertGetConfigurationPathWithSingleArgumentUsesDefaultPath() throws ReflectiveOperationException {
-        assertThat(new BootstrapArguments(new String[]{"3306"}).getConfigurationPath(), is(Plugins.getMemberAccessor().get(BootstrapArguments.class.getDeclaredField("DEFAULT_CONFIG_PATH"), null)));
-    }
-    
-    @Test
-    void assertGetConfigurationPathPadsLeadingSlash() {
-        assertThat(new BootstrapArguments(new String[]{"0", "test/"}).getConfigurationPath(), is("/test/"));
-    }
-    
-    @Test
-    void assertGetConfigurationPathPadsTrailingSlash() {
-        assertThat(new BootstrapArguments(new String[]{"0", "/test"}).getConfigurationPath(), is("/test/"));
-    }
-    
-    @Test
-    void assertGetAddressesDefaultWhenArgumentsLessThanThree() {
-        assertThat(new BootstrapArguments(new String[]{"0", "conf"}).getAddresses(), is(Collections.singletonList("0.0.0.0")));
-    }
-    
-    @Test
-    void assertGetSocketPathWhenArgumentsLessThanThree() {
-        assertFalse(new BootstrapArguments(new String[]{"0", "conf"}).getSocketPath().isPresent());
-    }
-    
-    @Test
-    void assertGetAddressesFiltersNonInetAddress() {
-        assertThat(new BootstrapArguments(new String[]{"0", "conf", "127.0.0.1,/tmp/shardingsphere.sock"}).getAddresses(), is(Collections.singletonList("127.0.0.1")));
-    }
-    
-    @Test
-    void assertGetSocketPathReturnsFirstNonInetAddress() {
-        Optional<String> actualSocketPath = new BootstrapArguments(new String[]{"0", "conf", "127.0.0.1,/tmp/shardingsphere.sock"}).getSocketPath();
-        assertTrue(actualSocketPath.isPresent());
-        assertThat(actualSocketPath.get(), is("/tmp/shardingsphere.sock"));
-    }
-    
-    @Test
-    void assertGetSocketPathWhenOnlyInetAddresses() {
-        assertFalse(new BootstrapArguments(new String[]{"0", "conf", "127.0.0.1,1.1.1.1"}).getSocketPath().isPresent());
-    }
-    
-    @Test
-    void assertGetSocketPathWithInvalidPath() {
-        BootstrapArguments arguments = new BootstrapArguments(new String[]{"0", "conf", "127.0.0.1,\0"});
-        IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, arguments::getSocketPath);
-        assertThat(ex.getMessage(), is("Invalid path `\0`."));
+    @SneakyThrows(ReflectiveOperationException.class)
+    private static String getDefaultConfigPath() {
+        return (String) Plugins.getMemberAccessor().get(BootstrapArguments.class.getDeclaredField("DEFAULT_CONFIG_PATH"), null);
     }
 }