1   /*
2    * Copyright 2010 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  
21  package org.apache.hadoop.hbase.rest.model;
22  
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 junit.framework.TestCase;
30  import org.apache.hadoop.hbase.SmallTests;
31  import org.junit.experimental.categories.Category;
32  
33  @Category(SmallTests.class)
34  public class TestColumnSchemaModel extends TestCase {
35  
36    protected static final String COLUMN_NAME = "testcolumn";
37    protected static final boolean BLOCKCACHE = true;
38    protected static final int BLOCKSIZE = 16384;
39    protected static final String BLOOMFILTER = "NONE";
40    protected static final String COMPRESSION = "GZ";
41    protected static final boolean IN_MEMORY = false;
42    protected static final int TTL = 86400;
43    protected static final int VERSIONS = 1;
44  
45    protected static final String AS_XML =
46      "<ColumnSchema name=\"testcolumn\"" +
47        " BLOCKSIZE=\"16384\"" +
48        " BLOOMFILTER=\"NONE\"" +
49        " BLOCKCACHE=\"true\"" +
50        " COMPRESSION=\"GZ\"" +
51        " VERSIONS=\"1\"" +
52        " TTL=\"86400\"" +
53        " IN_MEMORY=\"false\"/>";
54  
55    private JAXBContext context;
56  
57    public TestColumnSchemaModel() throws JAXBException {
58      super();
59      context = JAXBContext.newInstance(ColumnSchemaModel.class);
60    }
61  
62    protected static ColumnSchemaModel buildTestModel() {
63      ColumnSchemaModel model = new ColumnSchemaModel();
64      model.setName(COLUMN_NAME);
65      model.__setBlockcache(BLOCKCACHE);
66      model.__setBlocksize(BLOCKSIZE);
67      model.__setBloomfilter(BLOOMFILTER);
68      model.__setCompression(COMPRESSION);
69      model.__setInMemory(IN_MEMORY);
70      model.__setTTL(TTL);
71      model.__setVersions(VERSIONS);
72      return model;
73    }
74  
75    @SuppressWarnings("unused")
76    private String toXML(ColumnSchemaModel model) throws JAXBException {
77      StringWriter writer = new StringWriter();
78      context.createMarshaller().marshal(model, writer);
79      return writer.toString();
80    }
81  
82    private ColumnSchemaModel fromXML(String xml) throws JAXBException {
83      return (ColumnSchemaModel)
84        context.createUnmarshaller().unmarshal(new StringReader(xml));
85    }
86  
87    protected static void checkModel(ColumnSchemaModel model) {
88      assertEquals(model.getName(), COLUMN_NAME);
89      assertEquals(model.__getBlockcache(), BLOCKCACHE);
90      assertEquals(model.__getBlocksize(), BLOCKSIZE);
91      assertEquals(model.__getBloomfilter(), BLOOMFILTER);
92      assertTrue(model.__getCompression().equalsIgnoreCase(COMPRESSION));
93      assertEquals(model.__getInMemory(), IN_MEMORY);
94      assertEquals(model.__getTTL(), TTL);
95      assertEquals(model.__getVersions(), VERSIONS);
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   @org.junit.Rule
107   public org.apache.hadoop.hbase.ResourceCheckerJUnitRule cu =
108     new org.apache.hadoop.hbase.ResourceCheckerJUnitRule();
109 }
110