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  
30  /**
31   * Tests for {@link HServerAddress}
32   */
33  public class TestHServerAddress {
34    @Test
35    public void testHashCode() {
36      HServerAddress hsa1 = new HServerAddress("localhost", 1234);
37      HServerAddress hsa2 = new HServerAddress("localhost", 1234);
38      assertEquals(hsa1.hashCode(), hsa2.hashCode());
39      HServerAddress hsa3 = new HServerAddress("localhost", 1235);
40      assertNotSame(hsa1.hashCode(), hsa3.hashCode());
41    }
42  
43    @Test
44    public void testHServerAddress() {
45      new HServerAddress();
46    }
47  
48    @Test
49    public void testHServerAddressInetSocketAddress() {
50      HServerAddress hsa1 =
51        new HServerAddress(new InetSocketAddress("localhost", 1234));
52      System.out.println(hsa1.toString());
53    }
54  
55    @Test
56    public void testHServerAddressString() {
57      HServerAddress hsa1 = new HServerAddress("localhost", 1234);
58      HServerAddress hsa2 =
59        new HServerAddress(new InetSocketAddress("localhost", 1234));
60      assertTrue(hsa1.equals(hsa2));
61    }
62  
63    @Test
64    public void testHServerAddressHServerAddress() {
65      HServerAddress hsa1 = new HServerAddress("localhost", 1234);
66      HServerAddress hsa2 = new HServerAddress(hsa1);
67      assertEquals(hsa1, hsa2);
68    }
69  
70    @Test
71    public void testReadFields() throws IOException {
72      HServerAddress hsa1 = new HServerAddress("localhost", 1234);
73      HServerAddress hsa2 = new HServerAddress("localhost", 1235);
74      byte [] bytes = Writables.getBytes(hsa1);
75      HServerAddress deserialized =
76        (HServerAddress)Writables.getWritable(bytes, new HServerAddress());
77      assertEquals(hsa1, deserialized);
78      bytes = Writables.getBytes(hsa2);
79      deserialized =
80        (HServerAddress)Writables.getWritable(bytes, new HServerAddress());
81      assertNotSame(hsa1, deserialized);
82    }
83  }