View Javadoc

1   /*
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  package org.apache.hadoop.hbase.rest.model;
21  
22  import java.io.StringReader;
23  import java.io.StringWriter;
24  
25  import javax.xml.bind.JAXBContext;
26  import javax.xml.bind.JAXBException;
27  
28  import org.apache.hadoop.hbase.*;
29  import org.apache.hadoop.hbase.util.Bytes;
30  
31  import junit.framework.TestCase;
32  import org.junit.experimental.categories.Category;
33  
34  @Category(SmallTests.class)
35  public class TestTableRegionModel extends TestCase {
36    private static final String TABLE = "testtable";
37    private static final byte[] START_KEY = Bytes.toBytes("abracadbra");
38    private static final byte[] END_KEY = Bytes.toBytes("zzyzx");
39    private static final long ID = 8731042424L;
40    private static final String LOCATION = "testhost:9876";
41  
42    private static final String AS_XML =
43      "<Region location=\"testhost:9876\"" +
44        " endKey=\"enp5eng=\"" +
45        " startKey=\"YWJyYWNhZGJyYQ==\"" +
46        " id=\"8731042424\"" +
47        " name=\"testtable,abracadbra,8731042424\"/>";
48  
49    private JAXBContext context;
50  
51    public TestTableRegionModel() throws JAXBException {
52      super();
53      context = JAXBContext.newInstance(TableRegionModel.class);
54    }
55  
56    private TableRegionModel buildTestModel() {
57      TableRegionModel model =
58        new TableRegionModel(TABLE, ID, START_KEY, END_KEY, LOCATION);
59      return model;
60    }
61  
62    @SuppressWarnings("unused")
63    private String toXML(TableRegionModel model) throws JAXBException {
64      StringWriter writer = new StringWriter();
65      context.createMarshaller().marshal(model, writer);
66      return writer.toString();
67    }
68  
69    private TableRegionModel fromXML(String xml) throws JAXBException {
70      return (TableRegionModel)
71        context.createUnmarshaller().unmarshal(new StringReader(xml));
72    }
73  
74    private void checkModel(TableRegionModel model) {
75      assertTrue(Bytes.equals(model.getStartKey(), START_KEY));
76      assertTrue(Bytes.equals(model.getEndKey(), END_KEY));
77      assertEquals(model.getId(), ID);
78      assertEquals(model.getLocation(), LOCATION);
79      assertEquals(model.getName(), 
80        TABLE + "," + Bytes.toString(START_KEY) + "," + Long.toString(ID) +
81        ".ad9860f031282c46ed431d7af8f94aca.");
82    }
83  
84    public void testBuildModel() throws Exception {
85      checkModel(buildTestModel());
86    }
87  
88    public void testGetName() {
89      TableRegionModel model = buildTestModel();
90      String modelName = model.getName();
91      HRegionInfo hri = new HRegionInfo(TableName.valueOf(TABLE),
92        START_KEY, END_KEY, false, ID);
93      assertEquals(modelName, hri.getRegionNameAsString());
94    }
95  
96    public void testSetName() {
97      TableRegionModel model = buildTestModel();
98      String name = model.getName();
99      model.setName(name);
100     assertEquals(name, model.getName());
101   }
102 
103   public void testFromXML() throws Exception {
104     checkModel(fromXML(AS_XML));
105   }
106 
107 }
108