KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > validation > ValidateClassLinkageTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.core.validation;
21
22 import java.io.File JavaDoc;
23 import java.net.*;
24 import java.util.*;
25 import java.util.jar.JarEntry JavaDoc;
26 import java.util.jar.JarFile JavaDoc;
27 import org.netbeans.junit.*;
28 import junit.textui.TestRunner;
29
30 /**
31  * Test that all classes in the system can load and link.
32  * Since the default 64m is not enough to load all IDE classes, run with:
33  * ant -f core/test/build.xml -Dxtest.attribs=emptyide -Dxtest.includes=org/netbeans/core/ValidateClassLinkageTest.class -Dxtest.ide.jvmargs=-XX:MaxPermSize=128m
34  * @author Jesse Glick
35  */

36 public class ValidateClassLinkageTest extends NbTestCase {
37     
38     public ValidateClassLinkageTest(String JavaDoc name) {
39         super(name);
40     }
41     
42     /**
43      * Try to load every class we can find.
44      * @see org.netbeans.core.modules.NbInstaller#preresolveClasses
45      */

46     public void testClassLinkage() throws Exception JavaDoc {
47         if (ValidateClassLinkageTest.class.getClassLoader() == ClassLoader.getSystemClassLoader()) {
48             // do not check anything as this probably means we are running
49
// plain Unit test and not inside the IDE mode
50
return;
51         }
52         
53         
54         ClassLoader JavaDoc 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/*<File>*/ jars = new TreeSet();
58         while (e.hasMoreElements()) {
59             URL manifest = (URL)e.nextElement();
60             String JavaDoc 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 JavaDoc 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 JavaDoc f = new File JavaDoc(new URI(jarfileurl));
72                 jars.add(f);
73             }
74         }
75         Map/*<String,Throwable>*/ errorsByClazz = new TreeMap();
76         Map/*<String,File>*/ locationsByClass = new HashMap();
77         Iterator it = jars.iterator();
78         while (it.hasNext()) {
79             File JavaDoc jar = (File JavaDoc)it.next();
80             System.err.println("Checking JAR: " + jar);
81             JarFile JavaDoc jarfile = new JarFile JavaDoc(jar);
82             try {
83                 e = jarfile.entries();
84                 while (e.hasMoreElements()) {
85                     JarEntry JavaDoc entry = (JarEntry JavaDoc)e.nextElement();
86                     String JavaDoc name = entry.getName();
87                     if (name.endsWith(".class")) {
88                         String JavaDoc clazz = name.substring(0, name.length() - 6).replace('/', '.');
89                         if (clazz.startsWith("org.netbeans.xtest.")) {
90                             // Skip these; a lot seem to want to link against Ant. Test-time only anyway.
91
continue;
92                         }
93                         if (clazz.startsWith("javax.help.tagext.")) {
94                             // Servlet part of JavaHelp, which we don't use. Ignore.
95
continue;
96                         }
97                         //System.err.println("class: " + clazz);
98
Throwable JavaDoc t = null;
99                         try {
100                             Class.forName(clazz, false, l);
101                         } catch (ClassNotFoundException JavaDoc cnfe) {
102                             t = cnfe;
103                         } catch (LinkageError JavaDoc le) {
104                             t = le;
105                         } catch (RuntimeException JavaDoc re) { // e.g. IllegalArgumentException from package defs
106
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 JavaDoc clazz = (String JavaDoc)entry.getKey();
123                 Throwable JavaDoc t = (Throwable JavaDoc)entry.getValue();
124                 // Will go the logs:
125
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