add nacos config unit test

pull/288/head
pbting 6 years ago
parent 78911998ca
commit 431b246507

@ -80,6 +80,19 @@
<artifactId>spring-cloud-test-support</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.powermock.modules.test.powermockito/powermock-modules-test-powermockito -->
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>2.0.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<version>2.0.0</version>
<scope>test</scope>
</dependency>
</dependencies>

@ -1,101 +0,0 @@
/*
* Copyright (C) 2019 the original author or authors.
*
* Licensed 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.springframework.cloud.alibaba.nacos;
import org.junit.After;
import org.junit.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.alibaba.nacos.client.NacosPropertySourceBuilder;
import org.springframework.cloud.alibaba.nacos.endpoint.NacosConfigEndpointAutoConfiguration;
import org.springframework.cloud.context.refresh.ContextRefresher;
import org.springframework.cloud.context.scope.refresh.RefreshScope;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.alibaba.nacos.api.config.ConfigService;
/**
* @author pbting
* @date 2019-01-17 11:45 AM
*/
public abstract class BaseNacosConfigTests {
protected ConfigurableApplicationContext context;
@Before
public void setUp() throws Exception {
this.context = new SpringApplicationBuilder(
NacosConfigBootstrapConfiguration.class,
NacosConfigEndpointAutoConfiguration.class,
NacosConfigAutoConfiguration.class, TestConfiguration.class)
.web(WebApplicationType.SERVLET)
.run("--spring.cloud.nacos.config.name=sca-nacos-config",
"--spring.cloud.config.enabled=true",
"--server.port=18080",
"--spring.application.name=sca-nacos-config",
"--spring.cloud.nacos.config.server-addr=127.0.0.1:8848",
// "--spring.cloud.nacos.config.prefix=test",
"--spring.cloud.nacos.config.encode=utf-8",
// "--spring.cloud.nacos.config.file-extension=yaml",
"--spring.profiles.active=develop",
"--spring.cloud.nacos.config.shared-data-ids=base-common.properties,common.properties",
"--spring.cloud.nacos.config.refreshable-dataids=common.properties",
"--spring.cloud.nacos.config.ext-config[0].data-id=ext00.yaml",
"--spring.cloud.nacos.config.ext-config[1].data-id=ext01.yaml",
"--spring.cloud.nacos.config.ext-config[1].group=EXT01_GROUP",
"--spring.cloud.nacos.config.ext-config[1].refresh=true",
"--spring.cloud.nacos.config.ext-config[2].data-id=ext02.yaml");
}
public NacosPropertySourceBuilder nacosPropertySourceBuilderInstance() {
NacosConfigProperties nacosConfigProperties = this.context
.getBean(NacosConfigProperties.class);
ConfigService configService = nacosConfigProperties.configServiceInstance();
long timeout = nacosConfigProperties.getTimeout();
NacosPropertySourceBuilder nacosPropertySourceBuilder = new NacosPropertySourceBuilder(
configService, timeout);
return nacosPropertySourceBuilder;
}
@After
public void tearDown() throws Exception {
if (this.context != null) {
this.context.close();
}
}
@EnableAutoConfiguration
@Configuration
@AutoConfigureBefore(NacosConfigAutoConfiguration.class)
static class TestConfiguration {
@Autowired
ConfigurableApplicationContext context;
@Bean
ContextRefresher contextRefresher() {
RefreshScope refreshScope = new RefreshScope();
refreshScope.setApplicationContext(context);
return new ContextRefresher(context, refreshScope);
}
}
}

@ -24,12 +24,12 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author pbting
* @date 2019-01-17 2:25 PM
*/
public class EndpointTests extends BaseNacosConfigTests {
public class EndpointTests extends NacosPowerMockitBaseTests {
@Test
public void nacosConfigEndpoint() {
NacosConfigEndpoint nacosConfigEndpoint = this.context
NacosConfigEndpoint nacosConfigEndpoint = super.context
.getBean(NacosConfigEndpoint.class);
assertThat(nacosConfigEndpoint != null).isEqualTo(true);
}

@ -17,8 +17,6 @@
package org.springframework.cloud.alibaba.nacos;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.alibaba.nacos.client.NacosPropertySourceLocator;
import org.springframework.cloud.alibaba.nacos.refresh.NacosRefreshProperties;
import org.springframework.core.env.CompositePropertySource;
@ -28,32 +26,31 @@ import static org.assertj.core.api.Assertions.assertThat;
/**
* @author xiaojing
* @author pbting
*/
public class NacosConfigAutoConfigurationTests extends BaseNacosConfigTests {
private final static Logger log = LoggerFactory
.getLogger(NacosConfigAutoConfigurationTests.class);
public class NacosConfigAutoConfigurationTests extends NacosPowerMockitBaseTests {
@Test
public void testNacosConfigProperties() {
NacosConfigProperties nacosConfigProperties = context.getParent()
.getBean(NacosConfigProperties.class);
assertThat(nacosConfigProperties.getFileExtension()).isEqualTo("properties");
// assertThat(nacosConfigProperties.getPrefix()).isEqualTo("test");
assertThat(nacosConfigProperties.getPrefix() == null).isEqualTo(true);
assertThat(nacosConfigProperties.getNamespace() == null).isEqualTo(true);
assertThat(nacosConfigProperties.getName()).isEqualTo("sca-nacos-config");
assertThat(nacosConfigProperties.getServerAddr()).isEqualTo("127.0.0.1:8848");
assertThat(nacosConfigProperties.getEncode()).isEqualTo("utf-8");
assertThat(nacosConfigProperties.getActiveProfiles())
.isEqualTo(new String[] { "develop" });
// assertThat(nacosConfigProperties.getSharedDataids())
// .isEqualTo("base-common.properties,common.properties");
// assertThat(nacosConfigProperties.getRefreshableDataids())
// .isEqualTo("base-common.properties");
// assertThat(nacosConfigProperties.getExtConfig().size()).isEqualTo(3);
// assertThat(nacosConfigProperties.getExtConfig().get(0).getDataId())
// .isEqualTo("ext01.yaml");
// assertThat(nacosConfigProperties.getExtConfig().get(1).getGroup())
// .isEqualTo("EXT01_GROUP");
assertThat(nacosConfigProperties.getSharedDataids())
.isEqualTo("base-common.properties,common.properties");
assertThat(nacosConfigProperties.getRefreshableDataids())
.isEqualTo("common.properties");
assertThat(nacosConfigProperties.getExtConfig().size()).isEqualTo(3);
assertThat(nacosConfigProperties.getExtConfig().get(0).getDataId())
.isEqualTo("ext00.yaml");
assertThat(nacosConfigProperties.getExtConfig().get(1).getGroup())
.isEqualTo("EXT01_GROUP");
assertThat(nacosConfigProperties.getExtConfig().get(1).isRefresh())
.isEqualTo(true);
}
@ -67,9 +64,7 @@ public class NacosConfigAutoConfigurationTests extends BaseNacosConfigTests {
assertThat(propertySource instanceof CompositePropertySource).isEqualTo(true);
CompositePropertySource compositePropertySource = (CompositePropertySource) propertySource;
// assertThat(compositePropertySource.containsProperty("user.name")).isEqualTo(true);
// assertThat(compositePropertySource.getProperty("user.name"))
// .isEqualTo("sca-nacos-config-test-case");
assertThat(compositePropertySource.containsProperty("user.name")).isEqualTo(true);
}
@Test

@ -1,82 +0,0 @@
/*
* Copyright (C) 2018 the original author or authors.
*
* Licensed 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.springframework.cloud.alibaba.nacos;
import java.lang.reflect.Field;
import com.alibaba.nacos.api.config.ConfigService;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.alibaba.nacos.client.NacosPropertySourceLocator;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.util.ReflectionUtils;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author xiaojing
*/
public class NacosConfigBootstrapConfigurationTests {
private ConfigurableApplicationContext context;
@Before
public void setUp() throws Exception {
this.context = new SpringApplicationBuilder(
NacosConfigBootstrapConfiguration.class).web(WebApplicationType.NONE).run(
"--spring.cloud.nacos.config.name=myapp",
"--spring.cloud.config.enabled=true",
"--spring.cloud.nacos.config.server-addr=127.0.0.1:8848",
"--spring.cloud.nacos.config.prefix=test");
}
@After
public void tearDown() throws Exception {
if (this.context != null) {
this.context.close();
}
}
@Test
public void testNacosPropertySourceLocator() {
NacosPropertySourceLocator locator = this.context
.getBean(NacosPropertySourceLocator.class);
Environment environment = this.context.getEnvironment();
try {
locator.locate(environment);
}
catch (Exception e) {
}
Field nacosConfigPropertiesField = ReflectionUtils
.findField(NacosPropertySourceLocator.class, "nacosConfigProperties");
nacosConfigPropertiesField.setAccessible(true);
NacosConfigProperties configService = (NacosConfigProperties) ReflectionUtils
.getField(nacosConfigPropertiesField, locator);
assertThat(configService).isNotNull();
}
}

@ -16,10 +16,7 @@
package org.springframework.cloud.alibaba.nacos;
import org.junit.Test;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.alibaba.nacos.endpoint.NacosConfigEndpointAutoConfiguration;
import org.springframework.cloud.alibaba.nacos.endpoint.NacosConfigHealthIndicator;
import org.springframework.util.ReflectionUtils;
@ -32,19 +29,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author pbting
* @date 2019-01-17 2:58 PM
*/
public class NacosConfigHealthIndicatorTests extends BaseNacosConfigTests {
@Override
public void setUp() throws Exception {
this.context = new SpringApplicationBuilder(
NacosConfigBootstrapConfiguration.class,
NacosConfigEndpointAutoConfiguration.class,
NacosConfigAutoConfiguration.class, TestConfiguration.class)
.web(WebApplicationType.SERVLET)
.run("--spring.cloud.config.enabled=true", "--server.port=18080",
"--spring.application.name=sca-nacos-config",
"--spring.cloud.nacos.config.server-addr=127.0.0.1:8848");
}
public class NacosConfigHealthIndicatorTests extends NacosPowerMockitBaseTests {
@Test
public void nacosConfigHealthIndicatorInstance() {

@ -0,0 +1,171 @@
/*
* Copyright (C) 2019 the original author or authors.
*
* Licensed 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.springframework.cloud.alibaba.nacos;
import com.alibaba.nacos.api.config.ConfigService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.api.support.MethodProxy;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.modules.junit4.PowerMockRunnerDelegate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.alibaba.nacos.client.NacosPropertySource;
import org.springframework.cloud.alibaba.nacos.client.NacosPropertySourceBuilder;
import org.springframework.cloud.alibaba.nacos.endpoint.NacosConfigEndpointAutoConfiguration;
import org.springframework.cloud.context.refresh.ContextRefresher;
import org.springframework.cloud.context.scope.refresh.RefreshScope;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.ReflectionUtils;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.*;
/**
* @author pbting
* @date 2019-01-17 8:54 PM
*/
@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(SpringRunner.class)
@PowerMockIgnore({ "javax.management.*", "javax.net.ssl.*" })
@PrepareForTest({ NacosPropertySourceBuilder.class })
@SpringBootTest(classes = { NacosConfigBootstrapConfiguration.class,
NacosConfigEndpointAutoConfiguration.class, NacosConfigAutoConfiguration.class,
NacosPowerMockitBaseTests.TestConfiguration.class }, properties = {
"spring.application.name=sca-nacos-config",
"spring.cloud.nacos.config.server-addr=127.0.0.1:8848",
"spring.cloud.nacos.config.name=sca-nacos-config",
// "spring.cloud.nacos.config.refresh.enabled=false",
"spring.cloud.nacos.config.encode=utf-8",
"spring.cloud.nacos.config.shared-data-ids=base-common.properties,common.properties",
"spring.cloud.nacos.config.refreshable-dataids=common.properties",
"spring.cloud.nacos.config.ext-config[0].data-id=ext00.yaml",
"spring.cloud.nacos.config.ext-config[1].data-id=ext01.yml",
"spring.cloud.nacos.config.ext-config[1].group=EXT01_GROUP",
"spring.cloud.nacos.config.ext-config[1].refresh=true",
"spring.cloud.nacos.config.ext-config[2].data-id=ext02.yaml",
"spring.profiles.active=develop", "server.port=19090" })
public class NacosPowerMockitBaseTests {
private final static List<String> DATAIDS = Arrays.asList("common.properties",
"base-common.properties", "ext00.yaml", "ext01.yml", "ext02.yaml",
"sca-nacos-config.properties", "sca-nacos-config-develop.properties");
private final static HashMap<String, Properties> VALUES = new HashMap<>();
@Autowired
protected ApplicationContext context;
static {
initDataIds();
try {
final Constructor constructor = ReflectionUtils.accessibleConstructor(
NacosPropertySource.class, String.class, String.class, Map.class,
Date.class, boolean.class);
Method method = PowerMockito.method(NacosPropertySourceBuilder.class, "build",
String.class, String.class, String.class, boolean.class);
MethodProxy.proxy(method, new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
Properties properties = VALUES.get(args[0].toString());
if (properties == null) {
properties = new Properties();
properties.put("user.name", args[0].toString());
}
Object instance = constructor.newInstance(args[1].toString(),
args[0].toString(), properties, new Date(), args[3]);
return instance;
}
});
}
catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
private static void initDataIds() {
DATAIDS.forEach(dataId -> {
String realpath = "/" + dataId;
ClassPathResource classPathResource = new ClassPathResource(realpath);
if (realpath.endsWith("properties")) {
Properties properties = new Properties();
try {
properties.load(classPathResource.getInputStream());
VALUES.put(dataId, properties);
}
catch (IOException e) {
e.printStackTrace();
}
}
if (realpath.endsWith("yaml") || realpath.endsWith("yml")) {
YamlPropertiesFactoryBean yamlFactory = new YamlPropertiesFactoryBean();
yamlFactory.setResources(classPathResource);
try {
VALUES.put(dataId, yamlFactory.getObject());
}
catch (Exception e) {
e.printStackTrace();
}
}
});
}
public NacosPropertySourceBuilder nacosPropertySourceBuilderInstance() {
NacosConfigProperties nacosConfigProperties = this.context
.getBean(NacosConfigProperties.class);
ConfigService configService = nacosConfigProperties.configServiceInstance();
long timeout = nacosConfigProperties.getTimeout();
NacosPropertySourceBuilder nacosPropertySourceBuilder = new NacosPropertySourceBuilder(
configService, timeout);
return nacosPropertySourceBuilder;
}
@Configuration
@AutoConfigureBefore(NacosConfigAutoConfiguration.class)
static class TestConfiguration {
@Autowired
ConfigurableApplicationContext context;
@Bean
ContextRefresher contextRefresher() {
RefreshScope refreshScope = new RefreshScope();
refreshScope.setApplicationContext(context);
return new ContextRefresher(context, refreshScope);
}
}
@Test
public void testAppContext() {
System.err.println(this.context);
}
}

@ -16,14 +16,18 @@
package org.springframework.cloud.alibaba.nacos;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.powermock.api.support.MethodProxy;
import org.springframework.cloud.alibaba.nacos.client.NacosPropertySource;
import org.springframework.cloud.alibaba.nacos.client.NacosPropertySourceBuilder;
import org.springframework.util.ReflectionUtils;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import static org.assertj.core.api.Assertions.assertThat;
@ -31,10 +35,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author pbting
* @date 2019-01-17 11:49 AM
*/
public class NacosPropertySourceBuilderTests extends BaseNacosConfigTests {
private final static Logger log = LoggerFactory
.getLogger(NacosPropertySourceBuilderTests.class);
public class NacosPropertySourceBuilderTests extends NacosPowerMockitBaseTests {
@Test
public void nacosPropertySourceBuilder() {
@ -44,79 +45,120 @@ public class NacosPropertySourceBuilderTests extends BaseNacosConfigTests {
@Test
public void getConfigByProperties() {
NacosPropertySourceBuilder nacosPropertySourceBuilder = nacosPropertySourceBuilderInstance();
Method method = ReflectionUtils.findMethod(NacosPropertySourceBuilder.class,
"build", String.class, String.class, String.class, boolean.class);
ReflectionUtils.makeAccessible(method);
assertThat(method != null).isEqualTo(true);
try {
final HashMap<String, String> value = new HashMap<>();
value.put("dev.mode", "local-mock");
final Constructor constructor = ReflectionUtils.accessibleConstructor(
NacosPropertySource.class, String.class, String.class, Map.class,
Date.class, boolean.class);
NacosPropertySourceBuilder nacosPropertySourceBuilder = nacosPropertySourceBuilderInstance();
Method method = ReflectionUtils.findMethod(NacosPropertySourceBuilder.class,
"build", String.class, String.class, String.class, boolean.class);
ReflectionUtils.makeAccessible(method);
assertThat(method != null).isEqualTo(true);
MethodProxy.proxy(method, new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
Object instance = constructor.newInstance(args[1].toString(),
args[0].toString(), value, new Date(), args[3]);
return instance;
}
});
Object result = method.invoke(nacosPropertySourceBuilder,
"ext-config-common01.properties", "DEFAULT_GROUP", "properties",
true);
"mock-nacos-config.properties", "DEFAULT_GROUP", "properties", true);
assertThat(result != null).isEqualTo(true);
assertThat(result instanceof NacosPropertySource).isEqualTo(true);
NacosPropertySource nacosPropertySource = (NacosPropertySource) result;
// assertThat(nacosPropertySource.getProperty("ext.key"))
// .isEqualTo("ext.value01");
assertThat(nacosPropertySource.getProperty("dev.mode"))
.isEqualTo("local-mock");
}
catch (IllegalAccessException e) {
e.printStackTrace();
}
catch (InvocationTargetException e) {
catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void getConfigByYaml() {
NacosPropertySourceBuilder nacosPropertySourceBuilder = nacosPropertySourceBuilderInstance();
Method method = ReflectionUtils.findMethod(NacosPropertySourceBuilder.class,
"build", String.class, String.class, String.class, boolean.class);
ReflectionUtils.makeAccessible(method);
assertThat(method != null).isEqualTo(true);
try {
Object result = method.invoke(nacosPropertySourceBuilder,
"app-local-common.yaml", "DEFAULT_GROUP", "yaml", true);
//
final HashMap<String, String> value = new HashMap<>();
value.put("mock-ext-config", "mock-ext-config-value");
final Constructor constructor = ReflectionUtils.accessibleConstructor(
NacosPropertySource.class, String.class, String.class, Map.class,
Date.class, boolean.class);
Method method = ReflectionUtils.findMethod(NacosPropertySourceBuilder.class,
"build", String.class, String.class, String.class, boolean.class);
ReflectionUtils.makeAccessible(method);
assertThat(method != null).isEqualTo(true);
MethodProxy.proxy(method, new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
Object instance = constructor.newInstance(args[1].toString(),
args[0].toString(), value, new Date(), args[3]);
return instance;
}
});
NacosPropertySourceBuilder nacosPropertySourceBuilder = nacosPropertySourceBuilderInstance();
Object result = method.invoke(nacosPropertySourceBuilder, "ext-config.yaml",
"DEFAULT_GROUP", "yaml", true);
assertThat(result != null).isEqualTo(true);
assertThat(result instanceof NacosPropertySource).isEqualTo(true);
NacosPropertySource nacosPropertySource = (NacosPropertySource) result;
// assertThat(nacosPropertySource.getProperty("app-local-common"))
// .isEqualTo("update app local shared cguration for Nacos");
assertThat(nacosPropertySource.getProperty("mock-ext-config"))
.isEqualTo("mock-ext-config-value");
}
catch (IllegalAccessException e) {
e.printStackTrace();
}
catch (InvocationTargetException e) {
catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void getConfigByYml() {
NacosPropertySourceBuilder nacosPropertySourceBuilder = nacosPropertySourceBuilderInstance();
Method method = ReflectionUtils.findMethod(NacosPropertySourceBuilder.class,
"build", String.class, String.class, String.class, boolean.class);
ReflectionUtils.makeAccessible(method);
assertThat(method != null).isEqualTo(true);
try {
Object result = method.invoke(nacosPropertySourceBuilder, "nacos.yml",
//
final HashMap<String, String> value = new HashMap<>();
value.put("mock-ext-config-yml", "mock-ext-config-yml-value");
final Constructor constructor = ReflectionUtils.accessibleConstructor(
NacosPropertySource.class, String.class, String.class, Map.class,
Date.class, boolean.class);
Method method = ReflectionUtils.findMethod(NacosPropertySourceBuilder.class,
"build", String.class, String.class, String.class, boolean.class);
ReflectionUtils.makeAccessible(method);
assertThat(method != null).isEqualTo(true);
MethodProxy.proxy(method, new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
Object instance = constructor.newInstance(args[1].toString(),
args[0].toString(), value, new Date(), args[3]);
return instance;
}
});
NacosPropertySourceBuilder nacosPropertySourceBuilder = nacosPropertySourceBuilderInstance();
Object result = method.invoke(nacosPropertySourceBuilder, "ext-config.yml",
"DEFAULT_GROUP", "yml", true);
assertThat(result != null).isEqualTo(true);
assertThat(result instanceof NacosPropertySource).isEqualTo(true);
NacosPropertySource nacosPropertySource = (NacosPropertySource) result;
// assertThat(nacosPropertySource.getProperty("address"))
// .isEqualTo("zhejiang-hangzhou");
assertThat(nacosPropertySource.getProperty("mock-ext-config-yml"))
.isEqualTo("mock-ext-config-yml-value");
}
catch (IllegalAccessException e) {
e.printStackTrace();
}
catch (InvocationTargetException e) {
catch (Exception e) {
e.printStackTrace();
}
}

@ -18,35 +18,35 @@ package org.springframework.cloud.alibaba.nacos;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.context.refresh.ContextRefresher;
import java.util.concurrent.TimeUnit;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author pbting
* @date 2019-01-17 11:46 AM
*/
public class NacosSharedAndExtConfigTests extends BaseNacosConfigTests {
public class NacosSharedAndExtConfigTests extends NacosPowerMockitBaseTests {
private final static Logger log = LoggerFactory
.getLogger(NacosSharedAndExtConfigTests.class);
@Test
public void testSharedConfigPriority() {
String userName = this.context.getEnvironment().getProperty("user.name");
// assertThat(userName).isEqualTo("common-value");
String userName = this.context.getEnvironment().getProperty("user.address");
assertThat(userName).isEqualTo("zhejiang-ningbo");
}
@Test
public void testSharedConfigRefresh() {
while (true) {
ContextRefresher contextRefresher = this.context
.getBean(ContextRefresher.class);
contextRefresher.refresh();
String userName = this.context.getEnvironment().getProperty("user.name");
// ContextRefresher contextRefresher = this.context
// .getBean(ContextRefresher.class);
// contextRefresher.refresh();
String userName = this.context.getEnvironment().getProperty("user.address");
try {
// assertThat(userName).isEqualTo("common-value-update");
assertThat(userName).isEqualTo("zhejiang-ningbo");
TimeUnit.SECONDS.sleep(1);
log.info("user name is {}", userName);
}
@ -60,25 +60,25 @@ public class NacosSharedAndExtConfigTests extends BaseNacosConfigTests {
@Test
public void testExtConfigPriority() {
String userName = this.context.getEnvironment().getProperty("user.name");
// assertThat(userName).isEqualTo("ext-02-value");
String extKey = this.context.getEnvironment().getProperty("ext.key");
assertThat(extKey).isEqualTo("ext.value02");
}
@Test
public void testExtOtherGroup() {
String userExt = this.context.getEnvironment().getProperty("user.ext");
// assertThat(userExt).isEqualTo("EXT01_GROUP-value");
assertThat(userExt).isEqualTo("EXT01_GROUP-value");
}
@Test
public void testExtRefresh() {
while (true) {
ContextRefresher contextRefresher = this.context
.getBean(ContextRefresher.class);
contextRefresher.refresh();
// ContextRefresher contextRefresher = this.context
// .getBean(ContextRefresher.class);
// contextRefresher.refresh();
String userExt = this.context.getEnvironment().getProperty("user.ext");
try {
// assertThat(userExt).isEqualTo("EXT01_GROUP-value");
assertThat(userExt).isEqualTo("EXT01_GROUP-value");
TimeUnit.SECONDS.sleep(1);
log.info("user name is {}", userExt);
}

@ -0,0 +1,2 @@
user.name=base-common-value
user.address=zhejiang-hangzhou

@ -0,0 +1,2 @@
user.name=common-value
user.address=zhejiang-ningbo

@ -0,0 +1,4 @@
user:
name: ext-00-value
ext:
key: ext.value00

@ -0,0 +1,5 @@
user:
name: ext-01-value
ext: EXT01_GROUP-value
ext:
key: ext.value01

@ -0,0 +1,5 @@
user:
name: ext-02-value
ext:
key: ext.value02
app-local-common: update app local shared cguration for Nacos

@ -42,7 +42,7 @@ import org.springframework.test.context.junit4.SpringRunner;
@SpringBootTest(classes = NacosAutoServiceRegistrationIpTests.TestConfig.class, properties = {
"spring.application.name=myTestService1",
"spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848",
"spring.cloud.nacos.discovery.ip=123.123.123.123" }, webEnvironment = RANDOM_PORT)
"spring.cloud.nacos.discovery.ip=123.123.123.123" }, webEnvironment = RANDOM_PORT)
public class NacosAutoServiceRegistrationIpTests {
@Autowired

Loading…
Cancel
Save