1 16 package org.quartz; 17 18 import java.io.FileOutputStream ; 19 import java.io.InputStream ; 20 import java.io.ObjectInputStream ; 21 import java.io.ObjectOutputStream ; 22 23 import junit.framework.TestCase; 24 25 30 public abstract class SerializationTestSupport extends TestCase { 31 32 36 protected abstract Object getTargetObject() throws Exception ; 37 38 42 protected abstract String [] getVersions(); 43 44 48 protected abstract void verifyMatch(Object target, Object deserialized); 49 50 54 public void testSerialization() throws Exception { 55 Object targetObject = getTargetObject(); 56 57 for (int i = 0; i < getVersions().length; i++) { 58 String version = getVersions()[i]; 59 60 verifyMatch( 61 targetObject, 62 deserialize(version, targetObject.getClass())); 63 } 64 } 65 66 69 protected Object deserialize(String version, Class clazz) throws Exception { 70 InputStream is = getClass().getResourceAsStream(getSerializedFileName(version, clazz)); 71 72 ObjectInputStream ois = new ObjectInputStream (is); 73 74 Object obj = (Object )ois.readObject(); 75 76 ois.close(); 77 is.close(); 78 79 return obj; 80 } 81 82 86 public void writeJobDataFile(String version) throws Exception { 87 Object obj = getTargetObject(); 88 89 FileOutputStream fos = new FileOutputStream (getSerializedFileName(version, obj.getClass())); 90 ObjectOutputStream oos = new ObjectOutputStream (fos); 91 92 oos.writeObject(obj); 93 94 oos.flush(); 95 fos.close(); 96 oos.close(); 97 } 98 99 102 private String getSerializedFileName(String version, Class clazz) { 103 String className = clazz.getName(); 104 int index = className.lastIndexOf("."); 105 index = (index < 0) ? 0 : index + 1; 106 107 return className.substring(index) + "-" + version + ".ser"; 108 } 109 } 110 | Popular Tags |