This is an automated email from the ASF dual-hosted git repository. iluo pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/incubator-dubbo.git
The following commit(s) were added to refs/heads/master by this push: new 04c26e6 unit test for common-utils (#1891) 04c26e6 is described below commit 04c26e6cb0f07d776769f2e53edd7ef92e078f77 Author: Ian Luo <ian....@gmail.com> AuthorDate: Wed Jun 6 16:23:47 2018 +0800 unit test for common-utils (#1891) * #1682: Enhance the test coverage part-4 : dubbo-common/src/main/java/com/alibaba/dubbo/common/status(store|threadpoolutils) modules * fix test failure on travis --- .../com/alibaba/dubbo/common/utils/NetUtils.java | 2 +- .../com/alibaba/dubbo/common/utils/PojoUtils.java | 5 +- .../alibaba/dubbo/common/utils/LRUCacheTest.java | 61 +++++++++ .../dubbo/common/utils/NamedThreadFactoryTest.java | 48 +++++++ .../alibaba/dubbo/common/utils/NetUtilsTest.java | 143 ++++++++++++++++++++ .../alibaba/dubbo/common/utils/PojoUtilsTest.java | 145 +++++++++++++++++---- 6 files changed, 379 insertions(+), 25 deletions(-) diff --git a/dubbo-common/src/main/java/com/alibaba/dubbo/common/utils/NetUtils.java b/dubbo-common/src/main/java/com/alibaba/dubbo/common/utils/NetUtils.java index 59edd48..de62059 100644 --- a/dubbo-common/src/main/java/com/alibaba/dubbo/common/utils/NetUtils.java +++ b/dubbo-common/src/main/java/com/alibaba/dubbo/common/utils/NetUtils.java @@ -132,7 +132,7 @@ public class NetUtils { new InetSocketAddress(port) : new InetSocketAddress(host, port); } - private static boolean isValidAddress(InetAddress address) { + static boolean isValidAddress(InetAddress address) { if (address == null || address.isLoopbackAddress()) return false; String name = address.getHostAddress(); diff --git a/dubbo-common/src/main/java/com/alibaba/dubbo/common/utils/PojoUtils.java b/dubbo-common/src/main/java/com/alibaba/dubbo/common/utils/PojoUtils.java index 754fd2d..9520629 100644 --- a/dubbo-common/src/main/java/com/alibaba/dubbo/common/utils/PojoUtils.java +++ b/dubbo-common/src/main/java/com/alibaba/dubbo/common/utils/PojoUtils.java @@ -69,12 +69,15 @@ public class PojoUtils { } public static Object[] realize(Object[] objs, Class<?>[] types) { - if (objs.length != types.length) + if (objs.length != types.length) { throw new IllegalArgumentException("args.length != types.length"); + } + Object[] dests = new Object[objs.length]; for (int i = 0; i < objs.length; i++) { dests[i] = realize(objs[i], types[i]); } + return dests; } diff --git a/dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/LRUCacheTest.java b/dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/LRUCacheTest.java new file mode 100644 index 0000000..040344f --- /dev/null +++ b/dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/LRUCacheTest.java @@ -0,0 +1,61 @@ +/* + * 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 org.hamcrest.Matchers.equalTo; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; + +public class LRUCacheTest { + @Test + public void testCache() throws Exception { + LRUCache<String, Integer> cache = new LRUCache<String, Integer>(3); + cache.put("one", 1); + cache.put("two", 2); + cache.put("three", 3); + assertThat(cache.get("one"), equalTo(1)); + assertThat(cache.get("two"), equalTo(2)); + assertThat(cache.get("three"), equalTo(3)); + assertThat(cache.size(), equalTo(3)); + cache.put("four", 4); + assertThat(cache.size(), equalTo(3)); + assertFalse(cache.containsKey("one")); + assertTrue(cache.containsKey("two")); + assertTrue(cache.containsKey("three")); + assertTrue(cache.containsKey("four")); + cache.remove("four"); + assertThat(cache.size(), equalTo(2)); + cache.put("five", 5); + assertFalse(cache.containsKey("four")); + assertTrue(cache.containsKey("five")); + assertTrue(cache.containsKey("two")); + assertTrue(cache.containsKey("three")); + assertThat(cache.size(), equalTo(3)); + } + + @Test + public void testCapacity() throws Exception { + LRUCache<String, Integer> cache = new LRUCache<String, Integer>(); + assertThat(cache.getMaxCapacity(), equalTo(1000)); + cache.setMaxCapacity(10); + assertThat(cache.getMaxCapacity(), equalTo(10)); + } +} diff --git a/dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/NamedThreadFactoryTest.java b/dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/NamedThreadFactoryTest.java new file mode 100644 index 0000000..74d28e6 --- /dev/null +++ b/dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/NamedThreadFactoryTest.java @@ -0,0 +1,48 @@ +/* + * 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 org.hamcrest.Matchers.allOf; +import static org.hamcrest.Matchers.containsString; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; + +public class NamedThreadFactoryTest { + @Test + public void testNewThread() throws Exception { + NamedThreadFactory factory = new NamedThreadFactory(); + Thread t = factory.newThread(Mockito.mock(Runnable.class)); + assertThat(t.getName(), allOf(containsString("pool-"), containsString("-thread-"))); + assertFalse(t.isDaemon()); + // since security manager is not installed. + assertSame(t.getThreadGroup(), Thread.currentThread().getThreadGroup()); + } + + @Test + public void testPrefixAndDaemon() throws Exception { + NamedThreadFactory factory = new NamedThreadFactory("prefix", true); + Thread t = factory.newThread(Mockito.mock(Runnable.class)); + assertThat(t.getName(), allOf(containsString("prefix-"), containsString("-thread-"))); + assertTrue(t.isDaemon()); + } +} diff --git a/dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/NetUtilsTest.java b/dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/NetUtilsTest.java index c46b0d3..ea8739e 100644 --- a/dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/NetUtilsTest.java +++ b/dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/NetUtilsTest.java @@ -19,11 +19,37 @@ package com.alibaba.dubbo.common.utils; import org.junit.Test; +import java.net.InetAddress; +import java.net.InetSocketAddress; + import static junit.framework.TestCase.assertFalse; import static junit.framework.TestCase.assertTrue; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.greaterThan; +import static org.hamcrest.Matchers.greaterThanOrEqualTo; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; public class NetUtilsTest { @Test + public void testGetRandomPort() throws Exception { + assertThat(NetUtils.getRandomPort(), greaterThanOrEqualTo(30000)); + assertThat(NetUtils.getRandomPort(), greaterThanOrEqualTo(30000)); + assertThat(NetUtils.getRandomPort(), greaterThanOrEqualTo(30000)); + } + + @Test + public void testGetAvailablePort() throws Exception { + assertThat(NetUtils.getAvailablePort(), greaterThan(0)); + assertThat(NetUtils.getAvailablePort(12345), greaterThanOrEqualTo(12345)); + assertThat(NetUtils.getAvailablePort(-1), greaterThanOrEqualTo(30000)); + } + + @Test public void testValidAddress() throws Exception { assertTrue(NetUtils.isValidAddress("10.20.130.230:20880")); assertFalse(NetUtils.isValidAddress("10.20.130.230")); @@ -37,4 +63,121 @@ public class NetUtilsTest { assertFalse(NetUtils.isInvalidPort(1024)); } + @Test + public void testIsLocalHost() throws Exception { + assertTrue(NetUtils.isLocalHost("localhost")); + assertTrue(NetUtils.isLocalHost("127.1.2.3")); + assertFalse(NetUtils.isLocalHost("128.1.2.3")); + } + + @Test + public void testIsAnyHost() throws Exception { + assertTrue(NetUtils.isAnyHost("0.0.0.0")); + assertFalse(NetUtils.isAnyHost("1.1.1.1")); + } + + @Test + public void testIsInvalidLocalHost() throws Exception { + assertTrue(NetUtils.isInvalidLocalHost(null)); + assertTrue(NetUtils.isInvalidLocalHost("")); + assertTrue(NetUtils.isInvalidLocalHost("localhost")); + assertTrue(NetUtils.isInvalidLocalHost("0.0.0.0")); + assertTrue(NetUtils.isInvalidLocalHost("127.1.2.3")); + } + + @Test + public void testIsValidLocalHost() throws Exception { + assertTrue(NetUtils.isValidLocalHost("1.2.3.4")); + } + + @Test + public void testGetLocalSocketAddress() throws Exception { + InetSocketAddress address = NetUtils.getLocalSocketAddress("localhost", 12345); + assertTrue(address.getAddress().isAnyLocalAddress()); + assertEquals(address.getPort(), 12345); + address = NetUtils.getLocalSocketAddress("dubbo-addr", 12345); + assertEquals(address.getHostName(), "dubbo-addr"); + assertEquals(address.getPort(), 12345); + } + + @Test + public void testIsValidAddress() throws Exception { + assertFalse(NetUtils.isValidAddress((InetAddress) null)); + InetAddress address = mock(InetAddress.class); + when(address.isLoopbackAddress()).thenReturn(true); + assertFalse(NetUtils.isValidAddress(address)); + address = mock(InetAddress.class); + when(address.getHostAddress()).thenReturn("localhost"); + assertFalse(NetUtils.isValidAddress(address)); + address = mock(InetAddress.class); + when(address.getHostAddress()).thenReturn("0.0.0.0"); + assertFalse(NetUtils.isValidAddress(address)); + address = mock(InetAddress.class); + when(address.getHostAddress()).thenReturn("127.0.0.1"); + assertFalse(NetUtils.isValidAddress(address)); + address = mock(InetAddress.class); + when(address.getHostAddress()).thenReturn("1.2.3.4"); + assertTrue(NetUtils.isValidAddress(address)); + } + + @Test + public void testGetLocalHost() throws Exception { + assertNotNull(NetUtils.getLocalHost()); + } + + @Test + public void testGetLocalAddress() throws Exception { + InetAddress address = NetUtils.getLocalAddress(); + assertNotNull(address); + assertTrue(NetUtils.isValidLocalHost(address.getHostAddress())); + } + + @Test + public void testFilterLocalHost() throws Exception { + assertNull(NetUtils.filterLocalHost(null)); + assertEquals(NetUtils.filterLocalHost(""), ""); + String host = NetUtils.filterLocalHost("dubbo://127.0.0.1:8080/foo"); + assertThat(host, equalTo("dubbo://" + NetUtils.getLocalHost() + ":8080/foo")); + host = NetUtils.filterLocalHost("127.0.0.1:8080"); + assertThat(host, equalTo(NetUtils.getLocalHost() + ":8080")); + host = NetUtils.filterLocalHost("0.0.0.0"); + assertThat(host, equalTo(NetUtils.getLocalHost())); + host = NetUtils.filterLocalHost("88.88.88.88"); + assertThat(host, equalTo(host)); + } + + @Test + public void testGetHostName() throws Exception { + assertNotNull(NetUtils.getHostName("127.0.0.1")); + } + + @Test + public void testGetIpByHost() throws Exception { + assertThat(NetUtils.getIpByHost("localhost"), equalTo("127.0.0.1")); + assertThat(NetUtils.getIpByHost("dubbo"), equalTo("dubbo")); + } + + @Test + public void testToAddressString() throws Exception { + InetAddress address = mock(InetAddress.class); + when(address.getHostAddress()).thenReturn("dubbo"); + InetSocketAddress socketAddress = new InetSocketAddress(address, 1234); + assertThat(NetUtils.toAddressString(socketAddress), equalTo("dubbo:1234")); + } + + @Test + public void testToAddress() throws Exception { + InetSocketAddress address = NetUtils.toAddress("localhost:1234"); + assertThat(address.getHostName(), equalTo("localhost")); + assertThat(address.getPort(), equalTo(1234)); + address = NetUtils.toAddress("localhost"); + assertThat(address.getHostName(), equalTo("localhost")); + assertThat(address.getPort(), equalTo(0)); + } + + @Test + public void testToURL() throws Exception { + String url = NetUtils.toURL("dubbo", "host", 1234, "foo"); + assertThat(url, equalTo("dubbo://host:1234/foo")); + } } \ No newline at end of file diff --git a/dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/PojoUtilsTest.java b/dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/PojoUtilsTest.java index d8b458e..6984c4b 100644 --- a/dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/PojoUtilsTest.java +++ b/dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/PojoUtilsTest.java @@ -23,7 +23,6 @@ import com.alibaba.dubbo.common.model.person.FullAddress; import com.alibaba.dubbo.common.model.person.PersonInfo; import com.alibaba.dubbo.common.model.person.PersonStatus; import com.alibaba.dubbo.common.model.person.Phone; - import org.junit.Assert; import org.junit.Test; @@ -38,10 +37,13 @@ import java.util.List; import java.util.Map; import java.util.UUID; +import static org.hamcrest.Matchers.equalTo; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; public class PojoUtilsTest { @@ -199,6 +201,90 @@ public class PojoUtilsTest { assertArrayObject(array); } + @Test + public void testArrayToCollection() throws Exception { + Person[] array = new Person[2]; + Person person1 = new Person(); + person1.setName("person1"); + Person person2 = new Person(); + person2.setName("person2"); + array[0] = person1; + array[1] = person2; + Object o = PojoUtils.realize(PojoUtils.generalize(array), LinkedList.class); + assertTrue(o instanceof LinkedList); + assertEquals(((List) o).get(0), person1); + assertEquals(((List) o).get(1), person2); + } + + @Test + public void testCollectionToArray() throws Exception { + Person person1 = new Person(); + person1.setName("person1"); + Person person2 = new Person(); + person2.setName("person2"); + List<Person> list = new LinkedList<Person>(); + list.add(person1); + list.add(person2); + Object o = PojoUtils.realize(PojoUtils.generalize(list), Person[].class); + assertTrue(o instanceof Person[]); + assertEquals(((Person[]) o)[0], person1); + assertEquals(((Person[]) o)[1], person2); + } + + @Test + public void testMapToEnum() throws Exception { + Map map = new HashMap(); + map.put("name", "MONDAY"); + Object o = PojoUtils.realize(map, Day.class); + assertEquals(o, Day.MONDAY); + } + + @Test + public void testGeneralizeEnumArray() throws Exception { + Object days = new Enum[]{Day.FRIDAY, Day.SATURDAY}; + Object o = PojoUtils.generalize(days); + assertTrue(o instanceof String[]); + assertEquals(((String[]) o)[0], "FRIDAY"); + assertEquals(((String[]) o)[1], "SATURDAY"); + } + + @Test + public void testGeneralizePersons() throws Exception { + Object persons = new Person[]{new Person(), new Person()}; + Object o = PojoUtils.generalize(persons); + assertTrue(o instanceof Object[]); + assertEquals(((Object[]) o).length, 2); + } + + @Test + public void testMapToInterface() throws Exception { + Map map = new HashMap(); + map.put("content", "greeting"); + map.put("from", "dubbo"); + map.put("urgent", true); + Object o = PojoUtils.realize(map, Message.class); + Message message = (Message) o; + assertThat(message.getContent(), equalTo("greeting")); + assertThat(message.getFrom(), equalTo("dubbo")); + assertTrue(message.isUrgent()); + } + + @Test + public void testException() throws Exception { + Map map = new HashMap(); + map.put("message", "dubbo exception"); + Object o = PojoUtils.realize(map, RuntimeException.class); + assertEquals(((Throwable) o).getMessage(), "dubbo exception"); + } + + @Test + public void testIsPojo() throws Exception { + assertFalse(PojoUtils.isPojo(boolean.class)); + assertFalse(PojoUtils.isPojo(Map.class)); + assertFalse(PojoUtils.isPojo(List.class)); + assertTrue(PojoUtils.isPojo(Person.class)); + } + public List<Person> returnListPersonMethod() { return null; } @@ -342,6 +428,9 @@ public class PojoUtilsTest { assertEquals("haha", parent.getChild().getToy()); assertSame(parent, parent.getChild().getParent()); + + Object[] objects = PojoUtils.realize(new Object[]{generalize}, new Class[]{List.class}, new Type[]{getType("getListGenericType")}); + assertTrue(((List) objects[0]).get(0) instanceof Parent); } @Test @@ -377,8 +466,6 @@ public class PojoUtilsTest { return null; } - ; - // java.lang.IllegalArgumentException: argument type mismatch @Test public void test_realize_LongPararmter_IllegalArgumentException() throws Exception { @@ -390,8 +477,6 @@ public class PojoUtilsTest { method.invoke(new PojoUtilsTest(), value); } - ; - // java.lang.IllegalArgumentException: argument type mismatch @Test public void test_realize_IntPararmter_IllegalArgumentException() throws Exception { @@ -467,9 +552,9 @@ public class PojoUtilsTest { Object obj = PojoUtils.generalize(data); Assert.assertEquals(3, data.getChildren().size()); - Assert.assertTrue(data.getChildren().get("first").getClass() == Child.class); + assertTrue(data.getChildren().get("first").getClass() == Child.class); Assert.assertEquals(1, data.getList().size()); - Assert.assertTrue(data.getList().get(0).getClass() == Child.class); + assertTrue(data.getList().get(0).getClass() == Child.class); TestData realizadData = (TestData) PojoUtils.realize(obj, TestData.class); Assert.assertEquals(data.getChildren().size(), realizadData.getChildren().size()); @@ -488,13 +573,15 @@ public class PojoUtilsTest { @Test public void testRealize() throws Exception { - Map<String, String> inputMap = new LinkedHashMap<String, String>(); - inputMap.put("key", "value"); - Object obj = PojoUtils.generalize(inputMap); - Assert.assertTrue(obj instanceof LinkedHashMap); - Object outputObject = PojoUtils.realize(inputMap, LinkedHashMap.class); - System.out.println(outputObject.getClass().getName()); - Assert.assertTrue(outputObject instanceof LinkedHashMap); + Map<String, String> map = new LinkedHashMap<String, String>(); + map.put("key", "value"); + Object obj = PojoUtils.generalize(map); + assertTrue(obj instanceof LinkedHashMap); + Object outputObject = PojoUtils.realize(map, LinkedHashMap.class); + assertTrue(outputObject instanceof LinkedHashMap); + Object[] objects = PojoUtils.realize(new Object[]{map}, new Class[]{LinkedHashMap.class}); + assertTrue(objects[0] instanceof LinkedHashMap); + assertEquals(objects[0], outputObject); } @Test @@ -504,10 +591,10 @@ public class PojoUtilsTest { person.setAge(37); input.add(person); Object obj = PojoUtils.generalize(input); - Assert.assertTrue(obj instanceof List); - Assert.assertTrue(input.get(0) instanceof Person); + assertTrue(obj instanceof List); + assertTrue(input.get(0) instanceof Person); Object output = PojoUtils.realize(obj, LinkedList.class); - Assert.assertTrue(output instanceof LinkedList); + assertTrue(output instanceof LinkedList); } @Test @@ -522,11 +609,11 @@ public class PojoUtilsTest { Object generializeObject = PojoUtils.generalize(result); Object realizeObject = PojoUtils.realize(generializeObject, ListResult.class); - Assert.assertTrue(realizeObject instanceof ListResult); + assertTrue(realizeObject instanceof ListResult); ListResult listResult = (ListResult) realizeObject; List l = listResult.getResult(); - Assert.assertTrue(l.size() == 1); - Assert.assertTrue(l.get(0) instanceof Parent); + assertTrue(l.size() == 1); + assertTrue(l.get(0) instanceof Parent); Parent realizeParent = (Parent) l.get(0); Assert.assertEquals(parent.getName(), realizeParent.getName()); Assert.assertEquals(parent.getAge(), realizeParent.getAge()); @@ -546,19 +633,23 @@ public class PojoUtilsTest { Object generializeObject = PojoUtils.generalize(list); Object realizeObject = PojoUtils.realize(generializeObject, ListResult.class); - Assert.assertTrue(realizeObject instanceof ListResult); + assertTrue(realizeObject instanceof ListResult); ListResult realizeList = (ListResult) realizeObject; List realizeInnerList = realizeList.getResult(); Assert.assertEquals(1, realizeInnerList.size()); - Assert.assertTrue(realizeInnerList.get(0) instanceof InnerPojo); + assertTrue(realizeInnerList.get(0) instanceof InnerPojo); InnerPojo realizeParentList = (InnerPojo) realizeInnerList.get(0); Assert.assertEquals(1, realizeParentList.getList().size()); - Assert.assertTrue(realizeParentList.getList().get(0) instanceof Parent); + assertTrue(realizeParentList.getList().get(0) instanceof Parent); Parent realizeParent = (Parent) realizeParentList.getList().get(0); Assert.assertEquals(parent.getName(), realizeParent.getName()); Assert.assertEquals(parent.getAge(), realizeParent.getAge()); } + public enum Day { + SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY + } + public static class Parent { public String gender; public String email; @@ -696,4 +787,12 @@ public class PojoUtilsTest { this.result = result; } } + + interface Message { + String getContent(); + + String getFrom(); + + boolean isUrgent(); + } } \ No newline at end of file -- To stop receiving notification emails like this one, please contact i...@apache.org.