feature:support inject environment variable during test running.

pull/695/head
wulingxiao 3 years ago
parent 4ebfd3128f
commit df79539508

@ -99,6 +99,12 @@
<artifactId>spring-boot-actuator-autoconfigure</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.github.stefanbirkner</groupId>
<artifactId>system-rules</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
</project>

@ -23,9 +23,13 @@ import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.locks.Lock;
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.Test;
import org.junit.runner.RunWith;
@ -42,7 +46,14 @@ import static org.mockito.Mockito.when;
*
* @author lepdou 2022-06-27
*/
@RunWith(MockitoJUnitRunner.class)
@RunWith(EnvironmentVariableInjectedRunner.class)
@EnvironmentVariables(
value = {
@EnvironmentVariable(name = "SCT_METADATA_CONTENT_TRANSITIVE", value = "tkey1,tkey2,tkey3"),
@EnvironmentVariable(name = "SCT_METADATA_CONTENT_DISPOSABLE", value = "dkey1,dkey2,dkey3"),
@EnvironmentVariable(name = "SCT_METADATA_CONTENT_KEY", value = "key")
}
)
public class StaticMetadataManagerTest {
@Mock

@ -0,0 +1,48 @@
/*
* 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();
}

@ -0,0 +1,71 @@
/*
* 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.
* <p>
* typical usage idiom for this class would be:
* <pre> {@code
* @RunWith(EnvironmentVariableInjectedRunner.class)
* @EnvironmentVariable(name = "name",value = "value")
* public class Test {
* }
* }</pre>
*
* @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());
}
}

@ -0,0 +1,34 @@
/*
* 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();
}

@ -80,6 +80,7 @@
<byte-buddy.version>1.12.10</byte-buddy.version>
<protobuf-java.version>3.16.1</protobuf-java.version>
<bcprov-jdk15on.version>1.69</bcprov-jdk15on.version>
<system-rules.version>1.16.1</system-rules.version>
<!-- Maven Plugin Versions -->
<maven-source-plugin.version>3.2.0</maven-source-plugin.version>
@ -248,6 +249,13 @@
<version>${mocktio.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.stefanbirkner</groupId>
<artifactId>system-rules</artifactId>
<version>${system-rules.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>

Loading…
Cancel
Save