source: proiecte/HadoopJUnit/hadoop-0.20.1/src/test/org/apache/hadoop/io/serializer/TestWritableSerialization.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: 3.2 KB
Line 
1/**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements.  See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership.  The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License.  You may obtain a copy of the License at
9 *
10 *     http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19package org.apache.hadoop.io.serializer;
20
21import static org.apache.hadoop.io.TestGenericWritable.CONF_TEST_KEY;
22import static org.apache.hadoop.io.TestGenericWritable.CONF_TEST_VALUE;
23import junit.framework.TestCase;
24
25import org.apache.hadoop.conf.Configuration;
26import org.apache.hadoop.io.DataInputBuffer;
27import org.apache.hadoop.io.DataOutputBuffer;
28import org.apache.hadoop.io.Text;
29import org.apache.hadoop.io.TestGenericWritable.Baz;
30import org.apache.hadoop.io.TestGenericWritable.FooGenericWritable;
31import org.apache.hadoop.util.GenericsUtil;
32
33public class TestWritableSerialization extends TestCase {
34
35  private static final Configuration conf = new Configuration();
36 
37  static {
38    conf.set("io.serializations"
39        , "org.apache.hadoop.io.serializer.WritableSerialization");
40  }
41 
42  public void testWritableSerialization() throws Exception {
43    Text before = new Text("test writable"); 
44    testSerialization(conf, before);
45  }
46 
47 
48  public void testWritableConfigurable() throws Exception {
49   
50    //set the configuration parameter
51    conf.set(CONF_TEST_KEY, CONF_TEST_VALUE);
52
53    //reuse TestGenericWritable inner classes to test
54    //writables that also implement Configurable.
55    FooGenericWritable generic = new FooGenericWritable();
56    generic.setConf(conf);
57    Baz baz = new Baz();
58    generic.set(baz);
59    Baz result = testSerialization(conf, baz);
60    assertNotNull(result.getConf());
61  }
62 
63  /**
64   * A utility that tests serialization/deserialization.
65   * @param <K> the class of the item
66   * @param conf configuration to use, "io.serializations" is read to
67   * determine the serialization
68   * @param before item to (de)serialize
69   * @return deserialized item
70   */
71  public static<K> K testSerialization(Configuration conf, K before) 
72    throws Exception {
73   
74    SerializationFactory factory = new SerializationFactory(conf);
75    Serializer<K> serializer
76      = factory.getSerializer(GenericsUtil.getClass(before));
77    Deserializer<K> deserializer
78      = factory.getDeserializer(GenericsUtil.getClass(before));
79   
80    DataOutputBuffer out = new DataOutputBuffer();
81    serializer.open(out);
82    serializer.serialize(before);
83    serializer.close();
84   
85    DataInputBuffer in = new DataInputBuffer();
86    in.reset(out.getData(), out.getLength());
87    deserializer.open(in);
88    K after = deserializer.deserialize(null);
89    deserializer.close();
90   
91    assertEquals(before, after);
92    return after;
93  }
94 
95}
Note: See TracBrowser for help on using the repository browser.