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.mapred; |
---|
20 | |
---|
21 | import java.util.HashMap; |
---|
22 | import java.util.Iterator; |
---|
23 | import java.util.Map; |
---|
24 | |
---|
25 | import junit.framework.TestCase; |
---|
26 | |
---|
27 | import org.apache.hadoop.conf.Configuration; |
---|
28 | import org.apache.hadoop.io.DataInputBuffer; |
---|
29 | import org.apache.hadoop.io.DataOutputBuffer; |
---|
30 | import org.apache.hadoop.io.serializer.Deserializer; |
---|
31 | import org.apache.hadoop.io.serializer.SerializationFactory; |
---|
32 | import org.apache.hadoop.io.serializer.Serializer; |
---|
33 | import org.apache.hadoop.util.GenericsUtil; |
---|
34 | |
---|
35 | public class TestWritableJobConf extends TestCase { |
---|
36 | |
---|
37 | private static final Configuration CONF = new Configuration(); |
---|
38 | |
---|
39 | private <K> K serDeser(K conf) throws Exception { |
---|
40 | SerializationFactory factory = new SerializationFactory(CONF); |
---|
41 | Serializer<K> serializer = |
---|
42 | factory.getSerializer(GenericsUtil.getClass(conf)); |
---|
43 | Deserializer<K> deserializer = |
---|
44 | factory.getDeserializer(GenericsUtil.getClass(conf)); |
---|
45 | |
---|
46 | DataOutputBuffer out = new DataOutputBuffer(); |
---|
47 | serializer.open(out); |
---|
48 | serializer.serialize(conf); |
---|
49 | serializer.close(); |
---|
50 | |
---|
51 | DataInputBuffer in = new DataInputBuffer(); |
---|
52 | in.reset(out.getData(), out.getLength()); |
---|
53 | deserializer.open(in); |
---|
54 | K after = deserializer.deserialize(null); |
---|
55 | deserializer.close(); |
---|
56 | return after; |
---|
57 | } |
---|
58 | |
---|
59 | private void assertEquals(Configuration conf1, Configuration conf2) { |
---|
60 | assertEquals(conf1.size(), conf2.size()); |
---|
61 | |
---|
62 | Iterator<Map.Entry<String, String>> iterator1 = conf1.iterator(); |
---|
63 | Map<String, String> map1 = new HashMap<String,String>(); |
---|
64 | while (iterator1.hasNext()) { |
---|
65 | Map.Entry<String, String> entry = iterator1.next(); |
---|
66 | map1.put(entry.getKey(), entry.getValue()); |
---|
67 | } |
---|
68 | |
---|
69 | Iterator<Map.Entry<String, String>> iterator2 = conf1.iterator(); |
---|
70 | Map<String, String> map2 = new HashMap<String,String>(); |
---|
71 | while (iterator2.hasNext()) { |
---|
72 | Map.Entry<String, String> entry = iterator2.next(); |
---|
73 | map2.put(entry.getKey(), entry.getValue()); |
---|
74 | } |
---|
75 | |
---|
76 | assertEquals(map1, map2); |
---|
77 | } |
---|
78 | |
---|
79 | public void testEmptyConfiguration() throws Exception { |
---|
80 | JobConf conf = new JobConf(); |
---|
81 | Configuration deser = serDeser(conf); |
---|
82 | assertEquals(conf, deser); |
---|
83 | } |
---|
84 | |
---|
85 | public void testNonEmptyConfiguration() throws Exception { |
---|
86 | JobConf conf = new JobConf(); |
---|
87 | conf.set("a", "A"); |
---|
88 | conf.set("b", "B"); |
---|
89 | Configuration deser = serDeser(conf); |
---|
90 | assertEquals(conf, deser); |
---|
91 | } |
---|
92 | |
---|
93 | public void testConfigurationWithDefaults() throws Exception { |
---|
94 | JobConf conf = new JobConf(false); |
---|
95 | conf.set("a", "A"); |
---|
96 | conf.set("b", "B"); |
---|
97 | Configuration deser = serDeser(conf); |
---|
98 | assertEquals(conf, deser); |
---|
99 | } |
---|
100 | |
---|
101 | } |
---|