mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
add nacos config unit test
This commit is contained in:
parent
78911998ca
commit
431b246507
@ -80,6 +80,19 @@
|
|||||||
<artifactId>spring-cloud-test-support</artifactId>
|
<artifactId>spring-cloud-test-support</artifactId>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</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>
|
</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
|
* @author pbting
|
||||||
* @date 2019-01-17 2:25 PM
|
* @date 2019-01-17 2:25 PM
|
||||||
*/
|
*/
|
||||||
public class EndpointTests extends BaseNacosConfigTests {
|
public class EndpointTests extends NacosPowerMockitBaseTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void nacosConfigEndpoint() {
|
public void nacosConfigEndpoint() {
|
||||||
|
|
||||||
NacosConfigEndpoint nacosConfigEndpoint = this.context
|
NacosConfigEndpoint nacosConfigEndpoint = super.context
|
||||||
.getBean(NacosConfigEndpoint.class);
|
.getBean(NacosConfigEndpoint.class);
|
||||||
assertThat(nacosConfigEndpoint != null).isEqualTo(true);
|
assertThat(nacosConfigEndpoint != null).isEqualTo(true);
|
||||||
}
|
}
|
||||||
|
@ -17,8 +17,6 @@
|
|||||||
package org.springframework.cloud.alibaba.nacos;
|
package org.springframework.cloud.alibaba.nacos;
|
||||||
|
|
||||||
import org.junit.Test;
|
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.client.NacosPropertySourceLocator;
|
||||||
import org.springframework.cloud.alibaba.nacos.refresh.NacosRefreshProperties;
|
import org.springframework.cloud.alibaba.nacos.refresh.NacosRefreshProperties;
|
||||||
import org.springframework.core.env.CompositePropertySource;
|
import org.springframework.core.env.CompositePropertySource;
|
||||||
@ -28,32 +26,31 @@ import static org.assertj.core.api.Assertions.assertThat;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @author xiaojing
|
* @author xiaojing
|
||||||
|
* @author pbting
|
||||||
*/
|
*/
|
||||||
public class NacosConfigAutoConfigurationTests extends BaseNacosConfigTests {
|
public class NacosConfigAutoConfigurationTests extends NacosPowerMockitBaseTests {
|
||||||
private final static Logger log = LoggerFactory
|
|
||||||
.getLogger(NacosConfigAutoConfigurationTests.class);
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testNacosConfigProperties() {
|
public void testNacosConfigProperties() {
|
||||||
|
|
||||||
NacosConfigProperties nacosConfigProperties = context.getParent()
|
NacosConfigProperties nacosConfigProperties = context.getParent()
|
||||||
.getBean(NacosConfigProperties.class);
|
.getBean(NacosConfigProperties.class);
|
||||||
assertThat(nacosConfigProperties.getFileExtension()).isEqualTo("properties");
|
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.getName()).isEqualTo("sca-nacos-config");
|
||||||
assertThat(nacosConfigProperties.getServerAddr()).isEqualTo("127.0.0.1:8848");
|
assertThat(nacosConfigProperties.getServerAddr()).isEqualTo("127.0.0.1:8848");
|
||||||
assertThat(nacosConfigProperties.getEncode()).isEqualTo("utf-8");
|
assertThat(nacosConfigProperties.getEncode()).isEqualTo("utf-8");
|
||||||
assertThat(nacosConfigProperties.getActiveProfiles())
|
assertThat(nacosConfigProperties.getActiveProfiles())
|
||||||
.isEqualTo(new String[] { "develop" });
|
.isEqualTo(new String[] { "develop" });
|
||||||
// assertThat(nacosConfigProperties.getSharedDataids())
|
assertThat(nacosConfigProperties.getSharedDataids())
|
||||||
// .isEqualTo("base-common.properties,common.properties");
|
.isEqualTo("base-common.properties,common.properties");
|
||||||
// assertThat(nacosConfigProperties.getRefreshableDataids())
|
assertThat(nacosConfigProperties.getRefreshableDataids())
|
||||||
// .isEqualTo("base-common.properties");
|
.isEqualTo("common.properties");
|
||||||
// assertThat(nacosConfigProperties.getExtConfig().size()).isEqualTo(3);
|
assertThat(nacosConfigProperties.getExtConfig().size()).isEqualTo(3);
|
||||||
// assertThat(nacosConfigProperties.getExtConfig().get(0).getDataId())
|
assertThat(nacosConfigProperties.getExtConfig().get(0).getDataId())
|
||||||
// .isEqualTo("ext01.yaml");
|
.isEqualTo("ext00.yaml");
|
||||||
// assertThat(nacosConfigProperties.getExtConfig().get(1).getGroup())
|
assertThat(nacosConfigProperties.getExtConfig().get(1).getGroup())
|
||||||
// .isEqualTo("EXT01_GROUP");
|
.isEqualTo("EXT01_GROUP");
|
||||||
assertThat(nacosConfigProperties.getExtConfig().get(1).isRefresh())
|
assertThat(nacosConfigProperties.getExtConfig().get(1).isRefresh())
|
||||||
.isEqualTo(true);
|
.isEqualTo(true);
|
||||||
}
|
}
|
||||||
@ -67,9 +64,7 @@ public class NacosConfigAutoConfigurationTests extends BaseNacosConfigTests {
|
|||||||
|
|
||||||
assertThat(propertySource instanceof CompositePropertySource).isEqualTo(true);
|
assertThat(propertySource instanceof CompositePropertySource).isEqualTo(true);
|
||||||
CompositePropertySource compositePropertySource = (CompositePropertySource) propertySource;
|
CompositePropertySource compositePropertySource = (CompositePropertySource) propertySource;
|
||||||
// assertThat(compositePropertySource.containsProperty("user.name")).isEqualTo(true);
|
assertThat(compositePropertySource.containsProperty("user.name")).isEqualTo(true);
|
||||||
// assertThat(compositePropertySource.getProperty("user.name"))
|
|
||||||
// .isEqualTo("sca-nacos-config-test-case");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@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;
|
package org.springframework.cloud.alibaba.nacos;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.springframework.boot.WebApplicationType;
|
|
||||||
import org.springframework.boot.actuate.health.Health;
|
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.cloud.alibaba.nacos.endpoint.NacosConfigHealthIndicator;
|
||||||
import org.springframework.util.ReflectionUtils;
|
import org.springframework.util.ReflectionUtils;
|
||||||
|
|
||||||
@ -32,19 +29,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
|||||||
* @author pbting
|
* @author pbting
|
||||||
* @date 2019-01-17 2:58 PM
|
* @date 2019-01-17 2:58 PM
|
||||||
*/
|
*/
|
||||||
public class NacosConfigHealthIndicatorTests extends BaseNacosConfigTests {
|
public class NacosConfigHealthIndicatorTests extends NacosPowerMockitBaseTests {
|
||||||
|
|
||||||
@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");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void nacosConfigHealthIndicatorInstance() {
|
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;
|
package org.springframework.cloud.alibaba.nacos;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.slf4j.Logger;
|
import org.powermock.api.support.MethodProxy;
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
import org.springframework.cloud.alibaba.nacos.client.NacosPropertySource;
|
import org.springframework.cloud.alibaba.nacos.client.NacosPropertySource;
|
||||||
import org.springframework.cloud.alibaba.nacos.client.NacosPropertySourceBuilder;
|
import org.springframework.cloud.alibaba.nacos.client.NacosPropertySourceBuilder;
|
||||||
import org.springframework.util.ReflectionUtils;
|
import org.springframework.util.ReflectionUtils;
|
||||||
|
|
||||||
|
import java.lang.reflect.Constructor;
|
||||||
|
import java.lang.reflect.InvocationHandler;
|
||||||
import java.lang.reflect.InvocationTargetException;
|
import java.lang.reflect.InvocationTargetException;
|
||||||
import java.lang.reflect.Method;
|
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;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
@ -31,10 +35,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
|||||||
* @author pbting
|
* @author pbting
|
||||||
* @date 2019-01-17 11:49 AM
|
* @date 2019-01-17 11:49 AM
|
||||||
*/
|
*/
|
||||||
public class NacosPropertySourceBuilderTests extends BaseNacosConfigTests {
|
public class NacosPropertySourceBuilderTests extends NacosPowerMockitBaseTests {
|
||||||
|
|
||||||
private final static Logger log = LoggerFactory
|
|
||||||
.getLogger(NacosPropertySourceBuilderTests.class);
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void nacosPropertySourceBuilder() {
|
public void nacosPropertySourceBuilder() {
|
||||||
@ -44,79 +45,120 @@ public class NacosPropertySourceBuilderTests extends BaseNacosConfigTests {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getConfigByProperties() {
|
public void getConfigByProperties() {
|
||||||
|
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();
|
NacosPropertySourceBuilder nacosPropertySourceBuilder = nacosPropertySourceBuilderInstance();
|
||||||
|
|
||||||
Method method = ReflectionUtils.findMethod(NacosPropertySourceBuilder.class,
|
Method method = ReflectionUtils.findMethod(NacosPropertySourceBuilder.class,
|
||||||
"build", String.class, String.class, String.class, boolean.class);
|
"build", String.class, String.class, String.class, boolean.class);
|
||||||
ReflectionUtils.makeAccessible(method);
|
ReflectionUtils.makeAccessible(method);
|
||||||
assertThat(method != null).isEqualTo(true);
|
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;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
try {
|
|
||||||
Object result = method.invoke(nacosPropertySourceBuilder,
|
Object result = method.invoke(nacosPropertySourceBuilder,
|
||||||
"ext-config-common01.properties", "DEFAULT_GROUP", "properties",
|
"mock-nacos-config.properties", "DEFAULT_GROUP", "properties", true);
|
||||||
true);
|
|
||||||
assertThat(result != null).isEqualTo(true);
|
assertThat(result != null).isEqualTo(true);
|
||||||
assertThat(result instanceof NacosPropertySource).isEqualTo(true);
|
assertThat(result instanceof NacosPropertySource).isEqualTo(true);
|
||||||
NacosPropertySource nacosPropertySource = (NacosPropertySource) result;
|
NacosPropertySource nacosPropertySource = (NacosPropertySource) result;
|
||||||
// assertThat(nacosPropertySource.getProperty("ext.key"))
|
assertThat(nacosPropertySource.getProperty("dev.mode"))
|
||||||
// .isEqualTo("ext.value01");
|
.isEqualTo("local-mock");
|
||||||
}
|
}
|
||||||
catch (IllegalAccessException e) {
|
catch (Exception e) {
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
catch (InvocationTargetException e) {
|
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getConfigByYaml() {
|
public void getConfigByYaml() {
|
||||||
NacosPropertySourceBuilder nacosPropertySourceBuilder = nacosPropertySourceBuilderInstance();
|
|
||||||
|
try {
|
||||||
|
//
|
||||||
|
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,
|
Method method = ReflectionUtils.findMethod(NacosPropertySourceBuilder.class,
|
||||||
"build", String.class, String.class, String.class, boolean.class);
|
"build", String.class, String.class, String.class, boolean.class);
|
||||||
ReflectionUtils.makeAccessible(method);
|
ReflectionUtils.makeAccessible(method);
|
||||||
assertThat(method != null).isEqualTo(true);
|
assertThat(method != null).isEqualTo(true);
|
||||||
|
|
||||||
try {
|
MethodProxy.proxy(method, new InvocationHandler() {
|
||||||
Object result = method.invoke(nacosPropertySourceBuilder,
|
@Override
|
||||||
"app-local-common.yaml", "DEFAULT_GROUP", "yaml", true);
|
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 != null).isEqualTo(true);
|
||||||
assertThat(result instanceof NacosPropertySource).isEqualTo(true);
|
assertThat(result instanceof NacosPropertySource).isEqualTo(true);
|
||||||
NacosPropertySource nacosPropertySource = (NacosPropertySource) result;
|
NacosPropertySource nacosPropertySource = (NacosPropertySource) result;
|
||||||
// assertThat(nacosPropertySource.getProperty("app-local-common"))
|
assertThat(nacosPropertySource.getProperty("mock-ext-config"))
|
||||||
// .isEqualTo("update app local shared cguration for Nacos");
|
.isEqualTo("mock-ext-config-value");
|
||||||
}
|
}
|
||||||
catch (IllegalAccessException e) {
|
catch (Exception e) {
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
catch (InvocationTargetException e) {
|
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getConfigByYml() {
|
public void getConfigByYml() {
|
||||||
NacosPropertySourceBuilder nacosPropertySourceBuilder = nacosPropertySourceBuilderInstance();
|
try {
|
||||||
|
//
|
||||||
|
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,
|
Method method = ReflectionUtils.findMethod(NacosPropertySourceBuilder.class,
|
||||||
"build", String.class, String.class, String.class, boolean.class);
|
"build", String.class, String.class, String.class, boolean.class);
|
||||||
ReflectionUtils.makeAccessible(method);
|
ReflectionUtils.makeAccessible(method);
|
||||||
assertThat(method != null).isEqualTo(true);
|
assertThat(method != null).isEqualTo(true);
|
||||||
|
|
||||||
try {
|
MethodProxy.proxy(method, new InvocationHandler() {
|
||||||
Object result = method.invoke(nacosPropertySourceBuilder, "nacos.yml",
|
@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);
|
"DEFAULT_GROUP", "yml", true);
|
||||||
assertThat(result != null).isEqualTo(true);
|
assertThat(result != null).isEqualTo(true);
|
||||||
assertThat(result instanceof NacosPropertySource).isEqualTo(true);
|
assertThat(result instanceof NacosPropertySource).isEqualTo(true);
|
||||||
NacosPropertySource nacosPropertySource = (NacosPropertySource) result;
|
NacosPropertySource nacosPropertySource = (NacosPropertySource) result;
|
||||||
// assertThat(nacosPropertySource.getProperty("address"))
|
assertThat(nacosPropertySource.getProperty("mock-ext-config-yml"))
|
||||||
// .isEqualTo("zhejiang-hangzhou");
|
.isEqualTo("mock-ext-config-yml-value");
|
||||||
}
|
}
|
||||||
catch (IllegalAccessException e) {
|
catch (Exception e) {
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
catch (InvocationTargetException e) {
|
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -18,35 +18,35 @@ package org.springframework.cloud.alibaba.nacos;
|
|||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.cloud.context.refresh.ContextRefresher;
|
|
||||||
|
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author pbting
|
* @author pbting
|
||||||
* @date 2019-01-17 11:46 AM
|
* @date 2019-01-17 11:46 AM
|
||||||
*/
|
*/
|
||||||
public class NacosSharedAndExtConfigTests extends BaseNacosConfigTests {
|
public class NacosSharedAndExtConfigTests extends NacosPowerMockitBaseTests {
|
||||||
private final static Logger log = LoggerFactory
|
private final static Logger log = LoggerFactory
|
||||||
.getLogger(NacosSharedAndExtConfigTests.class);
|
.getLogger(NacosSharedAndExtConfigTests.class);
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSharedConfigPriority() {
|
public void testSharedConfigPriority() {
|
||||||
String userName = this.context.getEnvironment().getProperty("user.name");
|
String userName = this.context.getEnvironment().getProperty("user.address");
|
||||||
|
assertThat(userName).isEqualTo("zhejiang-ningbo");
|
||||||
// assertThat(userName).isEqualTo("common-value");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSharedConfigRefresh() {
|
public void testSharedConfigRefresh() {
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
ContextRefresher contextRefresher = this.context
|
// ContextRefresher contextRefresher = this.context
|
||||||
.getBean(ContextRefresher.class);
|
// .getBean(ContextRefresher.class);
|
||||||
contextRefresher.refresh();
|
// contextRefresher.refresh();
|
||||||
String userName = this.context.getEnvironment().getProperty("user.name");
|
String userName = this.context.getEnvironment().getProperty("user.address");
|
||||||
try {
|
try {
|
||||||
// assertThat(userName).isEqualTo("common-value-update");
|
assertThat(userName).isEqualTo("zhejiang-ningbo");
|
||||||
TimeUnit.SECONDS.sleep(1);
|
TimeUnit.SECONDS.sleep(1);
|
||||||
log.info("user name is {}", userName);
|
log.info("user name is {}", userName);
|
||||||
}
|
}
|
||||||
@ -60,25 +60,25 @@ public class NacosSharedAndExtConfigTests extends BaseNacosConfigTests {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testExtConfigPriority() {
|
public void testExtConfigPriority() {
|
||||||
String userName = this.context.getEnvironment().getProperty("user.name");
|
String extKey = this.context.getEnvironment().getProperty("ext.key");
|
||||||
// assertThat(userName).isEqualTo("ext-02-value");
|
assertThat(extKey).isEqualTo("ext.value02");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testExtOtherGroup() {
|
public void testExtOtherGroup() {
|
||||||
String userExt = this.context.getEnvironment().getProperty("user.ext");
|
String userExt = this.context.getEnvironment().getProperty("user.ext");
|
||||||
// assertThat(userExt).isEqualTo("EXT01_GROUP-value");
|
assertThat(userExt).isEqualTo("EXT01_GROUP-value");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testExtRefresh() {
|
public void testExtRefresh() {
|
||||||
while (true) {
|
while (true) {
|
||||||
ContextRefresher contextRefresher = this.context
|
// ContextRefresher contextRefresher = this.context
|
||||||
.getBean(ContextRefresher.class);
|
// .getBean(ContextRefresher.class);
|
||||||
contextRefresher.refresh();
|
// contextRefresher.refresh();
|
||||||
String userExt = this.context.getEnvironment().getProperty("user.ext");
|
String userExt = this.context.getEnvironment().getProperty("user.ext");
|
||||||
try {
|
try {
|
||||||
// assertThat(userExt).isEqualTo("EXT01_GROUP-value");
|
assertThat(userExt).isEqualTo("EXT01_GROUP-value");
|
||||||
TimeUnit.SECONDS.sleep(1);
|
TimeUnit.SECONDS.sleep(1);
|
||||||
log.info("user name is {}", userExt);
|
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
|
@ -0,0 +1 @@
|
|||||||
|
user.name=sca-nacos-config-value-develop
|
@ -0,0 +1,2 @@
|
|||||||
|
user.name=sca-nacos-config-value
|
||||||
|
dev.mode=local
|
Loading…
x
Reference in New Issue
Block a user