1   /**
2    * Copyright 2007 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  package org.apache.hadoop.hbase.io;
21  
22  import java.io.ByteArrayInputStream;
23  import java.io.ByteArrayOutputStream;
24  import java.io.DataInputStream;
25  import java.io.DataOutputStream;
26  import java.io.File;
27  import java.io.IOException;
28  import java.util.ArrayList;
29  import java.util.List;
30  
31  import junit.framework.TestCase;
32  
33  import org.apache.hadoop.hbase.HBaseConfiguration;
34  import org.apache.hadoop.hbase.HConstants;
35  import org.apache.hadoop.hbase.filter.PrefixFilter;
36  import org.apache.hadoop.io.Text;
37  import org.apache.hadoop.io.WritableComparator;
38  import org.junit.Assert;
39  
40  public class TestHbaseObjectWritable extends TestCase {
41  
42    @Override
43    protected void setUp() throws Exception {
44      super.setUp();
45    }
46  
47    @Override
48    protected void tearDown() throws Exception {
49      super.tearDown();
50    }
51  
52    @SuppressWarnings("boxing")
53    public void testReadObjectDataInputConfiguration() throws IOException {
54      HBaseConfiguration conf = new HBaseConfiguration();
55      // Do primitive type
56      final int COUNT = 101;
57      assertTrue(doType(conf, COUNT, int.class).equals(COUNT));
58      // Do array
59      final byte [] testing = "testing".getBytes();
60      byte [] result = (byte [])doType(conf, testing, testing.getClass());
61      assertTrue(WritableComparator.compareBytes(testing, 0, testing.length,
62         result, 0, result.length) == 0);
63      // Do unsupported type.
64      boolean exception = false;
65      try {
66        doType(conf, new File("a"), File.class);
67      } catch (UnsupportedOperationException uoe) {
68        exception = true;
69      }
70      assertTrue(exception);
71      // Try odd types
72      final byte A = 'A';
73      byte [] bytes = new byte[1];
74      bytes[0] = A;
75      Object obj = doType(conf, bytes, byte [].class);
76      assertTrue(((byte [])obj)[0] == A);
77      // Do 'known' Writable type.
78      obj = doType(conf, new Text(""), Text.class);
79      assertTrue(obj instanceof Text);
80      //List.class
81      List<String> list = new ArrayList<String>();
82      list.add("hello");
83      list.add("world");
84      list.add("universe");
85      obj = doType(conf, list, List.class);
86      assertTrue(obj instanceof List);
87      Assert.assertArrayEquals(list.toArray(), ((List)obj).toArray() );
88      //ArrayList.class
89      ArrayList<String> arr = new ArrayList<String>();
90      arr.add("hello");
91      arr.add("world");
92      arr.add("universe");
93      obj = doType(conf,  arr, ArrayList.class);
94      assertTrue(obj instanceof ArrayList);
95      Assert.assertArrayEquals(list.toArray(), ((ArrayList)obj).toArray() );
96      // Check that filters can be serialized
97      obj = doType(conf, new PrefixFilter(HConstants.EMPTY_BYTE_ARRAY),
98        PrefixFilter.class);
99      assertTrue(obj instanceof PrefixFilter);
100   }
101 
102   private Object doType(final HBaseConfiguration conf, final Object value,
103       final Class<?> clazz)
104   throws IOException {
105     ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
106     DataOutputStream out = new DataOutputStream(byteStream);
107     HbaseObjectWritable.writeObject(out, value, clazz, conf);
108     out.close();
109     ByteArrayInputStream bais =
110       new ByteArrayInputStream(byteStream.toByteArray());
111     DataInputStream dis = new DataInputStream(bais);
112     Object product = HbaseObjectWritable.readObject(dis, conf);
113     dis.close();
114     return product;
115   }
116 
117 }