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.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.util.Bytes;
31
32 import junit.framework.TestCase;
33
34 public class TestRowModel extends TestCase {
35
36 private static final byte[] ROW1 = Bytes.toBytes("testrow1");
37 private static final byte[] COLUMN1 = Bytes.toBytes("testcolumn1");
38 private static final byte[] VALUE1 = Bytes.toBytes("testvalue1");
39 private static final long TIMESTAMP1 = 1245219839331L;
40
41 private static final String AS_XML =
42 "<Row key=\"dGVzdHJvdzE=\">" +
43 "<Cell timestamp=\"1245219839331\" column=\"dGVzdGNvbHVtbjE=\">" +
44 "dGVzdHZhbHVlMQ==</Cell>" +
45 "</Row>";
46
47 private JAXBContext context;
48
49 public TestRowModel() throws JAXBException {
50 super();
51 context = JAXBContext.newInstance(
52 CellModel.class,
53 RowModel.class);
54 }
55
56 private RowModel buildTestModel() {
57 RowModel model = new RowModel();
58 model.setKey(ROW1);
59 model.addCell(new CellModel(COLUMN1, TIMESTAMP1, VALUE1));
60 return model;
61 }
62
63 @SuppressWarnings("unused")
64 private String toXML(RowModel model) throws JAXBException {
65 StringWriter writer = new StringWriter();
66 context.createMarshaller().marshal(model, writer);
67 return writer.toString();
68 }
69
70 private RowModel fromXML(String xml) throws JAXBException {
71 return (RowModel)
72 context.createUnmarshaller().unmarshal(new StringReader(xml));
73 }
74
75 private void checkModel(RowModel model) {
76 assertTrue(Bytes.equals(ROW1, model.getKey()));
77 Iterator<CellModel> cells = model.getCells().iterator();
78 CellModel cell = cells.next();
79 assertTrue(Bytes.equals(COLUMN1, cell.getColumn()));
80 assertTrue(Bytes.equals(VALUE1, cell.getValue()));
81 assertTrue(cell.hasUserTimestamp());
82 assertEquals(cell.getTimestamp(), TIMESTAMP1);
83 assertFalse(cells.hasNext());
84 }
85
86 public void testBuildModel() throws Exception {
87 checkModel(buildTestModel());
88 }
89
90 public void testFromXML() throws Exception {
91 checkModel(fromXML(AS_XML));
92 }
93 }