KickJava   Java API By Example, From Geeks To Geeks.

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


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.List JavaDoc;
24 import junit.framework.*;
25 import org.netbeans.api.editor.mimelookup.MimeLookup;
26 import org.netbeans.api.editor.mimelookup.MimePath;
27 import org.netbeans.junit.NbTestCase;
28 import org.openide.actions.CutAction;
29 import org.openide.actions.FindAction;
30 import org.openide.actions.NewAction;
31 import org.openide.actions.RenameAction;
32 import org.openide.actions.ReplaceAction;
33 import org.openide.util.Lookup;
34 import org.openide.util.Lookup.Template;
35 import org.openide.util.LookupEvent;
36 import org.openide.util.LookupListener;
37
38 /** Testing functionality of dynamic change over inherited folders
39  *
40  * @author Martin Roskanin
41  */

42 public class MimeLookupPopupItemsChangeTest extends NbTestCase {
43     
44     private static final int WAIT_TIME = 5000;
45     private String JavaDoc fsstruct [];
46     
47     public MimeLookupPopupItemsChangeTest(java.lang.String JavaDoc testName) {
48         super(testName);
49     }
50     
51     protected void setUp() throws java.lang.Exception JavaDoc {
52         fsstruct = new String JavaDoc [] {
53             "Editors/Popup/org-openide-actions-CutAction.instance", //NOI18N
54
"Editors/Popup/org-openide-actions-CopyAction.instance", //NOI18N
55
"Editors/Popup/org-openide-actions-PasteAction.instance", //NOI18N
56
"Editors/text/x-java/Popup/org-openide-actions-DeleteAction.instance", //NOI18N
57
"Editors/text/x-java/Popup/org-openide-actions-RenameAction.instance", //NOI18N
58
"Editors/text/x-java/text/xml/Popup/org-openide-actions-PrintAction.instance", //NOI18N
59
"Editors/text/x-java/text/xml/text/html/Popup/org-openide-actions-NewAction.instance", //NOI18N
60
};
61
62         EditorTestLookup.setLookup(fsstruct, getWorkDir(), new Object JavaDoc[] {},
63                    getClass().getClassLoader());
64         
65     }
66
67     /** Testing Base level popup items lookup and sorting */
68     public void testDynamicChangeInPopupFolders() throws IOException JavaDoc{
69         final int resultChangedCount[] = new int[1];
70         resultChangedCount[0] = 0;
71
72         MimePath mp = MimePath.parse("text/x-java/text/xml/text/html");
73         Lookup lookup = MimeLookup.getLookup(mp);
74         Lookup.Result result = lookup.lookup(new Template(PopupActions.class));
75         result.allInstances(); // remove this line if issue #60010 is fixed
76
LookupListener listener = new LookupListener(){
77             public void resultChanged(LookupEvent ev){
78                 resultChangedCount[0]++;
79             }
80         };
81         result.addLookupListener(listener);
82         PopupActions actions = (PopupActions) lookup.lookup(PopupActions.class);
83         assertTrue("PopupActions should be found", actions != null);
84         List JavaDoc popupActions = actions.getPopupActions();
85         int size = popupActions.size();
86         assertTrue("Number of PopupActions found:"+size+" and should be:"+fsstruct.length, size == fsstruct.length);
87
88         //delete RenameAction
89
TestUtilities.deleteFile(getWorkDir(),
90                 "Editors/text/x-java/Popup/org-openide-actions-RenameAction.instance");
91         checkPopupItemPresence(lookup, RenameAction.class, false);
92
93         // check firing the change
94
assertTrue(("resultChangedCount is:"+resultChangedCount[0]+" instead of 1"),resultChangedCount[0] == 1);
95         resultChangedCount[0] = 0;
96         
97         //delete base CutAction
98
TestUtilities.deleteFile(getWorkDir(),
99                 "Editors/Popup/org-openide-actions-CutAction.instance");
100
101         checkPopupItemPresence(lookup, CutAction.class, false);
102
103         // check firing the change
104
assertTrue(("resultChangedCount is:"+resultChangedCount[0]+" instead of 1"),resultChangedCount[0] == 1);
105         resultChangedCount[0] = 0;
106         
107         //simulate module installation, new action will be added
108
TestUtilities.createFile(getWorkDir(),
109                 "Editors/Popup/org-openide-actions-FindAction.instance"); //NOI18N
110

111         checkPopupItemPresence(lookup, FindAction.class, true);
112
113         // check firing the change
114
assertTrue(("resultChangedCount is:"+resultChangedCount[0]+" instead of 1"),resultChangedCount[0] == 1);
115         resultChangedCount[0] = 0;
116         
117         //simulate module installation, new action will be added
118
TestUtilities.createFile(getWorkDir(),
119                 "Editors/text/x-java/text/xml/text/html/Popup/org-openide-actions-ReplaceAction.instance"); //NOI18N
120

121         checkPopupItemPresence(lookup, ReplaceAction.class, true);
122         
123         //ReplaceAction was created in the uppermost folder
124
// let's try it is missing in the lower lookup
125
mp = MimePath.get(MimePath.get("text/x-java"), "text/xml");
126         lookup = MimeLookup.getLookup(mp);
127         checkPopupItemPresence(lookup, ReplaceAction.class, false);
128         checkPopupItemPresence(lookup, FindAction.class, true);
129         
130         // lookup for ReplaceAction in the folder that doesn't exist
131
lookup = MimeLookup.getMimeLookup("text/html"); //NOI18N
132
checkPopupItemPresence(lookup, ReplaceAction.class, false);
133         // create folder with ReplaceAction
134
TestUtilities.createFile(getWorkDir(),
135                 "Editors/text/html/Popup/org-openide-actions-ReplaceAction.instance"); //NOI18N
136

137         checkPopupItemPresence(lookup, ReplaceAction.class, true);
138         
139     }
140
141     private void checkPopupItemPresence(final Lookup lookup, final Class JavaDoc checkedClazz, final boolean shouldBePresent){
142         TestUtilities.waitMaxMilisForValue(WAIT_TIME, new TestUtilities.ValueResolver(){
143             public Object JavaDoc getValue(){
144                 PopupActions pa = (PopupActions)lookup.lookup(PopupActions.class);
145                 if (pa == null){
146                     return Boolean.FALSE;
147                 }
148                 boolean bool = false;
149                 List JavaDoc items = pa.getPopupActions();
150                 for (int i=0; i<items.size(); i++){
151                     Object JavaDoc obj = items.get(i);
152                     if (checkedClazz == obj.getClass()){
153                         bool = true;
154                         break;
155                     }
156                 }
157                 if (!shouldBePresent){
158                     bool = !bool;
159                 }
160                 return Boolean.valueOf(bool);
161             }
162         }, Boolean.TRUE);
163         PopupActions pa = (PopupActions)lookup.lookup(PopupActions.class);
164         assertTrue("PopupActions should be found", pa != null);
165         boolean bool = false;
166         List JavaDoc items = pa.getPopupActions();
167         for (int i=0; i<items.size(); i++){
168             Object JavaDoc obj = items.get(i);
169             if (checkedClazz == obj.getClass()){
170                 bool = true;
171                 break;
172             }
173         }
174         if (shouldBePresent){
175             assertTrue("Class: "+checkedClazz+" should be present in lookup", bool);
176         }else{
177             assertTrue("Class: "+checkedClazz+" should not be present in lookup", !bool);
178         }
179     }
180     
181
182 }
183
Popular Tags