From 11da57df94c95b52a97b7c039e1ba042ebac8ed7 Mon Sep 17 00:00:00 2001 From: Nikhil Bhide Date: Mon, 11 Sep 2017 18:44:29 +0530 Subject: [PATCH 1/6] ZOOKEEPER-2814: Ignore space after comma in connection string Added logic to trim host connection string for ZOOKEEPER-2814: Ignore space after comma in connection string Port ZOOKEEPER-2814 from master --- .../org/apache/zookeeper/client/ConnectStringParser.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/java/main/org/apache/zookeeper/client/ConnectStringParser.java b/src/java/main/org/apache/zookeeper/client/ConnectStringParser.java index ff0425a4eb0..d58b5c4a88a 100644 --- a/src/java/main/org/apache/zookeeper/client/ConnectStringParser.java +++ b/src/java/main/org/apache/zookeeper/client/ConnectStringParser.java @@ -22,6 +22,7 @@ import java.util.ArrayList; import org.apache.zookeeper.common.PathUtils; +import static org.apache.zookeeper.common.StringUtils.split; /** * A parser for ZooKeeper Client connect strings. @@ -62,7 +63,7 @@ public ConnectStringParser(String connectString) { this.chrootPath = null; } - String hostsList[] = connectString.split(","); + List hostsList = split(connectString,","); for (String host : hostsList) { int port = DEFAULT_PORT; int pidx = host.lastIndexOf(':'); @@ -84,4 +85,4 @@ public String getChrootPath() { public ArrayList getServerAddresses() { return serverAddresses; } -} \ No newline at end of file +} From 3f210394e989507282d1e42fb9f3e35512d314e8 Mon Sep 17 00:00:00 2001 From: Nikhil Bhide Date: Mon, 11 Sep 2017 18:49:44 +0530 Subject: [PATCH 2/6] Zookeeper 2814: ignore space after comma in connection string Port Zookeeper 2814 from master to branch - 3.4 Created StringUtils.java --- .../apache/zookeeper/common/StringUtils.java | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/java/main/org/apache/zookeeper/common/StringUtils.java diff --git a/src/java/main/org/apache/zookeeper/common/StringUtils.java b/src/java/main/org/apache/zookeeper/common/StringUtils.java new file mode 100644 index 00000000000..18098dd1edd --- /dev/null +++ b/src/java/main/org/apache/zookeeper/common/StringUtils.java @@ -0,0 +1,44 @@ +/* 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 org.apache.zookeeper.common; +import java.util.ArrayList; +import java.util.List; +import java.util.Collections; + +public class StringUtils { + + private StringUtils() {/** non instantiable and non inheritable **/} + + /** + * This method returns an immutable List, but different from String's split() + * it trims the results in the input String, and removes any empty string from + * the resulting List. + * + */ + public static List split(String value, String separator) { + String[] splits = value.split(separator); + List results = new ArrayList(); + for (int i = 0; i < splits.length; i++) { + splits[i] = splits[i].trim(); + if (splits[i].length() > 0) { + results.add(splits[i]); + } + } + return Collections.unmodifiableList(results); + } +} From 7da1c0786f7ca7aabe73f53f818db97d82e701e1 Mon Sep 17 00:00:00 2001 From: Nikhil Bhide Date: Mon, 11 Sep 2017 18:52:26 +0530 Subject: [PATCH 3/6] ZOOKEEPER-2814: Ignore space after comma in connection string Port ZOOKEEPER-2814 from master to branch-3.4 Added code to test split logic implemented in StringUtils.java --- .../apache/zookeeper/test/StringUtilTest.java | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/java/test/org/apache/zookeeper/test/StringUtilTest.java diff --git a/src/java/test/org/apache/zookeeper/test/StringUtilTest.java b/src/java/test/org/apache/zookeeper/test/StringUtilTest.java new file mode 100644 index 00000000000..29d197fd476 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/StringUtilTest.java @@ -0,0 +1,44 @@ +/** + * 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 org.apache.zookeeper.test; + + +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.common.StringUtils; +import org.junit.Test; + +import java.util.Arrays; + +import static org.junit.Assert.assertEquals; + +public class StringUtilTest extends ZKTestCase { + + @Test + public void testStrings() { + + String s1 = " a , b , "; + assertEquals("[a, b]", StringUtils.split(s1, ",").toString()); + + String s2 = ""; + assertEquals(0, StringUtils.split(s2, ",").size()); + + String s3 = "1, , 2"; + assertEquals("[1, 2]", StringUtils.split(s3, ",").toString()); + + } +} From 1629f64f58c7787bb993162e5c9b8565b4d45c64 Mon Sep 17 00:00:00 2001 From: Nikhil Bhide Date: Mon, 11 Sep 2017 18:56:08 +0530 Subject: [PATCH 4/6] ZOOKEEPER-2814: Ignore space after comma in connection string Port ZOOKEEPER-2814 from master to branch-3.4 Added test to verify the logic added to remove the spaces provided in connection string --- .../zookeeper/test/ConnectStringParserTest.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/java/test/org/apache/zookeeper/test/ConnectStringParserTest.java b/src/java/test/org/apache/zookeeper/test/ConnectStringParserTest.java index a169b7ba7d6..76c4e0ba6de 100644 --- a/src/java/test/org/apache/zookeeper/test/ConnectStringParserTest.java +++ b/src/java/test/org/apache/zookeeper/test/ConnectStringParserTest.java @@ -61,6 +61,18 @@ public void testParseServersWithPort(){ Assert.assertEquals(112, parser.getServerAddresses().get(0).getPort()); Assert.assertEquals(110, parser.getServerAddresses().get(1).getPort()); } + + @Test + public void testParseServersWithPort(){ + String servers = "10.10.10.1:112,10.10.10.2:110"; + ConnectStringParser parser = new ConnectStringParser(servers); + + Assert.assertEquals("10.10.10.1", parser.getServerAddresses().get(0).getHostString()); + Assert.assertEquals("10.10.10.2", parser.getServerAddresses().get(1).getHostString()); + + Assert.assertEquals(112, parser.getServerAddresses().get(0).getPort()); + Assert.assertEquals(110, parser.getServerAddresses().get(1).getPort()); + } private void assertChrootPath(String expected, ConnectStringParser parser){ Assert.assertEquals(expected, parser.getChrootPath()); From d5ed52d86e5717ccf984c18d9cbb44a2cf92c01a Mon Sep 17 00:00:00 2001 From: Nikhil Bhide Date: Mon, 11 Sep 2017 20:45:34 +0530 Subject: [PATCH 5/6] ZOOKEEPER-2814: Ignore space after comma in connection string Ported ZOOKEEPER-2814 from master to 3.4 --- .../test/org/apache/zookeeper/test/ConnectStringParserTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/java/test/org/apache/zookeeper/test/ConnectStringParserTest.java b/src/java/test/org/apache/zookeeper/test/ConnectStringParserTest.java index 76c4e0ba6de..da39caa9375 100644 --- a/src/java/test/org/apache/zookeeper/test/ConnectStringParserTest.java +++ b/src/java/test/org/apache/zookeeper/test/ConnectStringParserTest.java @@ -63,7 +63,7 @@ public void testParseServersWithPort(){ } @Test - public void testParseServersWithPort(){ + public void testParseServersWithSpaces(){ String servers = "10.10.10.1:112,10.10.10.2:110"; ConnectStringParser parser = new ConnectStringParser(servers); From f9c40c41f727a6c2d82b59a7eff2d0d9d0b7fba2 Mon Sep 17 00:00:00 2001 From: Nikhil Bhide Date: Mon, 11 Sep 2017 20:46:58 +0530 Subject: [PATCH 6/6] ZOOKEEPER-2814: Ignore space after comma in connection string --- .../main/org/apache/zookeeper/client/ConnectStringParser.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/java/main/org/apache/zookeeper/client/ConnectStringParser.java b/src/java/main/org/apache/zookeeper/client/ConnectStringParser.java index d58b5c4a88a..5a01c5299f5 100644 --- a/src/java/main/org/apache/zookeeper/client/ConnectStringParser.java +++ b/src/java/main/org/apache/zookeeper/client/ConnectStringParser.java @@ -20,6 +20,7 @@ import java.net.InetSocketAddress; import java.util.ArrayList; +import java.util.List; import org.apache.zookeeper.common.PathUtils; import static org.apache.zookeeper.common.StringUtils.split;