KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > compatibility > test > TestDefinitionOfSerialVersionUID


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.test.compatibility.test;
8
9 import java.io.Externalizable JavaDoc;
10 import java.io.File JavaDoc;
11 import java.io.Serializable JavaDoc;
12 import java.lang.reflect.Field JavaDoc;
13 import java.net.URL JavaDoc;
14 import java.net.URLClassLoader JavaDoc;
15 import java.util.Enumeration JavaDoc;
16 import java.util.zip.ZipEntry JavaDoc;
17 import java.util.zip.ZipFile JavaDoc;
18
19 import junit.framework.AssertionFailedError;
20 import junit.framework.Test;
21 import junit.framework.TestCase;
22 import junit.framework.TestResult;
23 import junit.framework.TestSuite;
24
25 import org.jboss.logging.Logger;
26
27 /**
28  * Verifies if all classes are implementing serialVersionUID in a given JAR.
29  * Use the method createCompatilibitySuite defined in this class embedded in suite methods.
30  * @author clebert.suconic@jboss.com
31  */

32 public class TestDefinitionOfSerialVersionUID extends TestCase
33 {
34     static Logger log = Logger.getLogger("TestDefinitionOfSerialVersionUID");
35
36
37     static class TestLookupIndividualSerialUID extends TestCase
38     {
39         
40         Class JavaDoc loadedClass;
41         public TestLookupIndividualSerialUID(Class JavaDoc loadedClass)
42         {
43             super(loadedClass.getName());
44             this.loadedClass=loadedClass;
45         }
46
47         public void run(TestResult result)
48         {
49             log.info("Testing " + this.getName());
50             try
51             {
52                 result.startTest(this);
53
54                 Field JavaDoc primaryField = lookupSerialField(loadedClass);
55                 assertNotNull("serialVersionUID not defined in current version of " + this.getName(), primaryField);
56                 primaryField.setAccessible(true);
57                 log.info("SerialUID for " + this.getName() + " is " + primaryField.getLong(null));
58             }
59             catch (AssertionFailedError error)
60             {
61                 result.addFailure(this, error);
62             } catch (Throwable JavaDoc e)
63             {
64                 result.addError(this,e);
65             }
66             log.info("Done " + this.getName());
67             result.endTest(this);
68         }
69
70         /** Using reflection as ObjectSerialClass will calculate a new one */
71         private Field JavaDoc lookupSerialField(Class JavaDoc parameterClass) throws NoSuchFieldException JavaDoc
72         {
73             try
74             {
75                 return parameterClass.getDeclaredField("serialVersionUID");
76             }
77             catch (Throwable JavaDoc e)
78             {
79                 return null;
80             }
81         }
82
83     }
84
85     /** Create a compatibility suite for a give JAR file */
86     public static Test createCompatilibitySuite(File JavaDoc jarFile) throws Exception JavaDoc
87     {
88         URLClassLoader JavaDoc urlClassLoader = new URLClassLoader JavaDoc(new URL JavaDoc[] {jarFile.toURL()},TestDefinitionOfSerialVersionUID.class.getClassLoader());
89         TestSuite suite = new TestSuite();
90         ZipFile JavaDoc zipFile = new ZipFile JavaDoc(jarFile);
91         Enumeration JavaDoc i = zipFile.entries();
92         while (i.hasMoreElements())
93         {
94             ZipEntry JavaDoc entry = (ZipEntry JavaDoc) i.nextElement();
95             if (!entry.isDirectory() && entry.getName().endsWith(".class"))
96             {
97                 String JavaDoc classname = entry.getName().replace('/', '.').substring(
98                         0, entry.getName().length() - 6);
99                 
100                 Class JavaDoc lookupClass = null;
101                 try {
102                     lookupClass = urlClassLoader.loadClass(classname);
103                 }
104                 catch (Throwable JavaDoc e)
105                 {
106                     System.out.println("Warning... Class " + classname + " couldn't be loaded at " + jarFile.getName() + " -> " + e.toString());
107                 }
108                 
109                 
110                 if (lookupClass!=null && (Externalizable JavaDoc.class.isAssignableFrom(lookupClass) || Serializable JavaDoc.class.isAssignableFrom(lookupClass)) && !lookupClass.isInterface()) {
111                     suite.addTest(new TestLookupIndividualSerialUID(lookupClass));
112                 }
113             }
114         }
115         return suite;
116     }
117     
118     public static Test createCompatilibitySuiteForLib(String JavaDoc libFileName) throws Exception JavaDoc
119     {
120         String JavaDoc jbossdist = System.getProperty("jboss.dist");
121         if (jbossdist==null) {
122                 System.out.println("jboss.dist not defined");
123                 throw new Exception JavaDoc("jboss.dist not defined");
124         }
125         String JavaDoc strfile = jbossdist + "/server/all/lib/" + libFileName;
126
127         File JavaDoc file = new File JavaDoc(strfile);
128         if (file.exists())
129         {
130            return createCompatilibitySuite(file);
131         } else
132         {
133            strfile = jbossdist + "/lib/" + libFileName;
134            file = new File JavaDoc(strfile);
135            if (file.exists())
136            {
137                return createCompatilibitySuite(file);
138            } else
139            {
140                    System.out.println("library " + strfile + " not found");
141                    throw new RuntimeException JavaDoc ("library " + strfile + " not found");
142            }
143         }
144         
145     }
146
147 }
148
Popular Tags