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