1   /**
2    * Copyright 2009 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 java.io.IOException;
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  import junit.framework.TestCase;
27  
28  import org.apache.hadoop.hbase.util.Bytes;
29  import org.apache.hadoop.hbase.util.Writables;
30  
31  public class TestHMsg extends TestCase {
32    public void testList() {
33      List<HMsg> msgs = new ArrayList<HMsg>();
34      HMsg hmsg = null;
35      final int size = 10;
36      for (int i = 0; i < size; i++) {
37        byte [] b = Bytes.toBytes(i);
38        hmsg = new HMsg(HMsg.Type.STOP_REGIONSERVER,
39          new HRegionInfo(new HTableDescriptor(Bytes.toBytes("test")), b, b));
40        msgs.add(hmsg);
41      }
42      assertEquals(size, msgs.size());
43      int index = msgs.indexOf(hmsg);
44      assertNotSame(-1, index);
45      msgs.remove(index);
46      assertEquals(size - 1, msgs.size());
47      byte [] other = Bytes.toBytes("other");
48      hmsg = new HMsg(HMsg.Type.STOP_REGIONSERVER,
49        new HRegionInfo(new HTableDescriptor(Bytes.toBytes("test")), other, other));
50      assertEquals(-1, msgs.indexOf(hmsg));
51      // Assert that two HMsgs are same if same content.
52      byte [] b = Bytes.toBytes(1);
53      hmsg = new HMsg(HMsg.Type.STOP_REGIONSERVER,
54       new HRegionInfo(new HTableDescriptor(Bytes.toBytes("test")), b, b));
55      assertNotSame(-1, msgs.indexOf(hmsg));
56    }
57  
58    public void testSerialization() throws IOException {
59      // Check out new HMsg that carries two daughter split regions.
60      byte [] abytes = Bytes.toBytes("a");
61      byte [] bbytes = Bytes.toBytes("b");
62      byte [] parentbytes = Bytes.toBytes("parent");
63      HRegionInfo parent =
64        new HRegionInfo(new HTableDescriptor(Bytes.toBytes("parent")),
65        parentbytes, parentbytes);
66      // Assert simple HMsg serializes
67      HMsg hmsg = new HMsg(HMsg.Type.STOP_REGIONSERVER, parent);
68      byte [] bytes = Writables.getBytes(hmsg);
69      HMsg close = (HMsg)Writables.getWritable(bytes, new HMsg());
70      assertTrue(close.equals(hmsg));
71      // Assert split serializes
72      HRegionInfo daughtera =
73        new HRegionInfo(new HTableDescriptor(Bytes.toBytes("a")), abytes, abytes);
74      HRegionInfo daughterb =
75        new HRegionInfo(new HTableDescriptor(Bytes.toBytes("b")), bbytes, bbytes);
76      HMsg splithmsg = new HMsg(HMsg.Type.REGION_SPLIT,
77        parent, daughtera, daughterb, Bytes.toBytes("REGION_SPLIT"));
78      bytes = Writables.getBytes(splithmsg);
79      hmsg = (HMsg)Writables.getWritable(bytes, new HMsg());
80      assertTrue(splithmsg.equals(hmsg));
81    }
82  }