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