zookeeper-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From r..@apache.org
Subject svn commit: r1718254 - in /zookeeper/branches/branch-3.5: CHANGES.txt src/java/main/org/apache/zookeeper/server/quorum/LocalPeerBean.java src/java/test/org/apache/zookeeper/server/quorum/LocalPeerBeanTest.java
Date Mon, 07 Dec 2015 05:08:09 GMT
Author: rgs
Date: Mon Dec  7 05:08:09 2015
New Revision: 1718254

URL: http://svn.apache.org/viewvc?rev=1718254&view=rev
Log:
ZOOKEEPER-2299: NullPointerException in LocalPeerBean for ClientAddress
(Arshad Mohammad via rgs)

Added:
    zookeeper/branches/branch-3.5/src/java/test/org/apache/zookeeper/server/quorum/LocalPeerBeanTest.java
Modified:
    zookeeper/branches/branch-3.5/CHANGES.txt
    zookeeper/branches/branch-3.5/src/java/main/org/apache/zookeeper/server/quorum/LocalPeerBean.java

Modified: zookeeper/branches/branch-3.5/CHANGES.txt
URL: http://svn.apache.org/viewvc/zookeeper/branches/branch-3.5/CHANGES.txt?rev=1718254&r1=1718253&r2=1718254&view=diff
==============================================================================
--- zookeeper/branches/branch-3.5/CHANGES.txt (original)
+++ zookeeper/branches/branch-3.5/CHANGES.txt Mon Dec  7 05:08:09 2015
@@ -80,6 +80,9 @@ BUGFIXES:
   ZOOKEEPER-2301: QuorumPeer does not listen on passed client IP in the constructor
   (Arshad Mohammad via rgs)
 
+  ZOOKEEPER-2299: NullPointerException in LocalPeerBean for ClientAddress
+  (Arshad Mohammad via rgs)
+
 IMPROVEMENTS:
 
   ZOOKEEPER-2270: Allow MBeanRegistry to be overridden for better unit tests

Modified: zookeeper/branches/branch-3.5/src/java/main/org/apache/zookeeper/server/quorum/LocalPeerBean.java
URL: http://svn.apache.org/viewvc/zookeeper/branches/branch-3.5/src/java/main/org/apache/zookeeper/server/quorum/LocalPeerBean.java?rev=1718254&r1=1718253&r2=1718254&view=diff
==============================================================================
--- zookeeper/branches/branch-3.5/src/java/main/org/apache/zookeeper/server/quorum/LocalPeerBean.java
(original)
+++ zookeeper/branches/branch-3.5/src/java/main/org/apache/zookeeper/server/quorum/LocalPeerBean.java
Mon Dec  7 05:08:09 2015
@@ -19,6 +19,7 @@
 package org.apache.zookeeper.server.quorum;
 
 
+
 /**
  * Implementation of the local peer MBean interface.
  */
@@ -83,8 +84,12 @@ public class LocalPeerBean extends Serve
     }
 
     public String getClientAddress() {
-        return peer.getClientAddress().getHostString() + ":" +
-            peer.getClientAddress().getPort();
+        if (null != peer.cnxnFactory) {
+            return String.format("%s:%d", peer.cnxnFactory.getLocalAddress()
+                    .getHostString(), peer.getClientPort());
+        } else {
+            return "";
+        }
     }
 
     public String getLearnerType(){

Added: zookeeper/branches/branch-3.5/src/java/test/org/apache/zookeeper/server/quorum/LocalPeerBeanTest.java
URL: http://svn.apache.org/viewvc/zookeeper/branches/branch-3.5/src/java/test/org/apache/zookeeper/server/quorum/LocalPeerBeanTest.java?rev=1718254&view=auto
==============================================================================
--- zookeeper/branches/branch-3.5/src/java/test/org/apache/zookeeper/server/quorum/LocalPeerBeanTest.java
(added)
+++ zookeeper/branches/branch-3.5/src/java/test/org/apache/zookeeper/server/quorum/LocalPeerBeanTest.java
Mon Dec  7 05:08:09 2015
@@ -0,0 +1,81 @@
+/**
+ * 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.server.quorum;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
+
+import org.apache.zookeeper.server.ServerCnxnFactory;
+import org.junit.Test;
+
+public class LocalPeerBeanTest {
+
+    /**
+     * Test case for https://issues.apache.org/jira/browse/ZOOKEEPER-2299
+     */
+    @Test
+    public void testClientAddress() throws Exception {
+        QuorumPeer quorumPeer = new QuorumPeer();
+        LocalPeerBean remotePeerBean = new LocalPeerBean(quorumPeer);
+
+        /**
+         * Case 1: When cnxnFactory is null
+         */
+        String result = remotePeerBean.getClientAddress();
+        assertNotNull(result);
+        assertEquals(0, result.length());
+
+        /**
+         * Case 2: When only client port is configured
+         */
+        ServerCnxnFactory cnxnFactory = ServerCnxnFactory.createFactory();
+        int clientPort = 2181;
+        InetSocketAddress address = new InetSocketAddress(clientPort);
+        cnxnFactory.configure(address, 5, false);
+        quorumPeer.setCnxnFactory(cnxnFactory);
+
+        result = remotePeerBean.getClientAddress();
+        String ipv4 = "0.0.0.0:" + clientPort;
+        String ipv6 = "0:0:0:0:0:0:0:0:" + clientPort;
+        assertTrue(result.equals(ipv4) || result.equals(ipv6));
+        // cleanup
+        cnxnFactory.shutdown();
+
+        /**
+         * Case 3: When both client port and client address is configured
+         */
+        InetAddress clientIP = InetAddress.getByAddress(new byte[] { 127, 0, 0,
+                2 });
+        address = new InetSocketAddress(clientIP, clientPort);
+        cnxnFactory = ServerCnxnFactory.createFactory();
+        cnxnFactory.configure(address, 5, false);
+        quorumPeer.setCnxnFactory(cnxnFactory);
+
+        result = remotePeerBean.getClientAddress();
+        String expectedResult = "127.0.0.2:" + clientPort;
+        assertEquals(expectedResult, result);
+        // cleanup
+        cnxnFactory.shutdown();
+    }
+
+}



Mime
View raw message