diff --git a/spring-cloud-alibaba-nacos-config/pom.xml b/spring-cloud-alibaba-nacos-config/pom.xml index b48b8655a..8096c7139 100644 --- a/spring-cloud-alibaba-nacos-config/pom.xml +++ b/spring-cloud-alibaba-nacos-config/pom.xml @@ -80,6 +80,19 @@ spring-cloud-test-support test + + + org.powermock + powermock-module-junit4 + 2.0.0 + test + + + org.powermock + powermock-api-mockito2 + 2.0.0 + test + 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 deleted file mode 100644 index aa01246e6..000000000 --- a/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/BaseNacosConfigTests.java +++ /dev/null @@ -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); - } - } -} \ 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 index 30260cbaa..5ef53f032 100644 --- 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 @@ -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); } 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 a6a584348..23fc2bd42 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 @@ -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 diff --git a/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/NacosConfigBootstrapConfigurationTests.java b/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/NacosConfigBootstrapConfigurationTests.java deleted file mode 100644 index 3d273f2ab..000000000 --- a/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/NacosConfigBootstrapConfigurationTests.java +++ /dev/null @@ -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(); - } - -} 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 index 5b5d4d864..1fd434b5a 100644 --- 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 @@ -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() { diff --git a/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/NacosPowerMockitBaseTests.java b/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/NacosPowerMockitBaseTests.java new file mode 100644 index 000000000..a54f19186 --- /dev/null +++ b/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/NacosPowerMockitBaseTests.java @@ -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 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 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); + } +} \ 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 index ec6960e46..40248d25a 100644 --- 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 @@ -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 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 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 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(); } } 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 index cb20f25c0..90fe3ca4e 100644 --- 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 @@ -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); } diff --git a/spring-cloud-alibaba-nacos-config/src/test/resources/base-common.properties b/spring-cloud-alibaba-nacos-config/src/test/resources/base-common.properties new file mode 100644 index 000000000..71137acf8 --- /dev/null +++ b/spring-cloud-alibaba-nacos-config/src/test/resources/base-common.properties @@ -0,0 +1,2 @@ +user.name=base-common-value +user.address=zhejiang-hangzhou \ No newline at end of file diff --git a/spring-cloud-alibaba-nacos-config/src/test/resources/common.properties b/spring-cloud-alibaba-nacos-config/src/test/resources/common.properties new file mode 100644 index 000000000..d8f617767 --- /dev/null +++ b/spring-cloud-alibaba-nacos-config/src/test/resources/common.properties @@ -0,0 +1,2 @@ +user.name=common-value +user.address=zhejiang-ningbo \ No newline at end of file diff --git a/spring-cloud-alibaba-nacos-config/src/test/resources/ext00.yaml b/spring-cloud-alibaba-nacos-config/src/test/resources/ext00.yaml new file mode 100644 index 000000000..4d70f6749 --- /dev/null +++ b/spring-cloud-alibaba-nacos-config/src/test/resources/ext00.yaml @@ -0,0 +1,4 @@ +user: + name: ext-00-value +ext: + key: ext.value00 \ No newline at end of file diff --git a/spring-cloud-alibaba-nacos-config/src/test/resources/ext01.yml b/spring-cloud-alibaba-nacos-config/src/test/resources/ext01.yml new file mode 100644 index 000000000..c689c09ee --- /dev/null +++ b/spring-cloud-alibaba-nacos-config/src/test/resources/ext01.yml @@ -0,0 +1,5 @@ +user: + name: ext-01-value + ext: EXT01_GROUP-value +ext: + key: ext.value01 diff --git a/spring-cloud-alibaba-nacos-config/src/test/resources/ext02.yaml b/spring-cloud-alibaba-nacos-config/src/test/resources/ext02.yaml new file mode 100644 index 000000000..9cc477408 --- /dev/null +++ b/spring-cloud-alibaba-nacos-config/src/test/resources/ext02.yaml @@ -0,0 +1,5 @@ +user: + name: ext-02-value +ext: + key: ext.value02 +app-local-common: update app local shared cguration for Nacos \ No newline at end of file diff --git a/spring-cloud-alibaba-nacos-config/src/test/resources/sca-nacos-config-develop.properties b/spring-cloud-alibaba-nacos-config/src/test/resources/sca-nacos-config-develop.properties new file mode 100644 index 000000000..21a6ef58f --- /dev/null +++ b/spring-cloud-alibaba-nacos-config/src/test/resources/sca-nacos-config-develop.properties @@ -0,0 +1 @@ +user.name=sca-nacos-config-value-develop \ No newline at end of file diff --git a/spring-cloud-alibaba-nacos-config/src/test/resources/sca-nacos-config.properties b/spring-cloud-alibaba-nacos-config/src/test/resources/sca-nacos-config.properties new file mode 100644 index 000000000..db268f2ca --- /dev/null +++ b/spring-cloud-alibaba-nacos-config/src/test/resources/sca-nacos-config.properties @@ -0,0 +1,2 @@ +user.name=sca-nacos-config-value +dev.mode=local \ No newline at end of file diff --git a/spring-cloud-alibaba-nacos-discovery/src/test/java/org/springframework/cloud/alibaba/nacos/registry/NacosAutoServiceRegistrationIpTests.java b/spring-cloud-alibaba-nacos-discovery/src/test/java/org/springframework/cloud/alibaba/nacos/registry/NacosAutoServiceRegistrationIpTests.java index aaf72093f..2b9a2beca 100644 --- a/spring-cloud-alibaba-nacos-discovery/src/test/java/org/springframework/cloud/alibaba/nacos/registry/NacosAutoServiceRegistrationIpTests.java +++ b/spring-cloud-alibaba-nacos-discovery/src/test/java/org/springframework/cloud/alibaba/nacos/registry/NacosAutoServiceRegistrationIpTests.java @@ -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