You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by pa...@apache.org on 2024/02/14 05:12:56 UTC
(shardingsphere) branch master updated: Refactor AbsolutePathWithEnvironmentURLProviderTest (#30118)
This is an automated email from the ASF dual-hosted git repository.
panjuan 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 cf9f6eaf009 Refactor AbsolutePathWithEnvironmentURLProviderTest (#30118)
cf9f6eaf009 is described below
commit cf9f6eaf009b77e7286e7271ddab0fdd95c181af
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Wed Feb 14 13:12:50 2024 +0800
Refactor AbsolutePathWithEnvironmentURLProviderTest (#30118)
---
.../AbsolutePathWithEnvironmentURLProvider.java | 5 +---
.../ClasspathWithEnvironmentURLProvider.java | 5 +---
...AbsolutePathWithEnvironmentURLProviderTest.java | 29 ++++++++++++++--------
.../ClasspathWithEnvironmentURLProviderTest.java | 27 +++++++++++++-------
4 files changed, 39 insertions(+), 27 deletions(-)
diff --git a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/spi/absolutepath/AbsolutePathWithEnvironmentURLProvider.java b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/spi/absolutepath/AbsolutePathWithEnvironmentURLProvider.java
index fee18054106..7a524fb2134 100644
--- a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/spi/absolutepath/AbsolutePathWithEnvironmentURLProvider.java
+++ b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/spi/absolutepath/AbsolutePathWithEnvironmentURLProvider.java
@@ -67,11 +67,8 @@ public final class AbsolutePathWithEnvironmentURLProvider implements AbstractAbs
return ArgsUtils.replaceArg(envValue, envNameAndDefaultValue[1], matcher);
}
- /**
+ /*
* This method is only used for mocking environment variables in unit tests and should not be used under any circumstances.
- *
- * @param name the name of the environment variable
- * @return the string value of the variable, or null if the variable is not defined in the system environment
*/
String getEnvironmentVariables(final String name) {
return System.getenv(name);
diff --git a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/spi/classpath/ClasspathWithEnvironmentURLProvider.java b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/spi/classpath/ClasspathWithEnvironmentURLProvider.java
index 9656de78923..6128fe2dfa3 100644
--- a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/spi/classpath/ClasspathWithEnvironmentURLProvider.java
+++ b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/spi/classpath/ClasspathWithEnvironmentURLProvider.java
@@ -65,11 +65,8 @@ public final class ClasspathWithEnvironmentURLProvider implements AbstractClassp
return ArgsUtils.replaceArg(envValue, envNameAndDefaultValue[1], matcher);
}
- /**
+ /*
* This method is only used for mocking environment variables in unit tests and should not be used under any circumstances.
- *
- * @param name the name of the environment variable
- * @return the string value of the variable, or null if the variable is not defined in the system environment
*/
String getEnvironmentVariables(final String name) {
return System.getenv(name);
diff --git a/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/driver/spi/absolutepath/AbsolutePathWithEnvironmentURLProviderTest.java b/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/driver/spi/absolutepath/AbsolutePathWithEnvironmentURLProviderTest.java
index c25e38e5c33..4dc8a7c1801 100644
--- a/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/driver/spi/absolutepath/AbsolutePathWithEnvironmentURLProviderTest.java
+++ b/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/driver/spi/absolutepath/AbsolutePathWithEnvironmentURLProviderTest.java
@@ -30,15 +30,24 @@ import static org.mockito.Mockito.when;
class AbsolutePathWithEnvironmentURLProviderTest {
@Test
- void assertReplaceEnvironmentVariables() {
- final String urlPrefix = "jdbc:shardingsphere:";
- AbsolutePathWithEnvironmentURLProvider spy = spy(new AbsolutePathWithEnvironmentURLProvider());
- when(spy.getEnvironmentVariables("FIXTURE_JDBC_URL")).thenReturn("jdbc:h2:mem:foo_ds_1;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MySQL");
- when(spy.getEnvironmentVariables("FIXTURE_USERNAME")).thenReturn("sa");
- String absoluteOriginPath = Objects.requireNonNull(Thread.currentThread().getContextClassLoader().getResource("config/driver/foo-driver-fixture.yaml")).getPath();
- byte[] actualOrigin = ShardingSphereURLManager.getContent("jdbc:shardingsphere:absolutepath:" + absoluteOriginPath, urlPrefix);
- String absolutePath = Objects.requireNonNull(Thread.currentThread().getContextClassLoader().getResource("config/driver/foo-driver-environment-variables-fixture.yaml")).getPath();
- byte[] actual = spy.getContent("jdbc:shardingsphere:absolutepath-environment:" + absolutePath, absolutePath);
- assertThat(actual, is(actualOrigin));
+ void assertGetContent() {
+ assertThat(getActual(createURLProvider()), is(getExpected()));
+ }
+
+ private AbsolutePathWithEnvironmentURLProvider createURLProvider() {
+ AbsolutePathWithEnvironmentURLProvider result = spy(new AbsolutePathWithEnvironmentURLProvider());
+ when(result.getEnvironmentVariables("FIXTURE_JDBC_URL")).thenReturn("jdbc:h2:mem:foo_ds_1;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MySQL");
+ when(result.getEnvironmentVariables("FIXTURE_USERNAME")).thenReturn("sa");
+ return result;
+ }
+
+ private byte[] getActual(final AbsolutePathWithEnvironmentURLProvider urlProvider) {
+ String absoluteActualPath = Objects.requireNonNull(Thread.currentThread().getContextClassLoader().getResource("config/driver/foo-driver-environment-variables-fixture.yaml")).getPath();
+ return urlProvider.getContent("jdbc:shardingsphere:absolutepath-environment:" + absoluteActualPath, absoluteActualPath);
+ }
+
+ private byte[] getExpected() {
+ String absoluteExpectedPath = Objects.requireNonNull(Thread.currentThread().getContextClassLoader().getResource("config/driver/foo-driver-fixture.yaml")).getPath();
+ return ShardingSphereURLManager.getContent("jdbc:shardingsphere:absolutepath:" + absoluteExpectedPath, "jdbc:shardingsphere:");
}
}
diff --git a/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/driver/spi/classpath/ClasspathWithEnvironmentURLProviderTest.java b/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/driver/spi/classpath/ClasspathWithEnvironmentURLProviderTest.java
index 9b9cae43d3f..8e529c593e0 100644
--- a/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/driver/spi/classpath/ClasspathWithEnvironmentURLProviderTest.java
+++ b/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/driver/spi/classpath/ClasspathWithEnvironmentURLProviderTest.java
@@ -28,14 +28,23 @@ import static org.mockito.Mockito.when;
class ClasspathWithEnvironmentURLProviderTest {
@Test
- void assertReplaceEnvironmentVariables() {
- final String urlPrefix = "jdbc:shardingsphere:";
- ClasspathWithEnvironmentURLProvider spy = spy(new ClasspathWithEnvironmentURLProvider());
- when(spy.getEnvironmentVariables("FIXTURE_JDBC_URL")).thenReturn("jdbc:h2:mem:foo_ds_1;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MySQL");
- when(spy.getEnvironmentVariables("FIXTURE_USERNAME")).thenReturn("sa");
- byte[] actual = spy.getContent("jdbc:shardingsphere:classpath-environment:config/driver/foo-driver-environment-variables-fixture.yaml",
- "config/driver/foo-driver-environment-variables-fixture.yaml");
- byte[] actualOrigin = ShardingSphereURLManager.getContent("jdbc:shardingsphere:classpath:config/driver/foo-driver-fixture.yaml", urlPrefix);
- assertThat(actual, is(actualOrigin));
+ void assertGetContent() {
+ assertThat(getActual(createURLProvider()), is(getExpected()));
+ }
+
+ private ClasspathWithEnvironmentURLProvider createURLProvider() {
+ ClasspathWithEnvironmentURLProvider result = spy(new ClasspathWithEnvironmentURLProvider());
+ when(result.getEnvironmentVariables("FIXTURE_JDBC_URL")).thenReturn("jdbc:h2:mem:foo_ds_1;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MySQL");
+ when(result.getEnvironmentVariables("FIXTURE_USERNAME")).thenReturn("sa");
+ return result;
+ }
+
+ private byte[] getActual(final ClasspathWithEnvironmentURLProvider urlProvider) {
+ return urlProvider.getContent(
+ "jdbc:shardingsphere:classpath-environment:config/driver/foo-driver-environment-variables-fixture.yaml", "config/driver/foo-driver-environment-variables-fixture.yaml");
+ }
+
+ private byte[] getExpected() {
+ return ShardingSphereURLManager.getContent("jdbc:shardingsphere:classpath:config/driver/foo-driver-fixture.yaml", "jdbc:shardingsphere:");
}
}