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 | import junit.framework.TestCase; |
---|
24 | |
---|
25 | /** Unit tests for VersionedWritable. */ |
---|
26 | |
---|
27 | public class TestVersionedWritable extends TestCase { |
---|
28 | |
---|
29 | public TestVersionedWritable(String name) { super(name); } |
---|
30 | |
---|
31 | |
---|
32 | /** Example class used in test cases below. */ |
---|
33 | public static class SimpleVersionedWritable extends VersionedWritable { |
---|
34 | |
---|
35 | private static final Random RANDOM = new Random(); |
---|
36 | int state = RANDOM.nextInt(); |
---|
37 | |
---|
38 | |
---|
39 | private static byte VERSION = 1; |
---|
40 | public byte getVersion() { |
---|
41 | return VERSION; |
---|
42 | } |
---|
43 | |
---|
44 | |
---|
45 | public void write(DataOutput out) throws IOException { |
---|
46 | super.write(out); // version. |
---|
47 | out.writeInt(state); |
---|
48 | } |
---|
49 | |
---|
50 | public void readFields(DataInput in) throws IOException { |
---|
51 | super.readFields(in); // version |
---|
52 | this.state = in.readInt(); |
---|
53 | } |
---|
54 | |
---|
55 | |
---|
56 | public static SimpleVersionedWritable read(DataInput in) throws IOException { |
---|
57 | SimpleVersionedWritable result = new SimpleVersionedWritable(); |
---|
58 | result.readFields(in); |
---|
59 | return result; |
---|
60 | } |
---|
61 | |
---|
62 | |
---|
63 | /** Required by test code, below. */ |
---|
64 | public boolean equals(Object o) { |
---|
65 | if (!(o instanceof SimpleVersionedWritable)) |
---|
66 | return false; |
---|
67 | SimpleVersionedWritable other = (SimpleVersionedWritable)o; |
---|
68 | return this.state == other.state; |
---|
69 | } |
---|
70 | |
---|
71 | } |
---|
72 | |
---|
73 | |
---|
74 | |
---|
75 | public static class AdvancedVersionedWritable extends SimpleVersionedWritable { |
---|
76 | |
---|
77 | String shortTestString = "Now is the time for all good men to come to the aid of the Party"; |
---|
78 | String longTestString = "Four score and twenty years ago. Blah. Blah. Blah. Blah. Blah. Blah. Blah. Blah."; |
---|
79 | |
---|
80 | String compressableTestString = |
---|
81 | "Blah. Blah. Blah. Blah. Blah. Blah. Blah. Blah. Blah. Blah. Blah. Blah. " + |
---|
82 | "Blah. Blah. Blah. Blah. Blah. Blah. Blah. Blah. Blah. Blah. Blah. Blah. " + |
---|
83 | "Blah. Blah. Blah. Blah. Blah. Blah. Blah. Blah. Blah. Blah. Blah. Blah. "; |
---|
84 | |
---|
85 | SimpleVersionedWritable containedObject = new SimpleVersionedWritable(); |
---|
86 | String[] testStringArray = {"The", "Quick", "Brown", "Fox", "Jumped", "Over", "The", "Lazy", "Dog"}; |
---|
87 | |
---|
88 | public void write(DataOutput out) throws IOException { |
---|
89 | super.write(out); |
---|
90 | out.writeUTF(shortTestString); |
---|
91 | WritableUtils.writeString(out, longTestString); |
---|
92 | int comp = WritableUtils.writeCompressedString(out, compressableTestString); |
---|
93 | System.out.println("Compression is " + comp + "%"); |
---|
94 | containedObject.write(out); // Warning if this is a recursive call, you need a null value. |
---|
95 | WritableUtils.writeStringArray(out, testStringArray); |
---|
96 | |
---|
97 | } |
---|
98 | |
---|
99 | |
---|
100 | public void readFields(DataInput in) throws IOException { |
---|
101 | super.readFields(in); |
---|
102 | shortTestString = in.readUTF(); |
---|
103 | longTestString = WritableUtils.readString(in); |
---|
104 | compressableTestString = WritableUtils.readCompressedString(in); |
---|
105 | containedObject.readFields(in); // Warning if this is a recursive call, you need a null value. |
---|
106 | testStringArray = WritableUtils.readStringArray(in); |
---|
107 | } |
---|
108 | |
---|
109 | |
---|
110 | |
---|
111 | public boolean equals(Object o) { |
---|
112 | super.equals(o); |
---|
113 | |
---|
114 | if (!shortTestString.equals(((AdvancedVersionedWritable)o).shortTestString)) { return false;} |
---|
115 | if (!longTestString.equals(((AdvancedVersionedWritable)o).longTestString)) { return false;} |
---|
116 | if (!compressableTestString.equals(((AdvancedVersionedWritable)o).compressableTestString)) { return false;} |
---|
117 | |
---|
118 | if (testStringArray.length != ((AdvancedVersionedWritable)o).testStringArray.length) { return false;} |
---|
119 | for(int i=0;i< testStringArray.length;i++){ |
---|
120 | if (!testStringArray[i].equals(((AdvancedVersionedWritable)o).testStringArray[i])) { |
---|
121 | return false; |
---|
122 | } |
---|
123 | } |
---|
124 | |
---|
125 | if (!containedObject.equals(((AdvancedVersionedWritable)o).containedObject)) { return false;} |
---|
126 | |
---|
127 | return true; |
---|
128 | } |
---|
129 | |
---|
130 | |
---|
131 | |
---|
132 | } |
---|
133 | |
---|
134 | /* This one checks that version mismatch is thrown... */ |
---|
135 | public static class SimpleVersionedWritableV2 extends SimpleVersionedWritable { |
---|
136 | static byte VERSION = 2; |
---|
137 | public byte getVersion() { |
---|
138 | return VERSION; |
---|
139 | } |
---|
140 | } |
---|
141 | |
---|
142 | |
---|
143 | /** Test 1: Check that SimpleVersionedWritable. */ |
---|
144 | public void testSimpleVersionedWritable() throws Exception { |
---|
145 | TestWritable.testWritable(new SimpleVersionedWritable()); |
---|
146 | } |
---|
147 | |
---|
148 | /** Test 2: Check that AdvancedVersionedWritable Works (well, why wouldn't it!). */ |
---|
149 | public void testAdvancedVersionedWritable() throws Exception { |
---|
150 | TestWritable.testWritable(new AdvancedVersionedWritable()); |
---|
151 | } |
---|
152 | |
---|
153 | /** Test 3: Check that SimpleVersionedWritable throws an Exception. */ |
---|
154 | public void testSimpleVersionedWritableMismatch() throws Exception { |
---|
155 | TestVersionedWritable.testVersionedWritable(new SimpleVersionedWritable(), new SimpleVersionedWritableV2()); |
---|
156 | } |
---|
157 | |
---|
158 | |
---|
159 | |
---|
160 | |
---|
161 | /** Utility method for testing VersionedWritables. */ |
---|
162 | public static void testVersionedWritable(Writable before, Writable after) throws Exception { |
---|
163 | DataOutputBuffer dob = new DataOutputBuffer(); |
---|
164 | before.write(dob); |
---|
165 | |
---|
166 | DataInputBuffer dib = new DataInputBuffer(); |
---|
167 | dib.reset(dob.getData(), dob.getLength()); |
---|
168 | |
---|
169 | try { |
---|
170 | after.readFields(dib); |
---|
171 | } catch (VersionMismatchException vmme) { |
---|
172 | System.out.println("Good, we expected this:" + vmme); |
---|
173 | return; |
---|
174 | } |
---|
175 | |
---|
176 | throw new Exception("A Version Mismatch Didn't Happen!"); |
---|
177 | } |
---|
178 | } |
---|
179 | |
---|