diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/URL.java b/dubbo-common/src/main/java/org/apache/dubbo/common/URL.java index 6e71e749d8c..31ac91e5716 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/URL.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/URL.java @@ -436,9 +436,8 @@ public List getBackupUrls() { return urls; } - private String appendDefaultPort(String address, int defaultPort) { - if (address != null && address.length() > 0 - && defaultPort > 0) { + static String appendDefaultPort(String address, int defaultPort) { + if (address != null && address.length() > 0 && defaultPort > 0) { int i = address.indexOf(':'); if (i < 0) { return address + ":" + defaultPort; diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/URLTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/URLTest.java index b4f547ce12b..8061ab2e270 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/URLTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/URLTest.java @@ -18,6 +18,7 @@ import org.apache.dubbo.common.utils.CollectionUtils; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import java.io.File; @@ -124,7 +125,7 @@ public void test_valueOf_noHost() throws Exception { assertEquals("home/user1/router.js", url.getPath()); assertEquals(0, url.getParameters().size()); - // Caution!! + // Caution!! url = URL.valueOf("file://home/user1/router.js"); // ^^ only tow slash! assertEquals("file", url.getProtocol()); @@ -680,4 +681,10 @@ public void testIpV6AddressWithScopeId(){ assertEquals("1.0.0", url.getParameter("version")); assertEquals("morgan", url.getParameter("application")); } -} \ No newline at end of file + + @Test + public void testDefaultPort() { + Assertions.assertEquals("10.20.153.10:2181", URL.appendDefaultPort("10.20.153.10:0", 2181)); + Assertions.assertEquals("10.20.153.10:2181", URL.appendDefaultPort("10.20.153.10", 2181)); + } +} diff --git a/dubbo-registry/dubbo-registry-zookeeper/src/main/java/org/apache/dubbo/registry/zookeeper/ZookeeperRegistry.java b/dubbo-registry/dubbo-registry-zookeeper/src/main/java/org/apache/dubbo/registry/zookeeper/ZookeeperRegistry.java index 3ead544a64f..14c3304b622 100644 --- a/dubbo-registry/dubbo-registry-zookeeper/src/main/java/org/apache/dubbo/registry/zookeeper/ZookeeperRegistry.java +++ b/dubbo-registry/dubbo-registry-zookeeper/src/main/java/org/apache/dubbo/registry/zookeeper/ZookeeperRegistry.java @@ -79,18 +79,6 @@ public ZookeeperRegistry(URL url, ZookeeperTransporter zookeeperTransporter) { }); } - static String appendDefaultPort(String address) { - if (address != null && address.length() > 0) { - int i = address.indexOf(':'); - if (i < 0) { - return address + ":" + DEFAULT_ZOOKEEPER_PORT; - } else if (Integer.parseInt(address.substring(i + 1)) == 0) { - return address.substring(0, i + 1) + DEFAULT_ZOOKEEPER_PORT; - } - } - return address; - } - @Override public boolean isAvailable() { return zkClient.isConnected(); diff --git a/dubbo-registry/dubbo-registry-zookeeper/src/test/java/org/apache/dubbo/registry/zookeeper/ZookeeperRegistryTest.java b/dubbo-registry/dubbo-registry-zookeeper/src/test/java/org/apache/dubbo/registry/zookeeper/ZookeeperRegistryTest.java index 7420d836e26..c2dd23129d6 100644 --- a/dubbo-registry/dubbo-registry-zookeeper/src/test/java/org/apache/dubbo/registry/zookeeper/ZookeeperRegistryTest.java +++ b/dubbo-registry/dubbo-registry-zookeeper/src/test/java/org/apache/dubbo/registry/zookeeper/ZookeeperRegistryTest.java @@ -36,7 +36,9 @@ import java.util.Set; import java.util.concurrent.CountDownLatch; -import static org.hamcrest.CoreMatchers.*; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.nullValue; +import static org.hamcrest.CoreMatchers.not; import static org.hamcrest.MatcherAssert.assertThat; import static org.mockito.Mockito.mock; @@ -65,12 +67,6 @@ public void tearDown() throws Exception { zkServer.stop(); } - @Test - public void testDefaultPort() { - Assertions.assertEquals("10.20.153.10:2181", ZookeeperRegistry.appendDefaultPort("10.20.153.10:0")); - Assertions.assertEquals("10.20.153.10:2181", ZookeeperRegistry.appendDefaultPort("10.20.153.10")); - } - @Test public void testAnyHost() { Assertions.assertThrows(IllegalStateException.class, () -> { @@ -153,13 +149,8 @@ public void testStatusChecker() { public void testSubscribeAnyValue() throws InterruptedException { final CountDownLatch latch = new CountDownLatch(1); zookeeperRegistry.register(serviceUrl); - zookeeperRegistry.subscribe(anyUrl, new NotifyListener() { - @Override - public void notify(List urls) { - latch.countDown(); - } - }); + zookeeperRegistry.subscribe(anyUrl, urls -> latch.countDown()); zookeeperRegistry.register(serviceUrl); latch.await(); } -} \ No newline at end of file +}