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; |
---|
20 | |
---|
21 | import java.io.DataInput; |
---|
22 | import java.io.DataOutput; |
---|
23 | import java.io.IOException; |
---|
24 | |
---|
25 | import junit.framework.TestCase; |
---|
26 | |
---|
27 | import org.apache.hadoop.conf.Configurable; |
---|
28 | import org.apache.hadoop.conf.Configuration; |
---|
29 | |
---|
30 | /** |
---|
31 | * TestCase for {@link GenericWritable} class. |
---|
32 | * @see TestWritable#testWritable(Writable) |
---|
33 | */ |
---|
34 | public class TestGenericWritable extends TestCase { |
---|
35 | |
---|
36 | private Configuration conf; |
---|
37 | public static final String CONF_TEST_KEY = "test.generic.writable"; |
---|
38 | public static final String CONF_TEST_VALUE = "dummy"; |
---|
39 | |
---|
40 | @Override |
---|
41 | protected void setUp() throws Exception { |
---|
42 | super.setUp(); |
---|
43 | conf = new Configuration(); |
---|
44 | //set the configuration parameter |
---|
45 | conf.set(CONF_TEST_KEY, CONF_TEST_VALUE); |
---|
46 | } |
---|
47 | |
---|
48 | /** Dummy class for testing {@link GenericWritable} */ |
---|
49 | public static class Foo implements Writable { |
---|
50 | private String foo = "foo"; |
---|
51 | public void readFields(DataInput in) throws IOException { |
---|
52 | foo = Text.readString(in); |
---|
53 | } |
---|
54 | public void write(DataOutput out) throws IOException { |
---|
55 | Text.writeString(out, foo); |
---|
56 | } |
---|
57 | @Override |
---|
58 | public boolean equals(Object obj) { |
---|
59 | if (!(obj instanceof Foo)) |
---|
60 | return false; |
---|
61 | return this.foo.equals(((Foo)obj).foo); |
---|
62 | } |
---|
63 | } |
---|
64 | /** Dummy class for testing {@link GenericWritable} */ |
---|
65 | public static class Bar implements Writable, Configurable { |
---|
66 | private int bar = 42; //The Answer to The Ultimate Question Of Life, the Universe and Everything |
---|
67 | private Configuration conf = null; |
---|
68 | public void readFields(DataInput in) throws IOException { |
---|
69 | bar = in.readInt(); |
---|
70 | } |
---|
71 | public void write(DataOutput out) throws IOException { |
---|
72 | out.writeInt(bar); |
---|
73 | } |
---|
74 | public Configuration getConf() { |
---|
75 | return conf; |
---|
76 | } |
---|
77 | public void setConf(Configuration conf) { |
---|
78 | this.conf = conf; |
---|
79 | } |
---|
80 | @Override |
---|
81 | public boolean equals(Object obj) { |
---|
82 | if (!(obj instanceof Bar)) |
---|
83 | return false; |
---|
84 | return this.bar == ((Bar)obj).bar; |
---|
85 | } |
---|
86 | } |
---|
87 | |
---|
88 | /** Dummy class for testing {@link GenericWritable} */ |
---|
89 | public static class Baz extends Bar { |
---|
90 | @Override |
---|
91 | public void readFields(DataInput in) throws IOException { |
---|
92 | super.readFields(in); |
---|
93 | //needs a configuration parameter |
---|
94 | assertEquals("Configuration is not set for the wrapped object", |
---|
95 | CONF_TEST_VALUE, getConf().get(CONF_TEST_KEY)); |
---|
96 | } |
---|
97 | @Override |
---|
98 | public void write(DataOutput out) throws IOException { |
---|
99 | super.write(out); |
---|
100 | } |
---|
101 | } |
---|
102 | |
---|
103 | /** Dummy class for testing {@link GenericWritable} */ |
---|
104 | public static class FooGenericWritable extends GenericWritable { |
---|
105 | @Override |
---|
106 | @SuppressWarnings("unchecked") |
---|
107 | protected Class<? extends Writable>[] getTypes() { |
---|
108 | return new Class[] {Foo.class, Bar.class, Baz.class}; |
---|
109 | } |
---|
110 | @Override |
---|
111 | public boolean equals(Object obj) { |
---|
112 | if(! (obj instanceof FooGenericWritable)) |
---|
113 | return false; |
---|
114 | return get().equals(((FooGenericWritable)obj).get()); |
---|
115 | } |
---|
116 | } |
---|
117 | |
---|
118 | public void testFooWritable() throws Exception { |
---|
119 | System.out.println("Testing Writable wrapped in GenericWritable"); |
---|
120 | FooGenericWritable generic = new FooGenericWritable(); |
---|
121 | generic.setConf(conf); |
---|
122 | Foo foo = new Foo(); |
---|
123 | generic.set(foo); |
---|
124 | TestWritable.testWritable(generic); |
---|
125 | } |
---|
126 | |
---|
127 | public void testBarWritable() throws Exception { |
---|
128 | System.out.println("Testing Writable, Configurable wrapped in GenericWritable"); |
---|
129 | FooGenericWritable generic = new FooGenericWritable(); |
---|
130 | generic.setConf(conf); |
---|
131 | Bar bar = new Bar(); |
---|
132 | bar.setConf(conf); |
---|
133 | generic.set(bar); |
---|
134 | |
---|
135 | //test writing generic writable |
---|
136 | FooGenericWritable after |
---|
137 | = (FooGenericWritable)TestWritable.testWritable(generic, conf); |
---|
138 | |
---|
139 | //test configuration |
---|
140 | System.out.println("Testing if Configuration is passed to wrapped classes"); |
---|
141 | assertTrue(after.get() instanceof Configurable); |
---|
142 | assertNotNull(((Configurable)after.get()).getConf()); |
---|
143 | } |
---|
144 | |
---|
145 | public void testBazWritable() throws Exception { |
---|
146 | System.out.println("Testing for GenericWritable to find class names"); |
---|
147 | FooGenericWritable generic = new FooGenericWritable(); |
---|
148 | generic.setConf(conf); |
---|
149 | Baz baz = new Baz(); |
---|
150 | generic.set(baz); |
---|
151 | TestWritable.testWritable(generic, conf); |
---|
152 | } |
---|
153 | |
---|
154 | public void testSet() throws Exception { |
---|
155 | Foo foo = new Foo(); |
---|
156 | FooGenericWritable generic = new FooGenericWritable(); |
---|
157 | //exception should not occur |
---|
158 | generic.set(foo); |
---|
159 | |
---|
160 | try { |
---|
161 | //exception should occur, since IntWritable is not registered |
---|
162 | generic = new FooGenericWritable(); |
---|
163 | generic.set(new IntWritable(1)); |
---|
164 | fail("Generic writable should have thrown an exception for a Writable not registered"); |
---|
165 | }catch (RuntimeException e) { |
---|
166 | //ignore |
---|
167 | } |
---|
168 | |
---|
169 | } |
---|
170 | |
---|
171 | public void testGet() throws Exception { |
---|
172 | Foo foo = new Foo(); |
---|
173 | FooGenericWritable generic = new FooGenericWritable(); |
---|
174 | generic.set(foo); |
---|
175 | assertEquals(foo, generic.get()); |
---|
176 | } |
---|
177 | |
---|
178 | } |
---|