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 2016/05/19 19:02:31 UTC

[16/35] curator git commit: test transactions

test transactions


Project: http://git-wip-us.apache.org/repos/asf/curator/repo
Commit: http://git-wip-us.apache.org/repos/asf/curator/commit/ae6cb4be
Tree: http://git-wip-us.apache.org/repos/asf/curator/tree/ae6cb4be
Diff: http://git-wip-us.apache.org/repos/asf/curator/diff/ae6cb4be

Branch: refs/heads/CURATOR-3.0
Commit: ae6cb4be4ee56215bef26e8d7fb5139d9b81d026
Parents: 923a2d8
Author: randgalt <ra...@apache.org>
Authored: Tue May 3 10:19:46 2016 -0500
Committer: randgalt <ra...@apache.org>
Committed: Tue May 3 10:19:46 2016 -0500

----------------------------------------------------------------------
 .../curator/framework/schema/TestSchema.java    | 83 ++++++++++++++++++++
 .../src/test/resources/schema4.json             | 14 ++++
 2 files changed, 97 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/curator/blob/ae6cb4be/curator-framework/src/test/java/org/apache/curator/framework/schema/TestSchema.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/test/java/org/apache/curator/framework/schema/TestSchema.java b/curator-framework/src/test/java/org/apache/curator/framework/schema/TestSchema.java
index f3754f5..e094c0e 100644
--- a/curator-framework/src/test/java/org/apache/curator/framework/schema/TestSchema.java
+++ b/curator-framework/src/test/java/org/apache/curator/framework/schema/TestSchema.java
@@ -22,6 +22,7 @@ import com.google.common.base.Charsets;
 import com.google.common.io.Resources;
 import org.apache.curator.framework.CuratorFramework;
 import org.apache.curator.framework.CuratorFrameworkFactory;
+import org.apache.curator.framework.api.transaction.CuratorOp;
 import org.apache.curator.retry.RetryOneTime;
 import org.apache.curator.test.BaseClassForTests;
 import org.apache.curator.utils.CloseableUtils;
@@ -139,6 +140,88 @@ public class TestSchema extends BaseClassForTests
         }
     }
 
+    @Test
+    public void testTransaction() throws Exception
+    {
+        final DataValidator dataValidator = new DataValidator()
+        {
+            @Override
+            public boolean isValid(String path, byte[] data)
+            {
+                return data.length > 0;
+            }
+        };
+        SchemaSetLoader.DataValidatorMapper dataValidatorMapper = new SchemaSetLoader.DataValidatorMapper()
+        {
+            @Override
+            public DataValidator getDataValidator(String name)
+            {
+                return dataValidator;
+            }
+        };
+        SchemaSet schemaSet = loadSchemaSet("schema4.json", dataValidatorMapper);
+        CuratorFramework client = newClient(schemaSet);
+        try
+        {
+            client.start();
+
+            CuratorOp createAPersistent = client.transactionOp().create().forPath("/a");
+            CuratorOp createAEphemeral = client.transactionOp().create().withMode(CreateMode.EPHEMERAL).forPath("/a");
+            CuratorOp deleteA = client.transactionOp().delete().forPath("/a");
+            CuratorOp createBEmptyData = client.transactionOp().create().forPath("/b", new byte[0]);
+            CuratorOp createBWithData = client.transactionOp().create().forPath("/b", new byte[10]);
+            CuratorOp setBEmptyData = client.transactionOp().setData().forPath("/b", new byte[0]);
+            CuratorOp setBWithData = client.transactionOp().setData().forPath("/b", new byte[10]);
+
+            try
+            {
+                client.transaction().forOperations(createAPersistent, createAEphemeral);
+                Assert.fail("Should've violated schema");
+            }
+            catch ( SchemaViolation dummy )
+            {
+                // expected
+            }
+            client.transaction().forOperations(createAEphemeral);
+
+            try
+            {
+                client.transaction().forOperations(deleteA);
+                Assert.fail("Should've violated schema");
+            }
+            catch ( SchemaViolation dummy )
+            {
+                // expected
+            }
+
+            try
+            {
+                client.transaction().forOperations(createBEmptyData);
+                Assert.fail("Should've violated schema");
+            }
+            catch ( SchemaViolation dummy )
+            {
+                // expected
+            }
+            client.transaction().forOperations(createBWithData);
+
+            try
+            {
+                client.transaction().forOperations(setBEmptyData);
+                Assert.fail("Should've violated schema");
+            }
+            catch ( SchemaViolation dummy )
+            {
+                // expected
+            }
+            client.transaction().forOperations(setBWithData);
+        }
+        finally
+        {
+            CloseableUtils.closeQuietly(client);
+        }
+    }
+
     @Override
     protected boolean enabledSessionExpiredStateAware()
     {

http://git-wip-us.apache.org/repos/asf/curator/blob/ae6cb4be/curator-framework/src/test/resources/schema4.json
----------------------------------------------------------------------
diff --git a/curator-framework/src/test/resources/schema4.json b/curator-framework/src/test/resources/schema4.json
new file mode 100644
index 0000000..6fec36d
--- /dev/null
+++ b/curator-framework/src/test/resources/schema4.json
@@ -0,0 +1,14 @@
+[
+  {
+    "name": "testa",
+    "path": "/a",
+    "ephemeral": "must",
+    "canBeDeleted": false
+  },
+
+  {
+    "name": "testb",
+    "path": "/b",
+    "dataValidator": "test"
+  }
+]
\ No newline at end of file