diff --git a/infra/argument/core/pom.xml b/infra/argument/core/pom.xml new file mode 100644 index 0000000000000..a1ceb2ebf4824 --- /dev/null +++ b/infra/argument/core/pom.xml @@ -0,0 +1,43 @@ + + + + + 4.0.0 + + org.apache.shardingsphere + shardingsphere-infra-argument + 5.5.2-SNAPSHOT + + shardingsphere-infra-argument-core + ${project.artifactId} + + + + org.apache.shardingsphere + shardingsphere-infra-spi + ${project.version} + + + + org.apache.shardingsphere + shardingsphere-test-util + ${project.version} + test + + + diff --git a/infra/url/core/src/main/java/org/apache/shardingsphere/infra/url/core/arg/URLArgumentPlaceholderType.java b/infra/argument/core/src/main/java/org/apache/shardingsphere/infra/argument/core/ShardingSpherePlaceholderLoader.java similarity index 62% rename from infra/url/core/src/main/java/org/apache/shardingsphere/infra/url/core/arg/URLArgumentPlaceholderType.java rename to infra/argument/core/src/main/java/org/apache/shardingsphere/infra/argument/core/ShardingSpherePlaceholderLoader.java index 9ee08609e8a69..902b073c4d939 100644 --- a/infra/url/core/src/main/java/org/apache/shardingsphere/infra/url/core/arg/URLArgumentPlaceholderType.java +++ b/infra/argument/core/src/main/java/org/apache/shardingsphere/infra/argument/core/ShardingSpherePlaceholderLoader.java @@ -15,12 +15,22 @@ * limitations under the License. */ -package org.apache.shardingsphere.infra.url.core.arg; +package org.apache.shardingsphere.infra.argument.core; + +import org.apache.shardingsphere.infra.spi.annotation.SingletonSPI; +import org.apache.shardingsphere.infra.spi.type.typed.TypedSPI; /** - * URL argument placeholder type. + * ShardingSphere placeholder loader. */ -public enum URLArgumentPlaceholderType { +@SingletonSPI +public interface ShardingSpherePlaceholderLoader extends TypedSPI { - NONE, ENVIRONMENT, SYSTEM_PROPS + /** + * Render configuration argument. + * + * @param argName configuration content + * @return argument value + */ + String getArgumentValue(String argName); } diff --git a/infra/argument/pom.xml b/infra/argument/pom.xml new file mode 100644 index 0000000000000..926773dfd693b --- /dev/null +++ b/infra/argument/pom.xml @@ -0,0 +1,33 @@ + + + + + 4.0.0 + + org.apache.shardingsphere + shardingsphere-infra + 5.5.2-SNAPSHOT + + shardingsphere-infra-argument + pom + ${project.artifactId} + + core + type + + diff --git a/infra/argument/type/environment-property/pom.xml b/infra/argument/type/environment-property/pom.xml new file mode 100644 index 0000000000000..fec1cd6a64256 --- /dev/null +++ b/infra/argument/type/environment-property/pom.xml @@ -0,0 +1,36 @@ + + + + + 4.0.0 + + org.apache.shardingsphere + shardingsphere-infra-argument-type + 5.5.2-SNAPSHOT + + shardingsphere-infra-argument-environment-property + ${project.artifactId} + + + + org.apache.shardingsphere + shardingsphere-infra-argument-core + ${project.version} + + + diff --git a/infra/argument/type/environment-property/src/main/java/org/apache/shardingsphere/infra/argument/environment/property/EnvironmentPropertyPlaceholderLoader.java b/infra/argument/type/environment-property/src/main/java/org/apache/shardingsphere/infra/argument/environment/property/EnvironmentPropertyPlaceholderLoader.java new file mode 100644 index 0000000000000..d2a13416dcc3a --- /dev/null +++ b/infra/argument/type/environment-property/src/main/java/org/apache/shardingsphere/infra/argument/environment/property/EnvironmentPropertyPlaceholderLoader.java @@ -0,0 +1,38 @@ +/* + * 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.argument.environment.property; + +import org.apache.shardingsphere.infra.argument.core.ShardingSpherePlaceholderLoader; +import org.apache.shardingsphere.infra.spi.annotation.SingletonSPI; + +/** + * Environment property placeholder loader. + */ +@SingletonSPI +public final class EnvironmentPropertyPlaceholderLoader implements ShardingSpherePlaceholderLoader { + + @Override + public String getArgumentValue(final String argName) { + return System.getenv(argName); + } + + @Override + public String getType() { + return "environment"; + } +} diff --git a/infra/argument/type/environment-property/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.argument.core.ShardingSpherePlaceholderLoader b/infra/argument/type/environment-property/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.argument.core.ShardingSpherePlaceholderLoader new file mode 100644 index 0000000000000..8fe3710c6718b --- /dev/null +++ b/infra/argument/type/environment-property/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.argument.core.ShardingSpherePlaceholderLoader @@ -0,0 +1,18 @@ +# +# 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. +# + +org.apache.shardingsphere.infra.argument.environment.property.EnvironmentPropertyPlaceholderLoader diff --git a/infra/argument/type/environment-property/src/test/java/org/apache/shardingsphere/infra/argument/environment/property/EnvironmentPropertyPlaceholderLoaderLoaderTest.java b/infra/argument/type/environment-property/src/test/java/org/apache/shardingsphere/infra/argument/environment/property/EnvironmentPropertyPlaceholderLoaderLoaderTest.java new file mode 100644 index 0000000000000..c26dcf2b6f5a9 --- /dev/null +++ b/infra/argument/type/environment-property/src/test/java/org/apache/shardingsphere/infra/argument/environment/property/EnvironmentPropertyPlaceholderLoaderLoaderTest.java @@ -0,0 +1,35 @@ +/* + * 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.argument.environment.property; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class EnvironmentPropertyPlaceholderLoaderLoaderTest { + + @Test + void assertGetArgumentValue() { + assertEquals(System.getenv("PATH"), new EnvironmentPropertyPlaceholderLoader().getArgumentValue("PATH")); + } + + @Test + void assertGetType() { + assertEquals("environment", new EnvironmentPropertyPlaceholderLoader().getType()); + } +} diff --git a/infra/argument/type/pom.xml b/infra/argument/type/pom.xml new file mode 100644 index 0000000000000..a597e610f7065 --- /dev/null +++ b/infra/argument/type/pom.xml @@ -0,0 +1,34 @@ + + + + + 4.0.0 + + org.apache.shardingsphere + shardingsphere-infra-argument + 5.5.2-SNAPSHOT + + shardingsphere-infra-argument-type + pom + ${project.artifactId} + + + environment-property + system-property + + diff --git a/infra/argument/type/system-property/pom.xml b/infra/argument/type/system-property/pom.xml new file mode 100644 index 0000000000000..9d5458228b6cf --- /dev/null +++ b/infra/argument/type/system-property/pom.xml @@ -0,0 +1,36 @@ + + + + + 4.0.0 + + org.apache.shardingsphere + shardingsphere-infra-argument-type + 5.5.2-SNAPSHOT + + shardingsphere-infra-argument-system-property + ${project.artifactId} + + + + org.apache.shardingsphere + shardingsphere-infra-argument-core + ${project.version} + + + diff --git a/infra/argument/type/system-property/src/main/java/org/apache/shardingsphere/infra/argument/system/property/SystemPropertyPlaceholderLoader.java b/infra/argument/type/system-property/src/main/java/org/apache/shardingsphere/infra/argument/system/property/SystemPropertyPlaceholderLoader.java new file mode 100644 index 0000000000000..d9b0cc53578d8 --- /dev/null +++ b/infra/argument/type/system-property/src/main/java/org/apache/shardingsphere/infra/argument/system/property/SystemPropertyPlaceholderLoader.java @@ -0,0 +1,39 @@ +/* + * 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.argument.system.property; + +import org.apache.shardingsphere.infra.argument.core.ShardingSpherePlaceholderLoader; +import org.apache.shardingsphere.infra.spi.annotation.SingletonSPI; + +/** + * System property placeholder loader. + */ +@SingletonSPI +public final class SystemPropertyPlaceholderLoader implements ShardingSpherePlaceholderLoader { + + @Override + public String getArgumentValue(final String argName) { + return System.getProperty(argName); + } + + @Override + public String getType() { + return "system_props"; + } + +} diff --git a/infra/argument/type/system-property/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.argument.core.ShardingSpherePlaceholderLoader b/infra/argument/type/system-property/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.argument.core.ShardingSpherePlaceholderLoader new file mode 100644 index 0000000000000..f4cf9bdd51d02 --- /dev/null +++ b/infra/argument/type/system-property/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.argument.core.ShardingSpherePlaceholderLoader @@ -0,0 +1,18 @@ +# +# 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. +# + +org.apache.shardingsphere.infra.argument.system.property.SystemPropertyPlaceholderLoader diff --git a/infra/argument/type/system-property/src/test/java/org/apache/shardingsphere/infra/argument/system/property/SystemPropertyPlaceholderLoaderTest.java b/infra/argument/type/system-property/src/test/java/org/apache/shardingsphere/infra/argument/system/property/SystemPropertyPlaceholderLoaderTest.java new file mode 100644 index 0000000000000..fa2529cc42ce2 --- /dev/null +++ b/infra/argument/type/system-property/src/test/java/org/apache/shardingsphere/infra/argument/system/property/SystemPropertyPlaceholderLoaderTest.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.shardingsphere.infra.argument.system.property; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class SystemPropertyPlaceholderLoaderTest { + + @Test + void assertGetArgumentValue() { + System.setProperty("test", "value"); + assertEquals("value", new SystemPropertyPlaceholderLoader().getArgumentValue("test")); + System.clearProperty("test"); + } + + @Test + void assertGetType() { + assertEquals("system_props", new SystemPropertyPlaceholderLoader().getType()); + } +} diff --git a/infra/pom.xml b/infra/pom.xml index a1105c650d5c4..4ecabb2d58b72 100644 --- a/infra/pom.xml +++ b/infra/pom.xml @@ -34,6 +34,7 @@ data-source-pool common context + argument url algorithm distsql-handler diff --git a/infra/url/core/pom.xml b/infra/url/core/pom.xml index 449accfe1a412..e008c2d105da9 100644 --- a/infra/url/core/pom.xml +++ b/infra/url/core/pom.xml @@ -32,7 +32,21 @@ shardingsphere-infra-url-spi ${project.version} - + + org.apache.shardingsphere + shardingsphere-infra-argument-core + ${project.version} + + + org.apache.shardingsphere + shardingsphere-infra-argument-environment-property + ${project.version} + + + org.apache.shardingsphere + shardingsphere-infra-argument-system-property + ${project.version} + org.apache.shardingsphere shardingsphere-test-util diff --git a/infra/url/core/src/main/java/org/apache/shardingsphere/infra/url/core/ShardingSphereURLLoadEngine.java b/infra/url/core/src/main/java/org/apache/shardingsphere/infra/url/core/ShardingSphereURLLoadEngine.java index 85ac4988e42a4..2a2da78e3ede5 100644 --- a/infra/url/core/src/main/java/org/apache/shardingsphere/infra/url/core/ShardingSphereURLLoadEngine.java +++ b/infra/url/core/src/main/java/org/apache/shardingsphere/infra/url/core/ShardingSphereURLLoadEngine.java @@ -18,7 +18,6 @@ package org.apache.shardingsphere.infra.url.core; import org.apache.shardingsphere.infra.url.core.arg.URLArgumentLineRender; -import org.apache.shardingsphere.infra.url.core.arg.URLArgumentPlaceholderTypeFactory; import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; import org.apache.shardingsphere.infra.url.spi.ShardingSphereURLLoader; @@ -46,6 +45,6 @@ public ShardingSphereURLLoadEngine(final ShardingSphereURL url) { */ public byte[] loadContent() { Collection lines = Arrays.asList(urlLoader.load(url.getConfigurationSubject(), url.getQueryProps()).split(System.lineSeparator())); - return URLArgumentLineRender.render(lines, URLArgumentPlaceholderTypeFactory.valueOf(url.getQueryProps())); + return URLArgumentLineRender.render(lines, url.getQueryProps()); } } 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 e635017bef966..1f47275bd89e6 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 @@ -20,8 +20,11 @@ import com.google.common.base.Strings; import lombok.AccessLevel; import lombok.RequiredArgsConstructor; +import org.apache.shardingsphere.infra.argument.core.ShardingSpherePlaceholderLoader; +import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; import java.util.Optional; +import java.util.Properties; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -35,18 +38,21 @@ public final class URLArgumentLine { private final Matcher placeholderMatcher; + private final Properties props; + /** * Parse URL argument line. * * @param line line + * @param props properties * @return parsed URL argument line */ - public static Optional parse(final String line) { + public static Optional parse(final String line, final Properties props) { Matcher matcher = PLACEHOLDER_PATTERN.matcher(line); if (!matcher.find()) { return Optional.empty(); } - return Optional.of(new URLArgumentLine(matcher)); + return Optional.of(new URLArgumentLine(matcher, props)); } /** @@ -55,7 +61,7 @@ public static Optional parse(final String line) { * @param type placeholder type * @return replaced argument */ - public String replaceArgument(final URLArgumentPlaceholderType type) { + public String replaceArgument(final String type) { placeholderMatcher.reset(); StringBuffer result = new StringBuffer(); while (placeholderMatcher.find()) { @@ -78,13 +84,11 @@ private String rightTrim(final StringBuffer buffer) { return buffer.toString(); } - private String getArgumentValue(final String argName, final URLArgumentPlaceholderType type) { - if (URLArgumentPlaceholderType.ENVIRONMENT == type) { - return System.getenv(argName); - } - if (URLArgumentPlaceholderType.SYSTEM_PROPS == type) { - return System.getProperty(argName); + private String getArgumentValue(final String argName, final String type) { + if (Strings.isNullOrEmpty(type) || "none".equalsIgnoreCase(type)) { + return null; } - return null; + ShardingSpherePlaceholderLoader shardingSpherePlaceholderLoader = TypedSPILoader.getService(ShardingSpherePlaceholderLoader.class, type, props); + return shardingSpherePlaceholderLoader.getArgumentValue(argName); } } diff --git a/infra/url/core/src/main/java/org/apache/shardingsphere/infra/url/core/arg/URLArgumentLineRender.java b/infra/url/core/src/main/java/org/apache/shardingsphere/infra/url/core/arg/URLArgumentLineRender.java index 6c3a7361873e8..d8983ebd6a88a 100644 --- a/infra/url/core/src/main/java/org/apache/shardingsphere/infra/url/core/arg/URLArgumentLineRender.java +++ b/infra/url/core/src/main/java/org/apache/shardingsphere/infra/url/core/arg/URLArgumentLineRender.java @@ -23,6 +23,7 @@ import java.nio.charset.StandardCharsets; import java.util.Collection; import java.util.Optional; +import java.util.Properties; /** * URL argument line render. @@ -34,13 +35,14 @@ public final class URLArgumentLineRender { * Render argument. * * @param lines lines to be rendered - * @param placeholderType configuration content placeholder type + * @param props properties * @return rendered content */ - public static byte[] render(final Collection lines, final URLArgumentPlaceholderType placeholderType) { + public static byte[] render(final Collection lines, final Properties props) { + String placeholderType = URLArgumentPlaceholderTypeFactory.valueOf(props); StringBuilder result = new StringBuilder(); for (String each : lines) { - Optional argLine = URLArgumentPlaceholderType.NONE == placeholderType ? Optional.empty() : URLArgumentLine.parse(each); + Optional argLine = "none".equalsIgnoreCase(placeholderType) ? Optional.empty() : URLArgumentLine.parse(each, props); result.append(argLine.map(optional -> optional.replaceArgument(placeholderType)).orElse(each)).append(System.lineSeparator()); } return result.toString().getBytes(StandardCharsets.UTF_8); diff --git a/infra/url/core/src/main/java/org/apache/shardingsphere/infra/url/core/arg/URLArgumentPlaceholderTypeFactory.java b/infra/url/core/src/main/java/org/apache/shardingsphere/infra/url/core/arg/URLArgumentPlaceholderTypeFactory.java index e87136d42403b..b24a03e64eb3a 100644 --- a/infra/url/core/src/main/java/org/apache/shardingsphere/infra/url/core/arg/URLArgumentPlaceholderTypeFactory.java +++ b/infra/url/core/src/main/java/org/apache/shardingsphere/infra/url/core/arg/URLArgumentPlaceholderTypeFactory.java @@ -31,16 +31,12 @@ public final class URLArgumentPlaceholderTypeFactory { private static final String KEY = "placeholder-type"; /** - * Get value of placeholder type. + * Get the value of placeholder type. * * @param queryProps query properties * @return placeholder type */ - public static URLArgumentPlaceholderType valueOf(final Properties queryProps) { - try { - return URLArgumentPlaceholderType.valueOf(queryProps.getProperty(KEY, URLArgumentPlaceholderType.NONE.name()).toUpperCase()); - } catch (final IllegalArgumentException ex) { - return URLArgumentPlaceholderType.NONE; - } + public static String valueOf(final Properties queryProps) { + return queryProps.getProperty(KEY, "none").toLowerCase(); } } diff --git a/infra/url/core/src/test/java/org/apache/shardingsphere/infra/url/core/arg/URLArgumentLineRenderTest.java b/infra/url/core/src/test/java/org/apache/shardingsphere/infra/url/core/arg/URLArgumentLineRenderTest.java index 4d5edef2d2954..a1150cd954af1 100644 --- a/infra/url/core/src/test/java/org/apache/shardingsphere/infra/url/core/arg/URLArgumentLineRenderTest.java +++ b/infra/url/core/src/test/java/org/apache/shardingsphere/infra/url/core/arg/URLArgumentLineRenderTest.java @@ -17,6 +17,8 @@ package org.apache.shardingsphere.infra.url.core.arg; +import org.apache.shardingsphere.test.util.PropertiesBuilder; +import org.apache.shardingsphere.test.util.PropertiesBuilder.Property; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; @@ -27,6 +29,7 @@ import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.util.Objects; +import java.util.Properties; import java.util.stream.Collectors; import static org.hamcrest.CoreMatchers.is; @@ -52,20 +55,20 @@ static void afterAll() { @Test void assertReadWithNonePlaceholder() throws IOException, URISyntaxException { - byte[] actual = readContent("config/to-be-replaced-fixture.yaml", URLArgumentPlaceholderType.NONE); - byte[] expected = readContent("config/to-be-replaced-fixture.yaml", URLArgumentPlaceholderType.NONE); + byte[] actual = readContent("config/to-be-replaced-fixture.yaml", PropertiesBuilder.build(new Property("placeholder-type", "none"))); + byte[] expected = readContent("config/to-be-replaced-fixture.yaml", PropertiesBuilder.build(new Property("placeholder-type", "none"))); assertThat(new String(actual), is(new String(expected))); } @Test void assertReadWithSystemPropertiesPlaceholder() throws IOException, URISyntaxException { - byte[] actual = readContent("config/to-be-replaced-fixture.yaml", URLArgumentPlaceholderType.SYSTEM_PROPS); - byte[] expected = readContent("config/replaced-fixture.yaml", URLArgumentPlaceholderType.SYSTEM_PROPS); + byte[] actual = readContent("config/to-be-replaced-fixture.yaml", PropertiesBuilder.build(new Property("placeholder-type", "system_props"))); + byte[] expected = readContent("config/replaced-fixture.yaml", PropertiesBuilder.build(new Property("placeholder-type", "system_props"))); assertThat(new String(actual), is(new String(expected))); } - private byte[] readContent(final String name, final URLArgumentPlaceholderType placeholderType) throws IOException, URISyntaxException { + private byte[] readContent(final String name, final Properties prop) throws IOException, URISyntaxException { File file = new File(Objects.requireNonNull(Thread.currentThread().getContextClassLoader().getResource(name)).toURI().getPath()); - return URLArgumentLineRender.render(Files.readAllLines(file.toPath(), StandardCharsets.UTF_8).stream().filter(each -> !each.startsWith("#")).collect(Collectors.toList()), placeholderType); + return URLArgumentLineRender.render(Files.readAllLines(file.toPath(), StandardCharsets.UTF_8).stream().filter(each -> !each.startsWith("#")).collect(Collectors.toList()), prop); } } 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 index ae9b61f8c8829..2ae955adf543b 100644 --- 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 @@ -20,6 +20,7 @@ import org.junit.jupiter.api.Test; import java.util.Optional; +import java.util.Properties; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; @@ -36,33 +37,33 @@ class URLArgumentLineTest { @Test void assertParseWithInvalidPattern() { - assertFalse(URLArgumentLine.parse("invalid").isPresent()); + assertFalse(URLArgumentLine.parse("invalid", new Properties()).isPresent()); } @Test void assertReplaceArgumentWithNone() { - Optional argLine = URLArgumentLine.parse(line); - Optional argLineMultiple1 = URLArgumentLine.parse(lineMultiple1); - Optional argLineMultiple2 = URLArgumentLine.parse(lineMultiple2); + Optional argLine = URLArgumentLine.parse(line, new Properties()); + Optional argLineMultiple1 = URLArgumentLine.parse(lineMultiple1, new Properties()); + Optional argLineMultiple2 = URLArgumentLine.parse(lineMultiple2, new Properties()); assertTrue(argLine.isPresent()); assertTrue(argLineMultiple1.isPresent()); assertTrue(argLineMultiple2.isPresent()); - assertThat(argLine.get().replaceArgument(URLArgumentPlaceholderType.NONE), is("key=default_value")); - assertThat(argLineMultiple1.get().replaceArgument(URLArgumentPlaceholderType.NONE), is("key1=default_value1:key2=default_value2:tail")); - assertThat(argLineMultiple2.get().replaceArgument(URLArgumentPlaceholderType.NONE), is("key1=:key2=:tail")); + assertThat(argLine.get().replaceArgument("none"), is("key=default_value")); + assertThat(argLineMultiple1.get().replaceArgument("none"), is("key1=default_value1:key2=default_value2:tail")); + assertThat(argLineMultiple2.get().replaceArgument("none"), is("key1=:key2=:tail")); } @Test void assertReplaceArgumentWithEnvironment() { - Optional argLine = URLArgumentLine.parse(line); - Optional argLineMultiple1 = URLArgumentLine.parse(lineMultiple1); - Optional argLineMultiple2 = URLArgumentLine.parse(lineMultiple2); + Optional argLine = URLArgumentLine.parse(line, new Properties()); + Optional argLineMultiple1 = URLArgumentLine.parse(lineMultiple1, new Properties()); + Optional argLineMultiple2 = URLArgumentLine.parse(lineMultiple2, new Properties()); assertTrue(argLine.isPresent()); assertTrue(argLineMultiple1.isPresent()); assertTrue(argLineMultiple2.isPresent()); - assertThat(argLine.get().replaceArgument(URLArgumentPlaceholderType.ENVIRONMENT), is("key=default_value")); - assertThat(argLineMultiple1.get().replaceArgument(URLArgumentPlaceholderType.ENVIRONMENT), is("key1=default_value1:key2=default_value2:tail")); - assertThat(argLineMultiple2.get().replaceArgument(URLArgumentPlaceholderType.ENVIRONMENT), is("key1=:key2=:tail")); + assertThat(argLine.get().replaceArgument("environment"), is("key=default_value")); + assertThat(argLineMultiple1.get().replaceArgument("environment"), is("key1=default_value1:key2=default_value2:tail")); + assertThat(argLineMultiple2.get().replaceArgument("environment"), is("key1=:key2=:tail")); } @Test @@ -70,14 +71,14 @@ void assertReplaceArgumentWithSystemProperty() { System.setProperty("value", "props_value"); System.setProperty("value1", "props_value1"); System.setProperty("value2", "props_value2"); - Optional argLine = URLArgumentLine.parse(line); - Optional argLineMultiple1 = URLArgumentLine.parse(lineMultiple1); - Optional argLineMultiple2 = URLArgumentLine.parse(lineMultiple2); + Optional argLine = URLArgumentLine.parse(line, new Properties()); + Optional argLineMultiple1 = URLArgumentLine.parse(lineMultiple1, new Properties()); + Optional argLineMultiple2 = URLArgumentLine.parse(lineMultiple2, new Properties()); assertTrue(argLine.isPresent()); assertTrue(argLineMultiple1.isPresent()); assertTrue(argLineMultiple2.isPresent()); - assertThat(argLine.get().replaceArgument(URLArgumentPlaceholderType.SYSTEM_PROPS), is("key=props_value")); - assertThat(argLineMultiple1.get().replaceArgument(URLArgumentPlaceholderType.SYSTEM_PROPS), is("key1=props_value1:key2=props_value2:tail")); - assertThat(argLineMultiple2.get().replaceArgument(URLArgumentPlaceholderType.SYSTEM_PROPS), is("key1=props_value1:key2=props_value2:tail")); + assertThat(argLine.get().replaceArgument("system_props"), is("key=props_value")); + assertThat(argLineMultiple1.get().replaceArgument("system_props"), is("key1=props_value1:key2=props_value2:tail")); + assertThat(argLineMultiple2.get().replaceArgument("system_props"), is("key1=props_value1:key2=props_value2:tail")); } } diff --git a/infra/url/core/src/test/java/org/apache/shardingsphere/infra/url/core/arg/URLArgumentPlaceholderTypeFactoryTest.java b/infra/url/core/src/test/java/org/apache/shardingsphere/infra/url/core/arg/URLArgumentPlaceholderTypeFactoryTest.java index aac502eea79c1..cc38343957be6 100644 --- a/infra/url/core/src/test/java/org/apache/shardingsphere/infra/url/core/arg/URLArgumentPlaceholderTypeFactoryTest.java +++ b/infra/url/core/src/test/java/org/apache/shardingsphere/infra/url/core/arg/URLArgumentPlaceholderTypeFactoryTest.java @@ -23,23 +23,17 @@ import java.util.Properties; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.jupiter.api.Assertions.assertEquals; class URLArgumentPlaceholderTypeFactoryTest { @Test void assertValueOfWithValidQueryProperties() { - assertThat(URLArgumentPlaceholderTypeFactory.valueOf(PropertiesBuilder.build(new Property("placeholder-type", "environment"))), is(URLArgumentPlaceholderType.ENVIRONMENT)); - } - - @Test - void assertValueOfWithInvalidQueryProperties() { - assertThat(URLArgumentPlaceholderTypeFactory.valueOf(PropertiesBuilder.build(new Property("placeholder-type", "invalid"))), is(URLArgumentPlaceholderType.NONE)); + assertEquals("environment", URLArgumentPlaceholderTypeFactory.valueOf(PropertiesBuilder.build(new Property("placeholder-type", "environment")))); } @Test void assertValueOfWithEmptyQueryProperties() { - assertThat(URLArgumentPlaceholderTypeFactory.valueOf(new Properties()), is(URLArgumentPlaceholderType.NONE)); + assertEquals("none", URLArgumentPlaceholderTypeFactory.valueOf(new Properties())); } }