KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quartz > SerializationTestSupport


1 /*
2  * Copyright 2004-2006 OpenSymphony
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy
6  * of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations
14  * under the License.
15  */

16 package org.quartz;
17
18 import java.io.FileOutputStream JavaDoc;
19 import java.io.InputStream JavaDoc;
20 import java.io.ObjectInputStream JavaDoc;
21 import java.io.ObjectOutputStream JavaDoc;
22
23 import junit.framework.TestCase;
24
25 /**
26  * Base class for unit tests that wish to verify
27  * backwards compatibily of serialization with earlier versions
28  * of Quartz.
29  */

30 public abstract class SerializationTestSupport extends TestCase {
31
32     /**
33      * Get the object to serialize when generating serialized file for future
34      * tests, and against which to validate deserialized object.
35      */

36     protected abstract Object JavaDoc getTargetObject() throws Exception JavaDoc;
37     
38     /**
39      * Get the Quartz versions for which we should verify
40      * serialization backwards compatibility.
41      */

42     protected abstract String JavaDoc[] getVersions();
43     
44     /**
45      * Verify that the target object and the object we just deserialized
46      * match.
47      */

48     protected abstract void verifyMatch(Object JavaDoc target, Object JavaDoc deserialized);
49     
50     /**
51      * Test that we can successfully deserialize our target
52      * class for all of the given Quartz versions.
53      */

54     public void testSerialization() throws Exception JavaDoc {
55         Object JavaDoc targetObject = getTargetObject();
56         
57         for (int i = 0; i < getVersions().length; i++) {
58             String JavaDoc version = getVersions()[i];
59             
60             verifyMatch(
61                 targetObject,
62                 deserialize(version, targetObject.getClass()));
63         }
64     }
65     
66     /**
67      * Deserialize the target object from disk.
68      */

69     protected Object JavaDoc deserialize(String JavaDoc version, Class JavaDoc clazz) throws Exception JavaDoc {
70         InputStream JavaDoc is = getClass().getResourceAsStream(getSerializedFileName(version, clazz));
71         
72         ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(is);
73         
74         Object JavaDoc obj = (Object JavaDoc)ois.readObject();
75
76         ois.close();
77         is.close();
78
79         return obj;
80     }
81     
82     /**
83      * Use this method in the future to generate other versions of
84      * of the serialized object file.
85      */

86     public void writeJobDataFile(String JavaDoc version) throws Exception JavaDoc {
87         Object JavaDoc obj = getTargetObject();
88         
89         FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(getSerializedFileName(version, obj.getClass()));
90         ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(fos);
91         
92         oos.writeObject(obj);
93
94         oos.flush();
95         fos.close();
96         oos.close();
97     }
98     
99     /**
100      * Generate the expected name of the serialized object file.
101      */

102     private String JavaDoc getSerializedFileName(String JavaDoc version, Class JavaDoc clazz) {
103         String JavaDoc 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