diff --git a/dubbo-common/src/main/java/com/alibaba/dubbo/common/utils/ConfigUtils.java b/dubbo-common/src/main/java/com/alibaba/dubbo/common/utils/ConfigUtils.java index ae057a89a5c..c5b51d8beb5 100644 --- a/dubbo-common/src/main/java/com/alibaba/dubbo/common/utils/ConfigUtils.java +++ b/dubbo-common/src/main/java/com/alibaba/dubbo/common/utils/ConfigUtils.java @@ -158,9 +158,7 @@ public static Properties getProperties() { } public static void setProperties(Properties properties) { - if (properties != null) { - PROPERTIES = properties; - } + PROPERTIES = properties; } public static void addProperties(Properties properties) { diff --git a/dubbo-config/dubbo-config-api/src/main/java/com/alibaba/dubbo/config/AbstractConfig.java b/dubbo-config/dubbo-config-api/src/main/java/com/alibaba/dubbo/config/AbstractConfig.java index 657eebdc713..73fc07da473 100644 --- a/dubbo-config/dubbo-config-api/src/main/java/com/alibaba/dubbo/config/AbstractConfig.java +++ b/dubbo-config/dubbo-config-api/src/main/java/com/alibaba/dubbo/config/AbstractConfig.java @@ -214,11 +214,11 @@ && isPrimitive(method.getReturnType())) { str = URL.encode(str); } if (parameter != null && parameter.append()) { - String pre = (String) parameters.get(Constants.DEFAULT_KEY + "." + key); + String pre = parameters.get(Constants.DEFAULT_KEY + "." + key); if (pre != null && pre.length() > 0) { str = pre + "," + str; } - pre = (String) parameters.get(key); + pre = parameters.get(key); if (pre != null && pre.length() > 0) { str = pre + "," + str; } @@ -387,7 +387,6 @@ protected static void checkParameterName(Map parameters) { return; } for (Map.Entry entry : parameters.entrySet()) { - //change by tony.chenl parameter value maybe has colon.for example napoli address checkNameHasSymbol(entry.getKey(), entry.getValue()); } } @@ -402,7 +401,8 @@ protected static void checkProperty(String property, String value, int maxlength if (pattern != null) { Matcher matcher = pattern.matcher(value); if (!matcher.matches()) { - throw new IllegalStateException("Invalid " + property + "=\"" + value + "\" contain illegal charactor, only digit, letter, '-', '_' and '.' is legal."); + throw new IllegalStateException("Invalid " + property + "=\"" + value + "\" contains illegal " + + "character, only digit, letter, '-', '_' or '.' is legal."); } } } diff --git a/dubbo-config/dubbo-config-api/src/main/java/com/alibaba/dubbo/config/AbstractInterfaceConfig.java b/dubbo-config/dubbo-config-api/src/main/java/com/alibaba/dubbo/config/AbstractInterfaceConfig.java index acb22a4fd0c..8a7ce9b0487 100644 --- a/dubbo-config/dubbo-config-api/src/main/java/com/alibaba/dubbo/config/AbstractInterfaceConfig.java +++ b/dubbo-config/dubbo-config-api/src/main/java/com/alibaba/dubbo/config/AbstractInterfaceConfig.java @@ -360,7 +360,7 @@ public String getStub() { } public void setStub(Boolean stub) { - if (local == null) { + if (stub == null) { setStub((String) null); } else { setStub(String.valueOf(stub)); diff --git a/dubbo-config/dubbo-config-api/src/test/java/com/alibaba/dubbo/config/AbstractConfigTest.java b/dubbo-config/dubbo-config-api/src/test/java/com/alibaba/dubbo/config/AbstractConfigTest.java new file mode 100644 index 00000000000..7cd83f0b28d --- /dev/null +++ b/dubbo-config/dubbo-config-api/src/test/java/com/alibaba/dubbo/config/AbstractConfigTest.java @@ -0,0 +1,513 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 com.alibaba.dubbo.config; + +import com.alibaba.dubbo.common.Constants; +import com.alibaba.dubbo.common.utils.ConfigUtils; +import com.alibaba.dubbo.config.support.Parameter; +import junit.framework.TestCase; +import org.junit.Test; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; + +public class AbstractConfigTest { + + @Test + public void testAppendProperties1() throws Exception { + try { + System.setProperty("dubbo.properties.i", "1"); + System.setProperty("dubbo.properties.c", "c"); + System.setProperty("dubbo.properties.b", "2"); + System.setProperty("dubbo.properties.d", "3"); + System.setProperty("dubbo.properties.f", "4"); + System.setProperty("dubbo.properties.l", "5"); + System.setProperty("dubbo.properties.s", "6"); + System.setProperty("dubbo.properties.str", "dubbo"); + System.setProperty("dubbo.properties.bool", "true"); + PropertiesConfig config = new PropertiesConfig(); + AbstractConfig.appendProperties(config); + TestCase.assertEquals(1, config.getI()); + TestCase.assertEquals('c', config.getC()); + TestCase.assertEquals((byte) 0x02, config.getB()); + TestCase.assertEquals(3d, config.getD()); + TestCase.assertEquals(4f, config.getF()); + TestCase.assertEquals(5L, config.getL()); + TestCase.assertEquals(6, config.getS()); + TestCase.assertEquals("dubbo", config.getStr()); + TestCase.assertTrue(config.isBool()); + } finally { + System.clearProperty("dubbo.properties.i"); + System.clearProperty("dubbo.properties.c"); + System.clearProperty("dubbo.properties.b"); + System.clearProperty("dubbo.properties.d"); + System.clearProperty("dubbo.properties.f"); + System.clearProperty("dubbo.properties.l"); + System.clearProperty("dubbo.properties.s"); + System.clearProperty("dubbo.properties.str"); + System.clearProperty("dubbo.properties.bool"); + } + } + + @Test + public void testAppendProperties2() throws Exception { + try { + System.setProperty("dubbo.properties.two.i", "2"); + PropertiesConfig config = new PropertiesConfig("two"); + AbstractConfig.appendProperties(config); + TestCase.assertEquals(2, config.getI()); + } finally { + System.clearProperty("dubbo.properties.two.i"); + } + } + + @Test + public void testAppendProperties3() throws Exception { + try { + Properties p = new Properties(); + p.put("dubbo.properties.str", "dubbo"); + ConfigUtils.setProperties(p); + PropertiesConfig config = new PropertiesConfig(); + AbstractConfig.appendProperties(config); + TestCase.assertEquals("dubbo", config.getStr()); + } finally { + System.clearProperty(Constants.DUBBO_PROPERTIES_KEY); + ConfigUtils.setProperties(null); + } + } + + @Test + public void testAppendParameters1() throws Exception { + Map parameters = new HashMap(); + parameters.put("default.num", "one"); + parameters.put("num", "ONE"); + AbstractConfig.appendParameters(parameters, new ParameterConfig(1, "hello/world", 30, "password"), "prefix"); + TestCase.assertEquals("one", parameters.get("prefix.key.1")); + TestCase.assertEquals("two", parameters.get("prefix.key.2")); + TestCase.assertEquals("ONE,one,1", parameters.get("prefix.num")); + TestCase.assertEquals("hello%2Fworld", parameters.get("prefix.naming")); + TestCase.assertEquals("30", parameters.get("prefix.age")); + TestCase.assertFalse(parameters.containsKey("prefix.key-2")); + TestCase.assertFalse(parameters.containsKey("prefix.secret")); + } + + @Test(expected = IllegalStateException.class) + public void testAppendParameters2() throws Exception { + Map parameters = new HashMap(); + AbstractConfig.appendParameters(parameters, new ParameterConfig()); + } + + @Test + public void testAppendParameters3() throws Exception { + Map parameters = new HashMap(); + AbstractConfig.appendParameters(parameters, null); + TestCase.assertTrue(parameters.isEmpty()); + } + + @Test + public void testAppendParameters4() throws Exception { + Map parameters = new HashMap(); + AbstractConfig.appendParameters(parameters, new ParameterConfig(1, "hello/world", 30, "password")); + TestCase.assertEquals("one", parameters.get("key.1")); + TestCase.assertEquals("two", parameters.get("key.2")); + TestCase.assertEquals("1", parameters.get("num")); + TestCase.assertEquals("hello%2Fworld", parameters.get("naming")); + TestCase.assertEquals("30", parameters.get("age")); + } + + @Test + public void testAppendAttributes1() throws Exception { + Map parameters = new HashMap(); + AbstractConfig.appendAttributes(parameters, new AttributeConfig('l', true, (byte) 0x01), "prefix"); + TestCase.assertEquals('l', parameters.get("prefix.let")); + TestCase.assertEquals(true, parameters.get("prefix.activate")); + TestCase.assertFalse(parameters.containsKey("prefix.flag")); + } + + @Test + public void testAppendAttributes2() throws Exception { + Map parameters = new HashMap(); + AbstractConfig.appendAttributes(parameters, new AttributeConfig('l', true, (byte) 0x01)); + TestCase.assertEquals('l', parameters.get("let")); + TestCase.assertEquals(true, parameters.get("activate")); + TestCase.assertFalse(parameters.containsKey("flag")); + } + + @Test(expected = IllegalStateException.class) + public void checkExtension() throws Exception { + AbstractConfig.checkExtension(Greeting.class, "hello", "world"); + } + + @Test(expected = IllegalStateException.class) + public void checkMultiExtension1() throws Exception { + AbstractConfig.checkMultiExtension(Greeting.class, "hello", "default,world"); + } + + @Test(expected = IllegalStateException.class) + public void checkMultiExtension2() throws Exception { + AbstractConfig.checkMultiExtension(Greeting.class, "hello", "default,-world"); + } + + @Test(expected = IllegalStateException.class) + public void checkLength() throws Exception { + StringBuilder builder = new StringBuilder(); + for (int i = 0; i <= 200; i++) { + builder.append("a"); + } + AbstractConfig.checkLength("hello", builder.toString()); + } + + @Test(expected = IllegalStateException.class) + public void checkPathLength() throws Exception { + StringBuilder builder = new StringBuilder(); + for (int i = 0; i <= 200; i++) { + builder.append("a"); + } + AbstractConfig.checkPathLength("hello", builder.toString()); + } + + @Test(expected = IllegalStateException.class) + public void checkName() throws Exception { + AbstractConfig.checkName("hello", "world%"); + } + + @Test + public void checkNameHasSymbol() throws Exception { + try { + AbstractConfig.checkNameHasSymbol("hello", ":*,/-0123abcdABCD"); + } catch (Exception e) { + TestCase.fail("the value should be legal."); + } + } + + @Test + public void checkKey() throws Exception { + try { + AbstractConfig.checkKey("hello", "*,-0123abcdABCD"); + } catch (Exception e) { + TestCase.fail("the value should be legal."); + } + } + + @Test + public void checkMultiName() throws Exception { + try { + AbstractConfig.checkMultiName("hello", ",-._0123abcdABCD"); + } catch (Exception e) { + TestCase.fail("the value should be legal."); + } + } + + @Test + public void checkPathName() throws Exception { + try { + AbstractConfig.checkPathName("hello", "/-$._0123abcdABCD"); + } catch (Exception e) { + TestCase.fail("the value should be legal."); + } + } + + @Test + public void checkMethodName() throws Exception { + try { + AbstractConfig.checkMethodName("hello", "abcdABCD0123abcd"); + } catch (Exception e) { + TestCase.fail("the value should be legal."); + } + + try { + AbstractConfig.checkMethodName("hello", "0a"); + TestCase.fail("the value should be illegal."); + } catch (Exception e) { + // ignore + } + } + + @Test + public void checkParameterName() throws Exception { + Map parameters = Collections.singletonMap("hello", ":*,/-._0123abcdABCD"); + try { + AbstractConfig.checkParameterName(parameters); + } catch (Exception e) { + TestCase.fail("the value should be legal."); + } + } + + @Test + @Config(interfaceClass = Greeting.class, filter = {"f1, f2"}, listener = {"l1, l2"}, + parameters = {"k1", "v1", "k2", "v2"}) + public void appendAnnotation() throws Exception { + Config config = getClass().getMethod("appendAnnotation").getAnnotation(Config.class); + AnnotationConfig annotationConfig = new AnnotationConfig(); + annotationConfig.appendAnnotation(Config.class, config); + TestCase.assertSame(Greeting.class, annotationConfig.getInterface()); + TestCase.assertEquals("f1, f2", annotationConfig.getFilter()); + TestCase.assertEquals("l1, l2", annotationConfig.getListener()); + TestCase.assertEquals(2, annotationConfig.getParameters().size()); + TestCase.assertEquals("v1", annotationConfig.getParameters().get("k1")); + TestCase.assertEquals("v2", annotationConfig.getParameters().get("k2")); + TestCase.assertEquals("", + annotationConfig.toString()); + } + + private static class PropertiesConfig extends AbstractConfig { + private char c; + private boolean bool; + private byte b; + private int i; + private long l; + private float f; + private double d; + private short s; + private String str; + + PropertiesConfig() { + } + + PropertiesConfig(String id) { + this.id = id; + } + + public char getC() { + return c; + } + + public void setC(char c) { + this.c = c; + } + + public boolean isBool() { + return bool; + } + + public void setBool(boolean bool) { + this.bool = bool; + } + + public byte getB() { + return b; + } + + public void setB(byte b) { + this.b = b; + } + + public int getI() { + return i; + } + + public void setI(int i) { + this.i = i; + } + + public long getL() { + return l; + } + + public void setL(long l) { + this.l = l; + } + + public float getF() { + return f; + } + + public void setF(float f) { + this.f = f; + } + + public double getD() { + return d; + } + + public void setD(double d) { + this.d = d; + } + + public String getStr() { + return str; + } + + public void setStr(String str) { + this.str = str; + } + + public short getS() { + return s; + } + + public void setS(short s) { + this.s = s; + } + } + + private static class ParameterConfig { + private int number; + private String name; + private int age; + private String secret; + + ParameterConfig() { + } + + ParameterConfig(int number, String name, int age, String secret) { + this.number = number; + this.name = name; + this.age = age; + this.secret = secret; + } + + @Parameter(key = "num", append = true) + public int getNumber() { + return number; + } + + public void setNumber(int number) { + this.number = number; + } + + @Parameter(key = "naming", append = true, escaped = true, required = true) + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + + @Parameter(excluded = true) + public String getSecret() { + return secret; + } + + public void setSecret(String secret) { + this.secret = secret; + } + + public Map getParameters() { + Map map = new HashMap(); + map.put("key.1", "one"); + map.put("key-2", "two"); + return map; + } + } + + private static class AttributeConfig { + private char letter; + private boolean activate; + private byte flag; + + public AttributeConfig(char letter, boolean activate, byte flag) { + this.letter = letter; + this.activate = activate; + this.flag = flag; + } + + @Parameter(attribute = true, key = "let") + public char getLetter() { + return letter; + } + + public void setLetter(char letter) { + this.letter = letter; + } + + @Parameter(attribute = true) + public boolean isActivate() { + return activate; + } + + public void setActivate(boolean activate) { + this.activate = activate; + } + + public byte getFlag() { + return flag; + } + + public void setFlag(byte flag) { + this.flag = flag; + } + } + + @Retention(RetentionPolicy.RUNTIME) + @Target({ElementType.FIELD, ElementType.METHOD, ElementType.ANNOTATION_TYPE}) + public @interface Config { + Class interfaceClass() default void.class; + + String interfaceName() default ""; + + String[] filter() default {}; + + String[] listener() default {}; + + String[] parameters() default {}; + } + + private static class AnnotationConfig extends AbstractConfig { + private Class interfaceClass; + private String filter; + private String listener; + private Map parameters; + + public Class getInterface() { + return interfaceClass; + } + + public void setInterface(Class interfaceName) { + this.interfaceClass = interfaceName; + } + + public String getFilter() { + return filter; + } + + public void setFilter(String filter) { + this.filter = filter; + } + + public String getListener() { + return listener; + } + + public void setListener(String listener) { + this.listener = listener; + } + + public Map getParameters() { + return parameters; + } + + public void setParameters(Map parameters) { + this.parameters = parameters; + } + } +} diff --git a/dubbo-config/dubbo-config-api/src/test/java/com/alibaba/dubbo/config/AbstractInterfaceConfigTest.java b/dubbo-config/dubbo-config-api/src/test/java/com/alibaba/dubbo/config/AbstractInterfaceConfigTest.java new file mode 100644 index 00000000000..94578b2cfd4 --- /dev/null +++ b/dubbo-config/dubbo-config-api/src/test/java/com/alibaba/dubbo/config/AbstractInterfaceConfigTest.java @@ -0,0 +1,398 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 com.alibaba.dubbo.config; + +import com.alibaba.dubbo.common.Constants; +import com.alibaba.dubbo.common.URL; +import com.alibaba.dubbo.common.utils.ConfigUtils; +import com.alibaba.dubbo.monitor.MonitorService; +import com.alibaba.dubbo.registry.RegistryService; +import junit.framework.TestCase; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.ClassRule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +import java.io.BufferedOutputStream; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.util.Collections; +import java.util.List; +import java.util.Properties; + +public class AbstractInterfaceConfigTest { + @ClassRule + public static TemporaryFolder tempDir = new TemporaryFolder(); + private static File dubboProperties; + + @BeforeClass + public static void setUp() throws Exception { + dubboProperties = tempDir.newFile(Constants.DUBBO_PROPERTIES_KEY); + System.setProperty(Constants.DUBBO_PROPERTIES_KEY, dubboProperties.getAbsolutePath()); + } + + @AfterClass + public static void tearDown() throws Exception { + System.clearProperty(Constants.DUBBO_PROPERTIES_KEY); + } + + @Test + public void testCheckRegistry1() throws Exception { + System.setProperty("dubbo.registry.address", "addr1|addr2"); + try { + InterfaceConfig interfaceConfig = new InterfaceConfig(); + interfaceConfig.checkRegistry(); + TestCase.assertEquals(2, interfaceConfig.getRegistries().size()); + } finally { + System.clearProperty("dubbo.registry.address"); + } + } + + @Test(expected = IllegalStateException.class) + public void testCheckRegistry2() throws Exception { + InterfaceConfig interfaceConfig = new InterfaceConfig(); + interfaceConfig.checkRegistry(); + } + + @Test + public void checkApplication1() throws Exception { + try { + ConfigUtils.setProperties(null); + writeDubboProperties(Constants.SHUTDOWN_WAIT_KEY, "100"); + System.setProperty("dubbo.application.name", "demo"); + InterfaceConfig interfaceConfig = new InterfaceConfig(); + interfaceConfig.checkApplication(); + ApplicationConfig appConfig = interfaceConfig.getApplication(); + TestCase.assertEquals("demo", appConfig.getName()); + TestCase.assertEquals("100", System.getProperty(Constants.SHUTDOWN_WAIT_KEY)); + + System.clearProperty(Constants.SHUTDOWN_WAIT_KEY); + ConfigUtils.setProperties(null); + writeDubboProperties(Constants.SHUTDOWN_WAIT_SECONDS_KEY, "1000"); + System.setProperty("dubbo.application.name", "demo"); + interfaceConfig = new InterfaceConfig(); + interfaceConfig.checkApplication(); + TestCase.assertEquals("1000", System.getProperty(Constants.SHUTDOWN_WAIT_SECONDS_KEY)); + } finally { + ConfigUtils.setProperties(null); + System.clearProperty("dubbo.application.name"); + System.clearProperty(Constants.SHUTDOWN_WAIT_KEY); + System.clearProperty(Constants.SHUTDOWN_WAIT_SECONDS_KEY); + } + } + + @Test(expected = IllegalStateException.class) + public void checkApplication2() throws Exception { + InterfaceConfig interfaceConfig = new InterfaceConfig(); + interfaceConfig.checkApplication(); + } + + @Test + public void testLoadRegistries() throws Exception { + System.setProperty("dubbo.registry.address", "addr1"); + InterfaceConfig interfaceConfig = new InterfaceConfig(); + List urls = interfaceConfig.loadRegistries(true); + TestCase.assertEquals(1, urls.size()); + URL url = urls.get(0); + TestCase.assertEquals("registry", url.getProtocol()); + TestCase.assertEquals("addr1:9090", url.getAddress()); + TestCase.assertEquals(RegistryService.class.getName(), url.getPath()); + TestCase.assertTrue(url.getParameters().containsKey("timestamp")); + TestCase.assertTrue(url.getParameters().containsKey("pid")); + TestCase.assertTrue(url.getParameters().containsKey("registry")); + TestCase.assertTrue(url.getParameters().containsKey("dubbo")); + } + + @Test + public void testLoadMonitor() throws Exception { + System.setProperty("dubbo.monitor.address", "monitor-addr:12080"); + System.setProperty("dubbo.monitor.protocol", "monitor"); + InterfaceConfig interfaceConfig = new InterfaceConfig(); + URL url = interfaceConfig.loadMonitor(new URL("dubbo", "addr1", 9090)); + TestCase.assertEquals("monitor-addr:12080", url.getAddress()); + TestCase.assertEquals(MonitorService.class.getName(), url.getParameter("interface")); + TestCase.assertNotNull(url.getParameter("dubbo")); + TestCase.assertNotNull(url.getParameter("pid")); + TestCase.assertNotNull(url.getParameter("timestamp")); + } + + @Test(expected = IllegalStateException.class) + public void checkInterfaceAndMethods1() throws Exception { + InterfaceConfig interfaceConfig = new InterfaceConfig(); + interfaceConfig.checkInterfaceAndMethods(null, null); + } + + @Test(expected = IllegalStateException.class) + public void checkInterfaceAndMethods2() throws Exception { + InterfaceConfig interfaceConfig = new InterfaceConfig(); + interfaceConfig.checkInterfaceAndMethods(AbstractInterfaceConfigTest.class, null); + } + + @Test(expected = IllegalStateException.class) + public void checkInterfaceAndMethod3() throws Exception { + MethodConfig methodConfig = new MethodConfig(); + InterfaceConfig interfaceConfig = new InterfaceConfig(); + interfaceConfig.checkInterfaceAndMethods(Greeting.class, Collections.singletonList(methodConfig)); + } + + @Test(expected = IllegalStateException.class) + public void checkInterfaceAndMethod4() throws Exception { + MethodConfig methodConfig = new MethodConfig(); + methodConfig.setName("nihao"); + InterfaceConfig interfaceConfig = new InterfaceConfig(); + interfaceConfig.checkInterfaceAndMethods(Greeting.class, Collections.singletonList(methodConfig)); + } + + @Test + public void checkInterfaceAndMethod5() throws Exception { + MethodConfig methodConfig = new MethodConfig(); + methodConfig.setName("hello"); + InterfaceConfig interfaceConfig = new InterfaceConfig(); + interfaceConfig.checkInterfaceAndMethods(Greeting.class, Collections.singletonList(methodConfig)); + } + + @Test(expected = IllegalStateException.class) + public void checkStubAndMock1() throws Exception { + InterfaceConfig interfaceConfig = new InterfaceConfig(); + interfaceConfig.setLocal(GreetingLocal1.class.getName()); + interfaceConfig.checkStubAndMock(Greeting.class); + } + + @Test(expected = IllegalStateException.class) + public void checkStubAndMock2() throws Exception { + InterfaceConfig interfaceConfig = new InterfaceConfig(); + interfaceConfig.setLocal(GreetingLocal2.class.getName()); + interfaceConfig.checkStubAndMock(Greeting.class); + } + + @Test + public void checkStubAndMock3() throws Exception { + InterfaceConfig interfaceConfig = new InterfaceConfig(); + interfaceConfig.setLocal(GreetingLocal3.class.getName()); + interfaceConfig.checkStubAndMock(Greeting.class); + } + + @Test(expected = IllegalStateException.class) + public void checkStubAndMock4() throws Exception { + InterfaceConfig interfaceConfig = new InterfaceConfig(); + interfaceConfig.setStub(GreetingLocal1.class.getName()); + interfaceConfig.checkStubAndMock(Greeting.class); + } + + @Test(expected = IllegalStateException.class) + public void checkStubAndMock5() throws Exception { + InterfaceConfig interfaceConfig = new InterfaceConfig(); + interfaceConfig.setStub(GreetingLocal2.class.getName()); + interfaceConfig.checkStubAndMock(Greeting.class); + } + + @Test + public void checkStubAndMock6() throws Exception { + InterfaceConfig interfaceConfig = new InterfaceConfig(); + interfaceConfig.setStub(GreetingLocal3.class.getName()); + interfaceConfig.checkStubAndMock(Greeting.class); + } + + @Test(expected = IllegalStateException.class) + public void checkStubAndMock7() throws Exception { + InterfaceConfig interfaceConfig = new InterfaceConfig(); + interfaceConfig.setMock("return {a, b}"); + interfaceConfig.checkStubAndMock(Greeting.class); + } + + @Test(expected = IllegalStateException.class) + public void checkStubAndMock8() throws Exception { + InterfaceConfig interfaceConfig = new InterfaceConfig(); + interfaceConfig.setMock(GreetingMock1.class.getName()); + interfaceConfig.checkStubAndMock(Greeting.class); + } + + @Test(expected = IllegalStateException.class) + public void checkStubAndMock9() throws Exception { + InterfaceConfig interfaceConfig = new InterfaceConfig(); + interfaceConfig.setMock(GreetingMock2.class.getName()); + interfaceConfig.checkStubAndMock(Greeting.class); + } + + @Test + public void testLocal() throws Exception { + InterfaceConfig interfaceConfig = new InterfaceConfig(); + interfaceConfig.setLocal((Boolean) null); + TestCase.assertNull(interfaceConfig.getLocal()); + interfaceConfig.setLocal(true); + TestCase.assertEquals("true", interfaceConfig.getLocal()); + interfaceConfig.setLocal("GreetingMock"); + TestCase.assertEquals("GreetingMock", interfaceConfig.getLocal()); + } + + @Test + public void testStub() throws Exception { + InterfaceConfig interfaceConfig = new InterfaceConfig(); + interfaceConfig.setStub((Boolean) null); + TestCase.assertNull(interfaceConfig.getStub()); + interfaceConfig.setStub(true); + TestCase.assertEquals("true", interfaceConfig.getStub()); + interfaceConfig.setStub("GreetingMock"); + TestCase.assertEquals("GreetingMock", interfaceConfig.getStub()); + } + + @Test + public void testCluster() throws Exception { + InterfaceConfig interfaceConfig = new InterfaceConfig(); + interfaceConfig.setCluster("mockcluster"); + TestCase.assertEquals("mockcluster", interfaceConfig.getCluster()); + } + + @Test + public void testProxy() throws Exception { + InterfaceConfig interfaceConfig = new InterfaceConfig(); + interfaceConfig.setProxy("mockproxyfactory"); + TestCase.assertEquals("mockproxyfactory", interfaceConfig.getProxy()); + } + + @Test + public void testConnections() throws Exception { + InterfaceConfig interfaceConfig = new InterfaceConfig(); + interfaceConfig.setConnections(1); + TestCase.assertEquals(1, interfaceConfig.getConnections().intValue()); + } + + @Test + public void testFilter() throws Exception { + InterfaceConfig interfaceConfig = new InterfaceConfig(); + interfaceConfig.setFilter("mockfilter"); + TestCase.assertEquals("mockfilter", interfaceConfig.getFilter()); + } + + @Test + public void testListener() throws Exception { + InterfaceConfig interfaceConfig = new InterfaceConfig(); + interfaceConfig.setListener("mockinvokerlistener"); + TestCase.assertEquals("mockinvokerlistener", interfaceConfig.getListener()); + } + + @Test + public void testLayer() throws Exception { + InterfaceConfig interfaceConfig = new InterfaceConfig(); + interfaceConfig.setLayer("layer"); + TestCase.assertEquals("layer", interfaceConfig.getLayer()); + } + + @Test + public void testApplication() throws Exception { + InterfaceConfig interfaceConfig = new InterfaceConfig(); + ApplicationConfig applicationConfig = new ApplicationConfig(); + interfaceConfig.setApplication(applicationConfig); + TestCase.assertSame(applicationConfig, interfaceConfig.getApplication()); + } + + @Test + public void testModule() throws Exception { + InterfaceConfig interfaceConfig = new InterfaceConfig(); + ModuleConfig moduleConfig = new ModuleConfig(); + interfaceConfig.setModule(moduleConfig); + TestCase.assertSame(moduleConfig, interfaceConfig.getModule()); + } + + @Test + public void testRegistry() throws Exception { + InterfaceConfig interfaceConfig = new InterfaceConfig(); + RegistryConfig registryConfig = new RegistryConfig(); + interfaceConfig.setRegistry(registryConfig); + TestCase.assertSame(registryConfig, interfaceConfig.getRegistry()); + } + + @Test + public void testRegistries() throws Exception { + InterfaceConfig interfaceConfig = new InterfaceConfig(); + RegistryConfig registryConfig = new RegistryConfig(); + interfaceConfig.setRegistries(Collections.singletonList(registryConfig)); + TestCase.assertEquals(1, interfaceConfig.getRegistries().size()); + TestCase.assertSame(registryConfig, interfaceConfig.getRegistries().get(0)); + } + + @Test + public void testMonitor() throws Exception { + InterfaceConfig interfaceConfig = new InterfaceConfig(); + interfaceConfig.setMonitor("monitor-addr"); + TestCase.assertEquals("monitor-addr", interfaceConfig.getMonitor().getAddress()); + MonitorConfig monitorConfig = new MonitorConfig(); + interfaceConfig.setMonitor(monitorConfig); + TestCase.assertSame(monitorConfig, interfaceConfig.getMonitor()); + } + + @Test + public void testOwner() throws Exception { + InterfaceConfig interfaceConfig = new InterfaceConfig(); + interfaceConfig.setOwner("owner"); + TestCase.assertEquals("owner", interfaceConfig.getOwner()); + } + + @Test + public void testCallbacks() throws Exception { + InterfaceConfig interfaceConfig = new InterfaceConfig(); + interfaceConfig.setCallbacks(2); + TestCase.assertEquals(2, interfaceConfig.getCallbacks().intValue()); + } + + @Test + public void testOnconnect() throws Exception { + InterfaceConfig interfaceConfig = new InterfaceConfig(); + interfaceConfig.setOnconnect("onConnect"); + TestCase.assertEquals("onConnect", interfaceConfig.getOnconnect()); + } + + @Test + public void testOndisconnect() throws Exception { + InterfaceConfig interfaceConfig = new InterfaceConfig(); + interfaceConfig.setOndisconnect("onDisconnect"); + TestCase.assertEquals("onDisconnect", interfaceConfig.getOndisconnect()); + } + + @Test + public void testScope() throws Exception { + InterfaceConfig interfaceConfig = new InterfaceConfig(); + interfaceConfig.setScope("scope"); + TestCase.assertEquals("scope", interfaceConfig.getScope()); + } + + private void writeDubboProperties(String key, String value) { + OutputStream os = null; + try { + os = new BufferedOutputStream(new FileOutputStream(dubboProperties)); + Properties properties = new Properties(); + properties.put(key, value); + properties.store(os, ""); + os.close(); + } catch (IOException e) { + if (os != null) { + try { + os.close(); + } catch (IOException ioe) { + // ignore + } + } + } + } + + private static class InterfaceConfig extends AbstractInterfaceConfig { + + } +} diff --git a/dubbo-config/dubbo-config-api/src/test/java/com/alibaba/dubbo/config/Greeting.java b/dubbo-config/dubbo-config-api/src/test/java/com/alibaba/dubbo/config/Greeting.java new file mode 100644 index 00000000000..65ab0f3b522 --- /dev/null +++ b/dubbo-config/dubbo-config-api/src/test/java/com/alibaba/dubbo/config/Greeting.java @@ -0,0 +1,24 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 com.alibaba.dubbo.config; + +import com.alibaba.dubbo.common.extension.SPI; + +@SPI +interface Greeting { + String hello(); +} diff --git a/dubbo-config/dubbo-config-api/src/test/java/com/alibaba/dubbo/config/GreetingLocal1.java b/dubbo-config/dubbo-config-api/src/test/java/com/alibaba/dubbo/config/GreetingLocal1.java new file mode 100644 index 00000000000..0bcfe0634e1 --- /dev/null +++ b/dubbo-config/dubbo-config-api/src/test/java/com/alibaba/dubbo/config/GreetingLocal1.java @@ -0,0 +1,21 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 com.alibaba.dubbo.config; + +class GreetingLocal1 { + +} diff --git a/dubbo-config/dubbo-config-api/src/test/java/com/alibaba/dubbo/config/GreetingLocal2.java b/dubbo-config/dubbo-config-api/src/test/java/com/alibaba/dubbo/config/GreetingLocal2.java new file mode 100644 index 00000000000..768ac7870ce --- /dev/null +++ b/dubbo-config/dubbo-config-api/src/test/java/com/alibaba/dubbo/config/GreetingLocal2.java @@ -0,0 +1,24 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 com.alibaba.dubbo.config; + +class GreetingLocal2 implements Greeting { + @Override + public String hello() { + return "local"; + } +} diff --git a/dubbo-config/dubbo-config-api/src/test/java/com/alibaba/dubbo/config/GreetingLocal3.java b/dubbo-config/dubbo-config-api/src/test/java/com/alibaba/dubbo/config/GreetingLocal3.java new file mode 100644 index 00000000000..8c041a39980 --- /dev/null +++ b/dubbo-config/dubbo-config-api/src/test/java/com/alibaba/dubbo/config/GreetingLocal3.java @@ -0,0 +1,30 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 com.alibaba.dubbo.config; + +class GreetingLocal3 implements Greeting { + private Greeting greeting; + + public GreetingLocal3(Greeting greeting) { + this.greeting = greeting; + } + + @Override + public String hello() { + return null; + } +} diff --git a/dubbo-config/dubbo-config-api/src/test/java/com/alibaba/dubbo/config/GreetingMock1.java b/dubbo-config/dubbo-config-api/src/test/java/com/alibaba/dubbo/config/GreetingMock1.java new file mode 100644 index 00000000000..edfcc58ec32 --- /dev/null +++ b/dubbo-config/dubbo-config-api/src/test/java/com/alibaba/dubbo/config/GreetingMock1.java @@ -0,0 +1,20 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 com.alibaba.dubbo.config; + +public class GreetingMock1 { +} diff --git a/dubbo-config/dubbo-config-api/src/test/java/com/alibaba/dubbo/config/GreetingMock2.java b/dubbo-config/dubbo-config-api/src/test/java/com/alibaba/dubbo/config/GreetingMock2.java new file mode 100644 index 00000000000..c3d504419a9 --- /dev/null +++ b/dubbo-config/dubbo-config-api/src/test/java/com/alibaba/dubbo/config/GreetingMock2.java @@ -0,0 +1,27 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 com.alibaba.dubbo.config; + +public class GreetingMock2 implements Greeting { + private GreetingMock2() { + } + + @Override + public String hello() { + return "mock"; + } +} diff --git a/dubbo-config/dubbo-config-api/src/test/java/com/alibaba/dubbo/config/MockCluster.java b/dubbo-config/dubbo-config-api/src/test/java/com/alibaba/dubbo/config/MockCluster.java new file mode 100644 index 00000000000..cef8061edfe --- /dev/null +++ b/dubbo-config/dubbo-config-api/src/test/java/com/alibaba/dubbo/config/MockCluster.java @@ -0,0 +1,29 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 com.alibaba.dubbo.config; + +import com.alibaba.dubbo.rpc.Invoker; +import com.alibaba.dubbo.rpc.RpcException; +import com.alibaba.dubbo.rpc.cluster.Cluster; +import com.alibaba.dubbo.rpc.cluster.Directory; + +public class MockCluster implements Cluster { + @Override + public Invoker join(Directory directory) throws RpcException { + return null; + } +} diff --git a/dubbo-config/dubbo-config-api/src/test/java/com/alibaba/dubbo/config/MockFilter.java b/dubbo-config/dubbo-config-api/src/test/java/com/alibaba/dubbo/config/MockFilter.java new file mode 100644 index 00000000000..4701590c2d3 --- /dev/null +++ b/dubbo-config/dubbo-config-api/src/test/java/com/alibaba/dubbo/config/MockFilter.java @@ -0,0 +1,30 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 com.alibaba.dubbo.config; + +import com.alibaba.dubbo.rpc.Filter; +import com.alibaba.dubbo.rpc.Invocation; +import com.alibaba.dubbo.rpc.Invoker; +import com.alibaba.dubbo.rpc.Result; +import com.alibaba.dubbo.rpc.RpcException; + +public class MockFilter implements Filter { + @Override + public Result invoke(Invoker invoker, Invocation invocation) throws RpcException { + return null; + } +} diff --git a/dubbo-config/dubbo-config-api/src/test/java/com/alibaba/dubbo/config/MockInvokerListener.java b/dubbo-config/dubbo-config-api/src/test/java/com/alibaba/dubbo/config/MockInvokerListener.java new file mode 100644 index 00000000000..00f925a032b --- /dev/null +++ b/dubbo-config/dubbo-config-api/src/test/java/com/alibaba/dubbo/config/MockInvokerListener.java @@ -0,0 +1,33 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 com.alibaba.dubbo.config; + +import com.alibaba.dubbo.rpc.Invoker; +import com.alibaba.dubbo.rpc.InvokerListener; +import com.alibaba.dubbo.rpc.RpcException; + +public class MockInvokerListener implements InvokerListener { + @Override + public void referred(Invoker invoker) throws RpcException { + + } + + @Override + public void destroyed(Invoker invoker) { + + } +} diff --git a/dubbo-config/dubbo-config-api/src/test/java/com/alibaba/dubbo/config/MockProxyFactory.java b/dubbo-config/dubbo-config-api/src/test/java/com/alibaba/dubbo/config/MockProxyFactory.java new file mode 100644 index 00000000000..f656c58d314 --- /dev/null +++ b/dubbo-config/dubbo-config-api/src/test/java/com/alibaba/dubbo/config/MockProxyFactory.java @@ -0,0 +1,34 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 com.alibaba.dubbo.config; + +import com.alibaba.dubbo.common.URL; +import com.alibaba.dubbo.rpc.Invoker; +import com.alibaba.dubbo.rpc.ProxyFactory; +import com.alibaba.dubbo.rpc.RpcException; + +public class MockProxyFactory implements ProxyFactory { + @Override + public T getProxy(Invoker invoker) throws RpcException { + return null; + } + + @Override + public Invoker getInvoker(T proxy, Class type, URL url) throws RpcException { + return null; + } +} diff --git a/dubbo-config/dubbo-config-api/src/test/resources/META-INF/services/com.alibaba.dubbo.rpc.Filter b/dubbo-config/dubbo-config-api/src/test/resources/META-INF/services/com.alibaba.dubbo.rpc.Filter new file mode 100644 index 00000000000..3ad1d56bec5 --- /dev/null +++ b/dubbo-config/dubbo-config-api/src/test/resources/META-INF/services/com.alibaba.dubbo.rpc.Filter @@ -0,0 +1 @@ +mockfilter=com.alibaba.dubbo.config.MockFilter \ No newline at end of file diff --git a/dubbo-config/dubbo-config-api/src/test/resources/META-INF/services/com.alibaba.dubbo.rpc.InvokerListener b/dubbo-config/dubbo-config-api/src/test/resources/META-INF/services/com.alibaba.dubbo.rpc.InvokerListener new file mode 100644 index 00000000000..00616035a96 --- /dev/null +++ b/dubbo-config/dubbo-config-api/src/test/resources/META-INF/services/com.alibaba.dubbo.rpc.InvokerListener @@ -0,0 +1 @@ +mockinvokerlistener=com.alibaba.dubbo.config.MockInvokerListener \ No newline at end of file diff --git a/dubbo-config/dubbo-config-api/src/test/resources/META-INF/services/com.alibaba.dubbo.rpc.ProxyFactory b/dubbo-config/dubbo-config-api/src/test/resources/META-INF/services/com.alibaba.dubbo.rpc.ProxyFactory new file mode 100644 index 00000000000..370be716cb5 --- /dev/null +++ b/dubbo-config/dubbo-config-api/src/test/resources/META-INF/services/com.alibaba.dubbo.rpc.ProxyFactory @@ -0,0 +1 @@ +mockproxyfactory=com.alibaba.dubbo.config.MockProxyFactory \ No newline at end of file diff --git a/dubbo-config/dubbo-config-api/src/test/resources/META-INF/services/com.alibaba.dubbo.rpc.cluster.Cluster b/dubbo-config/dubbo-config-api/src/test/resources/META-INF/services/com.alibaba.dubbo.rpc.cluster.Cluster new file mode 100644 index 00000000000..9ef684e92d5 --- /dev/null +++ b/dubbo-config/dubbo-config-api/src/test/resources/META-INF/services/com.alibaba.dubbo.rpc.cluster.Cluster @@ -0,0 +1 @@ +mockcluster=com.alibaba.dubbo.config.MockCluster \ No newline at end of file