2012-11-08 104 views

回答

5

也許你可以找到更簡單的方法來測試你writables,但手動做序列化/反序列化也將正常工作。 E.g:

MyUtils.java:

... 
import org.apache.commons.io.IOUtils; 
... 
public static byte[] serialize(Writable writable) throws IOException { 
     ByteArrayOutputStream out = new ByteArrayOutputStream(); 
     DataOutputStream dataOut = null; 
     try { 
      dataOut = new DataOutputStream(out); 
      writable.write(dataOut); 
      return out.toByteArray(); 
     } 
     finally { 
      IOUtils.closeQuietly(dataOut); 
     } 
    } 

public static <T extends Writable> T asWritable(byte[] bytes, Class<T> clazz) 
      throws IOException { 
     T result = null; 
     DataInputStream dataIn = null; 
     try { 
      result = clazz.newInstance(); 
      ByteArrayInputStream in = new ByteArrayInputStream(bytes); 
      dataIn = new DataInputStream(in); 
      result.readFields(dataIn); 
     } catch (InstantiationException e) { 
      // should not happen 
      assert false; 
     } catch (IllegalAccessException e) { 
      // should not happen 
      assert false; 
     } finally { 
      IOUtils.closeQuietly(dataIn); 
     } 
     return result; 
    } 

然後在您的測試類:

CustomWritable record = ... ; //your initialized Writable 
byte[] serializedBytes = MyUtils.serialize(record); 

CustomWritable deserialized = 
    MyUtils.asWritable(serializedBytes, CustomWritable.class); 

assertEquals("Value mismatch!", record.getFieldA(), deserialized.getFieldA()); 
...