|
|
@ -16,6 +16,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
package com.alibaba.cloud.nacos.registry;
|
|
|
|
package com.alibaba.cloud.nacos.registry;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.lang.reflect.InvocationHandler;
|
|
|
|
|
|
|
|
import java.lang.reflect.Method;
|
|
|
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
import java.util.Properties;
|
|
|
|
|
|
|
|
|
|
|
|
import com.alibaba.cloud.nacos.discovery.NacosDiscoveryClientConfiguration;
|
|
|
|
import com.alibaba.cloud.nacos.discovery.NacosDiscoveryClientConfiguration;
|
|
|
|
import com.alibaba.nacos.api.NacosFactory;
|
|
|
|
import com.alibaba.nacos.api.NacosFactory;
|
|
|
|
import org.junit.Assert;
|
|
|
|
import org.junit.Assert;
|
|
|
@ -27,6 +32,7 @@ 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;
|
|
|
@ -36,11 +42,6 @@ import org.springframework.context.annotation.Bean;
|
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
import org.springframework.test.context.junit4.SpringRunner;
|
|
|
|
import org.springframework.test.context.junit4.SpringRunner;
|
|
|
|
|
|
|
|
|
|
|
|
import java.lang.reflect.InvocationHandler;
|
|
|
|
|
|
|
|
import java.lang.reflect.Method;
|
|
|
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
import java.util.Properties;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
|
|
|
|
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -50,54 +51,54 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen
|
|
|
|
@PowerMockIgnore("javax.management.*")
|
|
|
|
@PowerMockIgnore("javax.management.*")
|
|
|
|
@PowerMockRunnerDelegate(SpringRunner.class)
|
|
|
|
@PowerMockRunnerDelegate(SpringRunner.class)
|
|
|
|
@PrepareForTest({ NacosFactory.class })
|
|
|
|
@PrepareForTest({ NacosFactory.class })
|
|
|
|
@SpringBootTest(
|
|
|
|
@SpringBootTest(classes = NacosRegistrationCustomizerTest.TestConfig.class,
|
|
|
|
classes = NacosRegistrationCustomizerTest.TestConfig.class,
|
|
|
|
properties = { "spring.application.name=myTestService1",
|
|
|
|
properties = { "spring.application.name=myTestService1",
|
|
|
|
"spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848" },
|
|
|
|
"spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848" },
|
|
|
|
webEnvironment = RANDOM_PORT)
|
|
|
|
webEnvironment = RANDOM_PORT)
|
|
|
|
|
|
|
|
public class NacosRegistrationCustomizerTest {
|
|
|
|
public class NacosRegistrationCustomizerTest {
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
@Autowired
|
|
|
|
private NacosAutoServiceRegistration nacosAutoServiceRegistration;
|
|
|
|
private NacosAutoServiceRegistration nacosAutoServiceRegistration;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
Method method = PowerMockito.method(NacosFactory.class, "createNamingService",
|
|
|
|
|
|
|
|
Properties.class);
|
|
|
|
|
|
|
|
MethodProxy.proxy(method, new InvocationHandler() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public Object invoke(Object proxy, Method method, Object[] args)
|
|
|
|
|
|
|
|
throws Throwable {
|
|
|
|
|
|
|
|
return new MockNamingService();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
catch (Exception e) {
|
|
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static {
|
|
|
|
@Test
|
|
|
|
try {
|
|
|
|
public void contextLoads() throws Exception {
|
|
|
|
Method method = PowerMockito.method(NacosFactory.class, "createNamingService",
|
|
|
|
NacosRegistration registration = nacosAutoServiceRegistration.getRegistration();
|
|
|
|
Properties.class);
|
|
|
|
Map<String, String> metadata = registration.getMetadata();
|
|
|
|
MethodProxy.proxy(method, new InvocationHandler() {
|
|
|
|
Assert.assertEquals("test1", metadata.get("test1"));
|
|
|
|
@Override
|
|
|
|
}
|
|
|
|
public Object invoke(Object proxy, Method method, Object[] args)
|
|
|
|
|
|
|
|
throws Throwable {
|
|
|
|
|
|
|
|
return new MockNamingService();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
catch (Exception e) {
|
|
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Configuration
|
|
|
|
public void contextLoads() throws Exception {
|
|
|
|
@EnableAutoConfiguration
|
|
|
|
NacosRegistration registration = nacosAutoServiceRegistration.getRegistration();
|
|
|
|
@ImportAutoConfiguration({ AutoServiceRegistrationConfiguration.class,
|
|
|
|
Map<String, String> metadata = registration.getMetadata();
|
|
|
|
NacosDiscoveryClientConfiguration.class,
|
|
|
|
Assert.assertEquals("test1", metadata.get("test1"));
|
|
|
|
NacosServiceRegistryAutoConfiguration.class })
|
|
|
|
}
|
|
|
|
public static class TestConfig {
|
|
|
|
|
|
|
|
|
|
|
|
@Configuration
|
|
|
|
@Bean
|
|
|
|
@EnableAutoConfiguration
|
|
|
|
public NacosRegistrationCustomizer nacosRegistrationCustomizer() {
|
|
|
|
@ImportAutoConfiguration({ AutoServiceRegistrationConfiguration.class,
|
|
|
|
return registration -> {
|
|
|
|
NacosDiscoveryClientConfiguration.class,
|
|
|
|
Map<String, String> metadata = registration.getMetadata();
|
|
|
|
NacosServiceRegistryAutoConfiguration.class })
|
|
|
|
metadata.put("test1", "test1");
|
|
|
|
public static class TestConfig {
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
}
|
|
|
|
public NacosRegistrationCustomizer nacosRegistrationCustomizer() {
|
|
|
|
|
|
|
|
return registration -> {
|
|
|
|
|
|
|
|
Map<String, String> metadata = registration.getMetadata();
|
|
|
|
|
|
|
|
metadata.put("test1", "test1");
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|