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.io; |
---|
21 | |
---|
22 | import java.util.Map; |
---|
23 | |
---|
24 | import junit.framework.TestCase; |
---|
25 | |
---|
26 | /** |
---|
27 | * Tests SortedMapWritable |
---|
28 | */ |
---|
29 | public class TestSortedMapWritable extends TestCase { |
---|
30 | /** the test */ |
---|
31 | @SuppressWarnings("unchecked") |
---|
32 | public void testSortedMapWritable() { |
---|
33 | Text[] keys = { |
---|
34 | new Text("key1"), |
---|
35 | new Text("key2"), |
---|
36 | new Text("key3"), |
---|
37 | }; |
---|
38 | |
---|
39 | BytesWritable[] values = { |
---|
40 | new BytesWritable("value1".getBytes()), |
---|
41 | new BytesWritable("value2".getBytes()), |
---|
42 | new BytesWritable("value3".getBytes()) |
---|
43 | }; |
---|
44 | |
---|
45 | SortedMapWritable inMap = new SortedMapWritable(); |
---|
46 | for (int i = 0; i < keys.length; i++) { |
---|
47 | inMap.put(keys[i], values[i]); |
---|
48 | } |
---|
49 | |
---|
50 | assertEquals(0, inMap.firstKey().compareTo(keys[0])); |
---|
51 | assertEquals(0, inMap.lastKey().compareTo(keys[2])); |
---|
52 | |
---|
53 | SortedMapWritable outMap = new SortedMapWritable(inMap); |
---|
54 | assertEquals(inMap.size(), outMap.size()); |
---|
55 | |
---|
56 | for (Map.Entry<WritableComparable, Writable> e: inMap.entrySet()) { |
---|
57 | assertTrue(outMap.containsKey(e.getKey())); |
---|
58 | assertEquals(0, ((WritableComparable) outMap.get(e.getKey())).compareTo( |
---|
59 | e.getValue())); |
---|
60 | } |
---|
61 | |
---|
62 | // Now for something a little harder... |
---|
63 | |
---|
64 | Text[] maps = { |
---|
65 | new Text("map1"), |
---|
66 | new Text("map2") |
---|
67 | }; |
---|
68 | |
---|
69 | SortedMapWritable mapOfMaps = new SortedMapWritable(); |
---|
70 | mapOfMaps.put(maps[0], inMap); |
---|
71 | mapOfMaps.put(maps[1], outMap); |
---|
72 | |
---|
73 | SortedMapWritable copyOfMapOfMaps = new SortedMapWritable(mapOfMaps); |
---|
74 | for (int i = 0; i < maps.length; i++) { |
---|
75 | assertTrue(copyOfMapOfMaps.containsKey(maps[i])); |
---|
76 | |
---|
77 | SortedMapWritable a = (SortedMapWritable) mapOfMaps.get(maps[i]); |
---|
78 | SortedMapWritable b = (SortedMapWritable) copyOfMapOfMaps.get(maps[i]); |
---|
79 | assertEquals(a.size(), b.size()); |
---|
80 | for (Writable key: a.keySet()) { |
---|
81 | assertTrue(b.containsKey(key)); |
---|
82 | |
---|
83 | // This will work because we know what we put into each set |
---|
84 | |
---|
85 | WritableComparable aValue = (WritableComparable) a.get(key); |
---|
86 | WritableComparable bValue = (WritableComparable) b.get(key); |
---|
87 | assertEquals(0, aValue.compareTo(bValue)); |
---|
88 | } |
---|
89 | } |
---|
90 | } |
---|
91 | |
---|
92 | /** |
---|
93 | * Test that number of "unknown" classes is propagated across multiple copies. |
---|
94 | */ |
---|
95 | @SuppressWarnings("deprecation") |
---|
96 | public void testForeignClass() { |
---|
97 | SortedMapWritable inMap = new SortedMapWritable(); |
---|
98 | inMap.put(new Text("key"), new UTF8("value")); |
---|
99 | inMap.put(new Text("key2"), new UTF8("value2")); |
---|
100 | SortedMapWritable outMap = new SortedMapWritable(inMap); |
---|
101 | SortedMapWritable copyOfCopy = new SortedMapWritable(outMap); |
---|
102 | assertEquals(1, copyOfCopy.getNewClasses()); |
---|
103 | } |
---|
104 | } |
---|