KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > project > ui > actions > ActionsUtilTest


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.modules.project.ui.actions;
21
22 import java.lang.ref.WeakReference JavaDoc;
23 import java.util.Arrays JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.Set JavaDoc;
27 import junit.framework.AssertionFailedError;
28 import org.netbeans.api.project.Project;
29 import org.netbeans.junit.NbTestCase;
30 import org.openide.filesystems.FileObject;
31 import org.openide.util.Lookup;
32 import org.openide.util.lookup.Lookups;
33 import org.openide.util.lookup.ProxyLookup;
34
35 /**
36  *
37  * @author Jan Lahoda
38  */

39 public class ActionsUtilTest extends NbTestCase {
40     
41     public ActionsUtilTest(String JavaDoc testName) {
42         super(testName);
43     }
44     
45     protected void setUp() throws Exception JavaDoc {
46     }
47     
48     //due to use of weak references in ActionsUtil.LookupResultsCache this test fails
49
//(there are cache misses after GC, and there may be even cache miss during the
50
//first "assertTrue(result == ActionsUtil.getProjectsFromLookup(projects, null))"
51
//statement.
52
// public void testCacheWorks() throws Exception {
53
// Project prj1 = new DummyProject();
54
// Project prj2 = new DummyProject();
55
// Lookup projects = Lookups.fixed(new Object[] {
56
// prj1, prj2,
57
// });
58
//
59
// Project[] result = ActionsUtil.getProjectsFromLookup(projects, null);
60
//
61
// assertTrue(result == ActionsUtil.getProjectsFromLookup(projects, null));
62
//
63
// //make sure the cache lives as long as the projects lookup:
64
// //try hard to force gc:
65
// for (int i = 0; i < 5; i++) {
66
// System.gc();
67
// System.runFinalization();
68
// }
69
//
70
// assertTrue(result == ActionsUtil.getProjectsFromLookup(projects, null));
71
// }
72

73     private static final Object JavaDoc o = new Object JavaDoc();
74     
75     public void testCacheUpdatesCorrectly() throws Exception JavaDoc {
76         Project prj1 = new DummyProject();
77         Project prj2 = new DummyProject();
78         TestProxyLookup projects = new TestProxyLookup(new Lookup[] {
79             prj1.getLookup(),
80             prj2.getLookup(),
81         });
82         
83         Set JavaDoc<Project> bothProjects = new HashSet JavaDoc<Project>(Arrays.asList(prj1, prj2));
84         Set JavaDoc<Project> result = new HashSet JavaDoc<Project>(Arrays.asList(ActionsUtil.getProjectsFromLookup(projects, null)));
85         
86         assertTrue(bothProjects.equals(result));
87         
88         //make sure cache is somehow updated even after hard GC:
89
//and try really hard to reclaim even (potential) SoftReferences:
90
boolean wasThrown = false;
91         
92         try {
93             assertGC("", new WeakReference JavaDoc<Object JavaDoc>(o));
94         } catch (AssertionFailedError e) {
95             //ignore
96
wasThrown = true;
97         }
98         
99         assertTrue(wasThrown);
100         
101         projects.setLookupsOverride(new Lookup[] {prj1.getLookup()});
102         
103         Set JavaDoc<Project> firstProject = new HashSet JavaDoc<Project>(Arrays.asList(prj1));
104         
105         result = new HashSet JavaDoc<Project>(Arrays.asList(ActionsUtil.getProjectsFromLookup(projects, null)));
106         
107         assertTrue(firstProject.equals(result));
108         
109         projects.setLookupsOverride(new Lookup[] {});
110         
111         result = new HashSet JavaDoc<Project>(Arrays.asList(ActionsUtil.getProjectsFromLookup(projects, null)));
112         
113         assertTrue(Collections.EMPTY_SET.equals(result));
114         
115         projects.setLookupsOverride(new Lookup[] {prj1.getLookup(), prj2.getLookup()});
116
117         result = new HashSet JavaDoc<Project>(Arrays.asList(ActionsUtil.getProjectsFromLookup(projects, null)));
118         
119         assertTrue(bothProjects.equals(result));
120     }
121     
122     public void testCanBeReclaimed() throws Exception JavaDoc {
123         Project prj1 = new DummyProject();
124         Project prj2 = new DummyProject();
125         Lookup projects = new TestProxyLookup(new Lookup[] {
126             prj1.getLookup(),
127             prj2.getLookup(),
128         });
129         
130         ActionsUtil.getProjectsFromLookup(projects, null);
131         
132         WeakReference JavaDoc<?> ref1 = new WeakReference JavaDoc<Object JavaDoc>(prj1);
133         WeakReference JavaDoc<?> ref2 = new WeakReference JavaDoc<Object JavaDoc>(prj2);
134         WeakReference JavaDoc<?> lookup = new WeakReference JavaDoc<Object JavaDoc>(projects);
135         
136         prj1 = null;
137         prj2 = null;
138         projects = null;
139         
140         assertGC("the projects can be reclaimed", ref1);
141         assertGC("the projects can be reclaimed", ref2);
142         assertGC("the lookup can be reclaimed", lookup);
143     }
144     
145     public void testCanBeReclaimedWithSimpleLookup() throws Exception JavaDoc {
146         Project prj1 = new DummyProject();
147         Project prj2 = new DummyProject();
148         Lookup projects = Lookups.fixed(new Object JavaDoc[] {
149             prj1,
150             prj2,
151         });
152         
153         ActionsUtil.getProjectsFromLookup(projects, null);
154         
155         WeakReference JavaDoc<?> ref1 = new WeakReference JavaDoc<Object JavaDoc>(prj1);
156         WeakReference JavaDoc<?> ref2 = new WeakReference JavaDoc<Object JavaDoc>(prj2);
157         WeakReference JavaDoc<?> lookup = new WeakReference JavaDoc<Object JavaDoc>(projects);
158         
159         prj1 = null;
160         prj2 = null;
161         projects = null;
162         
163         assertGC("the projects can be reclaimed", ref1);
164         assertGC("the projects can be reclaimed", ref2);
165         assertGC("the lookup can be reclaimed", lookup);
166     }
167     
168     private static final class TestProxyLookup extends ProxyLookup {
169         
170         public TestProxyLookup(Lookup[] lookups) {
171             super(lookups);
172         }
173         
174         public void setLookupsOverride(Lookup[] lookups) {
175             setLookups(lookups);
176         }
177         
178     }
179     
180     private static final class DummyProject implements Project {
181         
182         private final Lookup lookup = Lookups.singleton(this);
183         
184         public FileObject getProjectDirectory() {
185             return null;
186         }
187         
188         public Lookup getLookup() {
189             return lookup;
190         }
191         
192     }
193     
194 }
195
Popular Tags