KickJava   Java API By Example, From Geeks To Geeks.

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


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.net.URL JavaDoc;
23 import java.util.List JavaDoc;
24 import javax.swing.JSeparator JavaDoc;
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.CopyAction;
29 import org.openide.actions.CutAction;
30 import org.openide.actions.DeleteAction;
31 import org.openide.actions.PasteAction;
32 import org.openide.actions.ReplaceAction;
33 import org.openide.actions.FindAction;
34 import org.openide.actions.NewAction;
35 import org.openide.util.Lookup;
36
37 /** Testing merging and sorting merged objects
38  *
39  * @author Martin Roskanin
40  */

41 public class MimeLookupInheritanceTest extends NbTestCase {
42     
43     private static final int WAIT_TIME = 2000;
44     
45     public MimeLookupInheritanceTest(java.lang.String JavaDoc testName) {
46         super(testName);
47     }
48     
49     protected void setUp() throws java.lang.Exception JavaDoc {
50         // Set up the default lookup, repository, etc.
51
EditorTestLookup.setLookup(
52             new URL JavaDoc[] {
53                 getClass().getResource("test-layer.xml")
54             },
55             getWorkDir(),
56             new Object JavaDoc[] {},
57             getClass().getClassLoader(),
58             null
59         );
60     }
61
62     private void testPopupItems(Lookup lookup, Class JavaDoc[] layerObjects){
63         PopupActions actions = (PopupActions) lookup.lookup(PopupActions.class);
64         assertTrue("PopupActions should be found", actions != null);
65         if (actions != null){
66             List JavaDoc popupActions = actions.getPopupActions();
67             int popupSize = popupActions.size();
68             assertTrue("popupActions count is not the same as etalon action count" +
69                     "Expecting:"+layerObjects.length+" Found:"+popupSize,
70                     popupSize == layerObjects.length);
71             
72             for (int i = 0; i<layerObjects.length; i++){
73                 Object JavaDoc obj = popupActions.get(i);
74                 assertTrue("Incorrect sorting or item is missing in the popup menu." +
75                         "Expecting:"+layerObjects[i]+" Found:"+obj.getClass(),
76                         layerObjects[i].isAssignableFrom(obj.getClass()));
77             }
78         }
79     }
80     
81     /** Testing Base level popup items lookup and sorting */
82     public void testBaseLevelPopups(){
83         MimeLookup lookup = MimeLookup.getMimeLookup(""); //NOI18N
84
Class JavaDoc layerObjects[] = {CutAction.class, CopyAction.class, PasteAction.class};
85         testPopupItems(lookup, layerObjects);
86     }
87
88     /** Testing MIME level popup items lookup, inheritance and sorting */
89     public void testMimeLevelPopups(){
90         MimePath mp = MimePath.parse("text/x-java/text/html"); //NOI18N
91
Lookup lookup = MimeLookup.getLookup(mp);
92         Class JavaDoc layerObjects[] = {CutAction.class, DeleteAction.class, CopyAction.class,
93                 NewAction.class, PasteAction.class};
94         testPopupItems(lookup, layerObjects);
95     }
96
97     /** Testing MIME level popup items lookup, inheritance and sorting */
98     public void testMimeLevelPopupsWithStringAndSeparator(){
99         MimePath mp = MimePath.parse("text/x-java/text/html/text/xml"); //NOI18N
100
Lookup lookup = MimeLookup.getLookup(mp);
101         Class JavaDoc layerObjects[] = {CutAction.class, DeleteAction.class, CopyAction.class,
102                 NewAction.class, PasteAction.class, ReplaceAction.class, JSeparator JavaDoc.class, String JavaDoc.class};
103         testPopupItems(lookup, layerObjects);
104     }
105
106     /**
107      * Issue #61216: MimeLookup should support layer hidding
108      */

109     public void testHidding(){
110         Lookup lookup = MimeLookup.getLookup(MimePath.get("text/xml"));
111         checkLookupObject(lookup, CopyAction.class, true);
112         checkLookupObject(lookup, ReplaceAction.class, true);
113         checkLookupObject(lookup, PasteAction.class, false);
114         lookup = MimeLookup.getLookup(MimePath.get("text/x-ant+xml"));
115         checkLookupObject(lookup, CutAction.class, true);
116         checkLookupObject(lookup, CopyAction.class, false);
117         checkLookupObject(lookup, PasteAction.class, true);
118         checkLookupObject(lookup, ReplaceAction.class, false);
119     }
120     
121     /**
122      * Issue #61245: Delegate application/*+xml -> text/xml
123      */

124     public void test61245(){
125         MimeLookup lookup = MimeLookup.getMimeLookup("application/xml");
126         checkLookupObject(lookup, FindAction.class, true);
127         lookup = MimeLookup.getMimeLookup("application/xhtml+xml");
128         checkLookupObject(lookup, CutAction.class, true);
129         checkLookupObject(lookup, FindAction.class, false);
130         checkLookupObject(lookup, ReplaceAction.class, true);
131     }
132     
133     public void testAntXmlPopup(){
134         MimeLookup lookup = MimeLookup.getMimeLookup("text/xml"); //NOI18N
135
Class JavaDoc layerObjects[] = {CutAction.class, CopyAction.class, PasteAction.class, ReplaceAction.class};
136         testPopupItems(lookup, layerObjects);
137         lookup = MimeLookup.getMimeLookup("text/x-ant+xml"); //NOI18N
138
Class JavaDoc layerObjects2[] = {CutAction.class, CopyAction.class, PasteAction.class, ReplaceAction.class, FindAction.class};
139         testPopupItems(lookup, layerObjects2);
140     }
141     
142     /** Method will wait max. <code> maxMiliSeconds </code> miliseconds for the <code> requiredValue </code>
143      * gathered by <code> resolver </code>.
144      *
145      * @param maxMiliSeconds maximum time to wait for requiredValue
146      * @param resolver resolver, which is gathering an actual value
147      * @param requiredValue if resolver value equals requiredValue the wait cycle is finished
148      *
149      * @return false if the given maxMiliSeconds time elapsed and the requiredValue wasn't obtained
150      */

151     protected boolean waitMaxMilisForValue(int maxMiliSeconds, ValueResolver resolver, Object JavaDoc requiredValue){
152         int time = (int) maxMiliSeconds / 100;
153         while (time > 0) {
154             Object JavaDoc resolvedValue = resolver.getValue();
155             if (requiredValue == null && resolvedValue == null){
156                 return true;
157             }
158             if (requiredValue != null && requiredValue.equals(resolvedValue)){
159                 return true;
160             }
161             try {
162                 Thread.currentThread().sleep(100);
163             } catch (InterruptedException JavaDoc ex) {
164                 time=0;
165             }
166             time--;
167         }
168         return false;
169     }
170     
171     /** Interface for value resolver needed for i.e. waitMaxMilisForValue method.
172      * For more details, please look at {@link #waitMaxMilisForValue()}.
173      */

174     public static interface ValueResolver{
175         /** Returns checked value */
176         Object JavaDoc getValue();
177     }
178
179     
180     private void checkLookupObject(final Lookup lookup, final Class JavaDoc clazz, final boolean shouldBePresent){
181         waitMaxMilisForValue(WAIT_TIME, new ValueResolver(){
182             public Object JavaDoc getValue(){
183                 Object JavaDoc obj = lookup.lookup(clazz);
184                 boolean bool = (shouldBePresent) ? obj != null : obj == null;
185                 return Boolean.valueOf(bool);
186             }
187         }, Boolean.TRUE);
188         Object JavaDoc obj = lookup.lookup(clazz);
189         if (shouldBePresent){
190             assertTrue("Object should be present in the lookup",obj!=null);
191         } else {
192             assertTrue("Object should NOT be present in the lookup",obj==null);
193         }
194     }
195     
196     
197 }
198
Popular Tags