diff --git a/spring-cloud-starter-tencent-metadata-transfer/pom.xml b/spring-cloud-starter-tencent-metadata-transfer/pom.xml index 642bbec1a..e0aec90a1 100644 --- a/spring-cloud-starter-tencent-metadata-transfer/pom.xml +++ b/spring-cloud-starter-tencent-metadata-transfer/pom.xml @@ -50,6 +50,12 @@ spring-cloud-starter-loadbalancer test + + + com.github.stefanbirkner + system-rules + test + diff --git a/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/support/DynamicEnvironmentVariable.java b/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/support/DynamicEnvironmentVariable.java new file mode 100644 index 000000000..76b76ee7e --- /dev/null +++ b/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/support/DynamicEnvironmentVariable.java @@ -0,0 +1,50 @@ +/* + * 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.metadata.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; + +/** + * DynamicEnvironmentVariable. + * + * @author lingxiao.wlx + */ +@Repeatable(DynamicEnvironmentVariables.class) +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.TYPE}) +public @interface DynamicEnvironmentVariable { + + /** + * EnvironmentVariable name. + * + * @return EnvironmentVariable name. + */ + String name(); + + /** + * EnvironmentVariable value. + * + * @return EnvironmentVariable value. + */ + String value(); +} diff --git a/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/support/DynamicEnvironmentVariables.java b/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/support/DynamicEnvironmentVariables.java new file mode 100644 index 000000000..173c22e40 --- /dev/null +++ b/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/support/DynamicEnvironmentVariables.java @@ -0,0 +1,41 @@ +/* + * 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.metadata.support; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * DynamicEnvironmentVariables for multiDynamicEnvironmentVariable. + * + * @author lingxiao.wlx + */ +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.TYPE}) +public @interface DynamicEnvironmentVariables { + + /** + * DynamicEnvironmentVariable array. + * + * @return dynamicEnvironmentVariable array. + */ + DynamicEnvironmentVariable[] value(); +} diff --git a/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/support/DynamicEnvironmentVariablesSpringJUnit4ClassRunner.java b/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/support/DynamicEnvironmentVariablesSpringJUnit4ClassRunner.java new file mode 100644 index 000000000..4f3285ea1 --- /dev/null +++ b/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/support/DynamicEnvironmentVariablesSpringJUnit4ClassRunner.java @@ -0,0 +1,64 @@ +/* + * 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.metadata.support; + +import java.util.Arrays; +import java.util.Objects; + +import org.junit.Rule; +import org.junit.contrib.java.lang.system.EnvironmentVariables; +import org.junit.runners.model.InitializationError; + +import org.springframework.core.annotation.AnnotationUtils; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +/** + * Enhance {@link SpringJUnit4ClassRunner} to support add environment variables when Junit4 test run. + * + * + * @author lingxiao.wlx + * @see DynamicEnvironmentVariable + * @see DynamicEnvironmentVariables + */ +public class DynamicEnvironmentVariablesSpringJUnit4ClassRunner extends SpringJUnit4ClassRunner { + + /** + * EnvironmentVariables. + */ + @Rule + public final EnvironmentVariables environmentVariables = new EnvironmentVariables(); + + public DynamicEnvironmentVariablesSpringJUnit4ClassRunner(Class clazz) throws InitializationError { + super(clazz); + DynamicEnvironmentVariable dynamicEnvironmentVariable = AnnotationUtils.findAnnotation(clazz, DynamicEnvironmentVariable.class); + if (!Objects.isNull(dynamicEnvironmentVariable)) { + String key = dynamicEnvironmentVariable.name(); + String value = dynamicEnvironmentVariable.value(); + environmentVariables.set(key, value); + } + DynamicEnvironmentVariables dynamicEnvironmentVariables = AnnotationUtils.findAnnotation(clazz, DynamicEnvironmentVariables.class); + if (!Objects.isNull(dynamicEnvironmentVariables)) { + Arrays.stream(dynamicEnvironmentVariables.value()).forEach( + environmentVariable -> { + environmentVariables.set(environmentVariable.name(), environmentVariable.value()); + } + ); + } + } +} diff --git a/spring-cloud-tencent-dependencies/pom.xml b/spring-cloud-tencent-dependencies/pom.xml index cd8e6ad9c..9a4b035d3 100644 --- a/spring-cloud-tencent-dependencies/pom.xml +++ b/spring-cloud-tencent-dependencies/pom.xml @@ -80,6 +80,7 @@ 1.12.10 3.16.1 1.69 + 1.16.1 3.2.0 @@ -248,6 +249,13 @@ ${mocktio.version} test + + + com.github.stefanbirkner + system-rules + ${system-rules.version} + test +