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 org.apache.hadoop.conf.Configuration;
026import org.apache.hadoop.hbase.HBaseClassTestRule;
027import org.apache.hadoop.hbase.HBaseConfiguration;
028import org.apache.hadoop.hbase.net.Address;
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 =
044    User.createUserForTesting(testConfig, "test", new String[] { "testgroup" });
045  private User testUser2 =
046    User.createUserForTesting(testConfig, "test", new String[] { "testgroup" });
047  private String serviceName = "test";
048  private Address address = Address.fromParts("localhost", 999);
049  private ConnectionId connectionId1 = new ConnectionId(testUser1, serviceName, address);
050  private ConnectionId connectionId2 = new ConnectionId(testUser2, serviceName, address);
051
052  @Test
053  public void testGetServiceName() {
054    assertEquals("test", connectionId1.getServiceName());
055  }
056
057  @Test
058  public void testGetAddress() {
059    assertEquals(address, connectionId1.getAddress());
060    assertEquals(address, connectionId2.getAddress());
061  }
062
063  @Test
064  public void testGetTicket() {
065    assertEquals(testUser1, connectionId1.getTicket());
066    assertNotEquals(testUser2, connectionId1.getTicket());
067  }
068
069  @Test
070  public void testToString() {
071    String expectedString = "localhost:999/test/test (auth:SIMPLE)";
072    assertEquals(expectedString, connectionId1.toString());
073  }
074
075  /**
076   * Test if the over-ridden equals method satisfies all the properties (reflexive, symmetry,
077   * transitive and null) along with their hashcode
078   */
079  @Test
080  public void testEqualsWithHashCode() {
081    // Test the Reflexive Property
082    assertTrue(connectionId1.equals(connectionId1));
083
084    // Test the Symmetry Property
085    ConnectionId connectionId = new ConnectionId(testUser1, serviceName, address);
086    assertTrue(connectionId.equals(connectionId1) && connectionId1.equals(connectionId));
087    assertEquals(connectionId.hashCode(), connectionId1.hashCode());
088
089    // Test the Transitive Property
090    ConnectionId connectionId3 = new ConnectionId(testUser1, serviceName, address);
091    assertTrue(connectionId1.equals(connectionId) && connectionId.equals(connectionId3)
092      && connectionId1.equals(connectionId3));
093    assertEquals(connectionId.hashCode(), connectionId3.hashCode());
094
095    // Test For null
096    assertFalse(connectionId1.equals(null));
097
098    // Test different instances of same class
099    assertFalse(connectionId1.equals(connectionId2));
100  }
101
102  /**
103   * Test the hashcode for same object and different object with both hashcode function and static
104   * hashcode function
105   */
106  @Test
107  public void testHashCode() {
108    int testHashCode = connectionId1.hashCode();
109    int expectedHashCode = ConnectionId.hashCode(testUser1, serviceName, address);
110    assertEquals(expectedHashCode, testHashCode);
111
112    // Make sure two objects are not same and test for hashcode
113    assertNotEquals(connectionId1, connectionId2);
114    assertNotEquals(connectionId1.hashCode(), connectionId2.hashCode());
115  }
116}