1 19 20 package org.netbeans.core.validation; 21 22 import java.io.File ; 23 import java.net.*; 24 import java.util.*; 25 import java.util.jar.JarEntry ; 26 import java.util.jar.JarFile ; 27 import org.netbeans.junit.*; 28 import junit.textui.TestRunner; 29 30 36 public class ValidateClassLinkageTest extends NbTestCase { 37 38 public ValidateClassLinkageTest(String name) { 39 super(name); 40 } 41 42 46 public void testClassLinkage() throws Exception { 47 if (ValidateClassLinkageTest.class.getClassLoader() == ClassLoader.getSystemClassLoader()) { 48 return; 51 } 52 53 54 ClassLoader l = Thread.currentThread().getContextClassLoader(); 55 assertNotNull("Context CL has some autoloads in it", l.getResource("org/openide/windows/InputOutput.class")); 56 Enumeration e = l.getResources("META-INF/MANIFEST.MF"); 57 Set jars = new TreeSet(); 58 while (e.hasMoreElements()) { 59 URL manifest = (URL)e.nextElement(); 60 String murl = manifest.toExternalForm(); 61 assertTrue(murl.endsWith("/META-INF/MANIFEST.MF")); 62 if (murl.startsWith("jar:")) { 63 assertTrue(murl.endsWith("!/META-INF/MANIFEST.MF")); 64 String jarfileurl = murl.substring(4, murl.length() - "!/META-INF/MANIFEST.MF".length()); 65 assertTrue(jarfileurl.startsWith("file:/")); 66 assertTrue(jarfileurl.endsWith(".jar")); 67 if (jarfileurl.indexOf("/jre/lib/") != -1) { 68 System.err.println("Skipping " + jarfileurl); 69 continue; 70 } 71 File f = new File (new URI(jarfileurl)); 72 jars.add(f); 73 } 74 } 75 Map errorsByClazz = new TreeMap(); 76 Map locationsByClass = new HashMap(); 77 Iterator it = jars.iterator(); 78 while (it.hasNext()) { 79 File jar = (File )it.next(); 80 System.err.println("Checking JAR: " + jar); 81 JarFile jarfile = new JarFile (jar); 82 try { 83 e = jarfile.entries(); 84 while (e.hasMoreElements()) { 85 JarEntry entry = (JarEntry )e.nextElement(); 86 String name = entry.getName(); 87 if (name.endsWith(".class")) { 88 String clazz = name.substring(0, name.length() - 6).replace('/', '.'); 89 if (clazz.startsWith("org.netbeans.xtest.")) { 90 continue; 92 } 93 if (clazz.startsWith("javax.help.tagext.")) { 94 continue; 96 } 97 Throwable t = null; 99 try { 100 Class.forName(clazz, false, l); 101 } catch (ClassNotFoundException cnfe) { 102 t = cnfe; 103 } catch (LinkageError le) { 104 t = le; 105 } catch (RuntimeException re) { t = re; 107 } 108 if (t != null) { 109 errorsByClazz.put(clazz, t); 110 locationsByClass.put(clazz, jar); 111 } 112 } 113 } 114 } finally { 115 jarfile.close(); 116 } 117 } 118 if (!errorsByClazz.isEmpty()) { 119 it = errorsByClazz.entrySet().iterator(); 120 while (it.hasNext()) { 121 Map.Entry entry = (Map.Entry)it.next(); 122 String clazz = (String )entry.getKey(); 123 Throwable t = (Throwable )entry.getValue(); 124 System.err.println("From " + clazz + " in " + locationsByClass.get(clazz) + ":"); 126 t.printStackTrace(); 127 } 128 fail("Linkage or class loading errors encountered in " + errorsByClazz.keySet() + " (see logs for details)"); 129 } 130 } 131 132 } 133 | Popular Tags |