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;
19  
20  import static org.junit.Assert.*;
21  
22  import java.io.IOException;
23  
24  import org.apache.hadoop.hbase.util.Writables;
25  import org.junit.Test;
26  
27  public class TestHServerInfo {
28  
29    @Test
30    public void testHashCodeAndEquals() {
31      HServerAddress hsa1 = new HServerAddress("localhost", 1234);
32      HServerInfo hsi1 = new HServerInfo(hsa1, 1L, 5678);
33      HServerInfo hsi2 = new HServerInfo(hsa1, 1L, 5678);
34      HServerInfo hsi3 = new HServerInfo(hsa1, 2L, 5678);
35      HServerInfo hsi4 = new HServerInfo(hsa1, 1L, 5677);
36      HServerAddress hsa2 = new HServerAddress("localhost", 1235);
37      HServerInfo hsi5 = new HServerInfo(hsa2, 1L, 5678);
38      assertEquals(hsi1.hashCode(), hsi2.hashCode());
39      assertTrue(hsi1.equals(hsi2));
40      assertNotSame(hsi1.hashCode(), hsi3.hashCode());
41      assertFalse(hsi1.equals(hsi3));
42      assertNotSame(hsi1.hashCode(), hsi4.hashCode());
43      assertFalse(hsi1.equals(hsi4));
44      assertNotSame(hsi1.hashCode(), hsi5.hashCode());
45      assertFalse(hsi1.equals(hsi5));
46    }
47  
48    @Test
49    public void testHServerInfoHServerInfo() {
50      HServerAddress hsa1 = new HServerAddress("localhost", 1234);
51      HServerInfo hsi1 = new HServerInfo(hsa1, 1L, 5678);
52      HServerInfo hsi2 = new HServerInfo(hsi1);
53      assertEquals(hsi1, hsi2);
54    }
55  
56    @Test
57    public void testGetServerAddress() {
58      HServerAddress hsa1 = new HServerAddress("localhost", 1234);
59      HServerInfo hsi1 = new HServerInfo(hsa1, 1L, 5678);
60      assertEquals(hsi1.getServerAddress(), hsa1);
61    }
62  
63    @Test
64    public void testToString() {
65      HServerAddress hsa1 = new HServerAddress("localhost", 1234);
66      HServerInfo hsi1 = new HServerInfo(hsa1, 1L, 5678);
67      System.out.println(hsi1.toString());
68    }
69  
70    @Test
71    public void testReadFields() throws IOException {
72      HServerAddress hsa1 = new HServerAddress("localhost", 1234);
73      HServerInfo hsi1 = new HServerInfo(hsa1, 1L, 5678);
74      HServerAddress hsa2 = new HServerAddress("localhost", 1235);
75      HServerInfo hsi2 = new HServerInfo(hsa2, 1L, 5678);
76      byte [] bytes = Writables.getBytes(hsi1);
77      HServerInfo deserialized =
78        (HServerInfo)Writables.getWritable(bytes, new HServerInfo());
79      assertEquals(hsi1, deserialized);
80      bytes = Writables.getBytes(hsi2);
81      deserialized = (HServerInfo)Writables.getWritable(bytes, new HServerInfo());
82      assertNotSame(hsa1, deserialized);
83    }
84  
85    @Test
86    public void testCompareTo() {
87      HServerAddress hsa1 = new HServerAddress("localhost", 1234);
88      HServerInfo hsi1 = new HServerInfo(hsa1, 1L, 5678);
89      HServerAddress hsa2 = new HServerAddress("localhost", 1235);
90      HServerInfo hsi2 = new HServerInfo(hsa2, 1L, 5678);
91      assertTrue(hsi1.compareTo(hsi1) == 0);
92      assertTrue(hsi2.compareTo(hsi2) == 0);
93      int compare1 = hsi1.compareTo(hsi2);
94      int compare2 = hsi2.compareTo(hsi1);
95      assertTrue((compare1 > 0)? compare2 < 0: compare2 > 0);
96    }
97  }