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: * If you wish your version of this file to be governed by only the CDDL
025: * or only the GPL Version 2, indicate your decision by adding
026: * "[Contributor] elects to include this software in this distribution
027: * under the [CDDL or GPL Version 2] license." If you do not indicate a
028: * single choice of license, a recipient has the option to distribute
029: * your version of this file under either the CDDL, the GPL Version 2 or
030: * to extend the choice of license to its licensees as provided above.
031: * However, if you add GPL Version 2 code and therefore, elected the GPL
032: * Version 2 license, then the option applies only if the new code is
033: * made subject to such option by the copyright holder.
034: *
035: * Contributor(s):
036: *
037: * Portions Copyrighted 2007 Sun Microsystems, Inc.
038: */
039:
040: package org.netbeans.test.ide;
041:
042: import java.awt.Component;
043: import java.awt.Container;
044: import java.awt.Frame;
045: import java.lang.reflect.Field;
046: import java.lang.reflect.Method;
047: import java.util.logging.Level;
048: import java.util.logging.Logger;
049: import javax.swing.JTree;
050: import junit.framework.Assert;
051: import org.netbeans.junit.Log;
052: import org.openide.util.Lookup;
053:
054: /**
055: *
056: * @author Jaroslav Tulach <jtulach@netbeans.org>
057: */
058: final class WatchProjects {
059: private static Logger LOG = Logger.getLogger(WatchProjects.class
060: .getName());
061:
062: private static Method getProjects;
063: private static Method closeProjects;
064: private static Object projectManager;
065:
066: private WatchProjects() {
067: }
068:
069: public static void initialize() throws Exception {
070: Log.enableInstances(Logger.getLogger("TIMER"), "Project",
071: Level.FINEST);
072:
073: final ClassLoader loader = Lookup.getDefault().lookup(
074: ClassLoader.class);
075: Assert.assertNotNull("Classloader must exists", loader);
076: LOG.fine("Classloader: " + loader);
077: Class pmClass = Class.forName(
078: "org.netbeans.api.project.ui.OpenProjects", false,
079: loader); //NOI18N
080: LOG.fine("class: " + pmClass);
081: Method getDefault = pmClass.getMethod("getDefault");
082: LOG.fine(" getDefault: " + getDefault);
083: projectManager = getDefault.invoke(null);
084:
085: getProjects = pmClass.getMethod("getOpenProjects");
086: LOG.fine("getOpenProjects: " + getProjects);
087:
088: Class projectArray = Class
089: .forName("[Lorg.netbeans.api.project.Project;");
090:
091: closeProjects = pmClass.getMethod("close", projectArray);
092: LOG.fine("getOpenProjects: " + getProjects);
093: }
094:
095: public static void assertProjects() throws Exception {
096: closeProjects.invoke(projectManager, getProjects
097: .invoke(projectManager));
098:
099: if (System.getProperty("java.version").startsWith("1.5")) {
100: // hopefully this hack will be needed just on 1.5
101: resetJTreeUIs(Frame.getFrames());
102:
103: // clear input method memory leak on JDK 1.5
104: Class<?> inputMethod = Class
105: .forName("sun.awt.im.InputContext");
106: Field f = inputMethod
107: .getDeclaredField("previousInputMethod");
108: f.setAccessible(true);
109: f.set(null, null);
110: }
111:
112: System.setProperty("assertgc.paths", "5");
113: // disabled due to issue 121855
114: // Log.assertInstances("Checking if all projects are really garbage collected");
115: }
116:
117: private static void resetJTreeUIs(Component[] arr) {
118: for (Component c : arr) {
119: if (c instanceof JTree) {
120: JTree jt = (JTree) c;
121: jt.updateUI();
122: }
123: if (c instanceof Container) {
124: Container o = (Container) c;
125: resetJTreeUIs(o.getComponents());
126: }
127: }
128: }
129: }
|