1   /**
2    * Copyright 2011 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase;
21  
22  import static org.junit.Assert.*;
23  
24  import java.io.IOException;
25  import java.net.InetSocketAddress;
26  
27  import org.apache.hadoop.hbase.util.Writables;
28  import org.junit.Test;
29  import org.junit.experimental.categories.Category;
30  
31  /**
32   * Tests for {@link HServerAddress}
33   */
34  @Category(SmallTests.class)
35  public class TestHServerAddress {
36    @Test
37    public void testHashCode() {
38      HServerAddress hsa1 = new HServerAddress("localhost", 1234);
39      HServerAddress hsa2 = new HServerAddress("localhost", 1234);
40      assertEquals(hsa1.hashCode(), hsa2.hashCode());
41      HServerAddress hsa3 = new HServerAddress("localhost", 1235);
42      assertNotSame(hsa1.hashCode(), hsa3.hashCode());
43    }
44  
45    @Test
46    public void testHServerAddress() {
47      new HServerAddress();
48    }
49  
50    @Test
51    public void testHServerAddressInetSocketAddress() {
52      HServerAddress hsa1 =
53        new HServerAddress(new InetSocketAddress("localhost", 1234));
54      System.out.println(hsa1.toString());
55    }
56  
57    @Test
58    public void testHServerAddressString() {
59      HServerAddress hsa1 = new HServerAddress("localhost", 1234);
60      HServerAddress hsa2 =
61        new HServerAddress(new InetSocketAddress("localhost", 1234));
62      assertTrue(hsa1.equals(hsa2));
63    }
64  
65    @Test
66    public void testHServerAddressHServerAddress() {
67      HServerAddress hsa1 = new HServerAddress("localhost", 1234);
68      HServerAddress hsa2 = new HServerAddress(hsa1);
69      assertEquals(hsa1, hsa2);
70    }
71  
72    @Test
73    public void testReadFields() throws IOException {
74      HServerAddress hsa1 = new HServerAddress("localhost", 1234);
75      HServerAddress hsa2 = new HServerAddress("localhost", 1235);
76      byte [] bytes = Writables.getBytes(hsa1);
77      HServerAddress deserialized =
78        (HServerAddress)Writables.getWritable(bytes, new HServerAddress());
79      assertEquals(hsa1, deserialized);
80      bytes = Writables.getBytes(hsa2);
81      deserialized =
82        (HServerAddress)Writables.getWritable(bytes, new HServerAddress());
83      assertNotSame(hsa1, deserialized);
84    }
85  
86    @org.junit.Rule
87    public org.apache.hadoop.hbase.ResourceCheckerJUnitRule cu =
88      new org.apache.hadoop.hbase.ResourceCheckerJUnitRule();
89  }
90