001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.ipc; 019 020import static org.junit.jupiter.api.Assertions.assertEquals; 021import static org.junit.jupiter.api.Assertions.assertFalse; 022import static org.junit.jupiter.api.Assertions.assertNotEquals; 023import static org.junit.jupiter.api.Assertions.assertTrue; 024 025import org.apache.hadoop.conf.Configuration; 026import org.apache.hadoop.hbase.HBaseConfiguration; 027import org.apache.hadoop.hbase.net.Address; 028import org.apache.hadoop.hbase.security.User; 029import org.apache.hadoop.hbase.testclassification.ClientTests; 030import org.apache.hadoop.hbase.testclassification.SmallTests; 031import org.junit.jupiter.api.Tag; 032import org.junit.jupiter.api.Test; 033 034@Tag(SmallTests.TAG) 035@Tag(ClientTests.TAG) 036public class TestConnectionId { 037 038 private Configuration testConfig = HBaseConfiguration.create(); 039 private User testUser1 = 040 User.createUserForTesting(testConfig, "test", new String[] { "testgroup" }); 041 private User testUser2 = 042 User.createUserForTesting(testConfig, "test", new String[] { "testgroup" }); 043 private String serviceName = "test"; 044 private Address address = Address.fromParts("localhost", 999); 045 private ConnectionId connectionId1 = new ConnectionId(testUser1, serviceName, address); 046 private ConnectionId connectionId2 = new ConnectionId(testUser2, serviceName, address); 047 048 @Test 049 public void testGetServiceName() { 050 assertEquals("test", connectionId1.getServiceName()); 051 } 052 053 @Test 054 public void testGetAddress() { 055 assertEquals(address, connectionId1.getAddress()); 056 assertEquals(address, connectionId2.getAddress()); 057 } 058 059 @Test 060 public void testGetTicket() { 061 assertEquals(testUser1, connectionId1.getTicket()); 062 assertNotEquals(testUser2, connectionId1.getTicket()); 063 } 064 065 @Test 066 public void testToString() { 067 String expectedString = "localhost:999/test/test (auth:SIMPLE)"; 068 assertEquals(expectedString, connectionId1.toString()); 069 } 070 071 /** 072 * Test if the over-ridden equals method satisfies all the properties (reflexive, symmetry, 073 * transitive and null) along with their hashcode 074 */ 075 @Test 076 public void testEqualsWithHashCode() { 077 // Test the Reflexive Property 078 assertTrue(connectionId1.equals(connectionId1)); 079 080 // Test the Symmetry Property 081 ConnectionId connectionId = new ConnectionId(testUser1, serviceName, address); 082 assertTrue(connectionId.equals(connectionId1) && connectionId1.equals(connectionId)); 083 assertEquals(connectionId.hashCode(), connectionId1.hashCode()); 084 085 // Test the Transitive Property 086 ConnectionId connectionId3 = new ConnectionId(testUser1, serviceName, address); 087 assertTrue(connectionId1.equals(connectionId) && connectionId.equals(connectionId3) 088 && connectionId1.equals(connectionId3)); 089 assertEquals(connectionId.hashCode(), connectionId3.hashCode()); 090 091 // Test For null 092 assertFalse(connectionId1.equals(null)); 093 094 // Test different instances of same class 095 assertFalse(connectionId1.equals(connectionId2)); 096 } 097 098 /** 099 * Test the hashcode for same object and different object with both hashcode function and static 100 * hashcode function 101 */ 102 @Test 103 public void testHashCode() { 104 int testHashCode = connectionId1.hashCode(); 105 int expectedHashCode = ConnectionId.hashCode(testUser1, serviceName, address); 106 assertEquals(expectedHashCode, testHashCode); 107 108 // Make sure two objects are not same and test for hashcode 109 assertNotEquals(connectionId1, connectionId2); 110 assertNotEquals(connectionId1.hashCode(), connectionId2.hashCode()); 111 } 112}