KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > mimelookup > impl > Depr_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.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 /** Testing functionality of dynamic change over inherited folders
39  * Testing a deprecated MimePath.childLookup behaviour
40  *
41  * @author Martin Roskanin
42  */

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 JavaDoc fsstruct [];
49     
50     public Depr_MimeLookupPerformanceTest(java.lang.String JavaDoc testName) {
51         super(testName);
52     }
53     
54     protected void setUp() throws java.lang.Exception JavaDoc {
55         fsstruct = new String JavaDoc [] {
56             "Editors/Popup/org-openide-actions-CutAction.instance", //NOI18N
57
"Editors/Popup/org-openide-actions-CopyAction.instance", //NOI18N
58
"Editors/Popup/org-openide-actions-PasteAction.instance", //NOI18N
59
"Editors/text/x-java/Popup/org-openide-actions-DeleteAction.instance", //NOI18N
60
"Editors/text/x-java/Popup/org-openide-actions-RenameAction.instance", //NOI18N
61
"Editors/text/x-java/text/xml/Popup/org-openide-actions-PrintAction.instance", //NOI18N
62
"Editors/text/x-java/text/xml/text/html/Popup/org-openide-actions-NewAction.instance", //NOI18N
63
};
64
65         EditorTestLookup.setLookup(fsstruct, getWorkDir(), new Object JavaDoc[] {},
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 JavaDoc obj){
80                     return false;
81                 }
82             };
83         }
84         return filter;
85     }
86
87     public void testMimeLookupObjectInstallingUninstallingSize() throws IOException JavaDoc{
88         MimeLookup lookup = MimeLookup.getMimeLookup("text/x-java").childLookup("text/xml"). //NOI18N
89
childLookup("text/html"); //NOI18N
90
PopupActions popup = (PopupActions) lookup.lookup(PopupActions.class);
91         List JavaDoc list = popup.getPopupActions();
92         checkPopupItemPresence(lookup, RenameAction.class, true);
93         int size = 0;
94         
95         for (int i=0; i<30; i++){
96             //delete RenameAction
97
TestUtilities.deleteFile(getWorkDir(),
98                     "Editors/text/x-java/Popup/org-openide-actions-RenameAction.instance");
99             checkPopupItemPresence(lookup, RenameAction.class, false);
100
101             //delete base CutAction
102
TestUtilities.deleteFile(getWorkDir(),
103                     "Editors/Popup/org-openide-actions-CutAction.instance");
104             checkPopupItemPresence(lookup, CutAction.class, false);
105
106             //simulate module installation, new action will be added
107
TestUtilities.createFile(getWorkDir(),
108                     "Editors/Popup/org-openide-actions-FindAction.instance"); //NOI18N
109
checkPopupItemPresence(lookup, FindAction.class, true);
110
111             // now reverse the operations
112

113             //simulate module installation, new action will be added
114
TestUtilities.createFile(getWorkDir(),
115                     "Editors/text/x-java/Popup/org-openide-actions-RenameAction.instance"); //NOI18N
116
checkPopupItemPresence(lookup, RenameAction.class, true);
117
118             TestUtilities.createFile(getWorkDir(),
119                     "Editors/Popup/org-openide-actions-CutAction.instance"); //NOI18N
120
checkPopupItemPresence(lookup, CutAction.class, true);
121
122             //delete RenameAction
123
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 JavaDoc[] {lookup} ), 10000000, getFilter());
130             }
131         }
132         gc(); gc();
133         assertSize("", size + 3000, lookup); // 3000 is threshold
134
}
135     
136     public void testClassLookup() throws IOException JavaDoc{
137         MimeLookup lookup = MimeLookup.getMimeLookup("text/x-java").childLookup("text/xml"). //NOI18N
138
childLookup("text/html"); //NOI18N
139
PopupActions popup = (PopupActions) lookup.lookup(PopupActions.class);
140         List JavaDoc list = popup.getPopupActions();
141         checkPopupItemPresence(lookup, RenameAction.class, true);
142         gc();
143         int size = assertSize("", Arrays.asList( new Object JavaDoc[] {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 JavaDoc{
154         MimeLookup lookup = MimeLookup.getMimeLookup("text/x-java").childLookup("text/xml"). //NOI18N
155
childLookup("text/html"); //NOI18N
156
Result result = lookup.lookup(new Template(PopupActions.class));
157         Collection JavaDoc col = result.allInstances();
158         checkPopupItemPresence(lookup, RenameAction.class, true);
159         gc();
160         int size = assertSize("", Arrays.asList( new Object JavaDoc[] {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 JavaDoc checkedClazz, final boolean shouldBePresent){
173         TestUtilities.waitMaxMilisForValue(WAIT_TIME, new TestUtilities.ValueResolver(){
174             public Object JavaDoc getValue(){
175                 PopupActions pa = (PopupActions)lookup.lookup(PopupActions.class);
176                 if (pa == null){
177                     return Boolean.FALSE;
178                 }
179                 boolean bool = false;
180                 List JavaDoc items = pa.getPopupActions();
181                 for (int i=0; i<items.size(); i++){
182                     Object JavaDoc 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 JavaDoc items = pa.getPopupActions();
198         for (int i=0; i<items.size(); i++){
199             Object JavaDoc 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