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.Assert.assertEquals; 021import static org.junit.Assert.assertFalse; 022import static org.junit.Assert.assertNotEquals; 023import static org.junit.Assert.assertTrue; 024 025import java.net.InetSocketAddress; 026import org.apache.hadoop.conf.Configuration; 027import org.apache.hadoop.hbase.HBaseClassTestRule; 028import org.apache.hadoop.hbase.HBaseConfiguration; 029import org.apache.hadoop.hbase.security.User; 030import org.apache.hadoop.hbase.testclassification.ClientTests; 031import org.apache.hadoop.hbase.testclassification.SmallTests; 032import org.junit.ClassRule; 033import org.junit.Test; 034import org.junit.experimental.categories.Category; 035 036@Category({SmallTests.class, ClientTests.class}) 037public class TestConnectionId { 038 @ClassRule 039 public static final HBaseClassTestRule CLASS_RULE = 040 HBaseClassTestRule.forClass(TestConnectionId.class); 041 042 private Configuration testConfig = HBaseConfiguration.create(); 043 private User testUser1 = User.createUserForTesting(testConfig, "test", new String[]{"testgroup"}); 044 private User testUser2 = User.createUserForTesting(testConfig, "test", new String[]{"testgroup"}); 045 private String serviceName = "test"; 046 private InetSocketAddress address = new InetSocketAddress(999); 047 private ConnectionId connectionId1 = new ConnectionId(testUser1, serviceName, address); 048 private ConnectionId connectionId2 = new ConnectionId(testUser2, serviceName, address); 049 050 @Test 051 public void testGetServiceName() { 052 assertEquals("test", connectionId1.getServiceName()); 053 } 054 055 @Test 056 public void testGetAddress() { 057 assertEquals(address, connectionId1.getAddress()); 058 assertEquals(address, connectionId2.getAddress()); 059 } 060 061 @Test 062 public void testGetTicket() { 063 assertEquals(testUser1, connectionId1.getTicket()); 064 assertNotEquals(testUser2, connectionId1.getTicket()); 065 } 066 067 @Test 068 public void testToString() { 069 String expectedString = "0.0.0.0/0.0.0.0:999/test/test (auth:SIMPLE)"; 070 assertEquals(expectedString, connectionId1.toString()); 071 } 072 073 /** 074 * Test if the over-ridden equals method satisfies all the properties 075 * (reflexive, symmetry, transitive and null) 076 * along with their hashcode 077 */ 078 @Test 079 public void testEqualsWithHashCode() { 080 // Test the Reflexive Property 081 assertTrue(connectionId1.equals(connectionId1)); 082 083 // Test the Symmetry Property 084 ConnectionId connectionId = new ConnectionId(testUser1, serviceName, address); 085 assertTrue(connectionId.equals(connectionId1) && connectionId1.equals(connectionId)); 086 assertEquals(connectionId.hashCode(), connectionId1.hashCode()); 087 088 // Test the Transitive Property 089 ConnectionId connectionId3 = new ConnectionId(testUser1, serviceName, address); 090 assertTrue(connectionId1.equals(connectionId) && connectionId.equals(connectionId3) && 091 connectionId1.equals(connectionId3)); 092 assertEquals(connectionId.hashCode(), connectionId3.hashCode()); 093 094 // Test For null 095 assertFalse(connectionId1.equals(null)); 096 097 // Test different instances of same class 098 assertFalse(connectionId1.equals(connectionId2)); 099 } 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}