From 00a21386f6fe0b981aa9083243a1e54cbc7d2ac0 Mon Sep 17 00:00:00 2001 From: Michael Nitschinger Date: Mon, 8 Oct 2012 14:57:31 +0200 Subject: [PATCH] JCBC-119: Allow NULL for persist/replicate. This changeset adds the possibility of using null for persistTo and/or replicateTo. It defaults to ZERO for both enums, which are the defaults and are equal to "dont observe at all". Tests are also attached. Change-Id: I0d69c971792ad8118f5d9035ccf3e0d8bca0215b Reviewed-on: http://review.couchbase.org/21421 Tested-by: Michael Nitschinger Reviewed-by: Michael Wiederhold --- .../com/couchbase/client/CouchbaseClient.java | 10 +++++++ .../couchbase/client/CouchbaseClientTest.java | 26 +++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/src/main/java/com/couchbase/client/CouchbaseClient.java b/src/main/java/com/couchbase/client/CouchbaseClient.java index 0ffed969..a582bf77 100644 --- a/src/main/java/com/couchbase/client/CouchbaseClient.java +++ b/src/main/java/com/couchbase/client/CouchbaseClient.java @@ -1370,6 +1370,10 @@ public boolean shutdown(long timeout, TimeUnit unit) { * normally utilized through higher-level methods but can also be used * directly. * + * If persist is null, it will default to PersistTo.ZERO and if replicate is + * null, it will default to ReplicateTo.ZERO. This is the default behavior + * and is the same as not observing at all. + * * @param key the key to observe. * @param cas the CAS value for the key. * @param persist the persistence settings. @@ -1379,6 +1383,12 @@ public boolean shutdown(long timeout, TimeUnit unit) { public void observePoll(String key, long cas, PersistTo persist, ReplicateTo replicate, boolean isDelete) { boolean persistMaster = false; + if(persist == null) { + persist = PersistTo.ZERO; + } + if(replicate == null) { + replicate = ReplicateTo.ZERO; + } int persistReplica = persist.getValue(); int replicateTo = replicate.getValue(); int obsPolls = 0; diff --git a/src/test/java/com/couchbase/client/CouchbaseClientTest.java b/src/test/java/com/couchbase/client/CouchbaseClientTest.java index 441ad35f..a8f6f340 100644 --- a/src/test/java/com/couchbase/client/CouchbaseClientTest.java +++ b/src/test/java/com/couchbase/client/CouchbaseClientTest.java @@ -40,6 +40,8 @@ import net.spy.memcached.internal.OperationFuture; import org.junit.Ignore; +import static org.junit.Assert.assertTrue; + /** * A CouchbaseClientTest. */ @@ -219,6 +221,30 @@ public void testStatsKey() throws Exception { assertFalse(future.getStatus().isSuccess()); } + /** + * Tests if passing NULL for ReplicateTo and/or + * PersistTo correctly works and does not throw + * an exception. + */ + public void testNullObserve() throws Exception { + boolean success; + try { + success = true; + OperationFuture nullcheckOp = + (((CouchbaseClient)client).add("nullcheck", 0, "value", null, null)); + nullcheckOp.get(); + nullcheckOp = + (((CouchbaseClient)client).set("nullcheck", 0, "value1", null, null)); + nullcheckOp.get(); + nullcheckOp = + (((CouchbaseClient)client).replace("nullcheck", 0, "value1", null, null)); + nullcheckOp.get(); + } catch(NullPointerException ex) { + success = false; + } + assertTrue(success); + } + public void testGetStatsSlabs() throws Exception { // Empty }