[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; |
---|
| 20 | |
---|
| 21 | import java.io.*; |
---|
| 22 | import java.util.Random; |
---|
| 23 | |
---|
| 24 | import org.apache.hadoop.conf.Configurable; |
---|
| 25 | import org.apache.hadoop.conf.Configuration; |
---|
| 26 | import org.apache.hadoop.util.ReflectionUtils; |
---|
| 27 | |
---|
| 28 | import junit.framework.TestCase; |
---|
| 29 | |
---|
| 30 | /** Unit tests for Writable. */ |
---|
| 31 | public class TestWritable extends TestCase { |
---|
| 32 | public TestWritable(String name) { super(name); } |
---|
| 33 | |
---|
| 34 | /** Example class used in test cases below. */ |
---|
| 35 | public static class SimpleWritable implements Writable { |
---|
| 36 | private static final Random RANDOM = new Random(); |
---|
| 37 | |
---|
| 38 | int state = RANDOM.nextInt(); |
---|
| 39 | |
---|
| 40 | public void write(DataOutput out) throws IOException { |
---|
| 41 | out.writeInt(state); |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | public void readFields(DataInput in) throws IOException { |
---|
| 45 | this.state = in.readInt(); |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | public static SimpleWritable read(DataInput in) throws IOException { |
---|
| 49 | SimpleWritable result = new SimpleWritable(); |
---|
| 50 | result.readFields(in); |
---|
| 51 | return result; |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | /** Required by test code, below. */ |
---|
| 55 | public boolean equals(Object o) { |
---|
| 56 | if (!(o instanceof SimpleWritable)) |
---|
| 57 | return false; |
---|
| 58 | SimpleWritable other = (SimpleWritable)o; |
---|
| 59 | return this.state == other.state; |
---|
| 60 | } |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | /** Test 1: Check that SimpleWritable. */ |
---|
| 64 | public void testSimpleWritable() throws Exception { |
---|
| 65 | testWritable(new SimpleWritable()); |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | public void testByteWritable() throws Exception { |
---|
| 69 | testWritable(new ByteWritable((byte)128)); |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | public void testDoubleWritable() throws Exception { |
---|
| 73 | testWritable(new DoubleWritable(1.0)); |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | /** Utility method for testing writables. */ |
---|
| 77 | public static Writable testWritable(Writable before) |
---|
| 78 | throws Exception { |
---|
| 79 | return testWritable(before, null); |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | /** Utility method for testing writables. */ |
---|
| 83 | public static Writable testWritable(Writable before |
---|
| 84 | , Configuration conf) throws Exception { |
---|
| 85 | DataOutputBuffer dob = new DataOutputBuffer(); |
---|
| 86 | before.write(dob); |
---|
| 87 | |
---|
| 88 | DataInputBuffer dib = new DataInputBuffer(); |
---|
| 89 | dib.reset(dob.getData(), dob.getLength()); |
---|
| 90 | |
---|
| 91 | Writable after = (Writable)ReflectionUtils.newInstance( |
---|
| 92 | before.getClass(), conf); |
---|
| 93 | after.readFields(dib); |
---|
| 94 | |
---|
| 95 | assertEquals(before, after); |
---|
| 96 | return after; |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | } |
---|