1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
56 final int COUNT = 101;
57 assertTrue(doType(conf, COUNT, int.class).equals(COUNT));
58
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
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
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
78 obj = doType(conf, new Text(""), Text.class);
79 assertTrue(obj instanceof Text);
80
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
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
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 }