From de394f352cf8228802c86486c25e8822b1b79be7 Mon Sep 17 00:00:00 2001 From: pbting <314226532@qq.com> Date: Thu, 17 Jan 2019 15:43:35 +0800 Subject: [PATCH] add nacos config unit test --- .../src/main/resources/bootstrap.properties | 6 +- spring-cloud-alibaba-nacos-config/pom.xml | 5 + .../client/NacosPropertySourceLocator.java | 8 +- .../alibaba/nacos/BaseNacosConfigTests.java | 101 ++++++++++++ .../cloud/alibaba/nacos/EndpointTests.java | 43 +++++ .../NacosConfigAutoConfigurationTests.java | 89 +++++------ .../NacosConfigHealthIndicatorTests.java | 82 ++++++++++ .../NacosPropertySourceBuilderTests.java | 148 ++++++++++++++++++ .../nacos/NacosSharedAndExtConfigTests.java | 89 +++++++++++ 9 files changed, 514 insertions(+), 57 deletions(-) create mode 100644 spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/BaseNacosConfigTests.java create mode 100644 spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/EndpointTests.java create mode 100644 spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/NacosConfigHealthIndicatorTests.java create mode 100644 spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/NacosPropertySourceBuilderTests.java create mode 100644 spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/NacosSharedAndExtConfigTests.java diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/src/main/resources/bootstrap.properties b/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/src/main/resources/bootstrap.properties index 6adb653c..831d8ff7 100644 --- a/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/src/main/resources/bootstrap.properties +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/src/main/resources/bootstrap.properties @@ -1,2 +1,4 @@ -spring.application.name=nacos-config-example -spring.cloud.nacos.config.server-addr=127.0.0.1:8848 \ No newline at end of file +spring.application.name=sca-nacos-config +spring.cloud.nacos.config.server-addr=127.0.0.1:8848 +spring.cloud.nacos.config.shared-data-ids=base-common.properties,common.properties +spring.cloud.nacos.config.refreshable-dataids=common.properties \ No newline at end of file diff --git a/spring-cloud-alibaba-nacos-config/pom.xml b/spring-cloud-alibaba-nacos-config/pom.xml index 05a7f5bd..b48b8655 100644 --- a/spring-cloud-alibaba-nacos-config/pom.xml +++ b/spring-cloud-alibaba-nacos-config/pom.xml @@ -65,6 +65,11 @@ true + + org.springframework.boot + spring-boot-starter-web + test + org.springframework.boot spring-boot-starter-test diff --git a/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/client/NacosPropertySourceLocator.java b/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/client/NacosPropertySourceLocator.java index 5ffb7ac3..e1c73105 100644 --- a/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/client/NacosPropertySourceLocator.java +++ b/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/client/NacosPropertySourceLocator.java @@ -182,19 +182,19 @@ public class NacosPropertySourceLocator implements PropertySourceLocator { } } - private static void checkDataIdFileExtension(String[] sharedDataIdArry) { + private static void checkDataIdFileExtension(String[] dataIdArray) { StringBuilder stringBuilder = new StringBuilder(); - for (int i = 0; i < sharedDataIdArry.length; i++) { + for (int i = 0; i < dataIdArray.length; i++) { boolean isLegal = false; for (String fileExtension : SUPPORT_FILE_EXTENSION) { - if (sharedDataIdArry[i].indexOf(fileExtension) > 0) { + if (dataIdArray[i].indexOf(fileExtension) > 0) { isLegal = true; break; } } // add tips if (!isLegal) { - stringBuilder.append(sharedDataIdArry[i] + ","); + stringBuilder.append(dataIdArray[i] + ","); } } diff --git a/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/BaseNacosConfigTests.java b/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/BaseNacosConfigTests.java new file mode 100644 index 00000000..aa01246e --- /dev/null +++ b/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/BaseNacosConfigTests.java @@ -0,0 +1,101 @@ +/* + * 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); + } + } +} \ No newline at end of file diff --git a/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/EndpointTests.java b/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/EndpointTests.java new file mode 100644 index 00000000..30260cba --- /dev/null +++ b/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/EndpointTests.java @@ -0,0 +1,43 @@ +/* + * 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.Test; +import org.springframework.cloud.alibaba.nacos.endpoint.NacosConfigEndpoint; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author pbting + * @date 2019-01-17 2:25 PM + */ +public class EndpointTests extends BaseNacosConfigTests { + + @Test + public void nacosConfigEndpoint() { + + NacosConfigEndpoint nacosConfigEndpoint = this.context + .getBean(NacosConfigEndpoint.class); + assertThat(nacosConfigEndpoint != null).isEqualTo(true); + } + + @Test + public void endpointInvoke() { + NacosConfigEndpoint nacosConfigEndpoint = this.context + .getBean(NacosConfigEndpoint.class); + assertThat(nacosConfigEndpoint.invoke() != null).isEqualTo(true); + } +} \ No newline at end of file diff --git a/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/NacosConfigAutoConfigurationTests.java b/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/NacosConfigAutoConfigurationTests.java index 4ecb4ddf..1964b753 100644 --- a/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/NacosConfigAutoConfigurationTests.java +++ b/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/NacosConfigAutoConfigurationTests.java @@ -16,59 +16,60 @@ package org.springframework.cloud.alibaba.nacos; -import org.junit.After; -import org.junit.Before; import org.junit.Test; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.WebApplicationType; -import org.springframework.boot.autoconfigure.AutoConfigureBefore; -import org.springframework.boot.builder.SpringApplicationBuilder; +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.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 org.springframework.core.env.Environment; +import org.springframework.core.env.CompositePropertySource; +import org.springframework.core.env.PropertySource; import static org.assertj.core.api.Assertions.assertThat; /** * @author xiaojing */ -public class NacosConfigAutoConfigurationTests { - - private ConfigurableApplicationContext context; - - @Before - public void setUp() throws Exception { - this.context = new SpringApplicationBuilder( - NacosConfigBootstrapConfiguration.class, - NacosConfigAutoConfiguration.class, TestConfiguration.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(); - } - } +public class NacosConfigAutoConfigurationTests extends BaseNacosConfigTests { + private final static Logger log = LoggerFactory + .getLogger(NacosConfigAutoConfigurationTests.class); @Test public void testNacosConfigProperties() { - NacosConfigProperties nacosConfigProperties = this.context.getParent() + NacosConfigProperties nacosConfigProperties = context.getParent() .getBean(NacosConfigProperties.class); assertThat(nacosConfigProperties.getFileExtension()).isEqualTo("properties"); - assertThat(nacosConfigProperties.getPrefix()).isEqualTo("test"); - assertThat(nacosConfigProperties.getName()).isEqualTo("myapp"); + // assertThat(nacosConfigProperties.getPrefix()).isEqualTo("test"); + 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.getExtConfig().get(1).isRefresh()) + .isEqualTo(true); + } + @Test + public void nacosPropertySourceLocator() { + NacosPropertySourceLocator nacosPropertySourceLocator = this.context + .getBean(NacosPropertySourceLocator.class); + PropertySource propertySource = nacosPropertySourceLocator + .locate(this.context.getEnvironment()); + + 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"); } @Test @@ -80,18 +81,4 @@ public class NacosConfigAutoConfigurationTests { } - @Configuration - @AutoConfigureBefore(NacosConfigAutoConfiguration.class) - static class TestConfiguration { - - @Autowired - ConfigurableApplicationContext context; - - @Bean - ContextRefresher contextRefresher() { - return new ContextRefresher(context, new RefreshScope()); - } - - } - } diff --git a/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/NacosConfigHealthIndicatorTests.java b/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/NacosConfigHealthIndicatorTests.java new file mode 100644 index 00000000..5b5d4d86 --- /dev/null +++ b/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/NacosConfigHealthIndicatorTests.java @@ -0,0 +1,82 @@ +/* + * 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.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; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +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"); + } + + @Test + public void nacosConfigHealthIndicatorInstance() { + NacosConfigHealthIndicator nacosConfigHealthIndicator = this.context + .getBean(NacosConfigHealthIndicator.class); + + assertThat(nacosConfigHealthIndicator != null).isEqualTo(true); + } + + @Test + public void testHealthCheck() { + + NacosConfigHealthIndicator nacosConfigHealthIndicator = this.context + .getBean(NacosConfigHealthIndicator.class); + + Health.Builder builder = Health.up(); + + Method method = ReflectionUtils.findMethod(NacosConfigHealthIndicator.class, + "doHealthCheck", Health.Builder.class); + ReflectionUtils.makeAccessible(method); + assertThat(method != null).isEqualTo(true); + + try { + method.invoke(nacosConfigHealthIndicator, builder); + assertThat(builder != null).isEqualTo(true); + } + catch (IllegalAccessException e) { + e.printStackTrace(); + } + catch (InvocationTargetException e) { + e.printStackTrace(); + } + + } +} \ No newline at end of file diff --git a/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/NacosPropertySourceBuilderTests.java b/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/NacosPropertySourceBuilderTests.java new file mode 100644 index 00000000..d4eb77bf --- /dev/null +++ b/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/NacosPropertySourceBuilderTests.java @@ -0,0 +1,148 @@ +/* + * 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.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +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.InvocationTargetException; +import java.lang.reflect.Method; + +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); + + @Test + public void nacosPropertySourceBuilder() { + + assertThat(nacosPropertySourceBuilderInstance() != null).isEqualTo(true); + } + + @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 { + Object result = method.invoke(nacosPropertySourceBuilder, + "ext-config-common01.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"); + } + catch (IllegalAccessException e) { + e.printStackTrace(); + } + catch (InvocationTargetException 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); + 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"); + } + catch (IllegalAccessException e) { + e.printStackTrace(); + } + catch (InvocationTargetException 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", + "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"); + } + catch (IllegalAccessException e) { + e.printStackTrace(); + } + catch (InvocationTargetException e) { + e.printStackTrace(); + } + } + + @Test + public void getEmpty() { + 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-empty.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(null); + } + catch (IllegalAccessException e) { + e.printStackTrace(); + } + catch (InvocationTargetException e) { + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/NacosSharedAndExtConfigTests.java b/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/NacosSharedAndExtConfigTests.java new file mode 100644 index 00000000..4687c05b --- /dev/null +++ b/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/NacosSharedAndExtConfigTests.java @@ -0,0 +1,89 @@ +/* + * 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.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 { + 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"); + } + + @Test + public void testSharedConfigRefresh() { + while (true) { + ContextRefresher contextRefresher = this.context + .getBean(ContextRefresher.class); + contextRefresher.refresh(); + String userName = this.context.getEnvironment().getProperty("user.name"); + try { + assertThat(userName).isEqualTo("common-value-update"); + TimeUnit.SECONDS.sleep(1); + log.info("user name is {}", userName); + } + catch (InterruptedException e) { + e.printStackTrace(); + } + } + } + + @Test + public void testExtConfigPriority() { + String userName = this.context.getEnvironment().getProperty("user.name"); + assertThat(userName).isEqualTo("ext-02-value"); + } + + @Test + public void testExtOtherGroup() { + String userExt = this.context.getEnvironment().getProperty("user.ext"); + assertThat(userExt).isEqualTo("EXT01_GROUP-value"); + } + + @Test + public void testExtRefresh() { + while (true) { + ContextRefresher contextRefresher = this.context + .getBean(ContextRefresher.class); + contextRefresher.refresh(); + String userExt = this.context.getEnvironment().getProperty("user.ext"); + try { + assertThat(userExt).isEqualTo("EXT01_GROUP-value"); + TimeUnit.SECONDS.sleep(1); + log.info("user name is {}", userExt); + } + catch (InterruptedException e) { + e.printStackTrace(); + } + } + } +} \ No newline at end of file