|
@@ -0,0 +1,125 @@
|
|
|
+/**
|
|
|
+ * 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;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.List;
|
|
|
+import java.util.concurrent.atomic.AtomicInteger;
|
|
|
+
|
|
|
+import org.apache.zookeeper.AsyncCallback.VoidCallback;
|
|
|
+import org.apache.zookeeper.ZooDefs.Ids;
|
|
|
+import org.apache.zookeeper.test.ClientBase;
|
|
|
+import org.junit.Assert;
|
|
|
+import org.junit.Test;
|
|
|
+
|
|
|
+/**
|
|
|
+ *
|
|
|
+ * Testing Zookeeper public methods
|
|
|
+ *
|
|
|
+ */
|
|
|
+public class ZooKeeperTest extends ClientBase {
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testDeleteRecursive() throws IOException, InterruptedException,
|
|
|
+ KeeperException {
|
|
|
+ final ZooKeeper zk = createClient();
|
|
|
+ // making sure setdata works on /
|
|
|
+ zk.setData("/", "some".getBytes(), -1);
|
|
|
+ zk.create("/a", "some".getBytes(), Ids.OPEN_ACL_UNSAFE,
|
|
|
+ CreateMode.PERSISTENT);
|
|
|
+
|
|
|
+ zk.create("/a/b", "some".getBytes(), Ids.OPEN_ACL_UNSAFE,
|
|
|
+ CreateMode.PERSISTENT);
|
|
|
+
|
|
|
+ zk.create("/a/b/v", "some".getBytes(), Ids.OPEN_ACL_UNSAFE,
|
|
|
+ CreateMode.PERSISTENT);
|
|
|
+
|
|
|
+ zk.create("/a/b/v/1", "some".getBytes(), Ids.OPEN_ACL_UNSAFE,
|
|
|
+ CreateMode.PERSISTENT);
|
|
|
+
|
|
|
+ zk.create("/a/c", "some".getBytes(), Ids.OPEN_ACL_UNSAFE,
|
|
|
+ CreateMode.PERSISTENT);
|
|
|
+
|
|
|
+ zk.create("/a/c/v", "some".getBytes(), Ids.OPEN_ACL_UNSAFE,
|
|
|
+ CreateMode.PERSISTENT);
|
|
|
+
|
|
|
+ List<String> children = zk.getChildren("/a", false);
|
|
|
+
|
|
|
+ Assert.assertEquals("2 children - b & c should be present ", children
|
|
|
+ .size(), 2);
|
|
|
+ Assert.assertTrue(children.contains("b"));
|
|
|
+ Assert.assertTrue(children.contains("c"));
|
|
|
+
|
|
|
+ zk.deleteRecursive("/a");
|
|
|
+ Assert.assertNull(zk.exists("/a", null));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testDeleteRecursiveAsync() throws IOException,
|
|
|
+ InterruptedException, KeeperException {
|
|
|
+ final ZooKeeper zk = createClient();
|
|
|
+ // making sure setdata works on /
|
|
|
+ zk.setData("/", "some".getBytes(), -1);
|
|
|
+ zk.create("/a", "some".getBytes(), Ids.OPEN_ACL_UNSAFE,
|
|
|
+ CreateMode.PERSISTENT);
|
|
|
+
|
|
|
+ zk.create("/a/b", "some".getBytes(), Ids.OPEN_ACL_UNSAFE,
|
|
|
+ CreateMode.PERSISTENT);
|
|
|
+
|
|
|
+ zk.create("/a/b/v", "some".getBytes(), Ids.OPEN_ACL_UNSAFE,
|
|
|
+ CreateMode.PERSISTENT);
|
|
|
+
|
|
|
+ zk.create("/a/b/v/1", "some".getBytes(), Ids.OPEN_ACL_UNSAFE,
|
|
|
+ CreateMode.PERSISTENT);
|
|
|
+
|
|
|
+ zk.create("/a/c", "some".getBytes(), Ids.OPEN_ACL_UNSAFE,
|
|
|
+ CreateMode.PERSISTENT);
|
|
|
+
|
|
|
+ zk.create("/a/c/v", "some".getBytes(), Ids.OPEN_ACL_UNSAFE,
|
|
|
+ CreateMode.PERSISTENT);
|
|
|
+
|
|
|
+ for (int i = 0; i < 50; ++i) {
|
|
|
+ zk.create("/a/c/" + i, "some".getBytes(), Ids.OPEN_ACL_UNSAFE,
|
|
|
+ CreateMode.PERSISTENT);
|
|
|
+ }
|
|
|
+ List<String> children = zk.getChildren("/a", false);
|
|
|
+
|
|
|
+ Assert.assertEquals("2 children - b & c should be present ", children
|
|
|
+ .size(), 2);
|
|
|
+ Assert.assertTrue(children.contains("b"));
|
|
|
+ Assert.assertTrue(children.contains("c"));
|
|
|
+
|
|
|
+ VoidCallback cb = new VoidCallback() {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void processResult(int rc, String path, Object ctx) {
|
|
|
+ synchronized (ctx) {
|
|
|
+ ((AtomicInteger) ctx).set(4);
|
|
|
+ ctx.notify();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ };
|
|
|
+ final AtomicInteger ctx = new AtomicInteger(3);
|
|
|
+ zk.deleteRecursive("/a", cb, ctx);
|
|
|
+ synchronized (ctx) {
|
|
|
+ ctx.wait();
|
|
|
+ }
|
|
|
+ Assert.assertEquals(4, ((AtomicInteger) ctx).get());
|
|
|
+ }
|
|
|
+}
|