View Javadoc

1   /*
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
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