WEB开发网
开发学院软件开发Java 测试对象串行化:容易被遗漏的重要测试 阅读

测试对象串行化:容易被遗漏的重要测试

 2010-01-11 00:00:00 来源:WEB开发网   
核心提示: 清单 2. 反串行化对象,并将其与原始对象相比较publicvoidtestRoundTripSerialization()throwsIOException,ClassNotFoundException,UnresolvableException{//constructtestobjectSi

清单 2. 反串行化对象,并将其与原始对象相比较
public void testRoundTripSerialization() 
  throws IOException, ClassNotFoundException, UnresolvableException { 
 
  // construct test object 
  SimpleVariableContext original = new SimpleVariableContext(); 
  original.setVariableValue("s", "String Value"); 
  original.setVariableValue("x", new Double(3.1415292)); 
  original.setVariableValue("b", Boolean.TRUE); 
 
  // serialize 
  ByteArrayOutputStream out = new ByteArrayOutputStream(); 
  ObjectOutputStream oos = new ObjectOutputStream(out); 
  oos.writeObject(original); 
  oos.close(); 
 
  //deserialize 
  byte[] pickled = out.toByteArray(); 
  InputStream in = new ByteArrayInputStream(pickled); 
  ObjectInputStream ois = new ObjectInputStream(in); 
  Object o = ois.readObject(); 
  SimpleVariableContext copy = (SimpleVariableContext) o; 
 
  // test the result 
  assertEquals("String Value", copy.getVariableValue("", "", "s")); 
  assertEquals(Double.valueOf(3.1415292), copy.getVariableValue("", "", "x")); 
  assertEquals(Boolean.TRUE, copy.getVariableValue("", "", "b")); 
  assertEquals("", ""); 
 
 }

上一页  1 2 3 4 5 6 7  下一页

Tags:测试 对象 串行

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接