001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.core.validation;
043:
044: import java.io.File;
045: import java.net.*;
046: import java.util.*;
047: import java.util.jar.JarEntry;
048: import java.util.jar.JarFile;
049: import org.netbeans.junit.*;
050: import junit.textui.TestRunner;
051:
052: /**
053: * Test that all classes in the system can load and link.
054: * Since the default 64m is not enough to load all IDE classes, run with:
055: * ant -f core/test/build.xml -Dxtest.attribs=emptyide -Dxtest.includes=org/netbeans/core/ValidateClassLinkageTest.class -Dxtest.ide.jvmargs=-XX:MaxPermSize=128m
056: * @author Jesse Glick
057: */
058: public class ValidateClassLinkageTest extends NbTestCase {
059:
060: public ValidateClassLinkageTest(String name) {
061: super (name);
062: }
063:
064: /**
065: * Try to load every class we can find.
066: * @see org.netbeans.core.modules.NbInstaller#preresolveClasses
067: */
068: public void testClassLinkage() throws Exception {
069: if (ValidateClassLinkageTest.class.getClassLoader() == ClassLoader
070: .getSystemClassLoader()) {
071: // do not check anything as this probably means we are running
072: // plain Unit test and not inside the IDE mode
073: return;
074: }
075:
076: ClassLoader l = Thread.currentThread().getContextClassLoader();
077: assertNotNull("Context CL has some autoloads in it", l
078: .getResource("org/openide/windows/InputOutput.class"));
079: Enumeration e = l.getResources("META-INF/MANIFEST.MF");
080: Set/*<File>*/jars = new TreeSet();
081: while (e.hasMoreElements()) {
082: URL manifest = (URL) e.nextElement();
083: String murl = manifest.toExternalForm();
084: assertTrue(murl.endsWith("/META-INF/MANIFEST.MF"));
085: if (murl.startsWith("jar:")) {
086: assertTrue(murl.endsWith("!/META-INF/MANIFEST.MF"));
087: String jarfileurl = murl.substring(4, murl.length()
088: - "!/META-INF/MANIFEST.MF".length());
089: assertTrue(jarfileurl.startsWith("file:/"));
090: assertTrue(jarfileurl.endsWith(".jar"));
091: if (jarfileurl.indexOf("/jre/lib/") != -1) {
092: System.err.println("Skipping " + jarfileurl);
093: continue;
094: }
095: File f = new File(new URI(jarfileurl));
096: jars.add(f);
097: }
098: }
099: Map/*<String,Throwable>*/errorsByClazz = new TreeMap();
100: Map/*<String,File>*/locationsByClass = new HashMap();
101: Iterator it = jars.iterator();
102: while (it.hasNext()) {
103: File jar = (File) it.next();
104: System.err.println("Checking JAR: " + jar);
105: JarFile jarfile = new JarFile(jar);
106: try {
107: e = jarfile.entries();
108: while (e.hasMoreElements()) {
109: JarEntry entry = (JarEntry) e.nextElement();
110: String name = entry.getName();
111: if (name.endsWith(".class")) {
112: String clazz = name.substring(0,
113: name.length() - 6).replace('/', '.');
114: if (clazz.startsWith("org.netbeans.xtest.")) {
115: // Skip these; a lot seem to want to link against Ant. Test-time only anyway.
116: continue;
117: }
118: if (clazz.startsWith("javax.help.tagext.")) {
119: // Servlet part of JavaHelp, which we don't use. Ignore.
120: continue;
121: }
122: //System.err.println("class: " + clazz);
123: Throwable t = null;
124: try {
125: Class.forName(clazz, false, l);
126: } catch (ClassNotFoundException cnfe) {
127: t = cnfe;
128: } catch (LinkageError le) {
129: t = le;
130: } catch (RuntimeException re) { // e.g. IllegalArgumentException from package defs
131: t = re;
132: }
133: if (t != null) {
134: errorsByClazz.put(clazz, t);
135: locationsByClass.put(clazz, jar);
136: }
137: }
138: }
139: } finally {
140: jarfile.close();
141: }
142: }
143: if (!errorsByClazz.isEmpty()) {
144: it = errorsByClazz.entrySet().iterator();
145: while (it.hasNext()) {
146: Map.Entry entry = (Map.Entry) it.next();
147: String clazz = (String) entry.getKey();
148: Throwable t = (Throwable) entry.getValue();
149: // Will go the logs:
150: System.err.println("From " + clazz + " in "
151: + locationsByClass.get(clazz) + ":");
152: t.printStackTrace();
153: }
154: fail("Linkage or class loading errors encountered in "
155: + errorsByClazz.keySet()
156: + " (see logs for details)");
157: }
158: }
159:
160: }
|