KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > javax > management > compliance > serialization > support > SerializationVerifier


1 /*
2  * Copyright (C) The MX4J Contributors.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the MX4J License version 1.0.
6  * See the terms of the MX4J License in the documentation provided with this software.
7  */

8
9 package test.javax.management.compliance.serialization.support;
10
11 import java.lang.reflect.InvocationTargetException JavaDoc;
12 import java.lang.reflect.Method JavaDoc;
13
14 /**
15  * @version $Revision: 1.4 $
16  */

17 public class SerializationVerifier
18 {
19    private String JavaDoc instantiator;
20    private String JavaDoc comparator;
21
22    public SerializationVerifier(String JavaDoc instantiator, String JavaDoc comparator)
23    {
24       this.instantiator = instantiator;
25       this.comparator = comparator;
26    }
27
28    public void verifySerialization(String JavaDoc name, ClassLoader JavaDoc jmxriLoader, ClassLoader JavaDoc mx4jLoader) throws Exception JavaDoc
29    {
30       name = name.substring(name.lastIndexOf('.') + 1);
31
32       // Create the object, one with MX4J, one with JMXRI
33
Thread.currentThread().setContextClassLoader(mx4jLoader);
34       Object JavaDoc mx4j = create(name);
35       Thread.currentThread().setContextClassLoader(jmxriLoader);
36       Object JavaDoc jmxri = create(name);
37
38       // Be sure they're not the same class
39
if (mx4j.getClass().isInstance(jmxri)) throw new Exception JavaDoc("Classes must be different");
40       if (jmxri.getClass().isInstance(mx4j)) throw new Exception JavaDoc("Classes must be different");
41
42       // Serialize MX4J object
43
Thread.currentThread().setContextClassLoader(mx4jLoader);
44       byte[] mx4jBytes = serialize(mx4j);
45
46       // Deserialize with JMXRI
47
Thread.currentThread().setContextClassLoader(jmxriLoader);
48       Object JavaDoc jmxriObject = deserialize(mx4jBytes);
49
50       // Be sure they're not the same class
51
if (mx4j.getClass().isInstance(jmxriObject)) throw new Exception JavaDoc("Classes must be different");
52       if (jmxriObject.getClass().isInstance(mx4j)) throw new Exception JavaDoc("Classes must be different");
53       // Be also sure the deserialized is of the same type as JMXRI
54
if (jmxri.getClass() != jmxriObject.getClass()) throw new Exception JavaDoc("Classes must be equal");
55
56       // Now compare the original and the deserialized
57
compare(name, jmxri, jmxriObject);
58
59       // Now, do the opposite
60

61       // Serialize JMXRI object
62
Thread.currentThread().setContextClassLoader(jmxriLoader);
63       byte[] jmxriBytes = serialize(jmxri);
64
65       // Deserialize with MX4J
66
Thread.currentThread().setContextClassLoader(mx4jLoader);
67       Object JavaDoc mx4jObject = deserialize(jmxriBytes);
68
69       // Be sure they're not the same class
70
if (jmxri.getClass().isInstance(mx4jObject)) throw new Exception JavaDoc("Classes must be different");
71       if (mx4jObject.getClass().isInstance(jmxri)) throw new Exception JavaDoc("Classes must be different");
72       // Be also sure the deserialized is of the same type as MX4J
73
if (mx4j.getClass() != mx4jObject.getClass()) throw new Exception JavaDoc("Classes must be equal");
74
75       // Now compare the original and the deserialized
76
compare(name, mx4j, mx4jObject);
77
78    }
79
80    private Object JavaDoc create(String JavaDoc name) throws Exception JavaDoc
81    {
82       // Create an instance of the Instantiator
83
ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
84       Object JavaDoc creator = loader.loadClass(instantiator).newInstance();
85
86       // Lookup the creation method and call it
87
Method JavaDoc method = creator.getClass().getMethod("create" + name, new Class JavaDoc[0]);
88       Object JavaDoc object = method.invoke(creator, new Object JavaDoc[0]);
89       return object;
90    }
91
92    private byte[] serialize(Object JavaDoc object) throws Exception JavaDoc
93    {
94       // Must delegate again to another object loaded with the correct classloader,
95
// otherwise the deserialization will use the system classloader instead of
96
// the context classloader
97
ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
98       Object JavaDoc serializer = loader.loadClass("test.javax.management.compliance.serialization.support.Serializer").newInstance();
99       Method JavaDoc method = serializer.getClass().getMethod("serialize", new Class JavaDoc[]{Object JavaDoc.class});
100       return (byte[])method.invoke(serializer, new Object JavaDoc[]{object});
101    }
102
103    private Object JavaDoc deserialize(byte[] bytes) throws Exception JavaDoc
104    {
105       // Must delegate again to another object loaded with the correct classloader,
106
// otherwise the deserialization will use the system classloader instead of
107
// the context classloader
108
ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
109       Object JavaDoc serializer = loader.loadClass("test.javax.management.compliance.serialization.support.Serializer").newInstance();
110       Method JavaDoc method = serializer.getClass().getMethod("deserialize", new Class JavaDoc[]{byte[].class});
111       return method.invoke(serializer, new Object JavaDoc[]{bytes});
112    }
113
114    private void compare(String JavaDoc name, Object JavaDoc obj1, Object JavaDoc obj2) throws Exception JavaDoc
115    {
116       // First check if the class has the equals method
117
try
118       {
119          obj1.getClass().getDeclaredMethod("equals", new Class JavaDoc[]{Object JavaDoc.class});
120          // It's present
121
if (!obj1.equals(obj2)) throw new RuntimeException JavaDoc();
122
123       }
124       catch (NoSuchMethodException JavaDoc x)
125       {
126          // No equals(), create an instance of the Comparator
127
ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
128          Object JavaDoc creator = loader.loadClass(comparator).newInstance();
129
130          // Lookup the compare method
131
Method JavaDoc method = creator.getClass().getMethod("compare" + name, new Class JavaDoc[]{Object JavaDoc.class, Object JavaDoc.class});
132          try
133          {
134             method.invoke(creator, new Object JavaDoc[]{obj1, obj2});
135          }
136          catch (InvocationTargetException JavaDoc xx)
137          {
138             Throwable JavaDoc t = xx.getTargetException();
139             if (t instanceof Exception JavaDoc)
140                throw (Exception JavaDoc)t;
141             else
142                throw (Error JavaDoc)t;
143          }
144       }
145    }
146 }
147
Popular Tags