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.api.editor.mimelookup.MimePath; 29 import org.netbeans.junit.MemoryFilter; 30 import org.netbeans.junit.NbTestCase; 31 import org.openide.actions.CutAction; 32 import org.openide.actions.FindAction; 33 import org.openide.actions.NewAction; 34 import org.openide.actions.RenameAction; 35 import org.openide.actions.ReplaceAction; 36 import org.openide.util.Lookup; 37 import org.openide.util.Lookup.Result; 38 import org.openide.util.Lookup.Template; 39 40 44 public class MimeLookupPerformanceTest extends NbTestCase { 45 46 private static final int WAIT_TIME = 5000; 47 private static MemoryFilter filter; 48 49 private String fsstruct []; 50 51 public MimeLookupPerformanceTest(java.lang.String testName) { 52 super(testName); 53 } 54 55 protected void setUp() throws java.lang.Exception { 56 fsstruct = new String [] { 57 "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", }; 65 66 EditorTestLookup.setLookup(fsstruct, getWorkDir(), new Object [] {}, 67 getClass().getClassLoader()); 68 69 } 70 71 private void gc(){ 72 for (int i = 0; i<15; i++){ 73 System.gc(); 74 try { 75 Thread.sleep(123); 76 } catch (InterruptedException ex) { 77 } 79 } 80 } 81 82 private static synchronized MemoryFilter getFilter(){ 83 if (filter == null){ 84 filter = new MemoryFilter(){ 85 public boolean reject(Object obj){ 86 return false; 87 } 88 }; 89 } 90 return filter; 91 } 92 93 public void testMimeLookupObjectInstallingUninstallingSize() throws IOException { 94 MimePath mp = MimePath.get(MimePath.get("text/x-java"), "text/xml"); Lookup lookup = MimeLookup.getLookup(mp); 96 PopupActions popup = (PopupActions) lookup.lookup(PopupActions.class); 97 List list = popup.getPopupActions(); 98 checkPopupItemPresence(lookup, RenameAction.class, true); 99 int size = 0; 100 101 for (int i=0; i<30; i++){ 102 TestUtilities.deleteFile(getWorkDir(), 104 "Editors/text/x-java/Popup/org-openide-actions-RenameAction.instance"); 105 checkPopupItemPresence(lookup, RenameAction.class, false); 106 107 TestUtilities.deleteFile(getWorkDir(), 109 "Editors/Popup/org-openide-actions-CutAction.instance"); 110 checkPopupItemPresence(lookup, CutAction.class, false); 111 112 TestUtilities.createFile(getWorkDir(), 114 "Editors/Popup/org-openide-actions-FindAction.instance"); checkPopupItemPresence(lookup, FindAction.class, true); 116 117 119 TestUtilities.createFile(getWorkDir(), 121 "Editors/text/x-java/Popup/org-openide-actions-RenameAction.instance"); checkPopupItemPresence(lookup, RenameAction.class, true); 123 124 TestUtilities.createFile(getWorkDir(), 125 "Editors/Popup/org-openide-actions-CutAction.instance"); checkPopupItemPresence(lookup, CutAction.class, true); 127 128 TestUtilities.deleteFile(getWorkDir(), 130 "Editors/Popup/org-openide-actions-FindAction.instance"); 131 checkPopupItemPresence(lookup, FindAction.class, false); 132 133 if (i == 0){ 134 gc(); 135 size = assertSize("", Arrays.asList( new Object [] {lookup} ), 10000000, getFilter()); 136 } 137 } 138 gc(); gc(); 139 assertSize("", size + 3000, lookup); } 141 142 public void testClassLookuping() throws IOException { 143 MimePath mp = MimePath.parse("text/x-java/text/xml/text/html"); 144 Lookup lookup = MimeLookup.getLookup(mp); 145 PopupActions popup = (PopupActions) lookup.lookup(PopupActions.class); 146 List list = popup.getPopupActions(); 147 checkPopupItemPresence(lookup, RenameAction.class, true); 148 gc(); 149 int size = assertSize("", Arrays.asList( new Object [] {lookup} ), 10000000, getFilter()); 150 for (int i=0; i<30; i++){ 151 popup = (PopupActions) lookup.lookup(PopupActions.class); 152 list = popup.getPopupActions(); 153 checkPopupItemPresence(lookup, RenameAction.class, true); 154 } 155 gc(); 156 assertSize("", size, lookup); 157 } 158 159 public void testTemplateLookuping() throws IOException { 160 MimePath mp = MimePath.parse("text/x-java/text/xml/text/html"); 161 Lookup lookup = MimeLookup.getLookup(mp); 162 Result result = lookup.lookup(new Template(PopupActions.class)); 163 Collection col = result.allInstances(); 164 checkPopupItemPresence(lookup, RenameAction.class, true); 165 gc(); 166 int size = assertSize("", Arrays.asList( new Object [] {lookup} ), 10000000, getFilter()); 167 for (int i=0; i<30; i++){ 168 result = lookup.lookup(new Template(PopupActions.class)); 169 col = result.allInstances(); 170 checkPopupItemPresence(lookup, RenameAction.class, true); 171 } 172 gc(); 173 assertSize("", size, lookup); 174 } 175 176 177 178 private void checkPopupItemPresence(final Lookup lookup, final Class checkedClazz, final boolean shouldBePresent){ 179 TestUtilities.waitMaxMilisForValue(WAIT_TIME, new TestUtilities.ValueResolver(){ 180 public Object getValue(){ 181 PopupActions pa = (PopupActions)lookup.lookup(PopupActions.class); 182 if (pa == null){ 183 return Boolean.FALSE; 184 } 185 boolean bool = false; 186 List items = pa.getPopupActions(); 187 for (int i=0; i<items.size(); i++){ 188 Object obj = items.get(i); 189 if (checkedClazz == obj.getClass()){ 190 bool = true; 191 break; 192 } 193 } 194 if (!shouldBePresent){ 195 bool = !bool; 196 } 197 return Boolean.valueOf(bool); 198 } 199 }, Boolean.TRUE); 200 PopupActions pa = (PopupActions)lookup.lookup(PopupActions.class); 201 assertTrue("PopupActions should be found", pa != null); 202 boolean bool = false; 203 List items = pa.getPopupActions(); 204 for (int i=0; i<items.size(); i++){ 205 Object obj = items.get(i); 206 if (checkedClazz == obj.getClass()){ 207 bool = true; 208 break; 209 } 210 } 211 if (shouldBePresent){ 212 assertTrue("Class: "+checkedClazz+" should be present in lookup", bool); 213 }else{ 214 assertTrue("Class: "+checkedClazz+" should not be present in lookup", !bool); 215 } 216 } 217 218 219 } 220 | Popular Tags |