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 41b0c667606 Add test case for URLArgumentLine  (#30284)
41b0c667606 is described below

commit 41b0c66760602a5c870bb0ce6e0f42c046d3ade1
Author: ilyas ahsan <[email protected]>
AuthorDate: Sun Feb 25 22:00:14 2024 +0700

    Add test case for URLArgumentLine  (#30284)
    
    * Add test case for URLArgumentLine
    
    * Add test case for assertReplaceArgumentWithProperty
    
    * Add test case for assertReplaceArgumentWithNone
    
    * Add test case for assertReplaceArgumentWithEnvironment
---
 .../infra/url/core/arg/URLArgumentLine.java        | 10 +++
 .../infra/url/core/arg/URLArgumentLineTest.java    | 88 ++++++++++++++++++++++
 2 files changed, 98 insertions(+)

diff --git 
a/infra/url/core/src/main/java/org/apache/shardingsphere/infra/url/core/arg/URLArgumentLine.java
 
b/infra/url/core/src/main/java/org/apache/shardingsphere/infra/url/core/arg/URLArgumentLine.java
index c884d4bccf7..7ec5fa53cdd 100644
--- 
a/infra/url/core/src/main/java/org/apache/shardingsphere/infra/url/core/arg/URLArgumentLine.java
+++ 
b/infra/url/core/src/main/java/org/apache/shardingsphere/infra/url/core/arg/URLArgumentLine.java
@@ -56,6 +56,16 @@ public final class URLArgumentLine {
         return Optional.of(new URLArgumentLine(parsedArg[0], parsedArg[1], 
matcher));
     }
     
+    /**
+     * Set system property.
+     *
+     * @param key   property key
+     * @param value property value
+     */
+    public static void setSystemProperty(final String key, final String value) 
{
+        System.setProperty(key, value);
+    }
+    
     /**
      * Replace argument.
      *
diff --git 
a/infra/url/core/src/test/java/org/apache/shardingsphere/infra/url/core/arg/URLArgumentLineTest.java
 
b/infra/url/core/src/test/java/org/apache/shardingsphere/infra/url/core/arg/URLArgumentLineTest.java
new file mode 100644
index 00000000000..f2202cfa7cf
--- /dev/null
+++ 
b/infra/url/core/src/test/java/org/apache/shardingsphere/infra/url/core/arg/URLArgumentLineTest.java
@@ -0,0 +1,88 @@
+/*
+ * 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.shardingsphere.infra.url.core.arg;
+
+import org.junit.jupiter.api.Test;
+import java.lang.reflect.Field;
+import java.util.Optional;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+public class URLArgumentLineTest {
+    
+    private static final Pattern PLACEHOLDER_PATTERN = 
Pattern.compile("\\$\\$\\{(.+::.*)}$");
+    
+    private static final String NAME = "fixture.config.driver.jdbc-url";
+    
+    private static final String DEFAULT_VALUE = "jdbc-url";
+    
+    @Test
+    void assertParse() throws NoSuchFieldException, IllegalAccessException {
+        String line = String.format("%s=$${%s::%s}", NAME, NAME, 
DEFAULT_VALUE);
+        Matcher matcher = PLACEHOLDER_PATTERN.matcher(line);
+        matcher.find();
+        
+        URLArgumentLine actual = URLArgumentLine.parse(line).get();
+        
+        assertThat(getURLArgumentLineField("argName").get(actual), is(NAME));
+        assertThat(getURLArgumentLineField("argDefaultValue").get(actual), 
is(DEFAULT_VALUE));
+        
assertThat(getURLArgumentLineField("placehodlerMatcher").get(actual).toString(),
 is(matcher.toString()));
+    }
+    
+    @Test
+    void assertParseInvalidPattern() {
+        final String line = "invalid-value";
+        Optional<URLArgumentLine> actual = URLArgumentLine.parse(line);
+        
+        assertThat(actual, is(Optional.empty()));
+    }
+    
+    @Test
+    void assertReplaceArgumentWithEnvironment() {
+        String line = String.format("%s=$${%s::%s}", NAME, NAME, 
DEFAULT_VALUE);
+        String actual = 
URLArgumentLine.parse(line).get().replaceArgument(URLArgumentPlaceholderType.ENVIRONMENT);
+        
+        assertThat(actual, is(String.format("%s=%s", NAME, DEFAULT_VALUE)));
+    }
+    
+    @Test
+    void assertReplaceArgumentWithProperty() {
+        String line = String.format("%s=$${%s::%s}", NAME, NAME, 
DEFAULT_VALUE);
+        URLArgumentLine.setSystemProperty(NAME, DEFAULT_VALUE);
+        String actual = 
URLArgumentLine.parse(line).get().replaceArgument(URLArgumentPlaceholderType.SYSTEM_PROPS);
+        
+        assertThat(actual, is(String.format("%s=%s", NAME, DEFAULT_VALUE)));
+    }
+    
+    @Test
+    void assertReplaceArgumentWithNone() {
+        String line = String.format("%s=$${%s::}", NAME, NAME);
+        String actual = 
URLArgumentLine.parse(line).get().replaceArgument(URLArgumentPlaceholderType.NONE);
+        
+        assertThat(actual, is(NAME));
+    }
+    
+    private Field getURLArgumentLineField(final String name) throws 
NoSuchFieldException {
+        Field field = URLArgumentLine.class.getDeclaredField(name);
+        field.setAccessible(true);
+        return field;
+    }
+}

Reply via email to