1 19 20 package org.netbeans.modules.project.ui.actions; 21 22 import java.lang.ref.WeakReference ; 23 import java.util.Arrays ; 24 import java.util.Collections ; 25 import java.util.HashSet ; 26 import java.util.Set ; 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 39 public class ActionsUtilTest extends NbTestCase { 40 41 public ActionsUtilTest(String testName) { 42 super(testName); 43 } 44 45 protected void setUp() throws Exception { 46 } 47 48 73 private static final Object o = new Object (); 74 75 public void testCacheUpdatesCorrectly() throws Exception { 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 <Project> bothProjects = new HashSet <Project>(Arrays.asList(prj1, prj2)); 84 Set <Project> result = new HashSet <Project>(Arrays.asList(ActionsUtil.getProjectsFromLookup(projects, null))); 85 86 assertTrue(bothProjects.equals(result)); 87 88 boolean wasThrown = false; 91 92 try { 93 assertGC("", new WeakReference <Object >(o)); 94 } catch (AssertionFailedError e) { 95 wasThrown = true; 97 } 98 99 assertTrue(wasThrown); 100 101 projects.setLookupsOverride(new Lookup[] {prj1.getLookup()}); 102 103 Set <Project> firstProject = new HashSet <Project>(Arrays.asList(prj1)); 104 105 result = new HashSet <Project>(Arrays.asList(ActionsUtil.getProjectsFromLookup(projects, null))); 106 107 assertTrue(firstProject.equals(result)); 108 109 projects.setLookupsOverride(new Lookup[] {}); 110 111 result = new HashSet <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 <Project>(Arrays.asList(ActionsUtil.getProjectsFromLookup(projects, null))); 118 119 assertTrue(bothProjects.equals(result)); 120 } 121 122 public void testCanBeReclaimed() throws Exception { 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 <?> ref1 = new WeakReference <Object >(prj1); 133 WeakReference <?> ref2 = new WeakReference <Object >(prj2); 134 WeakReference <?> lookup = new WeakReference <Object >(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 { 146 Project prj1 = new DummyProject(); 147 Project prj2 = new DummyProject(); 148 Lookup projects = Lookups.fixed(new Object [] { 149 prj1, 150 prj2, 151 }); 152 153 ActionsUtil.getProjectsFromLookup(projects, null); 154 155 WeakReference <?> ref1 = new WeakReference <Object >(prj1); 156 WeakReference <?> ref2 = new WeakReference <Object >(prj2); 157 WeakReference <?> lookup = new WeakReference <Object >(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 |