curator-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cammcken...@apache.org
Subject [1/3] git commit: CURATOR-67 defer creation of serializer
Date Mon, 04 Aug 2014 22:55:06 GMT
Repository: curator
Updated Branches:
  refs/heads/CURATOR-67 [created] 756fd5fd9


CURATOR-67 defer creation of serializer

Instead of immediately creating a new JSONInstanceSerializer we can
defer until build() is actually called. This lets users specify their
own serializers and avoids issues where there may be version conflicts.


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

Branch: refs/heads/CURATOR-67
Commit: f24e11bdc9ca45e94479b1c6fc981b8b7393f619
Parents: f5767c8
Author: Mike Drob <mdrob@cloudera.com>
Authored: Fri Aug 1 11:29:30 2014 -0500
Committer: Mike Drob <mdrob@cloudera.com>
Committed: Fri Aug 1 11:29:30 2014 -0500

----------------------------------------------------------------------
 .../x/discovery/ServiceDiscoveryBuilder.java    |  7 +-
 .../details/TestServiceDiscoveryBuilder.java    | 68 ++++++++++++++++++++
 2 files changed, 74 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/curator/blob/f24e11bd/curator-x-discovery/src/main/java/org/apache/curator/x/discovery/ServiceDiscoveryBuilder.java
----------------------------------------------------------------------
diff --git a/curator-x-discovery/src/main/java/org/apache/curator/x/discovery/ServiceDiscoveryBuilder.java
b/curator-x-discovery/src/main/java/org/apache/curator/x/discovery/ServiceDiscoveryBuilder.java
index ab62004..28fc31c 100644
--- a/curator-x-discovery/src/main/java/org/apache/curator/x/discovery/ServiceDiscoveryBuilder.java
+++ b/curator-x-discovery/src/main/java/org/apache/curator/x/discovery/ServiceDiscoveryBuilder.java
@@ -29,6 +29,7 @@ public class ServiceDiscoveryBuilder<T>
     private String                  basePath;
     private InstanceSerializer<T>   serializer;
     private ServiceInstance<T>      thisInstance;
+    private Class<T>                payloadClass;
 
     /**
      * Return a new builder. The builder will be defaulted with a {@link JsonInstanceSerializer}.
@@ -39,7 +40,7 @@ public class ServiceDiscoveryBuilder<T>
      */
     public static<T> ServiceDiscoveryBuilder<T>     builder(Class<T> payloadClass)
     {
-        return new ServiceDiscoveryBuilder<T>(payloadClass).serializer(new JsonInstanceSerializer<T>(payloadClass));
+        return new ServiceDiscoveryBuilder<T>(payloadClass);
     }
 
     /**
@@ -49,6 +50,9 @@ public class ServiceDiscoveryBuilder<T>
      */
     public ServiceDiscovery<T>      build()
     {
+        if ( serializer == null ) {
+            serializer(new JsonInstanceSerializer<T>(payloadClass));
+        }
         return new ServiceDiscoveryImpl<T>(client, basePath, serializer, thisInstance);
     }
 
@@ -102,5 +106,6 @@ public class ServiceDiscoveryBuilder<T>
 
     ServiceDiscoveryBuilder(Class<T> payloadClass)
     {
+        this.payloadClass = payloadClass;
     }
 }

http://git-wip-us.apache.org/repos/asf/curator/blob/f24e11bd/curator-x-discovery/src/test/java/org/apache/curator/x/discovery/details/TestServiceDiscoveryBuilder.java
----------------------------------------------------------------------
diff --git a/curator-x-discovery/src/test/java/org/apache/curator/x/discovery/details/TestServiceDiscoveryBuilder.java
b/curator-x-discovery/src/test/java/org/apache/curator/x/discovery/details/TestServiceDiscoveryBuilder.java
new file mode 100644
index 0000000..072eabf
--- /dev/null
+++ b/curator-x-discovery/src/test/java/org/apache/curator/x/discovery/details/TestServiceDiscoveryBuilder.java
@@ -0,0 +1,68 @@
+/**
+ * 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.x.discovery.details;
+
+import org.apache.curator.framework.CuratorFramework;
+import org.apache.curator.framework.CuratorFrameworkFactory;
+import org.apache.curator.retry.RetryOneTime;
+import org.apache.curator.test.BaseClassForTests;
+import org.apache.curator.x.discovery.ServiceDiscoveryBuilder;
+import org.apache.curator.x.discovery.ServiceInstance;
+import org.testng.Assert;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+public class TestServiceDiscoveryBuilder extends BaseClassForTests
+{
+    @Test
+    public void testDefaultSerializer() throws Exception
+    {        
+        CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(),
new RetryOneTime(1));
+        ServiceDiscoveryBuilder<Object> builder = ServiceDiscoveryBuilder.builder(Object.class).client(client);
+        ServiceDiscoveryImpl<?> discovery = (ServiceDiscoveryImpl<?>) builder.basePath("/path").build();
+
+        Assert.assertNotNull(discovery.getSerializer(), "default serializer not set");
+        Assert.assertTrue(discovery.getSerializer() instanceof JsonInstanceSerializer, "default
serializer not JSON");
+    }
+
+    @Test
+    public void testSetSerializer() throws Exception
+    {
+        CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(),
new RetryOneTime(1));
+        ServiceDiscoveryBuilder<Object> builder = ServiceDiscoveryBuilder.builder(Object.class).client(client);
+        builder.serializer(new InstanceSerializer<Object>()
+        {
+            @Override
+            public byte[] serialize(ServiceInstance<Object> instance)
+            {
+                return null;
+            }
+
+            @Override
+            public ServiceInstance<Object> deserialize(byte[] bytes)
+            {
+                return null;
+            }
+        });
+
+        ServiceDiscoveryImpl<?> discovery = (ServiceDiscoveryImpl<?>) builder.basePath("/path").build();
+        Assert.assertNotNull(discovery.getSerializer(), "default serializer not set");
+        Assert.assertFalse(discovery.getSerializer() instanceof JsonInstanceSerializer, "set
serializer is JSON");
+    }
+}


Mime
View raw message