1 2 3 4 package net.nutch.io; 5 6 import java.io.*; 7 import java.util.Random ; 8 import junit.framework.TestCase; 9 import net.nutch.io.*; 10 11 12 public class TestWritable extends TestCase { 13 public TestWritable(String name) { super(name); } 14 15 16 public static class SimpleWritable implements Writable { 17 private static final Random RANDOM = new Random (); 18 19 int state = RANDOM.nextInt(); 20 21 public void write(DataOutput out) throws IOException { 22 out.writeInt(state); 23 } 24 25 public void readFields(DataInput in) throws IOException { 26 this.state = in.readInt(); 27 } 28 29 public static SimpleWritable read(DataInput in) throws IOException { 30 SimpleWritable result = new SimpleWritable(); 31 result.readFields(in); 32 return result; 33 } 34 35 36 public boolean equals(Object o) { 37 if (!(o instanceof SimpleWritable)) 38 return false; 39 SimpleWritable other = (SimpleWritable)o; 40 return this.state == other.state; 41 } 42 } 43 44 45 public void testSimpleWritable() throws Exception { 46 testWritable(new SimpleWritable()); 47 } 48 49 50 public static void testWritable(Writable before) throws Exception { 51 DataOutputBuffer dob = new DataOutputBuffer(); 52 before.write(dob); 53 54 DataInputBuffer dib = new DataInputBuffer(); 55 dib.reset(dob.getData(), dob.getLength()); 56 57 Writable after = (Writable)before.getClass().newInstance(); 58 after.readFields(dib); 59 60 assertEquals(before, after); 61 } 62 63 } 64 | Popular Tags |