Return-Path: X-Original-To: apmail-curator-commits-archive@minotaur.apache.org Delivered-To: apmail-curator-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 5200811185 for ; Wed, 30 Jul 2014 17:23:26 +0000 (UTC) Received: (qmail 86278 invoked by uid 500); 30 Jul 2014 17:23:26 -0000 Delivered-To: apmail-curator-commits-archive@curator.apache.org Received: (qmail 86231 invoked by uid 500); 30 Jul 2014 17:23:26 -0000 Mailing-List: contact commits-help@curator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@curator.apache.org Delivered-To: mailing list commits@curator.apache.org Received: (qmail 86155 invoked by uid 99); 30 Jul 2014 17:23:26 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 30 Jul 2014 17:23:26 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id BACEC99F780; Wed, 30 Jul 2014 17:23:25 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: randgalt@apache.org To: commits@curator.apache.org Date: Wed, 30 Jul 2014 17:23:27 -0000 Message-Id: <2a4f061c64f74fcd9f84755209497e39@git.apache.org> In-Reply-To: <0b0f8ac65fc04c13bd0189ac1d9e88f1@git.apache.org> References: <0b0f8ac65fc04c13bd0189ac1d9e88f1@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [3/3] git commit: some reformatting and added tests with nulls some reformatting and added tests with nulls Project: http://git-wip-us.apache.org/repos/asf/curator/repo Commit: http://git-wip-us.apache.org/repos/asf/curator/commit/bd319a92 Tree: http://git-wip-us.apache.org/repos/asf/curator/tree/bd319a92 Diff: http://git-wip-us.apache.org/repos/asf/curator/diff/bd319a92 Branch: refs/heads/master Commit: bd319a92cfc4c714f5dd48730f0fdf685e03445e Parents: 8699efc Author: randgalt Authored: Wed Jul 30 11:49:42 2014 -0500 Committer: randgalt Committed: Wed Jul 30 11:49:42 2014 -0500 ---------------------------------------------------------------------- .../java/org/apache/curator/utils/ZKPaths.java | 87 ++++++++++---------- .../org/apache/curator/utils/TestZKPaths.java | 15 ++++ 2 files changed, 59 insertions(+), 43 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/curator/blob/bd319a92/curator-client/src/main/java/org/apache/curator/utils/ZKPaths.java ---------------------------------------------------------------------- diff --git a/curator-client/src/main/java/org/apache/curator/utils/ZKPaths.java b/curator-client/src/main/java/org/apache/curator/utils/ZKPaths.java index dfc69a7..820f45f 100644 --- a/curator-client/src/main/java/org/apache/curator/utils/ZKPaths.java +++ b/curator-client/src/main/java/org/apache/curator/utils/ZKPaths.java @@ -16,6 +16,7 @@ * specific language governing permissions and limitations * under the License. */ + package org.apache.curator.utils; import com.google.common.collect.Lists; @@ -33,10 +34,10 @@ public class ZKPaths * Apply the namespace to the given path * * @param namespace namespace (can be null) - * @param path path + * @param path path * @return adjusted path */ - public static String fixForNamespace(String namespace, String path) + public static String fixForNamespace(String namespace, String path) { // Child path must be valid in and of itself. PathUtils.validatePath(path); @@ -50,7 +51,7 @@ public class ZKPaths /** * Given a full path, return the node name. i.e. "/one/two/three" will return "three" - * + * * @param path the path * @return the node */ @@ -62,7 +63,7 @@ public class ZKPaths { return path; } - if ( (i + 1) >= path.length() ) + if ( (i + 1) >= path.length() ) { return ""; } @@ -71,8 +72,8 @@ public class ZKPaths public static class PathAndNode { - private final String path; - private final String node; + private final String path; + private final String node; public PathAndNode(String path, String node) { @@ -105,7 +106,7 @@ public class ZKPaths { return new PathAndNode(path, ""); } - if ( (i + 1) >= path.length() ) + if ( (i + 1) >= path.length() ) { return new PathAndNode("/", ""); } @@ -120,12 +121,10 @@ public class ZKPaths * * @param zookeeper the client * @param path path to ensure - * @throws InterruptedException thread interruption - * @throws org.apache.zookeeper.KeeperException - * Zookeeper errors + * @throws InterruptedException thread interruption + * @throws org.apache.zookeeper.KeeperException Zookeeper errors */ - public static void mkdirs(ZooKeeper zookeeper, String path) - throws InterruptedException, KeeperException + public static void mkdirs(ZooKeeper zookeeper, String path) throws InterruptedException, KeeperException { mkdirs(zookeeper, path, true, null); } @@ -134,15 +133,13 @@ public class ZKPaths * Make sure all the nodes in the path are created. NOTE: Unlike File.mkdirs(), Zookeeper doesn't distinguish * between directories and files. So, every node in the path is created. The data for each node is an empty blob * - * @param zookeeper the client - * @param path path to ensure + * @param zookeeper the client + * @param path path to ensure * @param makeLastNode if true, all nodes are created. If false, only the parent nodes are created - * @throws InterruptedException thread interruption - * @throws org.apache.zookeeper.KeeperException - * Zookeeper errors + * @throws InterruptedException thread interruption + * @throws org.apache.zookeeper.KeeperException Zookeeper errors */ - public static void mkdirs(ZooKeeper zookeeper, String path, boolean makeLastNode) - throws InterruptedException, KeeperException + public static void mkdirs(ZooKeeper zookeeper, String path, boolean makeLastNode) throws InterruptedException, KeeperException { mkdirs(zookeeper, path, makeLastNode, null); } @@ -151,16 +148,14 @@ public class ZKPaths * Make sure all the nodes in the path are created. NOTE: Unlike File.mkdirs(), Zookeeper doesn't distinguish * between directories and files. So, every node in the path is created. The data for each node is an empty blob * - * @param zookeeper the client - * @param path path to ensure + * @param zookeeper the client + * @param path path to ensure * @param makeLastNode if true, all nodes are created. If false, only the parent nodes are created - * @throws InterruptedException thread interruption - * @throws org.apache.zookeeper.KeeperException - * Zookeeper errors - * @param aclProvider if not null, the ACL provider to use when creating parent nodes + * @param aclProvider if not null, the ACL provider to use when creating parent nodes + * @throws InterruptedException thread interruption + * @throws org.apache.zookeeper.KeeperException Zookeeper errors */ - public static void mkdirs(ZooKeeper zookeeper, String path, boolean makeLastNode, InternalACLProvider aclProvider) - throws InterruptedException, KeeperException + public static void mkdirs(ZooKeeper zookeeper, String path, boolean makeLastNode, InternalACLProvider aclProvider) throws InterruptedException, KeeperException { PathUtils.validatePath(path); @@ -214,28 +209,36 @@ public class ZKPaths /** * Recursively deletes children of a node. * - * @param zookeeper the client - * @param path path of the node to delete - * @param deleteSelf flag that indicates that the node should also get deleted + * @param zookeeper the client + * @param path path of the node to delete + * @param deleteSelf flag that indicates that the node should also get deleted * @throws InterruptedException * @throws KeeperException */ - public static void deleteChildren(ZooKeeper zookeeper, String path, boolean deleteSelf) throws InterruptedException, KeeperException { + public static void deleteChildren(ZooKeeper zookeeper, String path, boolean deleteSelf) throws InterruptedException, KeeperException + { PathUtils.validatePath(path); List children = zookeeper.getChildren(path, null); - for (String child : children) { + for ( String child : children ) + { String fullPath = makePath(path, child); deleteChildren(zookeeper, fullPath, true); } - if (deleteSelf) { - try { + if ( deleteSelf ) + { + try + { zookeeper.delete(path, -1); - } catch (KeeperException.NotEmptyException e) { + } + catch ( KeeperException.NotEmptyException e ) + { //someone has created a new child since we checked ... delete again. - deleteChildren(zookeeper, path, deleteSelf); - } catch (KeeperException.NoNodeException e) { + deleteChildren(zookeeper, path, true); + } + catch ( KeeperException.NoNodeException e ) + { // ignore... someone else has deleted the node it since we checked } } @@ -247,12 +250,10 @@ public class ZKPaths * @param zookeeper the client * @param path the path * @return sorted list of children - * @throws InterruptedException thread interruption - * @throws org.apache.zookeeper.KeeperException - * zookeeper errors + * @throws InterruptedException thread interruption + * @throws org.apache.zookeeper.KeeperException zookeeper errors */ - public static List getSortedChildren(ZooKeeper zookeeper, String path) - throws InterruptedException, KeeperException + public static List getSortedChildren(ZooKeeper zookeeper, String path) throws InterruptedException, KeeperException { List children = zookeeper.getChildren(path, false); List sortedList = Lists.newArrayList(children); @@ -272,7 +273,7 @@ public class ZKPaths StringBuilder path = new StringBuilder(); // Add parent piece, with no trailing slash. - if ( parent != null && parent.length() > 0) + if ( (parent != null) && (parent.length() > 0) ) { if ( !parent.startsWith("/") ) { http://git-wip-us.apache.org/repos/asf/curator/blob/bd319a92/curator-client/src/test/java/org/apache/curator/utils/TestZKPaths.java ---------------------------------------------------------------------- diff --git a/curator-client/src/test/java/org/apache/curator/utils/TestZKPaths.java b/curator-client/src/test/java/org/apache/curator/utils/TestZKPaths.java index f757984..fb49d8b 100644 --- a/curator-client/src/test/java/org/apache/curator/utils/TestZKPaths.java +++ b/curator-client/src/test/java/org/apache/curator/utils/TestZKPaths.java @@ -27,6 +27,11 @@ public class TestZKPaths @Test public void testMakePath() { + Assert.assertEquals(ZKPaths.makePath(null, "/"), "/"); + Assert.assertEquals(ZKPaths.makePath("", null), "/"); + Assert.assertEquals(ZKPaths.makePath("/", null), "/"); + Assert.assertEquals(ZKPaths.makePath(null, null), "/"); + Assert.assertEquals(ZKPaths.makePath("/", "/"), "/"); Assert.assertEquals(ZKPaths.makePath("", "/"), "/"); Assert.assertEquals(ZKPaths.makePath("/", ""), "/"); @@ -37,11 +42,21 @@ public class TestZKPaths Assert.assertEquals(ZKPaths.makePath("/foo", ""), "/foo"); Assert.assertEquals(ZKPaths.makePath("/foo", "/"), "/foo"); + Assert.assertEquals(ZKPaths.makePath("foo", null), "/foo"); + Assert.assertEquals(ZKPaths.makePath("foo", null), "/foo"); + Assert.assertEquals(ZKPaths.makePath("/foo", null), "/foo"); + Assert.assertEquals(ZKPaths.makePath("/foo", null), "/foo"); + Assert.assertEquals(ZKPaths.makePath("", "bar"), "/bar"); Assert.assertEquals(ZKPaths.makePath("/", "bar"), "/bar"); Assert.assertEquals(ZKPaths.makePath("", "/bar"), "/bar"); Assert.assertEquals(ZKPaths.makePath("/", "/bar"), "/bar"); + Assert.assertEquals(ZKPaths.makePath(null, "bar"), "/bar"); + Assert.assertEquals(ZKPaths.makePath(null, "bar"), "/bar"); + Assert.assertEquals(ZKPaths.makePath(null, "/bar"), "/bar"); + Assert.assertEquals(ZKPaths.makePath(null, "/bar"), "/bar"); + Assert.assertEquals(ZKPaths.makePath("foo", "bar"), "/foo/bar"); Assert.assertEquals(ZKPaths.makePath("/foo", "bar"), "/foo/bar"); Assert.assertEquals(ZKPaths.makePath("foo", "/bar"), "/foo/bar");