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  
31  public class TestColumnSchemaModel extends TestCase {
32  
33    protected static final String COLUMN_NAME = "testcolumn";
34    protected static final boolean BLOCKCACHE = true;
35    protected static final int BLOCKSIZE = 16384;
36    protected static final String BLOOMFILTER = "NONE";
37    protected static final String COMPRESSION = "GZ";
38    protected static final boolean IN_MEMORY = false;
39    protected static final int TTL = 86400;
40    protected static final int VERSIONS = 1;
41  
42    protected static final String AS_XML =
43      "<ColumnSchema name=\"testcolumn\"" +
44        " BLOCKSIZE=\"16384\"" +
45        " BLOOMFILTER=\"NONE\"" +
46        " BLOCKCACHE=\"true\"" +
47        " COMPRESSION=\"GZ\"" +
48        " VERSIONS=\"1\"" +
49        " TTL=\"86400\"" +
50        " IN_MEMORY=\"false\"/>";
51  
52    private JAXBContext context;
53  
54    public TestColumnSchemaModel() throws JAXBException {
55      super();
56      context = JAXBContext.newInstance(ColumnSchemaModel.class);
57    }
58  
59    protected static ColumnSchemaModel buildTestModel() {
60      ColumnSchemaModel model = new ColumnSchemaModel();
61      model.setName(COLUMN_NAME);
62      model.__setBlockcache(BLOCKCACHE);
63      model.__setBlocksize(BLOCKSIZE);
64      model.__setBloomfilter(BLOOMFILTER);
65      model.__setCompression(COMPRESSION);
66      model.__setInMemory(IN_MEMORY);
67      model.__setTTL(TTL);
68      model.__setVersions(VERSIONS);
69      return model;
70    }
71  
72    @SuppressWarnings("unused")
73    private String toXML(ColumnSchemaModel model) throws JAXBException {
74      StringWriter writer = new StringWriter();
75      context.createMarshaller().marshal(model, writer);
76      return writer.toString();
77    }
78  
79    private ColumnSchemaModel fromXML(String xml) throws JAXBException {
80      return (ColumnSchemaModel)
81        context.createUnmarshaller().unmarshal(new StringReader(xml));
82    }
83  
84    protected static void checkModel(ColumnSchemaModel model) {
85      assertEquals(model.getName(), COLUMN_NAME);
86      assertEquals(model.__getBlockcache(), BLOCKCACHE);
87      assertEquals(model.__getBlocksize(), BLOCKSIZE);
88      assertEquals(model.__getBloomfilter(), BLOOMFILTER);
89      assertTrue(model.__getCompression().equalsIgnoreCase(COMPRESSION));
90      assertEquals(model.__getInMemory(), IN_MEMORY);
91      assertEquals(model.__getTTL(), TTL);
92      assertEquals(model.__getVersions(), VERSIONS);
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 }