KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > mimelookup > impl > Depr_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.junit.NbTestCase;
27 import org.openide.actions.CopyAction;
28 import org.openide.actions.CutAction;
29 import org.openide.actions.DeleteAction;
30 import org.openide.actions.PasteAction;
31 import org.openide.actions.ReplaceAction;
32 import org.openide.actions.FindAction;
33 import org.openide.actions.NewAction;
34
35
36 /** Testing merging and sorting merged objects
37  * Testing a deprecated MimePath.childLookup behaviour
38  *
39  * @author Martin Roskanin
40  */

41 public class Depr_MimeLookupInheritanceTest extends NbTestCase {
42     
43     private static final int WAIT_TIME = 2000;
44     
45     public Depr_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(MimeLookup 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         MimeLookup lookup = MimeLookup.getMimeLookup("text/x-java").childLookup("text/html"); //NOI18N
91
Class JavaDoc layerObjects[] = {CutAction.class, DeleteAction.class, CopyAction.class,
92                 NewAction.class, PasteAction.class};
93         testPopupItems(lookup, layerObjects);
94     }
95
96     /** Testing MIME level popup items lookup, inheritance and sorting */
97     public void testMimeLevelPopupsWithStringAndSeparator(){
98         MimeLookup lookup = MimeLookup.getMimeLookup("text/x-java").childLookup("text/html").childLookup("text/xml"); //NOI18N
99
Class JavaDoc layerObjects[] = {CutAction.class, DeleteAction.class, CopyAction.class,
100                 NewAction.class, PasteAction.class, ReplaceAction.class, JSeparator JavaDoc.class, String JavaDoc.class};
101         testPopupItems(lookup, layerObjects);
102     }
103
104     /**
105      * Issue #61216: MimeLookup should support layer hidding
106      */

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

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

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

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