1 7 package org.jboss.test.compatibility.test; 8 9 import java.io.Externalizable ; 10 import java.io.File ; 11 import java.io.Serializable ; 12 import java.lang.reflect.Field ; 13 import java.net.URL ; 14 import java.net.URLClassLoader ; 15 import java.util.Enumeration ; 16 import java.util.zip.ZipEntry ; 17 import java.util.zip.ZipFile ; 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 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 loadedClass; 41 public TestLookupIndividualSerialUID(Class 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 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 e) 63 { 64 result.addError(this,e); 65 } 66 log.info("Done " + this.getName()); 67 result.endTest(this); 68 } 69 70 71 private Field lookupSerialField(Class parameterClass) throws NoSuchFieldException 72 { 73 try 74 { 75 return parameterClass.getDeclaredField("serialVersionUID"); 76 } 77 catch (Throwable e) 78 { 79 return null; 80 } 81 } 82 83 } 84 85 86 public static Test createCompatilibitySuite(File jarFile) throws Exception 87 { 88 URLClassLoader urlClassLoader = new URLClassLoader (new URL [] {jarFile.toURL()},TestDefinitionOfSerialVersionUID.class.getClassLoader()); 89 TestSuite suite = new TestSuite(); 90 ZipFile zipFile = new ZipFile (jarFile); 91 Enumeration i = zipFile.entries(); 92 while (i.hasMoreElements()) 93 { 94 ZipEntry entry = (ZipEntry ) i.nextElement(); 95 if (!entry.isDirectory() && entry.getName().endsWith(".class")) 96 { 97 String classname = entry.getName().replace('/', '.').substring( 98 0, entry.getName().length() - 6); 99 100 Class lookupClass = null; 101 try { 102 lookupClass = urlClassLoader.loadClass(classname); 103 } 104 catch (Throwable 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 .class.isAssignableFrom(lookupClass) || Serializable .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 libFileName) throws Exception 119 { 120 String jbossdist = System.getProperty("jboss.dist"); 121 if (jbossdist==null) { 122 System.out.println("jboss.dist not defined"); 123 throw new Exception ("jboss.dist not defined"); 124 } 125 String strfile = jbossdist + "/server/all/lib/" + libFileName; 126 127 File file = new File (strfile); 128 if (file.exists()) 129 { 130 return createCompatilibitySuite(file); 131 } else 132 { 133 strfile = jbossdist + "/lib/" + libFileName; 134 file = new File (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 ("library " + strfile + " not found"); 142 } 143 } 144 145 } 146 147 } 148 | Popular Tags |