KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > mimelookup > impl > MimeLookupPerformanceTest


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.editor.mimelookup.impl;
21
22 import java.io.IOException JavaDoc;
23 import java.util.Arrays JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.List JavaDoc;
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 /** Testing functionality of dynamic change over inherited folders
41  *
42  * @author Martin Roskanin
43  */

44 public class MimeLookupPerformanceTest extends NbTestCase {
45
46     private static final int WAIT_TIME = 5000;
47     private static MemoryFilter filter;
48     
49     private String JavaDoc fsstruct [];
50     
51     public MimeLookupPerformanceTest(java.lang.String JavaDoc testName) {
52         super(testName);
53     }
54     
55     protected void setUp() throws java.lang.Exception JavaDoc {
56         fsstruct = new String JavaDoc [] {
57             "Editors/Popup/org-openide-actions-CutAction.instance", //NOI18N
58
"Editors/Popup/org-openide-actions-CopyAction.instance", //NOI18N
59
"Editors/Popup/org-openide-actions-PasteAction.instance", //NOI18N
60
"Editors/text/x-java/Popup/org-openide-actions-DeleteAction.instance", //NOI18N
61
"Editors/text/x-java/Popup/org-openide-actions-RenameAction.instance", //NOI18N
62
"Editors/text/x-java/text/xml/Popup/org-openide-actions-PrintAction.instance", //NOI18N
63
"Editors/text/x-java/text/xml/text/html/Popup/org-openide-actions-NewAction.instance", //NOI18N
64
};
65
66         EditorTestLookup.setLookup(fsstruct, getWorkDir(), new Object JavaDoc[] {},
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 JavaDoc ex) {
77                 // ignore
78
}
79         }
80     }
81
82     private static synchronized MemoryFilter getFilter(){
83         if (filter == null){
84             filter = new MemoryFilter(){
85                 public boolean reject(Object JavaDoc obj){
86                     return false;
87                 }
88             };
89         }
90         return filter;
91     }
92
93     public void testMimeLookupObjectInstallingUninstallingSize() throws IOException JavaDoc{
94         MimePath mp = MimePath.get(MimePath.get("text/x-java"), "text/xml"); //NOI18N
95
Lookup lookup = MimeLookup.getLookup(mp);
96         PopupActions popup = (PopupActions) lookup.lookup(PopupActions.class);
97         List JavaDoc list = popup.getPopupActions();
98         checkPopupItemPresence(lookup, RenameAction.class, true);
99         int size = 0;
100         
101         for (int i=0; i<30; i++){
102             //delete RenameAction
103
TestUtilities.deleteFile(getWorkDir(),
104                     "Editors/text/x-java/Popup/org-openide-actions-RenameAction.instance");
105             checkPopupItemPresence(lookup, RenameAction.class, false);
106
107             //delete base CutAction
108
TestUtilities.deleteFile(getWorkDir(),
109                     "Editors/Popup/org-openide-actions-CutAction.instance");
110             checkPopupItemPresence(lookup, CutAction.class, false);
111
112             //simulate module installation, new action will be added
113
TestUtilities.createFile(getWorkDir(),
114                     "Editors/Popup/org-openide-actions-FindAction.instance"); //NOI18N
115
checkPopupItemPresence(lookup, FindAction.class, true);
116
117             // now reverse the operations
118

119             //simulate module installation, new action will be added
120
TestUtilities.createFile(getWorkDir(),
121                     "Editors/text/x-java/Popup/org-openide-actions-RenameAction.instance"); //NOI18N
122
checkPopupItemPresence(lookup, RenameAction.class, true);
123
124             TestUtilities.createFile(getWorkDir(),
125                     "Editors/Popup/org-openide-actions-CutAction.instance"); //NOI18N
126
checkPopupItemPresence(lookup, CutAction.class, true);
127
128             //delete RenameAction
129
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 JavaDoc[] {lookup} ), 10000000, getFilter());
136             }
137         }
138         gc(); gc();
139         assertSize("", size + 3000, lookup); // 3000 is threshold
140
}
141     
142     public void testClassLookuping() throws IOException JavaDoc{
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 JavaDoc list = popup.getPopupActions();
147         checkPopupItemPresence(lookup, RenameAction.class, true);
148         gc();
149         int size = assertSize("", Arrays.asList( new Object JavaDoc[] {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 JavaDoc{
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 JavaDoc col = result.allInstances();
164         checkPopupItemPresence(lookup, RenameAction.class, true);
165         gc();
166         int size = assertSize("", Arrays.asList( new Object JavaDoc[] {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 JavaDoc checkedClazz, final boolean shouldBePresent){
179         TestUtilities.waitMaxMilisForValue(WAIT_TIME, new TestUtilities.ValueResolver(){
180             public Object JavaDoc getValue(){
181                 PopupActions pa = (PopupActions)lookup.lookup(PopupActions.class);
182                 if (pa == null){
183                     return Boolean.FALSE;
184                 }
185                 boolean bool = false;
186                 List JavaDoc items = pa.getPopupActions();
187                 for (int i=0; i<items.size(); i++){
188                     Object JavaDoc 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 JavaDoc items = pa.getPopupActions();
204         for (int i=0; i<items.size(); i++){
205             Object JavaDoc 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