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.util.Bytes;
30
31 import junit.framework.TestCase;
32
33 public class TestTableRegionModel extends TestCase {
34 private static final String TABLE = "testtable";
35 private static final byte[] START_KEY = Bytes.toBytes("abracadbra");
36 private static final byte[] END_KEY = Bytes.toBytes("zzyzx");
37 private static final long ID = 8731042424L;
38 private static final String LOCATION = "testhost:9876";
39
40 private static final String AS_XML =
41 "<Region location=\"testhost:9876\"" +
42 " endKey=\"enp5eng=\"" +
43 " startKey=\"YWJyYWNhZGJyYQ==\"" +
44 " id=\"8731042424\"" +
45 " name=\"testtable,abracadbra,8731042424\"/>";
46
47 private JAXBContext context;
48
49 public TestTableRegionModel() throws JAXBException {
50 super();
51 context = JAXBContext.newInstance(TableRegionModel.class);
52 }
53
54 private TableRegionModel buildTestModel() {
55 TableRegionModel model =
56 new TableRegionModel(TABLE, ID, START_KEY, END_KEY, LOCATION);
57 return model;
58 }
59
60 @SuppressWarnings("unused")
61 private String toXML(TableRegionModel model) throws JAXBException {
62 StringWriter writer = new StringWriter();
63 context.createMarshaller().marshal(model, writer);
64 return writer.toString();
65 }
66
67 private TableRegionModel fromXML(String xml) throws JAXBException {
68 return (TableRegionModel)
69 context.createUnmarshaller().unmarshal(new StringReader(xml));
70 }
71
72 private void checkModel(TableRegionModel model) {
73 assertTrue(Bytes.equals(model.getStartKey(), START_KEY));
74 assertTrue(Bytes.equals(model.getEndKey(), END_KEY));
75 assertEquals(model.getId(), ID);
76 assertEquals(model.getLocation(), LOCATION);
77 assertEquals(model.getName(),
78 TABLE + "," + Bytes.toString(START_KEY) + "," + Long.toString(ID));
79 }
80
81 public void testBuildModel() throws Exception {
82 checkModel(buildTestModel());
83 }
84
85 public void testFromXML() throws Exception {
86 checkModel(fromXML(AS_XML));
87 }
88 }