1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.hadoop.hbase.rest.model;
22
23 import java.io.StringReader;
24 import java.io.StringWriter;
25
26 import javax.xml.bind.JAXBContext;
27 import javax.xml.bind.JAXBException;
28
29 import org.apache.hadoop.hbase.*;
30 import org.apache.hadoop.hbase.util.Bytes;
31
32 import junit.framework.TestCase;
33 import org.junit.experimental.categories.Category;
34
35 @Category(SmallTests.class)
36 public class TestTableRegionModel extends TestCase {
37 private static final String TABLE = "testtable";
38 private static final byte[] START_KEY = Bytes.toBytes("abracadbra");
39 private static final byte[] END_KEY = Bytes.toBytes("zzyzx");
40 private static final long ID = 8731042424L;
41 private static final String LOCATION = "testhost:9876";
42
43 private static final String AS_XML =
44 "<Region location=\"testhost:9876\"" +
45 " endKey=\"enp5eng=\"" +
46 " startKey=\"YWJyYWNhZGJyYQ==\"" +
47 " id=\"8731042424\"" +
48 " name=\"testtable,abracadbra,8731042424\"/>";
49
50 private JAXBContext context;
51
52 public TestTableRegionModel() throws JAXBException {
53 super();
54 context = JAXBContext.newInstance(TableRegionModel.class);
55 }
56
57 private TableRegionModel buildTestModel() {
58 TableRegionModel model =
59 new TableRegionModel(TABLE, ID, START_KEY, END_KEY, LOCATION);
60 return model;
61 }
62
63 @SuppressWarnings("unused")
64 private String toXML(TableRegionModel model) throws JAXBException {
65 StringWriter writer = new StringWriter();
66 context.createMarshaller().marshal(model, writer);
67 return writer.toString();
68 }
69
70 private TableRegionModel fromXML(String xml) throws JAXBException {
71 return (TableRegionModel)
72 context.createUnmarshaller().unmarshal(new StringReader(xml));
73 }
74
75 private void checkModel(TableRegionModel model) {
76 assertTrue(Bytes.equals(model.getStartKey(), START_KEY));
77 assertTrue(Bytes.equals(model.getEndKey(), END_KEY));
78 assertEquals(model.getId(), ID);
79 assertEquals(model.getLocation(), LOCATION);
80 assertEquals(model.getName(),
81 TABLE + "," + Bytes.toString(START_KEY) + "," + Long.toString(ID) +
82 ".ad9860f031282c46ed431d7af8f94aca.");
83 }
84
85 public void testBuildModel() throws Exception {
86 checkModel(buildTestModel());
87 }
88
89 public void testGetName() {
90 TableRegionModel model = buildTestModel();
91 String modelName = model.getName();
92 HRegionInfo hri = new HRegionInfo(Bytes.toBytes(TABLE),
93 START_KEY, END_KEY, false, ID);
94 assertEquals(modelName, hri.getRegionNameAsString());
95 }
96
97 public void testSetName() {
98 TableRegionModel model = buildTestModel();
99 String name = model.getName();
100 model.setName(name);
101 assertEquals(name, model.getName());
102 }
103
104 public void testFromXML() throws Exception {
105 checkModel(fromXML(AS_XML));
106 }
107
108 @org.junit.Rule
109 public org.apache.hadoop.hbase.ResourceCheckerJUnitRule cu =
110 new org.apache.hadoop.hbase.ResourceCheckerJUnitRule();
111 }
112