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
33 import junit.framework.TestCase;
34 import org.junit.experimental.categories.Category;
35
36 @Category(SmallTests.class)
37 public class TestTableSchemaModel extends TestCase {
38
39 public static final String TABLE_NAME = "testTable";
40 private static final boolean IS_META = false;
41 private static final boolean IS_ROOT = false;
42 private static final boolean READONLY = false;
43
44 private static final String AS_XML =
45 "<TableSchema name=\"testTable\"" +
46 " IS_META=\"false\"" +
47 " IS_ROOT=\"false\"" +
48 " READONLY=\"false\">" +
49 TestColumnSchemaModel.AS_XML +
50 "</TableSchema>";
51
52 private static final String AS_PB =
53 "Cgl0ZXN0VGFibGUSEAoHSVNfTUVUQRIFZmFsc2USEAoHSVNfUk9PVBIFZmFsc2USEQoIUkVBRE9O" +
54 "TFkSBWZhbHNlGpcBCgp0ZXN0Y29sdW1uEhIKCUJMT0NLU0laRRIFMTYzODQSEwoLQkxPT01GSUxU" +
55 "RVISBE5PTkUSEgoKQkxPQ0tDQUNIRRIEdHJ1ZRIRCgtDT01QUkVTU0lPThICR1oSDQoIVkVSU0lP" +
56 "TlMSATESDAoDVFRMEgU4NjQwMBISCglJTl9NRU1PUlkSBWZhbHNlGICjBSABKgJHWigA";
57
58 private JAXBContext context;
59
60 public TestTableSchemaModel() throws JAXBException {
61 super();
62 context = JAXBContext.newInstance(
63 ColumnSchemaModel.class,
64 TableSchemaModel.class);
65 }
66
67 public static TableSchemaModel buildTestModel() {
68 return buildTestModel(TABLE_NAME);
69 }
70
71 public static TableSchemaModel buildTestModel(String name) {
72 TableSchemaModel model = new TableSchemaModel();
73 model.setName(name);
74 model.__setIsMeta(IS_META);
75 model.__setIsRoot(IS_ROOT);
76 model.__setReadOnly(READONLY);
77 model.addColumnFamily(TestColumnSchemaModel.buildTestModel());
78 return model;
79 }
80
81 @SuppressWarnings("unused")
82 private String toXML(TableSchemaModel model) throws JAXBException {
83 StringWriter writer = new StringWriter();
84 context.createMarshaller().marshal(model, writer);
85 return writer.toString();
86 }
87
88 private TableSchemaModel fromXML(String xml) throws JAXBException {
89 return (TableSchemaModel)
90 context.createUnmarshaller().unmarshal(new StringReader(xml));
91 }
92
93 @SuppressWarnings("unused")
94 private byte[] toPB(TableSchemaModel model) {
95 return model.createProtobufOutput();
96 }
97
98 private TableSchemaModel fromPB(String pb) throws IOException {
99 return (TableSchemaModel)
100 new TableSchemaModel().getObjectFromMessage(Base64.decode(AS_PB));
101 }
102
103 public static void checkModel(TableSchemaModel model) {
104 checkModel(model, TABLE_NAME);
105 }
106
107 public static void checkModel(TableSchemaModel model, String tableName) {
108 assertEquals(model.getName(), tableName);
109 assertEquals(model.__getIsMeta(), IS_META);
110 assertEquals(model.__getIsRoot(), IS_ROOT);
111 assertEquals(model.__getReadOnly(), READONLY);
112 Iterator<ColumnSchemaModel> families = model.getColumns().iterator();
113 assertTrue(families.hasNext());
114 ColumnSchemaModel family = families.next();
115 TestColumnSchemaModel.checkModel(family);
116 assertFalse(families.hasNext());
117 }
118
119 public void testBuildModel() throws Exception {
120 checkModel(buildTestModel());
121 }
122
123 public void testFromXML() throws Exception {
124 checkModel(fromXML(AS_XML));
125 }
126
127 public void testFromPB() throws Exception {
128 checkModel(fromPB(AS_PB));
129 }
130
131 }
132