zookeeper-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ph...@apache.org
Subject svn commit: r1189823 - in /zookeeper/trunk: CHANGES.txt src/java/main/org/apache/zookeeper/ZooKeeperMain.java src/java/test/org/apache/zookeeper/ZooKeeperTest.java
Date Thu, 27 Oct 2011 16:11:15 GMT
Author: phunt
Date: Thu Oct 27 16:11:15 2011
New Revision: 1189823

URL: http://svn.apache.org/viewvc?rev=1189823&view=rev
Log:
ZOOKEEPER-1220. ./zkCli.sh 'create' command is throwing ArrayIndexOutOfBoundsException (kavita
sharma via phunt)

Modified:
    zookeeper/trunk/CHANGES.txt
    zookeeper/trunk/src/java/main/org/apache/zookeeper/ZooKeeperMain.java
    zookeeper/trunk/src/java/test/org/apache/zookeeper/ZooKeeperTest.java

Modified: zookeeper/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/zookeeper/trunk/CHANGES.txt?rev=1189823&r1=1189822&r2=1189823&view=diff
==============================================================================
--- zookeeper/trunk/CHANGES.txt (original)
+++ zookeeper/trunk/CHANGES.txt Thu Oct 27 16:11:15 2011
@@ -32,6 +32,10 @@ BUGFIXES:
   ZOOKEEPER-1241. Typo in ZooKeeper Recipes and Solutions
   documentation (Jingguo Yao via phunt)
 
+  ZOOKEEPER-1220. ./zkCli.sh 'create' command is throwing
+  ArrayIndexOutOfBoundsException (kavita sharma via phunt)
+
+
 IMPROVEMENTS:
 
   ZOOKEEPER-1170. Fix compiler (eclipse) warnings: unused imports,

Modified: zookeeper/trunk/src/java/main/org/apache/zookeeper/ZooKeeperMain.java
URL: http://svn.apache.org/viewvc/zookeeper/trunk/src/java/main/org/apache/zookeeper/ZooKeeperMain.java?rev=1189823&r1=1189822&r2=1189823&view=diff
==============================================================================
--- zookeeper/trunk/src/java/main/org/apache/zookeeper/ZooKeeperMain.java (original)
+++ zookeeper/trunk/src/java/main/org/apache/zookeeper/ZooKeeperMain.java Thu Oct 27 16:11:15
2011
@@ -670,11 +670,13 @@ public class ZooKeeperMain {
             return false;
         }
         
-        if (cmd.equals("create") && args.length >= 3) {
+        if (cmd.equals("create") && args.length >= 2) {
             int first = 0;
             CreateMode flags = CreateMode.PERSISTENT;
-            if ((args[1].equals("-e") && args[2].equals("-s"))
-                    || (args[1]).equals("-s") && (args[2].equals("-e"))) {
+            if ((args.length >= 3)
+                    && ((args[1].equals("-e") && args[2].equals("-s")) ||
(args[1])
+                            .equals("-s")
+                            && (args[2].equals("-e")))) {
                 first+=2;
                 flags = CreateMode.EPHEMERAL_SEQUENTIAL;
             } else if (args[1].equals("-e")) {
@@ -684,13 +686,24 @@ public class ZooKeeperMain {
                 first++;
                 flags = CreateMode.PERSISTENT_SEQUENTIAL;
             }
-            if (args.length == first + 4) {
+            if (args.length >= first+2) {
+                path = args[first+1];
+            }
+            if(path==null)
+            {
+                usage();
+                return false;
+            }
+            byte[] data = null;
+            if (args.length >= first + 3) {
+                data = args[first + 2].getBytes();
+            }
+            if (args.length >= first + 4) {
                 acl = parseACLs(args[first+3]);
             }
-            path = args[first + 1];
-            String newPath = zk.create(path, args[first+2].getBytes(), acl,
-                    flags);
+            String newPath = zk.create(path, data, acl, flags);
             System.err.println("Created " + newPath);
+            return true;
         } else if (cmd.equals("delete") && args.length >= 2) {
             path = args[1];
             zk.delete(path, watch ? Integer.parseInt(args[2]) : -1);

Modified: zookeeper/trunk/src/java/test/org/apache/zookeeper/ZooKeeperTest.java
URL: http://svn.apache.org/viewvc/zookeeper/trunk/src/java/test/org/apache/zookeeper/ZooKeeperTest.java?rev=1189823&r1=1189822&r2=1189823&view=diff
==============================================================================
--- zookeeper/trunk/src/java/test/org/apache/zookeeper/ZooKeeperTest.java (original)
+++ zookeeper/trunk/src/java/test/org/apache/zookeeper/ZooKeeperTest.java Thu Oct 27 16:11:15
2011
@@ -23,6 +23,7 @@ import java.util.concurrent.atomic.Atomi
 
 import org.apache.zookeeper.AsyncCallback.VoidCallback;
 import org.apache.zookeeper.ZooDefs.Ids;
+import org.apache.zookeeper.data.Stat;
 import org.apache.zookeeper.test.ClientBase;
 import org.junit.Assert;
 import org.junit.Test;
@@ -148,4 +149,160 @@ public class ZooKeeperTest extends Clien
         Assert.assertEquals("ls is not taken as first argument", zkMain.cl.getCmdArgument(0),
"ls");
         Assert.assertEquals("/ is not taken as second argument", zkMain.cl.getCmdArgument(1),
"/");
     }
+
+    @Test
+    public void testInvalidCommand() throws Exception {
+        final ZooKeeper zk = createClient();
+        ZooKeeperMain zkMain = new ZooKeeperMain(zk);
+        String cmdstring = "cret -s /node1";
+        zkMain.cl.parseCommand(cmdstring);
+        Assert.assertFalse("Doesn't validate the command", zkMain
+                .processZKCmd(zkMain.cl));
+    }
+
+    @Test
+    public void testCreateCommandWithoutPath() throws Exception {
+        final ZooKeeper zk = createClient();
+        ZooKeeperMain zkMain = new ZooKeeperMain(zk);
+        String cmdstring = "create ";
+        zkMain.cl.parseCommand(cmdstring);
+        Assert.assertFalse("Path is not validated.", zkMain
+                .processZKCmd(zkMain.cl));
+        // create ephemeral
+        cmdstring = "create -e ";
+        zkMain.cl.parseCommand(cmdstring);
+        Assert.assertFalse("Path is not validated.", zkMain
+                .processZKCmd(zkMain.cl));
+        // create sequential
+        cmdstring = "create -s ";
+        zkMain.cl.parseCommand(cmdstring);
+        Assert.assertFalse("Path is not validated.", zkMain
+                .processZKCmd(zkMain.cl));
+        // create ephemeral sequential
+        cmdstring = "create -s -e ";
+        zkMain.cl.parseCommand(cmdstring);
+        Assert.assertFalse("Path is not validated.", zkMain
+                .processZKCmd(zkMain.cl));
+    }
+
+    @Test
+    public void testCreateNodeWithoutData() throws Exception {
+        final ZooKeeper zk = createClient();
+        ZooKeeperMain zkMain = new ZooKeeperMain(zk);
+        // create persistent sequential node
+        String cmdstring = "create -s /node ";
+        zkMain.cl.parseCommand(cmdstring);
+        Assert.assertTrue("Doesn't create node without data", zkMain
+                .processZKCmd(zkMain.cl));
+        // create ephemeral node
+        cmdstring = "create  -e /node ";
+        zkMain.cl.parseCommand(cmdstring);
+        Assert.assertTrue("Doesn't create node without data", zkMain
+                .processZKCmd(zkMain.cl));
+        // create ephemeral sequential node
+        cmdstring = "create -s -e /node ";
+        zkMain.cl.parseCommand(cmdstring);
+        Assert.assertTrue("Doesn't create node without data", zkMain
+                .processZKCmd(zkMain.cl));
+        // creating ephemeral with wrong option.
+        cmdstring = "create -s y /node";
+        zkMain.cl.parseCommand(cmdstring);
+        try {
+            Assert.assertTrue("Created node with wrong option", zkMain
+                    .processZKCmd(zkMain.cl));
+            Assert
+                    .fail("Created the node with wrong option should throw Exception.");
+        } catch (IllegalArgumentException e) {
+            Assert.assertEquals("Path must start with / character", e
+                    .getMessage());
+        }
+    }
+
+    @Test
+    public void testACLWithExtraAgruments() throws Exception {
+        final ZooKeeper zk = createClient();
+        ZooKeeperMain zkMain = new ZooKeeperMain(zk);
+        // create persistent sequential node
+        String cmdstring = "create -s /l data ip:10.18.52.144:cdrwa f g h";
+        zkMain.cl.parseCommand(cmdstring);
+        Assert.assertTrue(
+                "Not considering the extra arguments after the acls.", zkMain
+                        .processZKCmd(zkMain.cl));
+    }
+
+    @Test
+    public void testCreatePersistentNode() throws Exception {
+        final ZooKeeper zk = createClient();
+        ZooKeeperMain zkMain = new ZooKeeperMain(zk);
+        String cmdstring = "create /node2";
+        zkMain.cl.parseCommand(cmdstring);
+        Assert.assertTrue("Not creating Persistent node.", zkMain
+                .processZKCmd(zkMain.cl));
+    }
+
+    @Test
+    public void testDelete() throws Exception {
+        final ZooKeeper zk = createClient();
+        ZooKeeperMain zkMain = new ZooKeeperMain(zk);
+        String cmdstring1 = "create -e /node2 data";
+        String cmdstring2 = "delete /node2";
+        String cmdstring3 = "ls /node2";
+        zkMain.cl.parseCommand(cmdstring1);
+        Assert.assertTrue(zkMain.processZKCmd(zkMain.cl));
+        zkMain.cl.parseCommand(cmdstring2);
+        Assert.assertFalse(zkMain.processZKCmd(zkMain.cl));
+        zkMain.cl.parseCommand(cmdstring3);
+        Assert.assertFalse("", zkMain.processCmd(zkMain.cl));
+    }
+
+    @Test
+    public void testStatCommand() throws Exception {
+        final ZooKeeper zk = createClient();
+        ZooKeeperMain zkMain = new ZooKeeperMain(zk);
+        String cmdstring1 = "create -e /node3 data";
+        String cmdstring2 = "stat /node3";
+        String cmdstring3 = "delete /node3";
+        zkMain.cl.parseCommand(cmdstring1);
+        Assert.assertTrue(zkMain.processZKCmd(zkMain.cl));
+        zkMain.cl.parseCommand(cmdstring2);
+        Assert.assertFalse(zkMain.processZKCmd(zkMain.cl));
+        zkMain.cl.parseCommand(cmdstring3);
+        Assert.assertFalse(zkMain.processZKCmd(zkMain.cl));
+    }
+
+    @Test
+    public void testInvalidStatCommand() throws Exception {
+        final ZooKeeper zk = createClient();
+        ZooKeeperMain zkMain = new ZooKeeperMain(zk);
+        // node doesn't exists
+        String cmdstring1 = "stat /node123";
+        zkMain.cl.parseCommand(cmdstring1);
+        try {
+            Assert.assertFalse(zkMain.processZKCmd(zkMain.cl));
+            Assert.fail("Path doesn't exists so, command should fail.");
+        } catch (KeeperException e) {
+            Assert.assertEquals(KeeperException.Code.NONODE, e.code());
+        }
+    }
+
+    @Test
+    public void testSetData() throws Exception {
+        final ZooKeeper zk = createClient();
+        ZooKeeperMain zkMain = new ZooKeeperMain(zk);
+        String cmdstring1 = "create -e /node4 data";
+        String cmdstring2 = "set /node4 " + "data";
+        String cmdstring3 = "delete /node4";
+        Stat stat = new Stat();
+        int version = 0;
+        zkMain.cl.parseCommand(cmdstring1);
+        Assert.assertTrue(zkMain.processZKCmd(zkMain.cl));
+        stat = zk.exists("/node4", true);
+        version = stat.getVersion();
+        zkMain.cl.parseCommand(cmdstring2);
+        Assert.assertTrue(zkMain.processZKCmd(zkMain.cl));
+        stat = zk.exists("/node4", true);
+        Assert.assertEquals(version + 1, stat.getVersion());
+        zkMain.cl.parseCommand(cmdstring3);
+        Assert.assertFalse(zkMain.processZKCmd(zkMain.cl));
+    }
 }



Mime
View raw message