Repository: curator Updated Branches: refs/heads/CURATOR-128 [created] bd319a92c
CURATOR-128: access root in a namespace; ZKPaths.makePath lenient. Project: http://git-wip-us.apache.org/repos/asf/curator/repo Commit: http://git-wip-us.apache.org/repos/asf/curator/commit/96d2a55a Tree: http://git-wip-us.apache.org/repos/asf/curator/tree/96d2a55a Diff: http://git-wip-us.apache.org/repos/asf/curator/diff/96d2a55a Branch: refs/heads/CURATOR-128 Commit: 96d2a55a03f8d0357f8f8cfa80a39a095d70667c Parents: b174dfb Author: Scott Blum <sco...@squareup.com> Authored: Tue Jul 29 20:17:11 2014 -0400 Committer: Scott Blum <sco...@squareup.com> Committed: Tue Jul 29 21:08:02 2014 -0400 ---------------------------------------------------------------------- .../java/org/apache/curator/utils/ZKPaths.java | 35 ++++++++++---- .../org/apache/curator/utils/TestZKPaths.java | 50 ++++++++++++++++++++ .../framework/imps/TestNamespaceFacade.java | 44 ++++++++++++++++- 3 files changed, 120 insertions(+), 9 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/curator/blob/96d2a55a/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 04b2141..dfc69a7 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 @@ -38,6 +38,9 @@ public class ZKPaths */ public static String fixForNamespace(String namespace, String path) { + // Child path must be valid in and of itself. + PathUtils.validatePath(path); + if ( namespace != null ) { return makePath(namespace, path); @@ -268,21 +271,37 @@ public class ZKPaths { StringBuilder path = new StringBuilder(); - if ( !parent.startsWith("/") ) + // Add parent piece, with no trailing slash. + if ( parent != null && parent.length() > 0) { - path.append("/"); + if ( !parent.startsWith("/") ) + { + path.append('/'); + } + if ( parent.endsWith("/") ) + { + path.append(parent.substring(0, parent.length() - 1)); + } + else + { + path.append(parent); + } } - path.append(parent); - if ( (child == null) || (child.length() == 0) ) + + if ( (child == null) || (child.length() == 0) || (child.equals("/")) ) { + // Special case, empty parent and child + if ( path.length() == 0 ) + { + return "/"; + } return path.toString(); } - if ( !parent.endsWith("/") ) - { - path.append("/"); - } + // Now add the separator between parent and child. + path.append('/'); + // Finally, add the child. if ( child.startsWith("/") ) { path.append(child.substring(1)); http://git-wip-us.apache.org/repos/asf/curator/blob/96d2a55a/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 new file mode 100644 index 0000000..f757984 --- /dev/null +++ b/curator-client/src/test/java/org/apache/curator/utils/TestZKPaths.java @@ -0,0 +1,50 @@ +/** + * 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.curator.utils; + +import org.testng.Assert; +import org.testng.annotations.Test; + +public class TestZKPaths +{ + @Test + public void testMakePath() + { + Assert.assertEquals(ZKPaths.makePath("/", "/"), "/"); + Assert.assertEquals(ZKPaths.makePath("", "/"), "/"); + Assert.assertEquals(ZKPaths.makePath("/", ""), "/"); + Assert.assertEquals(ZKPaths.makePath("", ""), "/"); + + Assert.assertEquals(ZKPaths.makePath("foo", ""), "/foo"); + Assert.assertEquals(ZKPaths.makePath("foo", "/"), "/foo"); + Assert.assertEquals(ZKPaths.makePath("/foo", ""), "/foo"); + Assert.assertEquals(ZKPaths.makePath("/foo", "/"), "/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("foo", "bar"), "/foo/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"); + } +} http://git-wip-us.apache.org/repos/asf/curator/blob/96d2a55a/curator-framework/src/test/java/org/apache/curator/framework/imps/TestNamespaceFacade.java ---------------------------------------------------------------------- diff --git a/curator-framework/src/test/java/org/apache/curator/framework/imps/TestNamespaceFacade.java b/curator-framework/src/test/java/org/apache/curator/framework/imps/TestNamespaceFacade.java index 32b26dd..f67c603 100644 --- a/curator-framework/src/test/java/org/apache/curator/framework/imps/TestNamespaceFacade.java +++ b/curator-framework/src/test/java/org/apache/curator/framework/imps/TestNamespaceFacade.java @@ -97,7 +97,7 @@ public class TestNamespaceFacade extends BaseClassForTests { client.start(); - Assert.assertEquals(client.usingNamespace("foo"), client.usingNamespace("foo")); + Assert.assertSame(client.usingNamespace("foo"), client.usingNamespace("foo")); Assert.assertNotSame(client.usingNamespace("foo"), client.usingNamespace("bar")); } finally @@ -122,6 +122,7 @@ public class TestNamespaceFacade extends BaseClassForTests client.usingNamespace("name").create().forPath("/one"); Assert.assertNotNull(client.getZookeeperClient().getZooKeeper().exists("/name", false)); + Assert.assertNotNull(client.getZookeeperClient().getZooKeeper().exists("/name/one", false)); } finally { @@ -129,6 +130,47 @@ public class TestNamespaceFacade extends BaseClassForTests } } + /** + * CURATOR-128: access root node within a namespace. + */ + @Test + public void testRootAccess() throws Exception + { + CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1)); + try + { + client.start(); + + client.create().forPath("/one"); + Assert.assertNotNull(client.getZookeeperClient().getZooKeeper().exists("/one", false)); + + Assert.assertNotNull(client.checkExists().forPath("/")); + try + { + client.checkExists().forPath(""); + Assert.fail("IllegalArgumentException expected"); + } + catch ( IllegalArgumentException expected ) + { + } + + Assert.assertNotNull(client.usingNamespace("one").checkExists().forPath("/")); + try + { + client.usingNamespace("one").checkExists().forPath(""); + Assert.fail("IllegalArgumentException expected"); + } + catch ( IllegalArgumentException expected ) + { + } + } + finally + { + CloseableUtils.closeQuietly(client); + } + } + + @Test public void testIsStarted() throws Exception {