source: proiecte/HadoopJUnit/hadoop-0.20.1/src/test/org/apache/hadoop/io/TestMapWritable.java @ 120

Last change on this file since 120 was 120, checked in by (none), 14 years ago

Added the mail files for the Hadoop JUNit Project

  • Property svn:executable set to *
File size: 4.5 KB
Line 
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 */
20package org.apache.hadoop.io;
21
22import java.io.ByteArrayInputStream;
23import java.io.ByteArrayOutputStream;
24import java.io.DataInputStream;
25import java.io.DataOutputStream;
26import java.util.Map;
27
28import junit.framework.TestCase;
29
30/**
31 * Tests MapWritable
32 */
33public class TestMapWritable extends TestCase {
34  /** the test */
35  @SuppressWarnings("unchecked")
36  public void testMapWritable() {
37    Text[] keys = {
38        new Text("key1"),
39        new Text("key2"),
40        new Text("Key3"),
41    };
42   
43    BytesWritable[] values = {
44        new BytesWritable("value1".getBytes()),
45        new BytesWritable("value2".getBytes()),
46        new BytesWritable("value3".getBytes())
47    };
48
49    MapWritable inMap = new MapWritable();
50    for (int i = 0; i < keys.length; i++) {
51      inMap.put(keys[i], values[i]);
52    }
53
54    MapWritable outMap = new MapWritable(inMap);
55    assertEquals(inMap.size(), outMap.size());
56   
57    for (Map.Entry<Writable, Writable> e: inMap.entrySet()) {
58      assertTrue(outMap.containsKey(e.getKey()));
59      assertEquals(0, ((WritableComparable) outMap.get(e.getKey())).compareTo(
60          e.getValue()));
61    }
62   
63    // Now for something a little harder...
64   
65    Text[] maps = {
66        new Text("map1"),
67        new Text("map2")
68    };
69   
70    MapWritable mapOfMaps = new MapWritable();
71    mapOfMaps.put(maps[0], inMap);
72    mapOfMaps.put(maps[1], outMap);
73   
74    MapWritable copyOfMapOfMaps = new MapWritable(mapOfMaps);
75    for (int i = 0; i < maps.length; i++) {
76      assertTrue(copyOfMapOfMaps.containsKey(maps[i]));
77      MapWritable a = (MapWritable) mapOfMaps.get(maps[i]);
78      MapWritable b = (MapWritable) 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    MapWritable inMap = new MapWritable();
98    inMap.put(new Text("key"), new UTF8("value"));
99    inMap.put(new Text("key2"), new UTF8("value2"));
100    MapWritable outMap = new MapWritable(inMap);
101    MapWritable copyOfCopy = new MapWritable(outMap);
102    assertEquals(1, copyOfCopy.getNewClasses());
103  }
104 
105  /**
106   * Assert MapWritable does not grow across calls to readFields.
107   * @throws Exception
108   * @see <a href="https://issues.apache.org/jira/browse/HADOOP-2244">HADOOP-2244</a>
109   */
110  public void testMultipleCallsToReadFieldsAreSafe() throws Exception {
111    // Create an instance and add a key/value.
112    MapWritable m = new MapWritable();
113    final Text t = new Text(getName());
114    m.put(t, t);
115    // Get current size of map.  Key values are 't'.
116    int count = m.size();
117    // Now serialize... save off the bytes.
118    ByteArrayOutputStream baos = new ByteArrayOutputStream();
119    DataOutputStream dos = new DataOutputStream(baos);
120    m.write(dos);
121    dos.close();
122    // Now add new values to the MapWritable.
123    m.put(new Text("key1"), new Text("value1"));
124    m.put(new Text("key2"), new Text("value2"));
125    // Now deserialize the original MapWritable.  Ensure count and key values
126    // match original state.
127    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
128    DataInputStream dis = new DataInputStream(bais);
129    m.readFields(dis);
130    assertEquals(count, m.size());
131    assertTrue(m.get(t).equals(t));
132    dis.close();
133  }
134}
Note: See TracBrowser for help on using the repository browser.