You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@curator.apache.org by ra...@apache.org on 2014/07/30 18:49:51 UTC
[1/3] git commit: CURATOR-128: access root in a namespace;
ZKPaths.makePath lenient.
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 <sc...@squareup.com>
Authored: Tue Jul 29 20:17:11 2014 -0400
Committer: Scott Blum <sc...@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
{
[3/3] git commit: some reformatting and added tests with nulls
Posted by ra...@apache.org.
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/CURATOR-128
Commit: bd319a92cfc4c714f5dd48730f0fdf685e03445e
Parents: 8699efc
Author: randgalt <ra...@apache.org>
Authored: Wed Jul 30 11:49:42 2014 -0500
Committer: randgalt <ra...@apache.org>
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<String> 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<String> getSortedChildren(ZooKeeper zookeeper, String path)
- throws InterruptedException, KeeperException
+ public static List<String> getSortedChildren(ZooKeeper zookeeper, String path) throws InterruptedException, KeeperException
{
List<String> children = zookeeper.getChildren(path, false);
List<String> 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");
[2/3] git commit: Merge remote-tracking branch 'temp/CURATOR-128'
into CURATOR-128
Posted by ra...@apache.org.
Merge remote-tracking branch 'temp/CURATOR-128' into CURATOR-128
Project: http://git-wip-us.apache.org/repos/asf/curator/repo
Commit: http://git-wip-us.apache.org/repos/asf/curator/commit/8699efc3
Tree: http://git-wip-us.apache.org/repos/asf/curator/tree/8699efc3
Diff: http://git-wip-us.apache.org/repos/asf/curator/diff/8699efc3
Branch: refs/heads/CURATOR-128
Commit: 8699efc39e7d3b7d0081eeb485e8f731e43d40b7
Parents: 3a563ac 96d2a55
Author: randgalt <ra...@apache.org>
Authored: Wed Jul 30 11:42:23 2014 -0500
Committer: randgalt <ra...@apache.org>
Committed: Wed Jul 30 11:42:23 2014 -0500
----------------------------------------------------------------------
.../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(-)
----------------------------------------------------------------------