From fa6984c95c788324f04214cd0878f6c2b1f40d09 Mon Sep 17 00:00:00 2001 From: beiwei30 Date: Tue, 15 May 2018 19:00:16 +0800 Subject: [PATCH] #1682: Enhance the test coverage part-4: dubbo-common/src/main/java/com/alibaba/dubbo/common/status(store|threadpoolutils) modules --- .../dubbo/common/utils/ConfigUtils.java | 8 +- .../dubbo/common/utils/AssertTest.java | 34 +++ .../utils/AtomicPositiveIntegerTest.java | 64 ++++-- .../dubbo/common/utils/ClassHelperTest.java | 121 +++++++++++ .../common/utils/CollectionUtilsTest.java | 56 ++++- .../dubbo/common/utils/ConfigUtilsTest.java | 205 +++++++++++++++--- .../src/test/resources/dubbo.properties | 18 ++ 7 files changed, 445 insertions(+), 61 deletions(-) create mode 100644 dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/AssertTest.java create mode 100644 dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/ClassHelperTest.java create mode 100644 dubbo-common/src/test/resources/dubbo.properties 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 c5b51d8beb5..8c4ac69aeab 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 @@ -226,7 +226,7 @@ public static Properties loadProperties(String fileName, boolean allowMultiFile, input.close(); } } catch (Throwable e) { - logger.warn("Failed to load " + fileName + " file from " + fileName + "(ingore this file): " + e.getMessage(), e); + logger.warn("Failed to load " + fileName + " file from " + fileName + "(ignore this file): " + e.getMessage(), e); } return properties; } @@ -261,7 +261,7 @@ public static Properties loadProperties(String fileName, boolean allowMultiFile, try { properties.load(ClassHelper.getClassLoader().getResourceAsStream(fileName)); } catch (Throwable e) { - logger.warn("Failed to load " + fileName + " file from " + fileName + "(ingore this file): " + e.getMessage(), e); + logger.warn("Failed to load " + fileName + " file from " + fileName + "(ignore this file): " + e.getMessage(), e); } return properties; } @@ -284,7 +284,7 @@ public static Properties loadProperties(String fileName, boolean allowMultiFile, } } } catch (Throwable e) { - logger.warn("Fail to load " + fileName + " file from " + url + "(ingore this file): " + e.getMessage(), e); + logger.warn("Fail to load " + fileName + " file from " + url + "(ignore this file): " + e.getMessage(), e); } } @@ -312,6 +312,7 @@ public static int getServerShutdownTimeout() { try { timeout = Integer.parseInt(value); } catch (Exception e) { + // ignore } } else { value = ConfigUtils.getProperty(Constants.SHUTDOWN_WAIT_SECONDS_KEY); @@ -319,6 +320,7 @@ public static int getServerShutdownTimeout() { try { timeout = Integer.parseInt(value) * 1000; } catch (Exception e) { + // ignore } } } diff --git a/dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/AssertTest.java b/dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/AssertTest.java new file mode 100644 index 00000000000..ec390b5ad89 --- /dev/null +++ b/dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/AssertTest.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.common.utils; + +import org.junit.Test; + +import static com.alibaba.dubbo.common.utils.Assert.notNull; + +public class AssertTest { + @Test(expected = IllegalArgumentException.class) + public void testNotNull1() throws Exception { + notNull(null, "null object"); + } + + @Test(expected = IllegalStateException.class) + public void testNotNull2() throws Exception { + notNull(null, new IllegalStateException("null object")); + } +} diff --git a/dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/AtomicPositiveIntegerTest.java b/dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/AtomicPositiveIntegerTest.java index 65873530df8..32d014e50d4 100644 --- a/dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/AtomicPositiveIntegerTest.java +++ b/dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/AtomicPositiveIntegerTest.java @@ -19,27 +19,29 @@ import org.junit.Test; import static org.hamcrest.CoreMatchers.allOf; +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThat; import static org.junit.Assert.fail; -import static org.junit.matchers.JUnitMatchers.containsString; public class AtomicPositiveIntegerTest { - AtomicPositiveInteger i1 = new AtomicPositiveInteger(); + private AtomicPositiveInteger i1 = new AtomicPositiveInteger(); - AtomicPositiveInteger i2 = new AtomicPositiveInteger(127); + private AtomicPositiveInteger i2 = new AtomicPositiveInteger(127); - AtomicPositiveInteger i3 = new AtomicPositiveInteger(Integer.MAX_VALUE); + private AtomicPositiveInteger i3 = new AtomicPositiveInteger(Integer.MAX_VALUE); @Test - public void test_get() throws Exception { + public void testGet() throws Exception { assertEquals(0, i1.get()); assertEquals(127, i2.get()); assertEquals(Integer.MAX_VALUE, i3.get()); } @Test - public void test_set() throws Exception { + public void testSet() throws Exception { i1.set(100); assertEquals(100, i1.get()); @@ -53,7 +55,7 @@ public void test_set() throws Exception { } @Test - public void test_getAndIncrement() throws Exception { + public void testGetAndIncrement() throws Exception { int get = i1.getAndIncrement(); assertEquals(0, get); assertEquals(1, i1.get()); @@ -68,7 +70,7 @@ public void test_getAndIncrement() throws Exception { } @Test - public void test_getAndDecrement() throws Exception { + public void testGetAndDecrement() throws Exception { int get = i1.getAndDecrement(); assertEquals(0, get); assertEquals(Integer.MAX_VALUE, i1.get()); @@ -83,7 +85,7 @@ public void test_getAndDecrement() throws Exception { } @Test - public void test_incrementAndGet() throws Exception { + public void testIncrementAndGet() throws Exception { int get = i1.incrementAndGet(); assertEquals(1, get); assertEquals(1, i1.get()); @@ -98,7 +100,7 @@ public void test_incrementAndGet() throws Exception { } @Test - public void test_decrementAndGet() throws Exception { + public void testDecrementAndGet() throws Exception { int get = i1.decrementAndGet(); assertEquals(Integer.MAX_VALUE, get); assertEquals(Integer.MAX_VALUE, i1.get()); @@ -113,7 +115,7 @@ public void test_decrementAndGet() throws Exception { } @Test - public void test_getAndSet() throws Exception { + public void testGetAndSet() throws Exception { int get = i1.getAndSet(100); assertEquals(0, get); assertEquals(100, i1.get()); @@ -127,7 +129,7 @@ public void test_getAndSet() throws Exception { } @Test - public void test_getAndAnd() throws Exception { + public void testGetAndAnd() throws Exception { int get = i1.getAndAdd(3); assertEquals(0, get); assertEquals(3, i1.get()); @@ -143,7 +145,7 @@ public void test_getAndAnd() throws Exception { @Test - public void test_addAndGet() throws Exception { + public void testAddAndGet() throws Exception { int get = i1.addAndGet(3); assertEquals(3, get); assertEquals(3, i1.get()); @@ -157,8 +159,42 @@ public void test_addAndGet() throws Exception { assertEquals(2, i3.get()); } + @Test(expected = IllegalArgumentException.class) + public void testCompareAndSet1() throws Exception { + i1.compareAndSet(i1.get(), -1); + } + + @Test + public void testCompareAndSet2() throws Exception { + assertThat(i1.compareAndSet(i1.get(), 2), is(true)); + assertThat(i1.get(), is(2)); + } + + @Test(expected = IllegalArgumentException.class) + public void testWeakCompareAndSet1() throws Exception { + i1.weakCompareAndSet(i1.get(), -1); + } + + @Test + public void testWeakCompareAndSet2() throws Exception { + assertThat(i1.weakCompareAndSet(i1.get(), 2), is(true)); + assertThat(i1.get(), is(2)); + } + + @Test + public void testValues() throws Exception { + Integer i = i1.get(); + assertThat(i1.byteValue(), equalTo(i.byteValue())); + assertThat(i1.shortValue(), equalTo(i.shortValue())); + assertThat(i1.intValue(), equalTo(i.intValue())); + assertThat(i1.longValue(), equalTo(i.longValue())); + assertThat(i1.floatValue(), equalTo(i.floatValue())); + assertThat(i1.doubleValue(), equalTo(i.doubleValue())); + assertThat(i1.toString(), equalTo(i.toString())); + } + @Test - public void test_equals() { + public void testEquals() { assertEquals(new AtomicPositiveInteger(), new AtomicPositiveInteger()); assertEquals(new AtomicPositiveInteger(1), new AtomicPositiveInteger(1)); } diff --git a/dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/ClassHelperTest.java b/dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/ClassHelperTest.java new file mode 100644 index 00000000000..834fcc1ad85 --- /dev/null +++ b/dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/ClassHelperTest.java @@ -0,0 +1,121 @@ +/* + * 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.common.utils; + +import org.junit.Test; +import org.mockito.Mockito; + +import static com.alibaba.dubbo.common.utils.ClassHelper.forName; +import static com.alibaba.dubbo.common.utils.ClassHelper.getCallerClassLoader; +import static com.alibaba.dubbo.common.utils.ClassHelper.getClassLoader; +import static com.alibaba.dubbo.common.utils.ClassHelper.resolvePrimitiveClassName; +import static com.alibaba.dubbo.common.utils.ClassHelper.toShortString; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.sameInstance; +import static org.hamcrest.Matchers.startsWith; +import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.verify; + +public class ClassHelperTest { + @Test + public void testForNameWithThreadContextClassLoader() throws Exception { + ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader(); + try { + ClassLoader classLoader = Mockito.mock(ClassLoader.class); + Thread.currentThread().setContextClassLoader(classLoader); + ClassHelper.forNameWithThreadContextClassLoader("a.b.c.D"); + verify(classLoader).loadClass("a.b.c.D"); + } finally { + Thread.currentThread().setContextClassLoader(oldClassLoader); + } + } + + @Test + public void tetForNameWithCallerClassLoader() throws Exception { + Class c = ClassHelper.forNameWithCallerClassLoader(ClassHelper.class.getName(), ClassHelperTest.class); + assertThat(c == ClassHelper.class, is(true)); + } + + @Test + public void testGetCallerClassLoader() throws Exception { + assertThat(getCallerClassLoader(ClassHelperTest.class), sameInstance(ClassHelperTest.class.getClassLoader())); + } + + @Test + public void testGetClassLoader1() throws Exception { + ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader(); + try { + assertThat(getClassLoader(ClassHelperTest.class), sameInstance(oldClassLoader)); + Thread.currentThread().setContextClassLoader(null); + assertThat(getClassLoader(ClassHelperTest.class), sameInstance(ClassHelperTest.class.getClassLoader())); + } finally { + Thread.currentThread().setContextClassLoader(oldClassLoader); + } + } + + @Test + public void testGetClassLoader2() throws Exception { + assertThat(getClassLoader(), sameInstance(ClassHelper.class.getClassLoader())); + } + + @Test + public void testForName1() throws Exception { + assertThat(forName(ClassHelperTest.class.getName()) == ClassHelperTest.class, is(true)); + } + + @Test + public void testForName2() throws Exception { + assertThat(forName("byte") == byte.class, is(true)); + assertThat(forName("java.lang.String[]") == String[].class, is(true)); + assertThat(forName("[Ljava.lang.String;") == String[].class, is(true)); + } + + @Test + public void testForName3() throws Exception { + ClassLoader classLoader = Mockito.mock(ClassLoader.class); + forName("a.b.c.D", classLoader); + verify(classLoader).loadClass("a.b.c.D"); + } + + @Test + public void testResolvePrimitiveClassName() throws Exception { + assertThat(resolvePrimitiveClassName("boolean") == boolean.class, is(true)); + assertThat(resolvePrimitiveClassName("byte") == byte.class, is(true)); + assertThat(resolvePrimitiveClassName("char") == char.class, is(true)); + assertThat(resolvePrimitiveClassName("double") == double.class, is(true)); + assertThat(resolvePrimitiveClassName("float") == float.class, is(true)); + assertThat(resolvePrimitiveClassName("int") == int.class, is(true)); + assertThat(resolvePrimitiveClassName("long") == long.class, is(true)); + assertThat(resolvePrimitiveClassName("short") == short.class, is(true)); + assertThat(resolvePrimitiveClassName("[Z") == boolean[].class, is(true)); + assertThat(resolvePrimitiveClassName("[B") == byte[].class, is(true)); + assertThat(resolvePrimitiveClassName("[C") == char[].class, is(true)); + assertThat(resolvePrimitiveClassName("[D") == double[].class, is(true)); + assertThat(resolvePrimitiveClassName("[F") == float[].class, is(true)); + assertThat(resolvePrimitiveClassName("[I") == int[].class, is(true)); + assertThat(resolvePrimitiveClassName("[J") == long[].class, is(true)); + assertThat(resolvePrimitiveClassName("[S") == short[].class, is(true)); + } + + @Test + public void testToShortString() throws Exception { + assertThat(toShortString(null), equalTo("null")); + assertThat(toShortString(new ClassHelperTest()), startsWith("ClassHelperTest@")); + } +} diff --git a/dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/CollectionUtilsTest.java b/dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/CollectionUtilsTest.java index d23f6d62dfe..996817e5cff 100644 --- a/dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/CollectionUtilsTest.java +++ b/dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/CollectionUtilsTest.java @@ -22,17 +22,27 @@ import java.util.Arrays; import java.util.Collections; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; +import static com.alibaba.dubbo.common.utils.CollectionUtils.isEmpty; +import static com.alibaba.dubbo.common.utils.CollectionUtils.isNotEmpty; +import static com.alibaba.dubbo.common.utils.CollectionUtils.toMap; +import static com.alibaba.dubbo.common.utils.CollectionUtils.toStringMap; +import static java.util.Collections.emptyList; +import static java.util.Collections.singleton; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; public class CollectionUtilsTest { @Test - public void test_sort() throws Exception { + public void testSort() throws Exception { List list = new ArrayList(); list.add(100); list.add(10); @@ -47,14 +57,14 @@ public void test_sort() throws Exception { } @Test - public void test_sort_null() throws Exception { + public void testSortNull() throws Exception { assertNull(CollectionUtils.sort(null)); assertTrue(CollectionUtils.sort(new ArrayList()).isEmpty()); } @Test - public void test_sortSimpleName() throws Exception { + public void testSortSimpleName() throws Exception { List list = new ArrayList(); list.add("aaa.z"); list.add("b"); @@ -69,14 +79,14 @@ public void test_sortSimpleName() throws Exception { } @Test - public void test_sortSimpleName_null() throws Exception { + public void testSortSimpleNameNull() throws Exception { assertNull(CollectionUtils.sortSimpleName(null)); assertTrue(CollectionUtils.sortSimpleName(new ArrayList()).isEmpty()); } @Test - public void test_splitAll() throws Exception { + public void testSplitAll() throws Exception { assertNull(CollectionUtils.splitAll(null, null)); assertNull(CollectionUtils.splitAll(null, "-")); @@ -98,7 +108,7 @@ public void test_splitAll() throws Exception { } @Test - public void test_joinAll() throws Exception { + public void testJoinAll() throws Exception { assertNull(CollectionUtils.joinAll(null, null)); assertNull(CollectionUtils.joinAll(null, "-")); @@ -125,7 +135,7 @@ public void test_joinAll() throws Exception { } @Test - public void test_joinList() throws Exception { + public void testJoinList() throws Exception { List list = Arrays.asList(); assertEquals("", CollectionUtils.join(list, "/")); @@ -137,7 +147,7 @@ public void test_joinList() throws Exception { } @Test - public void test_mapEquals() throws Exception { + public void testMapEquals() throws Exception { assertTrue(CollectionUtils.mapEquals(null, null)); assertFalse(CollectionUtils.mapEquals(null, new HashMap())); assertFalse(CollectionUtils.mapEquals(new HashMap(), null)); @@ -147,9 +157,18 @@ public void test_mapEquals() throws Exception { } @Test - public void test_toMap() throws Exception { - assertTrue(CollectionUtils.toMap().isEmpty()); + public void testStringMap1() throws Exception { + assertThat(toStringMap("key", "value"), equalTo(Collections.singletonMap("key", "value"))); + } + @Test(expected = IllegalArgumentException.class) + public void testStringMap2() throws Exception { + toStringMap("key", "value", "odd"); + } + + @Test + public void testToMap1() throws Exception { + assertTrue(CollectionUtils.toMap().isEmpty()); Map expected = new HashMap(); expected.put("a", 1); @@ -158,4 +177,21 @@ public void test_toMap() throws Exception { assertEquals(expected, CollectionUtils.toMap("a", 1, "b", 2, "c", 3)); } + + @Test(expected = IllegalArgumentException.class) + public void testToMap2() throws Exception { + toMap("a", "b", "c"); + } + + @Test + public void testIsEmpty() throws Exception { + assertThat(isEmpty(null), is(true)); + assertThat(isEmpty(new HashSet()), is(true)); + assertThat(isEmpty(emptyList()), is(true)); + } + + @Test + public void testIsNotEmpty() throws Exception { + assertThat(isNotEmpty(singleton("a")), is(true)); + } } \ No newline at end of file diff --git a/dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/ConfigUtilsTest.java b/dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/ConfigUtilsTest.java index 8b11281ac5c..b5e20165f14 100644 --- a/dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/ConfigUtilsTest.java +++ b/dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/ConfigUtilsTest.java @@ -16,76 +16,182 @@ */ package com.alibaba.dubbo.common.utils; +import com.alibaba.dubbo.common.Constants; import com.alibaba.dubbo.common.threadpool.ThreadPool; - +import org.junit.After; import org.junit.Assert; +import org.junit.Before; import org.junit.Ignore; import org.junit.Test; -import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.Properties; +import static java.util.Arrays.asList; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.greaterThan; +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThat; -import static org.junit.matchers.JUnitMatchers.containsString; public class ConfigUtilsTest { + @Before + public void setUp() throws Exception { + ConfigUtils.setProperties(null); + } - public static List toArray(T... args) { - List ret = new ArrayList(); - for (T a : args) { - ret.add(a); - } - return ret; + @After + public void tearDown() throws Exception { + ConfigUtils.setProperties(null); + } + + @Test + public void testIsNotEmpty() throws Exception { + assertThat(ConfigUtils.isNotEmpty("abc"), is(true)); + } + + @Test + public void testIsEmpty() throws Exception { + assertThat(ConfigUtils.isEmpty(null), is(true)); + assertThat(ConfigUtils.isEmpty(""), is(true)); + assertThat(ConfigUtils.isEmpty("false"), is(true)); + assertThat(ConfigUtils.isEmpty("FALSE"), is(true)); + assertThat(ConfigUtils.isEmpty("0"), is(true)); + assertThat(ConfigUtils.isEmpty("null"), is(true)); + assertThat(ConfigUtils.isEmpty("NULL"), is(true)); + assertThat(ConfigUtils.isEmpty("n/a"), is(true)); + assertThat(ConfigUtils.isEmpty("N/A"), is(true)); + } + + @Test + public void testIsDefault() throws Exception { + assertThat(ConfigUtils.isDefault("true"), is(true)); + assertThat(ConfigUtils.isDefault("TRUE"), is(true)); + assertThat(ConfigUtils.isDefault("default"), is(true)); + assertThat(ConfigUtils.isDefault("DEFAULT"), is(true)); } @Test public void testMergeValues() { - List merged = ConfigUtils.mergeValues(ThreadPool.class, "aaa,bbb,default.cunstom", - toArray("fixed", "default.limited", "cached")); - Assert.assertEquals(toArray("fixed", "cached", "aaa", "bbb", "default.cunstom"), merged); + List merged = ConfigUtils.mergeValues(ThreadPool.class, "aaa,bbb,default.custom", + asList("fixed", "default.limited", "cached")); + assertEquals(asList("fixed", "cached", "aaa", "bbb", "default.custom"), merged); } @Test - public void testMergeValues_addDefault() { + public void testMergeValuesAddDefault() { List merged = ConfigUtils.mergeValues(ThreadPool.class, "aaa,bbb,default,zzz", - toArray("fixed", "default.limited", "cached")); - Assert.assertEquals(toArray("aaa", "bbb", "fixed", "cached", "zzz"), merged); + asList("fixed", "default.limited", "cached")); + assertEquals(asList("aaa", "bbb", "fixed", "cached", "zzz"), merged); } @Test public void testMergeValuesDeleteDefault() { - List merged = ConfigUtils.mergeValues(ThreadPool.class, "-default", toArray("fixed", "default.limited", "cached")); - Assert.assertEquals(toArray(), merged); + List merged = ConfigUtils.mergeValues(ThreadPool.class, "-default", asList("fixed", "default.limited", "cached")); + assertEquals(asList(), merged); } @Test public void testMergeValuesDeleteDefault_2() { - List merged = ConfigUtils.mergeValues(ThreadPool.class, "-default,aaa", toArray("fixed", "default.limited", "cached")); - Assert.assertEquals(toArray("aaa"), merged); + List merged = ConfigUtils.mergeValues(ThreadPool.class, "-default,aaa", asList("fixed", "default.limited", "cached")); + assertEquals(asList("aaa"), merged); } /** - * Test Point 1: The user configures -default, which will delete all the default parameters + * The user configures -default, which will delete all the default parameters */ @Test public void testMergeValuesDelete() { - List merged = ConfigUtils.mergeValues(ThreadPool.class, "-fixed,aaa", toArray("fixed", "default.limited", "cached")); - Assert.assertEquals(toArray("cached", "aaa"), merged); + List merged = ConfigUtils.mergeValues(ThreadPool.class, "-fixed,aaa", asList("fixed", "default.limited", "cached")); + assertEquals(asList("cached", "aaa"), merged); + } + + @Test + public void testReplaceProperty() throws Exception { + String s = ConfigUtils.replaceProperty("1${a.b.c}2${a.b.c}3", Collections.singletonMap("a.b.c", "ABC")); + assertEquals(s, "1ABC2ABC3"); + s = ConfigUtils.replaceProperty("1${a.b.c}2${a.b.c}3", Collections.emptyMap()); + assertEquals(s, "123"); } @Test - public void test_loadProperties_noFile() throws Exception { + public void testGetProperties1() throws Exception { + try { + System.setProperty(Constants.DUBBO_PROPERTIES_KEY, "properties.load"); + Properties p = ConfigUtils.getProperties(); + assertThat((String) p.get("a"), equalTo("12")); + assertThat((String) p.get("b"), equalTo("34")); + assertThat((String) p.get("c"), equalTo("56")); + } finally { + System.clearProperty(Constants.DUBBO_PROPERTIES_KEY); + } + } + + @Test + public void testGetProperties2() throws Exception { + System.clearProperty(Constants.DUBBO_PROPERTIES_KEY); + Properties p = ConfigUtils.getProperties(); + assertThat((String) p.get("dubbo"), equalTo("properties")); + } + + @Test + public void testAddProperties() throws Exception { + Properties p = new Properties(); + p.put("key1", "value1"); + ConfigUtils.addProperties(p); + assertThat((String) ConfigUtils.getProperties().get("key1"), equalTo("value1")); + } + + @Test + public void testLoadPropertiesNoFile() throws Exception { Properties p = ConfigUtils.loadProperties("notExisted", true); Properties expected = new Properties(); - Assert.assertEquals(expected, p); + assertEquals(expected, p); p = ConfigUtils.loadProperties("notExisted", false); - Assert.assertEquals(expected, p); + assertEquals(expected, p); + } + + @Test + public void testGetProperty() throws Exception { + assertThat(ConfigUtils.getProperty("dubbo"), equalTo("properties")); + } + + @Test + public void testGetPropertyDefaultValue() throws Exception { + assertThat(ConfigUtils.getProperty("not-exist", "default"), equalTo("default")); + } + + @Test + public void testGetPropertyFromSystem() throws Exception { + try { + System.setProperty("dubbo", "system"); + assertThat(ConfigUtils.getProperty("dubbo"), equalTo("system")); + } finally { + System.clearProperty("dubbo"); + } + } + + @Test + public void testGetSystemProperty() throws Exception { + try { + System.setProperty("dubbo", "system-only"); + assertThat(ConfigUtils.getSystemProperty("dubbo"), equalTo("system-only")); + } finally { + System.clearProperty("dubbo"); + } + } + + @Test + public void testLoadProperties() throws Exception { + Properties p = ConfigUtils.loadProperties("dubbo.properties"); + assertThat((String)p.get("dubbo"), equalTo("properties")); } @Test - public void test_loadProperties_oneFile() throws Exception { + public void testLoadPropertiesOneFile() throws Exception { Properties p = ConfigUtils.loadProperties("properties.load", false); Properties expected = new Properties(); @@ -93,11 +199,11 @@ public void test_loadProperties_oneFile() throws Exception { expected.put("b", "34"); expected.put("c", "56"); - Assert.assertEquals(expected, p); + assertEquals(expected, p); } @Test - public void test_loadProperties_oneFile_allowMulti() throws Exception { + public void testLoadPropertiesOneFileAllowMulti() throws Exception { Properties p = ConfigUtils.loadProperties("properties.load", true); Properties expected = new Properties(); @@ -105,11 +211,11 @@ public void test_loadProperties_oneFile_allowMulti() throws Exception { expected.put("b", "34"); expected.put("c", "56"); - Assert.assertEquals(expected, p); + assertEquals(expected, p); } @Test - public void test_loadProperties_oneFile_notRootPath() throws Exception { + public void testLoadPropertiesOneFileNotRootPath() throws Exception { Properties p = ConfigUtils.loadProperties("META-INF/dubbo/internal/com.alibaba.dubbo.common.threadpool.ThreadPool", false); Properties expected = new Properties(); @@ -118,13 +224,13 @@ public void test_loadProperties_oneFile_notRootPath() throws Exception { expected.put("limited", "com.alibaba.dubbo.common.threadpool.support.limited.LimitedThreadPool"); expected.put("eager", "com.alibaba.dubbo.common.threadpool.support.eager.EagerThreadPool"); - Assert.assertEquals(expected, p); + assertEquals(expected, p); } @Ignore("see http://code.alibabatech.com/jira/browse/DUBBO-133") @Test - public void test_loadProperties_multiFile_notRootPath_Exception() throws Exception { + public void testLoadPropertiesMultiFileNotRootPathException() throws Exception { try { ConfigUtils.loadProperties("META-INF/services/com.alibaba.dubbo.common.status.StatusChecker", false); Assert.fail(); @@ -134,7 +240,7 @@ public void test_loadProperties_multiFile_notRootPath_Exception() throws Excepti } @Test - public void test_loadProperties_multiFile_notRootPath() throws Exception { + public void testLoadPropertiesMultiFileNotRootPath() throws Exception { Properties p = ConfigUtils.loadProperties("META-INF/dubbo/internal/com.alibaba.dubbo.common.status.StatusChecker", true); @@ -143,7 +249,38 @@ public void test_loadProperties_multiFile_notRootPath() throws Exception { expected.put("load", "com.alibaba.dubbo.common.status.support.LoadStatusChecker"); expected.put("aa", "12"); - Assert.assertEquals(expected, p); + assertEquals(expected, p); } + @Test + public void testGetPid() throws Exception { + assertThat(ConfigUtils.getPid(), greaterThan(0)); + } + + @Test + public void testGetServerShutdownTimeoutFromShutdownWait() throws Exception { + System.setProperty(Constants.SHUTDOWN_WAIT_KEY, "1234"); + try { + assertThat(ConfigUtils.getServerShutdownTimeout(), equalTo(1234)); + } finally { + System.clearProperty(Constants.SHUTDOWN_WAIT_KEY); + } + } + + @Test + public void testGetServerShutdownTimeoutFromShutdownWaitSeconds() throws Exception { + System.setProperty(Constants.SHUTDOWN_WAIT_SECONDS_KEY, "1234"); + try { + assertThat(ConfigUtils.getServerShutdownTimeout(), equalTo(1234 * 1000)); + } finally { + System.clearProperty(Constants.SHUTDOWN_WAIT_SECONDS_KEY); + } + } + + @Test + public void testGetServerShutdownTimeoutFromDefault() throws Exception { + System.clearProperty(Constants.SHUTDOWN_WAIT_KEY); + System.clearProperty(Constants.SHUTDOWN_WAIT_SECONDS_KEY); + assertThat(ConfigUtils.getServerShutdownTimeout(), equalTo(Constants.DEFAULT_SERVER_SHUTDOWN_TIMEOUT)); + } } \ No newline at end of file diff --git a/dubbo-common/src/test/resources/dubbo.properties b/dubbo-common/src/test/resources/dubbo.properties new file mode 100644 index 00000000000..15936f93a26 --- /dev/null +++ b/dubbo-common/src/test/resources/dubbo.properties @@ -0,0 +1,18 @@ +# +# 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. +# + +dubbo=properties \ No newline at end of file