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