|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package org.apache.hadoop.hbase.ipc; |
| 19 | + |
| 20 | +import static org.junit.Assert.assertEquals; |
| 21 | +import static org.junit.Assert.assertFalse; |
| 22 | +import static org.junit.Assert.assertNotEquals; |
| 23 | +import static org.junit.Assert.assertTrue; |
| 24 | + |
| 25 | +import java.net.InetSocketAddress; |
| 26 | +import org.apache.hadoop.conf.Configuration; |
| 27 | +import org.apache.hadoop.hbase.HBaseClassTestRule; |
| 28 | +import org.apache.hadoop.hbase.HBaseConfiguration; |
| 29 | +import org.apache.hadoop.hbase.security.User; |
| 30 | +import org.apache.hadoop.hbase.testclassification.ClientTests; |
| 31 | +import org.apache.hadoop.hbase.testclassification.SmallTests; |
| 32 | +import org.junit.ClassRule; |
| 33 | +import org.junit.Test; |
| 34 | +import org.junit.experimental.categories.Category; |
| 35 | + |
| 36 | +@Category({SmallTests.class, ClientTests.class}) |
| 37 | +public class TestConnectionId { |
| 38 | + @ClassRule |
| 39 | + public static final HBaseClassTestRule CLASS_RULE = |
| 40 | + HBaseClassTestRule.forClass(TestConnectionId.class); |
| 41 | + |
| 42 | + private Configuration testConfig = HBaseConfiguration.create(); |
| 43 | + private User testUser1 = User.createUserForTesting(testConfig, "test", new String[]{"testgroup"}); |
| 44 | + private User testUser2 = User.createUserForTesting(testConfig, "test", new String[]{"testgroup"}); |
| 45 | + private String serviceName = "test"; |
| 46 | + private InetSocketAddress address = new InetSocketAddress(999); |
| 47 | + private ConnectionId connectionId1 = new ConnectionId(testUser1, serviceName, address); |
| 48 | + private ConnectionId connectionId2 = new ConnectionId(testUser2, serviceName, address); |
| 49 | + |
| 50 | + @Test |
| 51 | + public void testGetServiceName() { |
| 52 | + assertEquals("test", connectionId1.getServiceName()); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void testGetAddress() { |
| 57 | + assertEquals(address, connectionId1.getAddress()); |
| 58 | + assertEquals(address, connectionId2.getAddress()); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void testGetTicket() { |
| 63 | + assertEquals(testUser1, connectionId1.getTicket()); |
| 64 | + assertNotEquals(testUser2, connectionId1.getTicket()); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void testToString() { |
| 69 | + String expectedString = "0.0.0.0/0.0.0.0:999/test/test (auth:SIMPLE)"; |
| 70 | + assertEquals(expectedString, connectionId1.toString()); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Test if the over-ridden equals method satisfies all the properties |
| 75 | + * (reflexive, symmetry, transitive and null) |
| 76 | + * along with their hashcode |
| 77 | + */ |
| 78 | + @Test |
| 79 | + public void testEqualsWithHashCode() { |
| 80 | + // Test the Reflexive Property |
| 81 | + assertTrue(connectionId1.equals(connectionId1)); |
| 82 | + |
| 83 | + // Test the Symmetry Property |
| 84 | + ConnectionId connectionId = new ConnectionId(testUser1, serviceName, address); |
| 85 | + assertTrue(connectionId.equals(connectionId1) && connectionId1.equals(connectionId)); |
| 86 | + assertEquals(connectionId.hashCode(), connectionId1.hashCode()); |
| 87 | + |
| 88 | + // Test the Transitive Property |
| 89 | + ConnectionId connectionId3 = new ConnectionId(testUser1, serviceName, address); |
| 90 | + assertTrue(connectionId1.equals(connectionId) && connectionId.equals(connectionId3) && |
| 91 | + connectionId1.equals(connectionId3)); |
| 92 | + assertEquals(connectionId.hashCode(), connectionId3.hashCode()); |
| 93 | + |
| 94 | + // Test For null |
| 95 | + assertFalse(connectionId1.equals(null)); |
| 96 | + |
| 97 | + // Test different instances of same class |
| 98 | + assertFalse(connectionId1.equals(connectionId2)); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Test the hashcode for same object and different object with both hashcode |
| 103 | + * function and static hashcode function |
| 104 | + */ |
| 105 | + @Test |
| 106 | + public void testHashCode() { |
| 107 | + int testHashCode = connectionId1.hashCode(); |
| 108 | + int expectedHashCode = ConnectionId.hashCode(testUser1, serviceName, address); |
| 109 | + assertEquals(expectedHashCode, testHashCode); |
| 110 | + |
| 111 | + // Make sure two objects are not same and test for hashcode |
| 112 | + assertNotEquals(connectionId1, connectionId2); |
| 113 | + assertNotEquals(connectionId1.hashCode(), connectionId2.hashCode()); |
| 114 | + } |
| 115 | +} |
0 commit comments