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.StringReader;
23  import java.io.StringWriter;
24  
25  import javax.xml.bind.JAXBContext;
26  import javax.xml.bind.JAXBException;
27  
28  import junit.framework.TestCase;
29  import org.apache.hadoop.hbase.SmallTests;
30  import org.junit.experimental.categories.Category;
31  
32  @Category(SmallTests.class)
33  public class TestColumnSchemaModel extends TestCase {
34  
35    protected static final String COLUMN_NAME = "testcolumn";
36    protected static final boolean BLOCKCACHE = true;
37    protected static final int BLOCKSIZE = 16384;
38    protected static final String BLOOMFILTER = "NONE";
39    protected static final String COMPRESSION = "GZ";
40    protected static final boolean IN_MEMORY = false;
41    protected static final int TTL = 86400;
42    protected static final int VERSIONS = 1;
43  
44    protected static final String AS_XML =
45      "<ColumnSchema name=\"testcolumn\"" +
46        " BLOCKSIZE=\"16384\"" +
47        " BLOOMFILTER=\"NONE\"" +
48        " BLOCKCACHE=\"true\"" +
49        " COMPRESSION=\"GZ\"" +
50        " VERSIONS=\"1\"" +
51        " TTL=\"86400\"" +
52        " IN_MEMORY=\"false\"/>";
53  
54    private JAXBContext context;
55  
56    public TestColumnSchemaModel() throws JAXBException {
57      super();
58      context = JAXBContext.newInstance(ColumnSchemaModel.class);
59    }
60  
61    protected static ColumnSchemaModel buildTestModel() {
62      ColumnSchemaModel model = new ColumnSchemaModel();
63      model.setName(COLUMN_NAME);
64      model.__setBlockcache(BLOCKCACHE);
65      model.__setBlocksize(BLOCKSIZE);
66      model.__setBloomfilter(BLOOMFILTER);
67      model.__setCompression(COMPRESSION);
68      model.__setInMemory(IN_MEMORY);
69      model.__setTTL(TTL);
70      model.__setVersions(VERSIONS);
71      return model;
72    }
73  
74    @SuppressWarnings("unused")
75    private String toXML(ColumnSchemaModel model) throws JAXBException {
76      StringWriter writer = new StringWriter();
77      context.createMarshaller().marshal(model, writer);
78      return writer.toString();
79    }
80  
81    private ColumnSchemaModel fromXML(String xml) throws JAXBException {
82      return (ColumnSchemaModel)
83        context.createUnmarshaller().unmarshal(new StringReader(xml));
84    }
85  
86    protected static void checkModel(ColumnSchemaModel model) {
87      assertEquals(model.getName(), COLUMN_NAME);
88      assertEquals(model.__getBlockcache(), BLOCKCACHE);
89      assertEquals(model.__getBlocksize(), BLOCKSIZE);
90      assertEquals(model.__getBloomfilter(), BLOOMFILTER);
91      assertTrue(model.__getCompression().equalsIgnoreCase(COMPRESSION));
92      assertEquals(model.__getInMemory(), IN_MEMORY);
93      assertEquals(model.__getTTL(), TTL);
94      assertEquals(model.__getVersions(), VERSIONS);
95    }
96  
97    public void testBuildModel() throws Exception {
98      checkModel(buildTestModel());
99    }
100 
101   public void testFromXML() throws Exception {
102     checkModel(fromXML(AS_XML));
103   }
104 
105 }
106