Line 0
Link Here
|
|
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. |
5 |
* |
6 |
* The contents of this file are subject to the terms of either the GNU |
7 |
* General Public License Version 2 only ("GPL") or the Common |
8 |
* Development and Distribution License("CDDL") (collectively, the |
9 |
* "License"). You may not use this file except in compliance with the |
10 |
* License. You can obtain a copy of the License at |
11 |
* http://www.netbeans.org/cddl-gplv2.html |
12 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
13 |
* specific language governing permissions and limitations under the |
14 |
* License. When distributing the software, include this License Header |
15 |
* Notice in each file and include the License file at |
16 |
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this |
17 |
* particular file as subject to the "Classpath" exception as provided |
18 |
* by Sun in the GPL Version 2 section of the License file that |
19 |
* accompanied this code. If applicable, add the following below the |
20 |
* License Header, with the fields enclosed by brackets [] replaced by |
21 |
* your own identifying information: |
22 |
* "Portions Copyrighted [year] [name of copyright owner]" |
23 |
* |
24 |
* If you wish your version of this file to be governed by only the CDDL |
25 |
* or only the GPL Version 2, indicate your decision by adding |
26 |
* "[Contributor] elects to include this software in this distribution |
27 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
28 |
* single choice of license, a recipient has the option to distribute |
29 |
* your version of this file under either the CDDL, the GPL Version 2 or |
30 |
* to extend the choice of license to its licensees as provided above. |
31 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
32 |
* Version 2 license, then the option applies only if the new code is |
33 |
* made subject to such option by the copyright holder. |
34 |
* |
35 |
* Contributor(s): |
36 |
* |
37 |
* Portions Copyrighted 2007 Sun Microsystems, Inc. |
38 |
*/ |
39 |
|
40 |
package org.netbeans.test.ide; |
41 |
|
42 |
import java.beans.PropertyChangeEvent; |
43 |
import java.beans.PropertyChangeListener; |
44 |
import java.lang.ref.WeakReference; |
45 |
import java.lang.reflect.InvocationTargetException; |
46 |
import java.lang.reflect.Method; |
47 |
import java.util.Arrays; |
48 |
import java.util.HashSet; |
49 |
import java.util.Set; |
50 |
import java.util.logging.Logger; |
51 |
import junit.framework.Assert; |
52 |
import org.netbeans.junit.NbTestCase; |
53 |
import org.openide.util.Exceptions; |
54 |
import org.openide.util.Lookup; |
55 |
import org.openide.util.WeakSet; |
56 |
|
57 |
/** |
58 |
* |
59 |
* @author Jaroslav Tulach <jtulach@netbeans.org> |
60 |
*/ |
61 |
final class WatchProjects implements PropertyChangeListener { |
62 |
private static Logger LOG = Logger.getLogger(WatchProjects.class.getName()); |
63 |
|
64 |
|
65 |
private Method getProjects; |
66 |
private Method closeProjects; |
67 |
private Object projectManager; |
68 |
|
69 |
private static int maxCount; |
70 |
private static Set<Object> projects = new WeakSet<Object>(); |
71 |
private static Exception failure; |
72 |
private static WatchProjects INST; |
73 |
|
74 |
private WatchProjects() throws Exception { |
75 |
final ClassLoader loader = Lookup.getDefault().lookup(ClassLoader.class); |
76 |
Assert.assertNotNull("Classloader must exists", loader); |
77 |
LOG.fine("Classloader: " + loader); |
78 |
Class pmClass = Class.forName( |
79 |
"org.netbeans.api.project.ui.OpenProjects", false, loader); //NOI18N |
80 |
LOG.fine("class: " + pmClass); |
81 |
Method add = pmClass.getMethod("addPropertyChangeListener", PropertyChangeListener.class); |
82 |
LOG.fine(" add: " + add); |
83 |
Method getDefault = pmClass.getMethod("getDefault"); |
84 |
LOG.fine(" getDefault: " + getDefault); |
85 |
projectManager = getDefault.invoke(null); |
86 |
add.invoke(projectManager, this); |
87 |
LOG.info("listening on " + projectManager); |
88 |
|
89 |
getProjects = pmClass.getMethod("getOpenProjects"); |
90 |
LOG.fine("getOpenProjects: " + getProjects); |
91 |
|
92 |
Class projectArray = Class.forName("[Lorg.netbeans.api.project.Project;"); |
93 |
|
94 |
closeProjects = pmClass.getMethod("close", projectArray); |
95 |
LOG.fine("getOpenProjects: " + getProjects); |
96 |
} |
97 |
|
98 |
public static void initialize() throws Exception { |
99 |
INST = new WatchProjects(); |
100 |
} |
101 |
|
102 |
public static void assertProjects() throws Exception { |
103 |
INST.closeProjects.invoke( |
104 |
INST.projectManager, |
105 |
INST.getProjects.invoke(INST.projectManager) |
106 |
); |
107 |
|
108 |
Set<WeakReference<?>> refs = new HashSet<WeakReference<?>>(); |
109 |
for (Object project : projects) { |
110 |
refs.add(new WeakReference<Object>(project)); |
111 |
} |
112 |
projects.clear(); |
113 |
|
114 |
for (WeakReference<?> weakReference : refs) { |
115 |
NbTestCase.assertGC("Can project be GCed?", weakReference); |
116 |
} |
117 |
|
118 |
|
119 |
if (maxCount == 0) { |
120 |
Assert.fail("No projects were opened or closed and that is strange"); |
121 |
} |
122 |
|
123 |
if (failure != null) { |
124 |
throw failure; |
125 |
} |
126 |
} |
127 |
|
128 |
public void propertyChange(PropertyChangeEvent evt) { |
129 |
try { |
130 |
LOG.fine("event: " + evt.getPropertyName()); |
131 |
Object[] arr = (Object[]) getProjects.invoke(projectManager); |
132 |
projects.addAll(Arrays.asList(arr)); |
133 |
LOG.finest("existing projects: " + projects); |
134 |
|
135 |
int p = projects.size(); |
136 |
if (p > maxCount) { |
137 |
maxCount = p; |
138 |
} |
139 |
} catch (Exception ex) { |
140 |
failure = ex; |
141 |
} |
142 |
} |
143 |
|
144 |
} |