|
|
|
@ -16,8 +16,6 @@
|
|
|
|
|
|
|
|
|
|
package com.alibaba.cloud.nacos;
|
|
|
|
|
|
|
|
|
|
import java.lang.reflect.InvocationHandler;
|
|
|
|
|
import java.lang.reflect.Method;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
import com.alibaba.cloud.nacos.client.NacosPropertySourceLocator;
|
|
|
|
@ -25,14 +23,10 @@ import com.alibaba.cloud.nacos.endpoint.NacosConfigEndpoint;
|
|
|
|
|
import com.alibaba.cloud.nacos.endpoint.NacosConfigEndpointAutoConfiguration;
|
|
|
|
|
import com.alibaba.cloud.nacos.refresh.NacosRefreshHistory;
|
|
|
|
|
import com.alibaba.nacos.client.config.NacosConfigService;
|
|
|
|
|
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.junit.jupiter.api.Test;
|
|
|
|
|
import org.mockito.Mockito;
|
|
|
|
|
import org.mockito.invocation.InvocationOnMock;
|
|
|
|
|
import org.mockito.stubbing.Answer;
|
|
|
|
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
|
|
|
@ -40,22 +34,19 @@ import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
|
|
|
|
|
import org.springframework.boot.test.context.SpringBootTest;
|
|
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
|
import org.springframework.core.env.Environment;
|
|
|
|
|
import org.springframework.test.context.junit4.SpringRunner;
|
|
|
|
|
import org.springframework.test.util.ReflectionTestUtils;
|
|
|
|
|
|
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
|
|
|
|
import static org.mockito.ArgumentMatchers.any;
|
|
|
|
|
import static org.mockito.ArgumentMatchers.anyLong;
|
|
|
|
|
import static org.mockito.Mockito.when;
|
|
|
|
|
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.NONE;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* TODO refactor, remove powermock.
|
|
|
|
|
*
|
|
|
|
|
* @author zkz
|
|
|
|
|
* @author freeman
|
|
|
|
|
*/
|
|
|
|
|
@RunWith(PowerMockRunner.class)
|
|
|
|
|
@PowerMockIgnore({ "javax.management.*", "javax.xml.parsers.*",
|
|
|
|
|
"com.sun.org.apache.xerces.internal.jaxp.*", "org.w3c.dom.*" })
|
|
|
|
|
@PowerMockRunnerDelegate(SpringRunner.class)
|
|
|
|
|
@PrepareForTest({ NacosConfigService.class })
|
|
|
|
|
@SpringBootTest(classes = NacosConfigurationXmlJsonTest.TestConfig.class, webEnvironment = NONE, properties = {
|
|
|
|
|
"spring.application.name=xmlApp", "spring.profiles.active=dev",
|
|
|
|
|
"spring.cloud.nacos.config.server-addr=127.0.0.1:8848",
|
|
|
|
@ -73,97 +64,106 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen
|
|
|
|
|
"spring.cloud.nacos.config.shared-dataids=shared-data1.properties,shared-data.json",
|
|
|
|
|
"spring.cloud.nacos.config.accessKey=test-accessKey",
|
|
|
|
|
"spring.cloud.nacos.config.secretKey=test-secretKey",
|
|
|
|
|
"spring.cloud.bootstrap.enabled=true"
|
|
|
|
|
})
|
|
|
|
|
"spring.cloud.bootstrap.enabled=true" })
|
|
|
|
|
public class NacosConfigurationXmlJsonTest {
|
|
|
|
|
|
|
|
|
|
static {
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
|
|
Method method = PowerMockito.method(NacosConfigService.class, "getConfig",
|
|
|
|
|
String.class, String.class, long.class);
|
|
|
|
|
MethodProxy.proxy(method, new InvocationHandler() {
|
|
|
|
|
@Override
|
|
|
|
|
public Object invoke(Object proxy, Method method, Object[] args)
|
|
|
|
|
throws Throwable {
|
|
|
|
|
|
|
|
|
|
if ("xmlApp.xml".equals(args[0]) && "test-group".equals(args[1])) {
|
|
|
|
|
return "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + "<top>\n"
|
|
|
|
|
+ " <first>one</first>\n"
|
|
|
|
|
+ " <sencond value=\"two\">\n"
|
|
|
|
|
+ " <third>three</third>\n" + " </sencond>\n"
|
|
|
|
|
+ "</top>";
|
|
|
|
|
}
|
|
|
|
|
if ("test-name.xml".equals(args[0]) && "test-group".equals(args[1])) {
|
|
|
|
|
return "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
|
|
|
|
|
+ "<Server port=\"8005\" shutdown=\"SHUTDOWN\"> \n"
|
|
|
|
|
+ " <Service name=\"Catalina\"> \n"
|
|
|
|
|
+ " <Connector value=\"第二个连接器\"> \n"
|
|
|
|
|
+ " <open>开启服务</open> \n"
|
|
|
|
|
+ " <init>初始化一下</init> \n"
|
|
|
|
|
+ " <process>\n" + " <top>\n"
|
|
|
|
|
+ " <first>one</first>\n"
|
|
|
|
|
+ " <sencond value=\"two\">\n"
|
|
|
|
|
+ " <third>three</third>\n"
|
|
|
|
|
+ " </sencond>\n"
|
|
|
|
|
+ " </top>\n" + " </process> \n"
|
|
|
|
|
+ " <destory>销毁一下</destory> \n"
|
|
|
|
|
+ " <close>关闭服务</close> \n"
|
|
|
|
|
+ " </Connector> \n" + " </Service> \n"
|
|
|
|
|
+ "</Server> ";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ("test-name-dev.xml".equals(args[0])
|
|
|
|
|
&& "test-group".equals(args[1])) {
|
|
|
|
|
return "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
|
|
|
|
|
+ "<application android:label=\"@string/app_name\" android:icon=\"@drawable/osg\">\n"
|
|
|
|
|
+ " <activity android:name=\".osgViewer\"\n"
|
|
|
|
|
+ " android:label=\"@string/app_name\" android:screenOrientation=\"landscape\">\n"
|
|
|
|
|
+ " <intent-filter>\n"
|
|
|
|
|
+ " <action android:name=\"android.intent.action.MAIN\" />\n"
|
|
|
|
|
+ " <category android:name=\"android.intent.category.LAUNCHER\" />\n"
|
|
|
|
|
+ " </intent-filter>\n" + " </activity>\n"
|
|
|
|
|
+ "</application>";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ("ext-json-test.json".equals(args[0])
|
|
|
|
|
&& "DEFAULT_GROUP".equals(args[1])) {
|
|
|
|
|
return "{\n" + " \"people\":{\n"
|
|
|
|
|
+ " \"firstName\":\"Brett\",\n"
|
|
|
|
|
+ " \"lastName\":\"McLaughlin\"\n" + " }\n"
|
|
|
|
|
+ "}";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ("ext-config-common02.properties".equals(args[0])
|
|
|
|
|
&& "GLOBAL_GROUP".equals(args[1])) {
|
|
|
|
|
return "global-ext-config=global-config-value-2";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ("shared-data1.properties".equals(args[0])
|
|
|
|
|
&& "DEFAULT_GROUP".equals(args[1])) {
|
|
|
|
|
return "shared-name=shared-value-1";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ("shared-data.json".equals(args[0])
|
|
|
|
|
&& "DEFAULT_GROUP".equals(args[1])) {
|
|
|
|
|
return "{\n" + " \"test\" : {\n"
|
|
|
|
|
+ " \"name\" : \"test\",\n"
|
|
|
|
|
+ " \"list\" : [\n" + " {\n"
|
|
|
|
|
+ " \"name\" :\"listname1\",\n"
|
|
|
|
|
+ " \"age\":1\n" + " },\n"
|
|
|
|
|
+ " {\n"
|
|
|
|
|
+ " \"name\" :\"listname2\",\n"
|
|
|
|
|
+ " \"age\":2\n" + " }\n"
|
|
|
|
|
+ " ],\n" + " \"metadata\" : {\n"
|
|
|
|
|
+ " \"intKey\" : 123,\n"
|
|
|
|
|
+ " \"booleanKey\" : true\n" + " }\n"
|
|
|
|
|
+ " }\n" + "}";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
NacosConfigService mockedNacosConfigService = Mockito
|
|
|
|
|
.mock(NacosConfigService.class);
|
|
|
|
|
when(mockedNacosConfigService.getConfig(any(), any(), anyLong()))
|
|
|
|
|
.thenAnswer(new Answer<String>() {
|
|
|
|
|
@Override
|
|
|
|
|
public String answer(InvocationOnMock invocationOnMock)
|
|
|
|
|
throws Throwable {
|
|
|
|
|
String dataId = invocationOnMock.getArgument(0, String.class);
|
|
|
|
|
String group = invocationOnMock.getArgument(1, String.class);
|
|
|
|
|
if ("xmlApp.xml".equals(dataId)
|
|
|
|
|
&& "test-group".equals(group)) {
|
|
|
|
|
return "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
|
|
|
|
|
+ "<top>\n" + " <first>one</first>\n"
|
|
|
|
|
+ " <sencond value=\"two\">\n"
|
|
|
|
|
+ " <third>three</third>\n"
|
|
|
|
|
+ " </sencond>\n" + "</top>";
|
|
|
|
|
}
|
|
|
|
|
if ("test-name.xml".equals(dataId)
|
|
|
|
|
&& "test-group".equals(group)) {
|
|
|
|
|
return "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
|
|
|
|
|
+ "<Server port=\"8005\" shutdown=\"SHUTDOWN\"> \n"
|
|
|
|
|
+ " <Service name=\"Catalina\"> \n"
|
|
|
|
|
+ " <Connector value=\"第二个连接器\"> \n"
|
|
|
|
|
+ " <open>开启服务</open> \n"
|
|
|
|
|
+ " <init>初始化一下</init> \n"
|
|
|
|
|
+ " <process>\n"
|
|
|
|
|
+ " <top>\n"
|
|
|
|
|
+ " <first>one</first>\n"
|
|
|
|
|
+ " <sencond value=\"two\">\n"
|
|
|
|
|
+ " <third>three</third>\n"
|
|
|
|
|
+ " </sencond>\n"
|
|
|
|
|
+ " </top>\n"
|
|
|
|
|
+ " </process> \n"
|
|
|
|
|
+ " <destory>销毁一下</destory> \n"
|
|
|
|
|
+ " <close>关闭服务</close> \n"
|
|
|
|
|
+ " </Connector> \n" + " </Service> \n"
|
|
|
|
|
+ "</Server> ";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ("test-name-dev.xml".equals(dataId)
|
|
|
|
|
&& "test-group".equals(group)) {
|
|
|
|
|
return "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
|
|
|
|
|
+ "<application android:label=\"@string/app_name\" android:icon=\"@drawable/osg\">\n"
|
|
|
|
|
+ " <activity android:name=\".osgViewer\"\n"
|
|
|
|
|
+ " android:label=\"@string/app_name\" android:screenOrientation=\"landscape\">\n"
|
|
|
|
|
+ " <intent-filter>\n"
|
|
|
|
|
+ " <action android:name=\"android.intent.action.MAIN\" />\n"
|
|
|
|
|
+ " <category android:name=\"android.intent.category.LAUNCHER\" />\n"
|
|
|
|
|
+ " </intent-filter>\n"
|
|
|
|
|
+ " </activity>\n" + "</application>";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ("ext-json-test.json".equals(dataId)
|
|
|
|
|
&& "DEFAULT_GROUP".equals(group)) {
|
|
|
|
|
return "{\n" + " \"people\":{\n"
|
|
|
|
|
+ " \"firstName\":\"Brett\",\n"
|
|
|
|
|
+ " \"lastName\":\"McLaughlin\"\n"
|
|
|
|
|
+ " }\n" + "}";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ("ext-config-common02.properties".equals(dataId)
|
|
|
|
|
&& "GLOBAL_GROUP".equals(group)) {
|
|
|
|
|
return "global-ext-config=global-config-value-2";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ("shared-data1.properties".equals(dataId)
|
|
|
|
|
&& "DEFAULT_GROUP".equals(group)) {
|
|
|
|
|
return "shared-name=shared-value-1";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ("shared-data.json".equals(dataId)
|
|
|
|
|
&& "DEFAULT_GROUP".equals(group)) {
|
|
|
|
|
return "{\n" + " \"test\" : {\n"
|
|
|
|
|
+ " \"name\" : \"test\",\n"
|
|
|
|
|
+ " \"list\" : [\n" + " {\n"
|
|
|
|
|
+ " \"name\" :\"listname1\",\n"
|
|
|
|
|
+ " \"age\":1\n"
|
|
|
|
|
+ " },\n" + " {\n"
|
|
|
|
|
+ " \"name\" :\"listname2\",\n"
|
|
|
|
|
+ " \"age\":2\n"
|
|
|
|
|
+ " }\n" + " ],\n"
|
|
|
|
|
+ " \"metadata\" : {\n"
|
|
|
|
|
+ " \"intKey\" : 123,\n"
|
|
|
|
|
+ " \"booleanKey\" : true\n"
|
|
|
|
|
+ " }\n" + " }\n" + "}";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
ReflectionTestUtils.setField(NacosConfigManager.class, "service",
|
|
|
|
|
mockedNacosConfigService);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ignore) {
|
|
|
|
|