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 TestModelBase<TableRegionModel> {
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    public TestTableRegionModel() throws Exception {
43      super(TableRegionModel.class);
44  
45      AS_XML =
46        "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><Region endKey=\"enp5eng=\" " +
47            "id=\"8731042424\" location=\"testhost:9876\" " +
48            "name=\"testtable,abracadbra,8731042424.ad9860f031282c46ed431d7af8f94aca.\" " +
49            "startKey=\"YWJyYWNhZGJyYQ==\"/>";
50  
51      AS_JSON =
52        "{\"endKey\":\"enp5eng=\",\"id\":8731042424,\"location\":\"testhost:9876\"," +
53            "\"name\":\"testtable,abracadbra,8731042424.ad9860f031282c46ed431d7af8f94aca.\",\"" +
54            "startKey\":\"YWJyYWNhZGJyYQ==\"}";
55    }
56  
57    protected TableRegionModel buildTestModel() {
58      TableRegionModel model =
59        new TableRegionModel(TABLE, ID, START_KEY, END_KEY, LOCATION);
60      return model;
61    }
62  
63    protected void checkModel(TableRegionModel model) {
64      assertTrue(Bytes.equals(model.getStartKey(), START_KEY));
65      assertTrue(Bytes.equals(model.getEndKey(), END_KEY));
66      assertEquals(model.getId(), ID);
67      assertEquals(model.getLocation(), LOCATION);
68      assertEquals(model.getName(), 
69        TABLE + "," + Bytes.toString(START_KEY) + "," + Long.toString(ID) +
70        ".ad9860f031282c46ed431d7af8f94aca.");
71    }
72  
73    public void testGetName() {
74      TableRegionModel model = buildTestModel();
75      String modelName = model.getName();
76      HRegionInfo hri = new HRegionInfo(TableName.valueOf(TABLE),
77        START_KEY, END_KEY, false, ID);
78      assertEquals(modelName, hri.getRegionNameAsString());
79    }
80  
81    public void testSetName() {
82      TableRegionModel model = buildTestModel();
83      String name = model.getName();
84      model.setName(name);
85      assertEquals(name, model.getName());
86    }
87  
88    @Override
89    public void testFromPB() throws Exception {
90      //no pb ignore
91    }
92  }
93