1 2 3 4 package net.nutch.io; 5 6 import java.io.*; 7 import java.util.Random ; 8 import junit.framework.TestCase; 9 10 11 12 public class TestVersionedWritable extends TestCase { 13 14 public TestVersionedWritable(String name) { super(name); } 15 16 17 18 public static class SimpleVersionedWritable extends VersionedWritable { 19 20 private static final Random RANDOM = new Random (); 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); out.writeInt(state); 33 } 34 35 public void readFields(DataInput in) throws IOException { 36 super.readFields(in); 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 49 public boolean equals(Object 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 shortTestString = "Now is the time for all good men to come to the aid of the Party"; 63 String longTestString = "Four score and twenty years ago. Blah. Blah. Blah. Blah. Blah. Blah. Blah. Blah."; 64 65 String 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 [] 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); 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); testStringArray = WritableUtils.readStringArray(in); 92 } 93 94 95 96 public boolean equals(Object 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 120 public static class SimpleVersionedWritableV2 extends SimpleVersionedWritable { 121 static byte VERSION = 2; 122 public byte getVersion() { 123 return VERSION; 124 } 125 } 126 127 128 129 public void testSimpleVersionedWritable() throws Exception { 130 TestWritable.testWritable(new SimpleVersionedWritable()); 131 } 132 133 134 public void testAdvancedVersionedWritable() throws Exception { 135 TestWritable.testWritable(new AdvancedVersionedWritable()); 136 } 137 138 139 public void testSimpleVersionedWritableMismatch() throws Exception { 140 TestVersionedWritable.testVersionedWritable(new SimpleVersionedWritable(), new SimpleVersionedWritableV2()); 141 } 142 143 144 145 146 147 public static void testVersionedWritable(Writable before, Writable after) throws Exception { 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 ("A Version Mismatch Didn't Happen!"); 162 } 163 } 164 165 | Popular Tags |