KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > nutch > io > TestVersionedWritable


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