From 5b29eee931d788fa7e9580a5df36389c42db25cf Mon Sep 17 00:00:00 2001 From: ravowlga123 Date: Fri, 23 Aug 2019 16:08:31 +0200 Subject: [PATCH 1/3] HBASE-22886 Code Coverage Improvement: Create Unit Tests for ConnectionId --- .../hadoop/hbase/ipc/TestConnectionId.java | 118 ++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 hbase-client/src/test/java/org/apache/hadoop/hbase/ipc/TestConnectionId.java diff --git a/hbase-client/src/test/java/org/apache/hadoop/hbase/ipc/TestConnectionId.java b/hbase-client/src/test/java/org/apache/hadoop/hbase/ipc/TestConnectionId.java new file mode 100644 index 000000000000..e107c64ada4e --- /dev/null +++ b/hbase-client/src/test/java/org/apache/hadoop/hbase/ipc/TestConnectionId.java @@ -0,0 +1,118 @@ +/** + * 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.hadoop.hbase.ipc; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNotSame; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; +import java.net.InetSocketAddress; +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.hbase.HBaseClassTestRule; +import org.apache.hadoop.hbase.HBaseConfiguration; +import org.apache.hadoop.hbase.security.User; +import org.apache.hadoop.hbase.testclassification.ClientTests; +import org.apache.hadoop.hbase.testclassification.SmallTests; +import org.junit.ClassRule; +import org.junit.Test; +import org.junit.experimental.categories.Category; + +@Category({SmallTests.class, ClientTests.class}) +public class TestConnectionId { + @ClassRule + public static final HBaseClassTestRule CLASS_RULE = + HBaseClassTestRule.forClass(TestConnectionId.class); + + private Configuration testConfig = HBaseConfiguration.create(); + private User testUser1 = User.createUserForTesting(testConfig, "test", new String[]{"testgroup"}); + private User testUser2 = User.createUserForTesting(testConfig, "test", new String[]{"testgroup"}); + private String serviceName = "test"; + private InetSocketAddress address = new InetSocketAddress(999); + private ConnectionId connectionId1 = new ConnectionId(testUser1, serviceName, address); + private ConnectionId connectionId2 = new ConnectionId(testUser2, serviceName, address); + + @Test + public void testGetServiceName() { + assertSame("test", connectionId1.getServiceName()); + assertNotSame("test1", connectionId1.getServiceName()); + } + + @Test + public void testGetAddress() { + assertSame(address, connectionId1.getAddress()); + assertSame(address, connectionId2.getAddress()); + assertEquals(connectionId1.getAddress(), connectionId2.getAddress()); + } + + @Test + public void testGetTicket() { + assertSame(testUser1, connectionId1.getTicket()); + assertNotSame(testUser2, connectionId1.getTicket()); + } + + @Test + public void testToString() { + String expectedString = address.toString() + "/" + serviceName + "/" + testUser1; + assertEquals(expectedString, connectionId1.toString()); + } + + /** + * Test if the over-ridden equals method satisfies all the properties + * (reflexive, symmetry, transitive and null) + * along with their hashcode + */ + @Test + public void testEqualsWithHashCode() { + // Test the Reflexive Property + assertTrue(connectionId1.equals(connectionId1)); + + // Test the Symmetry Property + ConnectionId connectionId = new ConnectionId(testUser1, serviceName, address); + assertTrue(connectionId.equals(connectionId1) && connectionId1.equals(connectionId)); + assertEquals(connectionId.hashCode(), connectionId1.hashCode()); + + // Test the Transitive Property + ConnectionId connectionId3 = new ConnectionId(testUser1, serviceName, address); + assertTrue(connectionId1.equals(connectionId) && connectionId.equals(connectionId3) && + connectionId1.equals(connectionId3)); + assertEquals(connectionId.hashCode(), connectionId3.hashCode()); + + // Test For null + assertFalse(connectionId1.equals(null)); + + // Test different instances of same class + assertFalse(connectionId1.equals(connectionId2)); + } + + /** + * Test the hashcode for same object and different object with both hashcode + * function and static hashcode function + */ + @Test + public void testHashCode() { + int testHashCode = connectionId1.hashCode(); + int expectedHashCode = ConnectionId.hashCode(testUser1, serviceName, address); + assertEquals(expectedHashCode, testHashCode); + + // Make sure two objects are not same and test for hashcode + assertNotSame(connectionId1, connectionId2); + assertNotEquals(connectionId1.hashCode(), connectionId2.hashCode()); + } +} \ No newline at end of file From a61496e5e4a29479e838a753226c08511508c304 Mon Sep 17 00:00:00 2001 From: ravowlga123 Date: Fri, 23 Aug 2019 16:11:19 +0200 Subject: [PATCH 2/3] HBASE-22886 Code Coverage Improvement: Create Unit Tests for ConnectionId. Improved Formatting --- .../test/java/org/apache/hadoop/hbase/ipc/TestConnectionId.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hbase-client/src/test/java/org/apache/hadoop/hbase/ipc/TestConnectionId.java b/hbase-client/src/test/java/org/apache/hadoop/hbase/ipc/TestConnectionId.java index e107c64ada4e..074347940ba3 100644 --- a/hbase-client/src/test/java/org/apache/hadoop/hbase/ipc/TestConnectionId.java +++ b/hbase-client/src/test/java/org/apache/hadoop/hbase/ipc/TestConnectionId.java @@ -115,4 +115,4 @@ public void testHashCode() { assertNotSame(connectionId1, connectionId2); assertNotEquals(connectionId1.hashCode(), connectionId2.hashCode()); } -} \ No newline at end of file +} From f15c154dbd4fd9b56e33f47077f252866b7e1fbb Mon Sep 17 00:00:00 2001 From: ravowlga123 Date: Mon, 2 Sep 2019 16:40:41 +0200 Subject: [PATCH 3/3] HBASE-22886 Code Coverage Improvement for ConnectionId with requested changes. --- .../hadoop/hbase/ipc/TestConnectionId.java | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/hbase-client/src/test/java/org/apache/hadoop/hbase/ipc/TestConnectionId.java b/hbase-client/src/test/java/org/apache/hadoop/hbase/ipc/TestConnectionId.java index 074347940ba3..3e10f7409c5e 100644 --- a/hbase-client/src/test/java/org/apache/hadoop/hbase/ipc/TestConnectionId.java +++ b/hbase-client/src/test/java/org/apache/hadoop/hbase/ipc/TestConnectionId.java @@ -20,9 +20,8 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotEquals; -import static org.junit.Assert.assertNotSame; -import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; + import java.net.InetSocketAddress; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseClassTestRule; @@ -50,26 +49,24 @@ public class TestConnectionId { @Test public void testGetServiceName() { - assertSame("test", connectionId1.getServiceName()); - assertNotSame("test1", connectionId1.getServiceName()); + assertEquals("test", connectionId1.getServiceName()); } @Test public void testGetAddress() { - assertSame(address, connectionId1.getAddress()); - assertSame(address, connectionId2.getAddress()); - assertEquals(connectionId1.getAddress(), connectionId2.getAddress()); + assertEquals(address, connectionId1.getAddress()); + assertEquals(address, connectionId2.getAddress()); } @Test public void testGetTicket() { - assertSame(testUser1, connectionId1.getTicket()); - assertNotSame(testUser2, connectionId1.getTicket()); + assertEquals(testUser1, connectionId1.getTicket()); + assertNotEquals(testUser2, connectionId1.getTicket()); } @Test public void testToString() { - String expectedString = address.toString() + "/" + serviceName + "/" + testUser1; + String expectedString = "0.0.0.0/0.0.0.0:999/test/test (auth:SIMPLE)"; assertEquals(expectedString, connectionId1.toString()); } @@ -112,7 +109,7 @@ public void testHashCode() { assertEquals(expectedHashCode, testHashCode); // Make sure two objects are not same and test for hashcode - assertNotSame(connectionId1, connectionId2); + assertNotEquals(connectionId1, connectionId2); assertNotEquals(connectionId1.hashCode(), connectionId2.hashCode()); } }