source: sample/hadoop-0.16/tw/org/nchc/tuple/HashMapWritableTest.java @ 21

Last change on this file since 21 was 21, checked in by waue, 16 years ago

hadoop 0.16

File size: 4.9 KB
Line 
1/*
2 * Cloud9: A MapReduce Library for Hadoop
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you
5 * may not use this file except in compliance with the License. You may
6 * obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13 * implied. See the License for the specific language governing
14 * permissions and limitations under the License.
15 */
16
17package tw.org.nchc.tuple;
18
19import static org.junit.Assert.assertEquals;
20import static org.junit.Assert.assertTrue;
21
22import java.io.ByteArrayInputStream;
23import java.io.ByteArrayOutputStream;
24import java.io.DataInputStream;
25import java.io.DataOutputStream;
26import java.io.IOException;
27
28import junit.framework.JUnit4TestAdapter;
29
30import org.apache.hadoop.io.FloatWritable;
31import org.apache.hadoop.io.IntWritable;
32import org.apache.hadoop.io.LongWritable;
33import org.apache.hadoop.io.Text;
34import org.apache.hadoop.io.Writable;
35import org.junit.Test;
36
37
38public class HashMapWritableTest {
39
40  @Test
41  public void testBasic() throws IOException {
42    HashMapWritable<Text, IntWritable> map = new HashMapWritable<Text, IntWritable>();
43
44    map.put(new Text("hi"), new IntWritable(5));
45    map.put(new Text("there"), new IntWritable(22));
46
47    Text key;
48    IntWritable value;
49
50    assertEquals(map.size(), 2);
51   
52    key=new Text("hi");
53    value=map.get(key);
54    assertTrue(value!=null);
55    assertEquals(value.get(), 5);
56   
57    value=map.remove(key);
58    assertEquals(map.size(), 1);
59   
60    key=new Text("there");
61    value=map.get(key);
62    assertTrue(value!=null);
63    assertEquals(value.get(), 22);
64  }
65
66  @Test
67  public void testSerialize1() throws IOException {
68    HashMapWritable<Text, IntWritable> origMap = new HashMapWritable<Text, IntWritable>();
69
70    origMap.put(new Text("hi"), new IntWritable(5));
71    origMap.put(new Text("there"), new IntWritable(22));
72 
73    ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
74    DataOutputStream dataOut = new DataOutputStream(bytesOut);
75
76    origMap.write(dataOut);
77
78    HashMapWritable<Text, IntWritable> map = new HashMapWritable<Text, IntWritable>();
79
80    map.readFields(new DataInputStream(new ByteArrayInputStream(
81        bytesOut.toByteArray())));
82
83    Text key;
84    IntWritable value;
85
86    assertEquals(map.size(), 2);
87   
88    key=new Text("hi");
89    value=map.get(key);
90    assertTrue(value!=null);
91    assertEquals(value.get(), 5);
92   
93    value=map.remove(key);
94    assertEquals(map.size(), 1);
95   
96    key=new Text("there");
97    value=map.get(key);
98    assertTrue(value!=null);
99    assertEquals(value.get(), 22);
100  }
101
102  @Test
103  public void testSerialize2() throws IOException {
104    HashMapWritable<Text, LongWritable> origMap = new HashMapWritable<Text, LongWritable>();
105
106    origMap.put(new Text("hi"), new LongWritable(52));
107    origMap.put(new Text("there"), new LongWritable(77));
108 
109    ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
110    DataOutputStream dataOut = new DataOutputStream(bytesOut);
111
112    origMap.write(dataOut);
113
114    HashMapWritable<Text, LongWritable> map = new HashMapWritable<Text, LongWritable>();
115
116    map.readFields(new DataInputStream(new ByteArrayInputStream(
117        bytesOut.toByteArray())));
118
119    Text key;
120    LongWritable value;
121
122    assertEquals(map.size(), 2);
123   
124    key=new Text("hi");
125    value=map.get(key);
126    assertTrue(value!=null);
127    assertEquals(value.get(), 52);
128   
129    value=map.remove(key);
130    assertEquals(map.size(), 1);
131   
132    key=new Text("there");
133    value=map.get(key);
134    assertTrue(value!=null);
135    assertEquals(value.get(), 77);
136  }
137
138
139  @Test
140  public void testTypeSafety() throws IOException {
141    HashMapWritable<Writable, Writable> origMap = new HashMapWritable<Writable, Writable>();
142
143    origMap.put(new Text("hi"), new FloatWritable(5.3f));
144    origMap.put(new Text("there"), new Text("bbb"));
145 
146    ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
147    DataOutputStream dataOut = new DataOutputStream(bytesOut);
148
149    origMap.write(dataOut);
150
151    HashMapWritable<Writable, Writable> map = new HashMapWritable<Writable, Writable>();
152
153    try {
154          map.readFields(new DataInputStream(new ByteArrayInputStream(
155              bytesOut.toByteArray())));
156          assertTrue(false);
157        } catch (Exception e) {
158        }
159  }
160
161
162
163  @Test
164  public void testSerializeEmpty() throws IOException {
165    HashMapWritable<IntWritable, Text> map = new HashMapWritable<IntWritable, Text>();
166   
167    assertTrue(map.size() == 0);
168   
169    ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
170    DataOutputStream dataOut = new DataOutputStream(bytesOut);
171
172    map.write(dataOut);
173
174    HashMapWritable<IntWritable, Text> newList = new HashMapWritable<IntWritable, Text>();
175    newList.readFields(new DataInputStream(new ByteArrayInputStream(
176        bytesOut.toByteArray())));
177    assertTrue(newList.size() == 0);
178  }
179 
180  public static junit.framework.Test suite() {
181    return new JUnit4TestAdapter(HashMapWritableTest.class);
182  }
183
184}
Note: See TracBrowser for help on using the repository browser.