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
26 import javax.xml.bind.JAXBContext;
27 import javax.xml.bind.JAXBException;
28
29 import org.apache.hadoop.hbase.SmallTests;
30 import org.apache.hadoop.hbase.util.Base64;
31 import org.apache.hadoop.hbase.util.Bytes;
32
33 import junit.framework.TestCase;
34 import org.junit.experimental.categories.Category;
35
36 @Category(SmallTests.class)
37 public class TestCellModel extends TestCase {
38
39 private static final long TIMESTAMP = 1245219839331L;
40 private static final byte[] COLUMN = Bytes.toBytes("testcolumn");
41 private static final byte[] VALUE = Bytes.toBytes("testvalue");
42
43 private static final String AS_XML =
44 "<Cell timestamp=\"1245219839331\"" +
45 " column=\"dGVzdGNvbHVtbg==\">" +
46 "dGVzdHZhbHVl</Cell>";
47
48 private static final String AS_PB =
49 "Egp0ZXN0Y29sdW1uGOO6i+eeJCIJdGVzdHZhbHVl";
50
51 private JAXBContext context;
52
53 public TestCellModel() throws JAXBException {
54 super();
55 context = JAXBContext.newInstance(CellModel.class);
56 }
57
58 private CellModel buildTestModel() {
59 CellModel model = new CellModel();
60 model.setColumn(COLUMN);
61 model.setTimestamp(TIMESTAMP);
62 model.setValue(VALUE);
63 return model;
64 }
65
66 @SuppressWarnings("unused")
67 private String toXML(CellModel model) throws JAXBException {
68 StringWriter writer = new StringWriter();
69 context.createMarshaller().marshal(model, writer);
70 return writer.toString();
71 }
72
73 private CellModel fromXML(String xml) throws JAXBException {
74 return (CellModel)
75 context.createUnmarshaller().unmarshal(new StringReader(xml));
76 }
77
78 @SuppressWarnings("unused")
79 private byte[] toPB(CellModel model) {
80 return model.createProtobufOutput();
81 }
82
83 private CellModel fromPB(String pb) throws IOException {
84 return (CellModel)
85 new CellModel().getObjectFromMessage(Base64.decode(AS_PB));
86 }
87
88 private void checkModel(CellModel model) {
89 assertTrue(Bytes.equals(model.getColumn(), COLUMN));
90 assertTrue(Bytes.equals(model.getValue(), VALUE));
91 assertTrue(model.hasUserTimestamp());
92 assertEquals(model.getTimestamp(), TIMESTAMP);
93 }
94
95 public void testBuildModel() throws Exception {
96 checkModel(buildTestModel());
97 }
98
99 public void testFromXML() throws Exception {
100 checkModel(fromXML(AS_XML));
101 }
102
103 public void testFromPB() throws Exception {
104 checkModel(fromPB(AS_PB));
105 }
106
107 }
108