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  import java.util.Iterator;
26  
27  import javax.xml.bind.JAXBContext;
28  import javax.xml.bind.JAXBException;
29  
30  import org.apache.hadoop.hbase.SmallTests;
31  import org.apache.hadoop.hbase.util.Base64;
32  
33  import junit.framework.TestCase;
34  import org.junit.experimental.categories.Category;
35  
36  @Category(SmallTests.class)
37  public class TestTableListModel extends TestCase {
38    private static final String TABLE1 = "table1";
39    private static final String TABLE2 = "table2";
40    private static final String TABLE3 = "table3";
41    
42    private static final String AS_XML =
43      "<TableList><table name=\"table1\"/><table name=\"table2\"/>" +
44        "<table name=\"table3\"/></TableList>";
45  
46    private static final String AS_PB = "CgZ0YWJsZTEKBnRhYmxlMgoGdGFibGUz";
47  
48    private JAXBContext context;
49  
50    public TestTableListModel() throws JAXBException {
51      super();
52      context = JAXBContext.newInstance(
53          TableListModel.class,
54          TableModel.class);
55    }
56  
57    private TableListModel buildTestModel() {
58      TableListModel model = new TableListModel();
59      model.add(new TableModel(TABLE1));
60      model.add(new TableModel(TABLE2));
61      model.add(new TableModel(TABLE3));
62      return model;
63    }
64  
65    @SuppressWarnings("unused")
66    private String toXML(TableListModel model) throws JAXBException {
67      StringWriter writer = new StringWriter();
68      context.createMarshaller().marshal(model, writer);
69      return writer.toString();
70    }
71  
72    private TableListModel fromXML(String xml) throws JAXBException {
73      return (TableListModel)
74        context.createUnmarshaller().unmarshal(new StringReader(xml));
75    }
76  
77    @SuppressWarnings("unused")
78    private byte[] toPB(TableListModel model) {
79      return model.createProtobufOutput();
80    }
81  
82    private TableListModel fromPB(String pb) throws IOException {
83      return (TableListModel) 
84        new TableListModel().getObjectFromMessage(Base64.decode(AS_PB));
85    }
86  
87    private void checkModel(TableListModel model) {
88      Iterator<TableModel> tables = model.getTables().iterator();
89      TableModel table = tables.next();
90      assertEquals(table.getName(), TABLE1);
91      table = tables.next();
92      assertEquals(table.getName(), TABLE2);
93      table = tables.next();
94      assertEquals(table.getName(), TABLE3);
95      assertFalse(tables.hasNext());
96    }
97  
98    public void testBuildModel() throws Exception {
99      checkModel(buildTestModel());
100   }
101 
102   public void testFromXML() throws Exception {
103     checkModel(fromXML(AS_XML));
104   }
105 
106   public void testFromPB() throws Exception {
107     checkModel(fromPB(AS_PB));
108   }
109 
110 }
111