1 19 20 package org.netbeans.modules.editor.mimelookup.impl; 21 22 import java.io.IOException ; 23 import java.util.Arrays ; 24 import java.util.Collection ; 25 import java.util.List ; 26 import junit.framework.*; 27 import org.netbeans.api.editor.mimelookup.MimeLookup; 28 import org.netbeans.junit.MemoryFilter; 29 import org.netbeans.junit.NbTestCase; 30 import org.openide.actions.CutAction; 31 import org.openide.actions.FindAction; 32 import org.openide.actions.NewAction; 33 import org.openide.actions.RenameAction; 34 import org.openide.actions.ReplaceAction; 35 import org.openide.util.Lookup.Result; 36 import org.openide.util.Lookup.Template; 37 38 43 public class Depr_MimeLookupPerformanceTest extends NbTestCase { 44 45 private static final int WAIT_TIME = 5000; 46 private static MemoryFilter filter; 47 48 private String fsstruct []; 49 50 public Depr_MimeLookupPerformanceTest(java.lang.String testName) { 51 super(testName); 52 } 53 54 protected void setUp() throws java.lang.Exception { 55 fsstruct = new String [] { 56 "Editors/Popup/org-openide-actions-CutAction.instance", "Editors/Popup/org-openide-actions-CopyAction.instance", "Editors/Popup/org-openide-actions-PasteAction.instance", "Editors/text/x-java/Popup/org-openide-actions-DeleteAction.instance", "Editors/text/x-java/Popup/org-openide-actions-RenameAction.instance", "Editors/text/x-java/text/xml/Popup/org-openide-actions-PrintAction.instance", "Editors/text/x-java/text/xml/text/html/Popup/org-openide-actions-NewAction.instance", }; 64 65 EditorTestLookup.setLookup(fsstruct, getWorkDir(), new Object [] {}, 66 getClass().getClassLoader()); 67 68 } 69 70 private void gc(){ 71 for (int i = 0; i<5; i++){ 72 System.gc(); 73 } 74 } 75 76 private static synchronized MemoryFilter getFilter(){ 77 if (filter == null){ 78 filter = new MemoryFilter(){ 79 public boolean reject(Object obj){ 80 return false; 81 } 82 }; 83 } 84 return filter; 85 } 86 87 public void testMimeLookupObjectInstallingUninstallingSize() throws IOException { 88 MimeLookup lookup = MimeLookup.getMimeLookup("text/x-java").childLookup("text/xml"). childLookup("text/html"); PopupActions popup = (PopupActions) lookup.lookup(PopupActions.class); 91 List list = popup.getPopupActions(); 92 checkPopupItemPresence(lookup, RenameAction.class, true); 93 int size = 0; 94 95 for (int i=0; i<30; i++){ 96 TestUtilities.deleteFile(getWorkDir(), 98 "Editors/text/x-java/Popup/org-openide-actions-RenameAction.instance"); 99 checkPopupItemPresence(lookup, RenameAction.class, false); 100 101 TestUtilities.deleteFile(getWorkDir(), 103 "Editors/Popup/org-openide-actions-CutAction.instance"); 104 checkPopupItemPresence(lookup, CutAction.class, false); 105 106 TestUtilities.createFile(getWorkDir(), 108 "Editors/Popup/org-openide-actions-FindAction.instance"); checkPopupItemPresence(lookup, FindAction.class, true); 110 111 113 TestUtilities.createFile(getWorkDir(), 115 "Editors/text/x-java/Popup/org-openide-actions-RenameAction.instance"); checkPopupItemPresence(lookup, RenameAction.class, true); 117 118 TestUtilities.createFile(getWorkDir(), 119 "Editors/Popup/org-openide-actions-CutAction.instance"); checkPopupItemPresence(lookup, CutAction.class, true); 121 122 TestUtilities.deleteFile(getWorkDir(), 124 "Editors/Popup/org-openide-actions-FindAction.instance"); 125 checkPopupItemPresence(lookup, FindAction.class, false); 126 127 if (i == 0){ 128 gc(); 129 size = assertSize("", Arrays.asList( new Object [] {lookup} ), 10000000, getFilter()); 130 } 131 } 132 gc(); gc(); 133 assertSize("", size + 3000, lookup); } 135 136 public void testClassLookup() throws IOException { 137 MimeLookup lookup = MimeLookup.getMimeLookup("text/x-java").childLookup("text/xml"). childLookup("text/html"); PopupActions popup = (PopupActions) lookup.lookup(PopupActions.class); 140 List list = popup.getPopupActions(); 141 checkPopupItemPresence(lookup, RenameAction.class, true); 142 gc(); 143 int size = assertSize("", Arrays.asList( new Object [] {lookup} ), 10000000, getFilter()); 144 for (int i=0; i<30; i++){ 145 popup = (PopupActions) lookup.lookup(PopupActions.class); 146 list = popup.getPopupActions(); 147 checkPopupItemPresence(lookup, RenameAction.class, true); 148 } 149 gc(); 150 assertSize("", size, lookup); 151 } 152 153 public void testTemplateLookup() throws IOException { 154 MimeLookup lookup = MimeLookup.getMimeLookup("text/x-java").childLookup("text/xml"). childLookup("text/html"); Result result = lookup.lookup(new Template(PopupActions.class)); 157 Collection col = result.allInstances(); 158 checkPopupItemPresence(lookup, RenameAction.class, true); 159 gc(); 160 int size = assertSize("", Arrays.asList( new Object [] {lookup} ), 10000000, getFilter()); 161 for (int i=0; i<30; i++){ 162 result = lookup.lookup(new Template(PopupActions.class)); 163 col = result.allInstances(); 164 checkPopupItemPresence(lookup, RenameAction.class, true); 165 } 166 gc(); 167 assertSize("", size, lookup); 168 } 169 170 171 172 private void checkPopupItemPresence(final MimeLookup lookup, final Class checkedClazz, final boolean shouldBePresent){ 173 TestUtilities.waitMaxMilisForValue(WAIT_TIME, new TestUtilities.ValueResolver(){ 174 public Object getValue(){ 175 PopupActions pa = (PopupActions)lookup.lookup(PopupActions.class); 176 if (pa == null){ 177 return Boolean.FALSE; 178 } 179 boolean bool = false; 180 List items = pa.getPopupActions(); 181 for (int i=0; i<items.size(); i++){ 182 Object obj = items.get(i); 183 if (checkedClazz == obj.getClass()){ 184 bool = true; 185 break; 186 } 187 } 188 if (!shouldBePresent){ 189 bool = !bool; 190 } 191 return Boolean.valueOf(bool); 192 } 193 }, Boolean.TRUE); 194 PopupActions pa = (PopupActions)lookup.lookup(PopupActions.class); 195 assertTrue("PopupActions should be found", pa != null); 196 boolean bool = false; 197 List items = pa.getPopupActions(); 198 for (int i=0; i<items.size(); i++){ 199 Object obj = items.get(i); 200 if (checkedClazz == obj.getClass()){ 201 bool = true; 202 break; 203 } 204 } 205 if (shouldBePresent){ 206 assertTrue("Class: "+checkedClazz+" should be present in lookup", bool); 207 }else{ 208 assertTrue("Class: "+checkedClazz+" should not be present in lookup", !bool); 209 } 210 } 211 212 213 } 214 | Popular Tags |