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 TestScannerModel extends TestCase {
38    private static final byte[] START_ROW = Bytes.toBytes("abracadabra");
39    private static final byte[] END_ROW = Bytes.toBytes("zzyzx");
40    private static final byte[] COLUMN1 = Bytes.toBytes("column1");
41    private static final byte[] COLUMN2 = Bytes.toBytes("column2:foo");
42    private static final long START_TIME = 1245219839331L;
43    private static final long END_TIME = 1245393318192L;
44    private static final int CACHING = 1000;
45    private static final int BATCH = 100;
46  
47    private static final String AS_XML =
48      "<Scanner startTime=\"1245219839331\"" +
49        " startRow=\"YWJyYWNhZGFicmE=\"" +
50        " endTime=\"1245393318192\"" +
51        " endRow=\"enp5eng=\"" +
52        " batch=\"100\"" +
53        " caching=\"1000\">" +
54          "<column>Y29sdW1uMQ==</column>" +
55          "<column>Y29sdW1uMjpmb28=</column>" +
56        "</Scanner>";
57  
58    private static final String AS_PB =
59      "CgthYnJhY2FkYWJyYRIFenp5engaB2NvbHVtbjEaC2NvbHVtbjI6Zm9" +
60      "vIGQo47qL554kMLDi57mfJDj/////B0joBw==";
61  
62    private JAXBContext context;
63  
64    public TestScannerModel() throws JAXBException {
65      super();
66      context = JAXBContext.newInstance(ScannerModel.class);
67    }
68  
69    private ScannerModel buildTestModel() {
70      ScannerModel model = new ScannerModel();
71      model.setStartRow(START_ROW);
72      model.setEndRow(END_ROW);
73      model.addColumn(COLUMN1);
74      model.addColumn(COLUMN2);
75      model.setStartTime(START_TIME);
76      model.setEndTime(END_TIME);
77      model.setBatch(BATCH);
78      model.setCaching(CACHING);
79      return model;
80    }
81  
82    @SuppressWarnings("unused")
83    private String toXML(ScannerModel model) throws JAXBException {
84      StringWriter writer = new StringWriter();
85      context.createMarshaller().marshal(model, writer);
86      return writer.toString();
87    }
88  
89    private ScannerModel fromXML(String xml) throws JAXBException {
90      return (ScannerModel)
91        context.createUnmarshaller().unmarshal(new StringReader(xml));
92    }
93  
94    @SuppressWarnings("unused")
95    private byte[] toPB(ScannerModel model) {
96      return model.createProtobufOutput();
97    }
98  
99    private ScannerModel fromPB(String pb) throws IOException {
100     return (ScannerModel) 
101       new ScannerModel().getObjectFromMessage(Base64.decode(AS_PB));
102   }
103 
104   private void checkModel(ScannerModel model) {
105     assertTrue(Bytes.equals(model.getStartRow(), START_ROW));
106     assertTrue(Bytes.equals(model.getEndRow(), END_ROW));
107     boolean foundCol1 = false, foundCol2 = false;
108     for (byte[] column: model.getColumns()) {
109       if (Bytes.equals(column, COLUMN1)) {
110         foundCol1 = true;
111       } else if (Bytes.equals(column, COLUMN2)) {
112         foundCol2 = true;
113       }
114     }
115     assertTrue(foundCol1);
116     assertTrue(foundCol2);
117     assertEquals(model.getStartTime(), START_TIME);
118     assertEquals(model.getEndTime(), END_TIME);
119     assertEquals(model.getBatch(), BATCH);
120     assertEquals(model.getCaching(), CACHING);
121   }
122 
123   public void testBuildModel() throws Exception {
124     checkModel(buildTestModel());
125   }
126 
127   public void testFromXML() throws Exception {
128     checkModel(fromXML(AS_XML));
129   }
130 
131   public void testFromPB() throws Exception {
132     checkModel(fromPB(AS_PB));
133   }
134 }
135