HBASE-25292 Improve InetSocketAddress usage discipline (#2669)
[hbase.git] / hbase-client / src / test / java / org / apache / hadoop / hbase / ipc / TestConnectionId.java
blob48a079d3e75b607f53060e317880a035e544b17e
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.apache.hadoop.hbase.ipc;
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;
25 import org.apache.hadoop.conf.Configuration;
26 import org.apache.hadoop.hbase.HBaseClassTestRule;
27 import org.apache.hadoop.hbase.HBaseConfiguration;
28 import org.apache.hadoop.hbase.net.Address;
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;
36 @Category({SmallTests.class, ClientTests.class})
37 public class TestConnectionId {
38 @ClassRule
39 public static final HBaseClassTestRule CLASS_RULE =
40 HBaseClassTestRule.forClass(TestConnectionId.class);
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 Address address = Address.fromParts("localhost", 999);
47 private ConnectionId connectionId1 = new ConnectionId(testUser1, serviceName, address);
48 private ConnectionId connectionId2 = new ConnectionId(testUser2, serviceName, address);
50 @Test
51 public void testGetServiceName() {
52 assertEquals("test", connectionId1.getServiceName());
55 @Test
56 public void testGetAddress() {
57 assertEquals(address, connectionId1.getAddress());
58 assertEquals(address, connectionId2.getAddress());
61 @Test
62 public void testGetTicket() {
63 assertEquals(testUser1, connectionId1.getTicket());
64 assertNotEquals(testUser2, connectionId1.getTicket());
67 @Test
68 public void testToString() {
69 String expectedString = "localhost:999/test/test (auth:SIMPLE)";
70 assertEquals(expectedString, connectionId1.toString());
73 /**
74 * Test if the over-ridden equals method satisfies all the properties
75 * (reflexive, symmetry, transitive and null)
76 * along with their hashcode
78 @Test
79 public void testEqualsWithHashCode() {
80 // Test the Reflexive Property
81 assertTrue(connectionId1.equals(connectionId1));
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());
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());
94 // Test For null
95 assertFalse(connectionId1.equals(null));
97 // Test different instances of same class
98 assertFalse(connectionId1.equals(connectionId2));
102 * Test the hashcode for same object and different object with both hashcode
103 * function and static hashcode function
105 @Test
106 public void testHashCode() {
107 int testHashCode = connectionId1.hashCode();
108 int expectedHashCode = ConnectionId.hashCode(testUser1, serviceName, address);
109 assertEquals(expectedHashCode, testHashCode);
111 // Make sure two objects are not same and test for hashcode
112 assertNotEquals(connectionId1, connectionId2);
113 assertNotEquals(connectionId1.hashCode(), connectionId2.hashCode());