mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
fix nacos config test case
This commit is contained in:
parent
d04281e16f
commit
16742f70c1
@ -57,14 +57,10 @@ import org.springframework.test.context.junit4.SpringRunner;
|
|||||||
"spring.cloud.nacos.config.encode=utf-8",
|
"spring.cloud.nacos.config.encode=utf-8",
|
||||||
"spring.cloud.nacos.config.timeout=1000",
|
"spring.cloud.nacos.config.timeout=1000",
|
||||||
"spring.cloud.nacos.config.file-extension=properties",
|
"spring.cloud.nacos.config.file-extension=properties",
|
||||||
|
|
||||||
"spring.cloud.nacos.config.ext-config[0].data-id=ext-config-common01.properties",
|
"spring.cloud.nacos.config.ext-config[0].data-id=ext-config-common01.properties",
|
||||||
|
|
||||||
"spring.cloud.nacos.config.ext-config[1].data-id=ext-config-common02.properties",
|
"spring.cloud.nacos.config.ext-config[1].data-id=ext-config-common02.properties",
|
||||||
"spring.cloud.nacos.config.ext-config[1].group=GLOBAL_GROUP",
|
"spring.cloud.nacos.config.ext-config[1].group=GLOBAL_GROUP",
|
||||||
|
|
||||||
"spring.cloud.nacos.config.shared-dataids=common1.properties,common2.properties",
|
"spring.cloud.nacos.config.shared-dataids=common1.properties,common2.properties",
|
||||||
|
|
||||||
"spring.cloud.nacos.config.accessKey=test-accessKey",
|
"spring.cloud.nacos.config.accessKey=test-accessKey",
|
||||||
"spring.cloud.nacos.config.secretKey=test-secretKey" }, webEnvironment = NONE)
|
"spring.cloud.nacos.config.secretKey=test-secretKey" }, webEnvironment = NONE)
|
||||||
public class NacosConfigurationExtConfigTests {
|
public class NacosConfigurationExtConfigTests {
|
||||||
|
@ -1,254 +1,239 @@
|
|||||||
///*
|
/*
|
||||||
// * Copyright (C) 2018 the original author or authors.
|
* Copyright (C) 2018 the original author or authors.
|
||||||
// *
|
*
|
||||||
// * Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
// * you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
// * You may obtain a copy of the License at
|
* You may obtain a copy of the License at
|
||||||
// *
|
*
|
||||||
// * http://www.apache.org/licenses/LICENSE-2.0
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
// *
|
*
|
||||||
// * Unless required by applicable law or agreed to in writing, software
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
// * distributed under the License is distributed on an "AS IS" BASIS,
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
// * See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
// * limitations under the License.
|
* limitations under the License.
|
||||||
// */
|
*/
|
||||||
//
|
|
||||||
//package org.springframework.cloud.alibaba.nacos;
|
package org.springframework.cloud.alibaba.nacos;
|
||||||
//
|
|
||||||
//import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
//import static org.junit.Assert.assertNotNull;
|
import static org.junit.Assert.assertNotNull;
|
||||||
//import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.NONE;
|
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.NONE;
|
||||||
//
|
|
||||||
//import java.lang.reflect.InvocationHandler;
|
import java.lang.reflect.InvocationHandler;
|
||||||
//import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
//import java.util.Map;
|
import java.util.Map;
|
||||||
//
|
|
||||||
//import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
//import org.junit.Test;
|
import org.junit.Test;
|
||||||
//import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
//import org.powermock.api.mockito.PowerMockito;
|
import org.powermock.api.mockito.PowerMockito;
|
||||||
//import org.powermock.api.support.MethodProxy;
|
import org.powermock.api.support.MethodProxy;
|
||||||
//import org.powermock.core.classloader.annotations.PowerMockIgnore;
|
import org.powermock.core.classloader.annotations.PowerMockIgnore;
|
||||||
//import org.powermock.core.classloader.annotations.PrepareForTest;
|
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||||
//import org.powermock.modules.junit4.PowerMockRunner;
|
import org.powermock.modules.junit4.PowerMockRunner;
|
||||||
//import org.powermock.modules.junit4.PowerMockRunnerDelegate;
|
import org.powermock.modules.junit4.PowerMockRunnerDelegate;
|
||||||
//import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
//import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||||
//import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
|
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
|
||||||
//import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
//import org.springframework.cloud.alibaba.nacos.client.NacosPropertySourceLocator;
|
import org.springframework.cloud.alibaba.nacos.client.NacosPropertySourceLocator;
|
||||||
//import org.springframework.cloud.alibaba.nacos.endpoint.NacosConfigEndpoint;
|
import org.springframework.cloud.alibaba.nacos.endpoint.NacosConfigEndpoint;
|
||||||
//import org.springframework.cloud.alibaba.nacos.endpoint.NacosConfigEndpointAutoConfiguration;
|
import org.springframework.cloud.alibaba.nacos.endpoint.NacosConfigEndpointAutoConfiguration;
|
||||||
//import org.springframework.cloud.alibaba.nacos.refresh.NacosRefreshHistory;
|
import org.springframework.cloud.alibaba.nacos.refresh.NacosRefreshHistory;
|
||||||
//import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
//import org.springframework.core.env.Environment;
|
import org.springframework.core.env.Environment;
|
||||||
//import org.springframework.test.context.junit4.SpringRunner;
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
//
|
|
||||||
//import com.alibaba.nacos.client.config.NacosConfigService;
|
import com.alibaba.nacos.client.config.NacosConfigService;
|
||||||
//
|
|
||||||
///**
|
/**
|
||||||
// * @author xiaojing
|
* @author xiaojing
|
||||||
// */
|
*/
|
||||||
//
|
|
||||||
//@RunWith(PowerMockRunner.class)
|
@RunWith(PowerMockRunner.class)
|
||||||
//@PowerMockIgnore("javax.management.*")
|
@PowerMockIgnore("javax.management.*")
|
||||||
//@PowerMockRunnerDelegate(SpringRunner.class)
|
@PowerMockRunnerDelegate(SpringRunner.class)
|
||||||
//@PrepareForTest({ NacosConfigService.class })
|
@PrepareForTest({ NacosConfigService.class })
|
||||||
//@SpringBootTest(classes = NacosConfigurationTests.TestConfig.class, properties = {
|
@SpringBootTest(classes = NacosConfigurationTests.TestConfig.class, properties = {
|
||||||
// "spring.application.name=myTestService1", "spring.profiles.active=dev,test",
|
"spring.application.name=myTestService1", "spring.profiles.active=dev,test",
|
||||||
// "spring.cloud.nacos.config.server-addr=127.0.0.1:8848",
|
"spring.cloud.nacos.config.server-addr=127.0.0.1:8848",
|
||||||
// "spring.cloud.nacos.config.endpoint=test-endpoint",
|
"spring.cloud.nacos.config.namespace=test-namespace",
|
||||||
// "spring.cloud.nacos.config.namespace=test-namespace",
|
"spring.cloud.nacos.config.encode=utf-8",
|
||||||
// "spring.cloud.nacos.config.encode=utf-8",
|
"spring.cloud.nacos.config.timeout=1000",
|
||||||
// "spring.cloud.nacos.config.timeout=1000",
|
"spring.cloud.nacos.config.group=test-group",
|
||||||
// "spring.cloud.nacos.config.group=test-group",
|
"spring.cloud.nacos.config.name=test-name",
|
||||||
// "spring.cloud.nacos.config.name=test-name",
|
"spring.cloud.nacos.config.cluster-name=test-cluster",
|
||||||
// "spring.cloud.nacos.config.cluster-name=test-cluster",
|
"spring.cloud.nacos.config.file-extension=properties",
|
||||||
// "spring.cloud.nacos.config.file-extension=properties",
|
"spring.cloud.nacos.config.contextPath=test-contextpath",
|
||||||
// "spring.cloud.nacos.config.contextPath=test-contextpath",
|
"spring.cloud.nacos.config.ext-config[0].data-id=ext-config-common01.properties",
|
||||||
//
|
"spring.cloud.nacos.config.ext-config[1].data-id=ext-config-common02.properties",
|
||||||
// "spring.cloud.nacos.config.ext-config[0].data-id=ext-config-common01.properties",
|
"spring.cloud.nacos.config.ext-config[1].group=GLOBAL_GROUP",
|
||||||
//
|
"spring.cloud.nacos.config.shared-dataids=common1.properties,common2.properties",
|
||||||
// "spring.cloud.nacos.config.ext-config[1].data-id=ext-config-common02.properties",
|
"spring.cloud.nacos.config.accessKey=test-accessKey",
|
||||||
// "spring.cloud.nacos.config.ext-config[1].group=GLOBAL_GROUP",
|
"spring.cloud.nacos.config.secretKey=test-secretKey" }, webEnvironment = NONE)
|
||||||
//
|
public class NacosConfigurationTests {
|
||||||
// "spring.cloud.nacos.config.shared-dataids=common1.properties,common2.properties",
|
|
||||||
//
|
static {
|
||||||
// "spring.cloud.nacos.config.accessKey=test-accessKey",
|
|
||||||
// "spring.cloud.nacos.config.secretKey=test-secretKey" }, webEnvironment = NONE)
|
try {
|
||||||
//public class NacosConfigurationTests {
|
|
||||||
//
|
Method method = PowerMockito.method(NacosConfigService.class, "getConfig",
|
||||||
// static {
|
String.class, String.class, long.class);
|
||||||
//
|
MethodProxy.proxy(method, new InvocationHandler() {
|
||||||
// try {
|
@Override
|
||||||
// // when(any(ConfigService.class).getConfig(eq("test-name.properties"),
|
public Object invoke(Object proxy, Method method, Object[] args)
|
||||||
// // eq("test-group"), any())).thenReturn("user.name=hello");
|
throws Throwable {
|
||||||
//
|
|
||||||
// Method method = PowerMockito.method(NacosConfigService.class, "getConfig",
|
if ("test-name.properties".equals(args[0])
|
||||||
// String.class, String.class, long.class);
|
&& "test-group".equals(args[1])) {
|
||||||
// MethodProxy.proxy(method, new InvocationHandler() {
|
return "user.name=hello\nuser.age=12";
|
||||||
// @Override
|
}
|
||||||
// public Object invoke(Object proxy, Method method, Object[] args)
|
|
||||||
// throws Throwable {
|
if ("test-name-dev.properties".equals(args[0])
|
||||||
//
|
&& "test-group".equals(args[1])) {
|
||||||
// if ("test-name.properties".equals(args[0])
|
return "user.name=dev";
|
||||||
// && "test-group".equals(args[1])) {
|
}
|
||||||
// return "user.name=hello\nuser.age=12";
|
|
||||||
// }
|
if ("ext-config-common01.properties".equals(args[0])
|
||||||
//
|
&& "DEFAULT_GROUP".equals(args[1])) {
|
||||||
// if ("test-name-dev.properties".equals(args[0])
|
return "test-ext-config1=config1\ntest-ext-config2=config1";
|
||||||
// && "test-group".equals(args[1])) {
|
}
|
||||||
// return "user.name=dev";
|
if ("ext-config-common02.properties".equals(args[0])
|
||||||
// }
|
&& "GLOBAL_GROUP".equals(args[1])) {
|
||||||
//
|
return "test-ext-config2=config2";
|
||||||
// if ("ext-config-common01.properties".equals(args[0])
|
}
|
||||||
// && "DEFAULT_GROUP".equals(args[1])) {
|
|
||||||
// return "test-ext-config1=config1\ntest-ext-config2=config1";
|
if ("common1.properties".equals(args[0])
|
||||||
// }
|
&& "DEFAULT_GROUP".equals(args[1])) {
|
||||||
// if ("ext-config-common02.properties".equals(args[0])
|
return "test-common1=common1\ntest-common2=common1";
|
||||||
// && "GLOBAL_GROUP".equals(args[1])) {
|
}
|
||||||
// return "test-ext-config2=config2";
|
|
||||||
// }
|
if ("common2.properties".equals(args[0])
|
||||||
//
|
&& "DEFAULT_GROUP".equals(args[1])) {
|
||||||
// if ("common1.properties".equals(args[0])
|
return "test-common2=common2";
|
||||||
// && "DEFAULT_GROUP".equals(args[1])) {
|
}
|
||||||
// return "test-common1=common1\ntest-common2=common1";
|
|
||||||
// }
|
return "";
|
||||||
//
|
}
|
||||||
// if ("common2.properties".equals(args[0])
|
});
|
||||||
// && "DEFAULT_GROUP".equals(args[1])) {
|
|
||||||
// return "test-common2=common2";
|
}
|
||||||
// }
|
catch (Exception ignore) {
|
||||||
//
|
ignore.printStackTrace();
|
||||||
// return "";
|
|
||||||
// }
|
}
|
||||||
// });
|
}
|
||||||
//
|
|
||||||
// }
|
@Autowired
|
||||||
// catch (Exception ignore) {
|
private Environment environment;
|
||||||
// ignore.printStackTrace();
|
|
||||||
//
|
@Autowired
|
||||||
// }
|
private NacosPropertySourceLocator locator;
|
||||||
// }
|
|
||||||
//
|
@Autowired
|
||||||
// @Autowired
|
private NacosConfigProperties properties;
|
||||||
// private Environment environment;
|
|
||||||
//
|
@Autowired
|
||||||
// @Autowired
|
private NacosRefreshHistory refreshHistory;
|
||||||
// private NacosPropertySourceLocator locator;
|
|
||||||
//
|
@Test
|
||||||
// @Autowired
|
public void contextLoads() throws Exception {
|
||||||
// private NacosConfigProperties properties;
|
|
||||||
//
|
assertNotNull("NacosPropertySourceLocator was not created", locator);
|
||||||
// @Autowired
|
assertNotNull("NacosConfigProperties was not created", properties);
|
||||||
// private NacosRefreshHistory refreshHistory;
|
|
||||||
//
|
checkoutNacosConfigServerAddr();
|
||||||
// // @Test
|
checkoutNacosConfigNamespace();
|
||||||
// public void contextLoads() throws Exception {
|
checkoutNacosConfigClusterName();
|
||||||
//
|
checkoutNacosConfigAccessKey();
|
||||||
// assertNotNull("NacosPropertySourceLocator was not created", locator);
|
checkoutNacosConfigSecrectKey();
|
||||||
// assertNotNull("NacosConfigProperties was not created", properties);
|
checkoutNacosConfigName();
|
||||||
//
|
checkoutNacosConfigGroup();
|
||||||
// checkoutNacosConfigServerAddr();
|
checkoutNacosConfigContextPath();
|
||||||
// checkoutNacosConfigEndpoint();
|
checkoutNacosConfigFileExtension();
|
||||||
// checkoutNacosConfigNamespace();
|
checkoutNacosConfigTimeout();
|
||||||
// checkoutNacosConfigClusterName();
|
checkoutNacosConfigEncode();
|
||||||
// checkoutNacosConfigAccessKey();
|
|
||||||
// checkoutNacosConfigSecrectKey();
|
checkoutEndpoint();
|
||||||
// checkoutNacosConfigName();
|
checkoutDataLoad();
|
||||||
// checkoutNacosConfigGroup();
|
|
||||||
// checkoutNacosConfigContextPath();
|
}
|
||||||
// checkoutNacosConfigFileExtension();
|
|
||||||
// checkoutNacosConfigTimeout();
|
private void checkoutNacosConfigServerAddr() {
|
||||||
// checkoutNacosConfigEncode();
|
assertEquals("NacosConfigProperties server address is wrong", "127.0.0.1:8848",
|
||||||
//
|
properties.getServerAddr());
|
||||||
// checkoutEndpoint();
|
}
|
||||||
// checkoutDataLoad();
|
|
||||||
//
|
|
||||||
// }
|
private void checkoutNacosConfigNamespace() {
|
||||||
//
|
assertEquals("NacosConfigProperties namespace is wrong", "test-namespace",
|
||||||
// private void checkoutNacosConfigServerAddr() {
|
properties.getNamespace());
|
||||||
// assertEquals("NacosConfigProperties server address is wrong", "127.0.0.1:8848",
|
}
|
||||||
// properties.getServerAddr());
|
|
||||||
//
|
private void checkoutNacosConfigClusterName() {
|
||||||
// }
|
assertEquals("NacosConfigProperties' cluster is wrong", "test-cluster",
|
||||||
//
|
properties.getClusterName());
|
||||||
// private void checkoutNacosConfigEndpoint() {
|
}
|
||||||
// assertEquals("NacosConfigProperties endpoint is wrong", "test-endpoint",
|
|
||||||
// properties.getEndpoint());
|
private void checkoutNacosConfigAccessKey() {
|
||||||
//
|
assertEquals("NacosConfigProperties' is access key is wrong", "test-accessKey",
|
||||||
// }
|
properties.getAccessKey());
|
||||||
//
|
}
|
||||||
// private void checkoutNacosConfigNamespace() {
|
|
||||||
// assertEquals("NacosConfigProperties namespace is wrong", "test-namespace",
|
private void checkoutNacosConfigSecrectKey() {
|
||||||
// properties.getNamespace());
|
assertEquals("NacosConfigProperties' is secret key is wrong", "test-secretKey",
|
||||||
//
|
properties.getSecretKey());
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// private void checkoutNacosConfigClusterName() {
|
private void checkoutNacosConfigContextPath() {
|
||||||
// assertEquals("NacosConfigProperties' cluster is wrong", "test-cluster",
|
assertEquals("NacosConfigProperties' context path is wrong", "test-contextpath",
|
||||||
// properties.getClusterName());
|
properties.getContextPath());
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// private void checkoutNacosConfigAccessKey() {
|
private void checkoutNacosConfigName() {
|
||||||
// assertEquals("NacosConfigProperties' is access key is wrong", "test-accessKey",
|
assertEquals("NacosConfigProperties' name is wrong", "test-name",
|
||||||
// properties.getAccessKey());
|
properties.getName());
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// private void checkoutNacosConfigSecrectKey() {
|
private void checkoutNacosConfigGroup() {
|
||||||
// assertEquals("NacosConfigProperties' is secret key is wrong", "test-secretKey",
|
assertEquals("NacosConfigProperties' group is wrong", "test-group",
|
||||||
// properties.getSecretKey());
|
properties.getGroup());
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// private void checkoutNacosConfigContextPath() {
|
private void checkoutNacosConfigFileExtension() {
|
||||||
// assertEquals("NacosConfigProperties' context path is wrong", "test-contextpath",
|
assertEquals("NacosConfigProperties' file extension is wrong", "properties",
|
||||||
// properties.getContextPath());
|
properties.getFileExtension());
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// private void checkoutNacosConfigName() {
|
private void checkoutNacosConfigTimeout() {
|
||||||
// assertEquals("NacosConfigProperties' name is wrong", "test-name",
|
assertEquals("NacosConfigProperties' timeout is wrong", 1000,
|
||||||
// properties.getName());
|
properties.getTimeout());
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// private void checkoutNacosConfigGroup() {
|
private void checkoutNacosConfigEncode() {
|
||||||
// assertEquals("NacosConfigProperties' group is wrong", "test-group",
|
assertEquals("NacosConfigProperties' encode is wrong", "utf-8",
|
||||||
// properties.getGroup());
|
properties.getEncode());
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// private void checkoutNacosConfigFileExtension() {
|
private void checkoutDataLoad() {
|
||||||
// assertEquals("NacosConfigProperties' file extension is wrong", "properties",
|
|
||||||
// properties.getFileExtension());
|
Assert.assertEquals("dev", environment.getProperty("user.name"));
|
||||||
// }
|
Assert.assertEquals("12", environment.getProperty("user.age"));
|
||||||
//
|
}
|
||||||
// private void checkoutNacosConfigTimeout() {
|
|
||||||
// assertEquals("NacosConfigProperties' timeout is wrong", 1000,
|
private void checkoutEndpoint() throws Exception {
|
||||||
// properties.getTimeout());
|
NacosConfigEndpoint nacosConfigEndpoint = new NacosConfigEndpoint(properties,
|
||||||
// }
|
refreshHistory);
|
||||||
//
|
Map<String, Object> map = nacosConfigEndpoint.invoke();
|
||||||
// private void checkoutNacosConfigEncode() {
|
assertEquals(map.get("NacosConfigProperties"), properties);
|
||||||
// assertEquals("NacosConfigProperties' encode is wrong", "utf-8",
|
assertEquals(map.get("RefreshHistory"), refreshHistory.getRecords());
|
||||||
// properties.getEncode());
|
}
|
||||||
// }
|
|
||||||
//
|
@Configuration
|
||||||
// private void checkoutDataLoad() {
|
@EnableAutoConfiguration
|
||||||
//
|
@ImportAutoConfiguration({ NacosConfigEndpointAutoConfiguration.class,
|
||||||
// Assert.assertEquals(environment.getProperty("user.name"), "dev");
|
NacosConfigAutoConfiguration.class, NacosConfigBootstrapConfiguration.class })
|
||||||
// Assert.assertEquals(environment.getProperty("user.age"), "12");
|
public static class TestConfig {
|
||||||
// }
|
}
|
||||||
//
|
}
|
||||||
// private void checkoutEndpoint() throws Exception {
|
|
||||||
// NacosConfigEndpoint nacosConfigEndpoint = new NacosConfigEndpoint(properties,
|
|
||||||
// refreshHistory);
|
|
||||||
// Map<String, Object> map = nacosConfigEndpoint.invoke();
|
|
||||||
// assertEquals(map.get("NacosConfigProperties"), properties);
|
|
||||||
// assertEquals(map.get("RefreshHistory"), refreshHistory.getRecords());
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// @Configuration
|
|
||||||
// @EnableAutoConfiguration
|
|
||||||
// @ImportAutoConfiguration({ NacosConfigEndpointAutoConfiguration.class,
|
|
||||||
// NacosConfigAutoConfiguration.class, NacosConfigBootstrapConfiguration.class })
|
|
||||||
// public static class TestConfig {
|
|
||||||
// }
|
|
||||||
//}
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user