diff --git a/spring-cloud-tencent-commons/pom.xml b/spring-cloud-tencent-commons/pom.xml index 956a004c7..5e373d142 100644 --- a/spring-cloud-tencent-commons/pom.xml +++ b/spring-cloud-tencent-commons/pom.xml @@ -101,10 +101,9 @@ - com.github.stefanbirkner - system-rules - true + uk.org.webcompere + system-stubs-junit4 + test - diff --git a/spring-cloud-tencent-commons/src/test/java/com/tencent/cloud/common/metadata/StaticMetadataManagerTest.java b/spring-cloud-tencent-commons/src/test/java/com/tencent/cloud/common/metadata/StaticMetadataManagerTest.java index 1431fc851..09a77afb5 100644 --- a/spring-cloud-tencent-commons/src/test/java/com/tencent/cloud/common/metadata/StaticMetadataManagerTest.java +++ b/spring-cloud-tencent-commons/src/test/java/com/tencent/cloud/common/metadata/StaticMetadataManagerTest.java @@ -26,13 +26,13 @@ import java.util.Set; import com.tencent.cloud.common.metadata.config.MetadataLocalProperties; import com.tencent.cloud.common.spi.InstanceMetadataProvider; -import com.tencent.cloud.common.support.EnvironmentVariable; -import com.tencent.cloud.common.support.EnvironmentVariableInjectedRunner; -import com.tencent.cloud.common.support.EnvironmentVariables; import org.junit.Assert; +import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; +import org.springframework.test.context.junit4.SpringRunner; +import uk.org.webcompere.systemstubs.rules.EnvironmentVariablesRule; import static org.mockito.Mockito.when; @@ -42,18 +42,15 @@ import static org.mockito.Mockito.when; * * @author lepdou 2022-06-27 */ -@RunWith(EnvironmentVariableInjectedRunner.class) -@EnvironmentVariables({ - @EnvironmentVariable(name = "SCT_METADATA_CONTENT_TRANSITIVE", value = "transitiveKey"), - @EnvironmentVariable(name = "SCT_METADATA_CONTENT_DISPOSABLE", value = "disposableKey"), - @EnvironmentVariable(name = "SCT_METADATA_CONTENT_transitiveKey", value = "transitiveValue"), - @EnvironmentVariable(name = "SCT_METADATA_CONTENT_disposableKey", value = "disposableValue"), - @EnvironmentVariable(name = "SCT_TRAFFIC_CONTENT_RAW_TRANSHEADERS", value = "header1,header2,header3")}) +@RunWith(SpringRunner.class) public class StaticMetadataManagerTest { @Mock private MetadataLocalProperties metadataLocalProperties; + @Rule + public EnvironmentVariablesRule rule = new EnvironmentVariablesRule(); + @Test public void testParseConfigMetadata() { Map content = new HashMap<>(); @@ -130,13 +127,13 @@ public class StaticMetadataManagerTest { new MockedMetadataProvider()); Map metadata = metadataManager.getMergedStaticMetadata(); - Assert.assertEquals(8, metadata.size()); + Assert.assertEquals(6, metadata.size()); Assert.assertEquals("v1", metadata.get("k1")); Assert.assertEquals("v22", metadata.get("k2")); Assert.assertEquals("v33", metadata.get("k3")); Map transitiveMetadata = metadataManager.getMergedStaticTransitiveMetadata(); - Assert.assertEquals(3, transitiveMetadata.size()); + Assert.assertEquals(2, transitiveMetadata.size()); Assert.assertEquals("v1", metadata.get("k1")); Assert.assertEquals("v22", metadata.get("k2")); @@ -152,6 +149,11 @@ public class StaticMetadataManagerTest { @Test public void testEnvMetadata() { + rule.set("SCT_METADATA_CONTENT_TRANSITIVE", "transitiveKey"); + rule.set("SCT_METADATA_CONTENT_DISPOSABLE", "disposableKey"); + rule.set("SCT_METADATA_CONTENT_transitiveKey", "transitiveValue"); + rule.set("SCT_METADATA_CONTENT_disposableKey", "disposableValue"); + rule.set("SCT_TRAFFIC_CONTENT_RAW_TRANSHEADERS", "header1,header2,header3"); StaticMetadataManager metadataManager = new StaticMetadataManager(metadataLocalProperties, null); Map allEnvMetadata = metadataManager.getAllEnvMetadata(); Assert.assertTrue(allEnvMetadata.containsKey("transitiveKey")); diff --git a/spring-cloud-tencent-commons/src/test/java/com/tencent/cloud/common/support/EnvironmentVariable.java b/spring-cloud-tencent-commons/src/test/java/com/tencent/cloud/common/support/EnvironmentVariable.java deleted file mode 100644 index 20f5057f3..000000000 --- a/spring-cloud-tencent-commons/src/test/java/com/tencent/cloud/common/support/EnvironmentVariable.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. - * - * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * 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 com.tencent.cloud.common.support; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Repeatable; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -/** - * @author lingxiao.wlx - */ -@Repeatable(EnvironmentVariables.class) -@Retention(RetentionPolicy.RUNTIME) -@Target({ElementType.TYPE}) -public @interface EnvironmentVariable { - - /** - * EnvironmentVariable name. - * - * @return EnvironmentVariable name - */ - String name(); - - /** - * EnvironmentVariable value. - * - * @return EnvironmentVariable value. - */ - String value(); -} diff --git a/spring-cloud-tencent-commons/src/test/java/com/tencent/cloud/common/support/EnvironmentVariableInjectedRunner.java b/spring-cloud-tencent-commons/src/test/java/com/tencent/cloud/common/support/EnvironmentVariableInjectedRunner.java deleted file mode 100644 index b48a97bd8..000000000 --- a/spring-cloud-tencent-commons/src/test/java/com/tencent/cloud/common/support/EnvironmentVariableInjectedRunner.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. - * - * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * 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 com.tencent.cloud.common.support; - -import java.util.Arrays; -import java.util.Objects; - -import org.junit.Rule; -import org.junit.runners.model.InitializationError; - -import org.springframework.core.annotation.AnnotationUtils; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; - -/** - * Enhance {@link SpringJUnit4ClassRunner} to support inject environment variable during test running. - *

- * typical usage idiom for this class would be: - *

 {@code
- * @RunWith(EnvironmentVariableInjectedRunner.class)
- * @EnvironmentVariable(name = "name",value = "value")
- * public class Test {
- * }
- * }
- * - * @author lingxiao.wlx - * @see EnvironmentVariable - * @see EnvironmentVariables - */ -public class EnvironmentVariableInjectedRunner extends SpringJUnit4ClassRunner { - - @Rule - private final org.junit.contrib.java.lang.system.EnvironmentVariables injectedEnvironmentVariables - = new org.junit.contrib.java.lang.system.EnvironmentVariables(); - - public EnvironmentVariableInjectedRunner(Class clazz) throws InitializationError { - super(clazz); - EnvironmentVariable environmentVariable = AnnotationUtils.findAnnotation(clazz, EnvironmentVariable.class); - if (!Objects.isNull(environmentVariable)) { - injectEnvironmentVariable(environmentVariable); - } - EnvironmentVariables environmentVariables = AnnotationUtils.findAnnotation(clazz, EnvironmentVariables.class); - if (!Objects.isNull(environmentVariables)) { - injectEnvironmentVariables(environmentVariables); - } - } - - private void injectEnvironmentVariables(EnvironmentVariables environmentVariables) { - Arrays.stream(environmentVariables.value()).forEach( - this::injectEnvironmentVariable - ); - } - - private void injectEnvironmentVariable(EnvironmentVariable environmentVariable) { - injectedEnvironmentVariables.set(environmentVariable.name(), environmentVariable.value()); - } -} diff --git a/spring-cloud-tencent-commons/src/test/java/com/tencent/cloud/common/support/EnvironmentVariables.java b/spring-cloud-tencent-commons/src/test/java/com/tencent/cloud/common/support/EnvironmentVariables.java deleted file mode 100644 index 416f151ec..000000000 --- a/spring-cloud-tencent-commons/src/test/java/com/tencent/cloud/common/support/EnvironmentVariables.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. - * - * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * 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 com.tencent.cloud.common.support; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -/** - * @author lingxiao.wlx - */ -@Retention(RetentionPolicy.RUNTIME) -@Target(ElementType.TYPE) -public @interface EnvironmentVariables { - - EnvironmentVariable[] value(); -} diff --git a/spring-cloud-tencent-dependencies/pom.xml b/spring-cloud-tencent-dependencies/pom.xml index 6cef9e7fc..3c89b07cf 100644 --- a/spring-cloud-tencent-dependencies/pom.xml +++ b/spring-cloud-tencent-dependencies/pom.xml @@ -80,7 +80,7 @@ 1.12.10 3.16.1 1.69 - 1.16.1 + 2.0.1 3.2.0 @@ -251,9 +251,9 @@ - com.github.stefanbirkner - system-rules - ${system-rules.version} + uk.org.webcompere + system-stubs-junit4 + ${system-stubs-junit4.version} test