KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > mimelookup > impl > Depr_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.junit.NbTestCase;
27 import org.openide.actions.CutAction;
28 import org.openide.actions.FindAction;
29 import org.openide.actions.NewAction;
30 import org.openide.actions.RenameAction;
31 import org.openide.actions.ReplaceAction;
32 import org.openide.util.Lookup;
33 import org.openide.util.Lookup.Template;
34 import org.openide.util.LookupEvent;
35 import org.openide.util.LookupListener;
36
37 /** Testing functionality of dynamic change over inherited folders
38  * Testing a deprecated MimePath.childLookup behaviour
39  *
40  * @author Martin Roskanin
41  */

42 public class Depr_MimeLookupPopupItemsChangeTest extends NbTestCase {
43     
44     private static final int WAIT_TIME = 5000;
45     private String JavaDoc fsstruct [];
46     
47     public Depr_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         MimeLookup lookup = MimeLookup.getMimeLookup("text/x-java").childLookup("text/xml"). //NOI18N
73
childLookup("text/html"); //NOI18N
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
lookup = MimeLookup.getMimeLookup("text/x-java").childLookup("text/xml"); //NOI18N
126
checkPopupItemPresence(lookup, ReplaceAction.class, false);
127         checkPopupItemPresence(lookup, FindAction.class, true);
128         
129         // lookup for ReplaceAction in the folder that doesn't exist
130
lookup = MimeLookup.getMimeLookup("text/html"); //NOI18N
131
checkPopupItemPresence(lookup, ReplaceAction.class, false);
132         // create folder with ReplaceAction
133
TestUtilities.createFile(getWorkDir(),
134                 "Editors/text/html/Popup/org-openide-actions-ReplaceAction.instance"); //NOI18N
135

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