[120] | 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 | |
---|
| 19 | package org.apache.hadoop.io.serializer; |
---|
| 20 | |
---|
| 21 | import static org.apache.hadoop.io.TestGenericWritable.CONF_TEST_KEY; |
---|
| 22 | import static org.apache.hadoop.io.TestGenericWritable.CONF_TEST_VALUE; |
---|
| 23 | import junit.framework.TestCase; |
---|
| 24 | |
---|
| 25 | import org.apache.hadoop.conf.Configuration; |
---|
| 26 | import org.apache.hadoop.io.DataInputBuffer; |
---|
| 27 | import org.apache.hadoop.io.DataOutputBuffer; |
---|
| 28 | import org.apache.hadoop.io.Text; |
---|
| 29 | import org.apache.hadoop.io.TestGenericWritable.Baz; |
---|
| 30 | import org.apache.hadoop.io.TestGenericWritable.FooGenericWritable; |
---|
| 31 | import org.apache.hadoop.util.GenericsUtil; |
---|
| 32 | |
---|
| 33 | public 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 | } |
---|