KickJava   Java API By Example, From Geeks To Geeks.

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


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.Collection JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import org.netbeans.api.editor.mimelookup.MimeLookup;
26 import org.netbeans.junit.NbTestCase;
27 import org.openide.util.Lookup;
28 import org.openide.util.Lookup.Result;
29 import org.openide.util.Lookup.Template;
30 import org.openide.util.LookupEvent;
31 import org.openide.util.LookupListener;
32
33 /**
34  * Testing corrected functionality of found issues.
35  *
36  * @author Martin Roskanin
37  */

38 public class IssuesTest extends NbTestCase {
39     
40     private static final int WAIT_TIME = 5000;
41     private static final int WAIT_TIME_FIRING = 1500;
42     final int resultChangedCount[] = new int[1];
43     
44     public IssuesTest(java.lang.String JavaDoc testName) {
45         super(testName);
46     }
47     
48     protected void setUp() throws java.lang.Exception JavaDoc {
49         String JavaDoc fsstruct [] = new String JavaDoc [] {
50             "Editors/text/jsp/testLookup/org-netbeans-modules-editor-mimelookup-impl-TestLookupObject.instance", //NOI18N
51
"Editors/text/x-java/testLookupTwo/org-netbeans-modules-editor-mimelookup-impl-TestLookupObjectTwo.instance", //NOI18N
52
"Editors/testLookupTwo/org-netbeans-modules-editor-mimelookup-impl-TestLookupObjectTwo.instance", //NOI18N
53
};
54
55         EditorTestLookup.setLookup(fsstruct, getWorkDir(), new Object JavaDoc[] {},
56                    getClass().getClassLoader());
57         
58     }
59
60     private void createFile(String JavaDoc file) throws IOException JavaDoc{
61         TestUtilities.createFile(getWorkDir(), file); //NOI18N
62
}
63     
64     private void checkResultChange(final int count) throws IOException JavaDoc{
65         // wait for firing event
66
TestUtilities.waitMaxMilisForValue(WAIT_TIME_FIRING, new TestUtilities.ValueResolver(){
67             public Object JavaDoc getValue(){
68                 return Boolean.FALSE;
69             }
70         }, Boolean.TRUE);
71         assertTrue(("resultChangedCount is:"+resultChangedCount[0]+" instead of "+count), resultChangedCount[0] == count);
72     }
73     
74     /**
75      * Issues of changes in layer
76      */

77     public void testForChangeInLayer() throws IOException JavaDoc{
78         
79         // issue #63338
80
// http://www.netbeans.org/issues/show_bug.cgi?id=63338
81
// Subj: deadlock during showing annotations
82
// fix: deadlock occured in after inproper firing of lookup changed event.
83
// event was fired even in cases, the lookup listener should be quiet.
84
MimeLookup lookup = MimeLookup.getMimeLookup("text/jsp"); //NOI18N
85
Result result = lookup.lookup(new Template(TestLookupObject.class));
86         result.allInstances(); // remove this line if issue #60010 is fixed
87
LookupListener listener = new LookupListener(){
88             public void resultChanged(LookupEvent ev){
89                 resultChangedCount[0]++;
90             }
91         };
92         result.addLookupListener(listener);
93         
94         //simulate module installation, new file will be added
95
createFile("Editors/text/jsp/testLookup/org-openide-actions-PasteAction.instance"); //NOI18N
96

97         checkResultChange(0);
98         
99         TestUtilities.deleteFile(getWorkDir(),
100                 "Editors/text/jsp/testLookup/org-netbeans-modules-editor-mimelookup-impl-TestLookupObject.instance");
101
102         checkResultChange(1);
103         
104         result.removeLookupListener(listener);
105         resultChangedCount[0] = 0;
106         // end of issue #63338 ------------------------------------------------
107

108         
109         
110     }
111     
112     /** Issue #72873
113      * MimeLookup duplicates objects from default mimetype folder
114      */

115     public void testDoubleItems(){
116         MimeLookup lookup = MimeLookup.getMimeLookup("text/x-java"); //NOI18N
117
Result result = lookup.lookup(new Template(TestLookupObjectTwo.class));
118         Collection JavaDoc col = result.allInstances();
119         assertTrue(col.size() == 1);
120         
121         lookup = MimeLookup.getMimeLookup(""); //NOI18N
122
result = lookup.lookup(new Template(TestLookupObjectTwo.class));
123         col = result.allInstances();
124         assertTrue(col.size() == 1);
125         
126     }
127
128     
129     private void checkLookupObject(final MimeLookup lookup, final Class JavaDoc clazz, final boolean shouldBePresent){
130         TestUtilities.waitMaxMilisForValue(WAIT_TIME, new TestUtilities.ValueResolver(){
131             public Object JavaDoc getValue(){
132                 Object JavaDoc obj = lookup.lookup(clazz);
133                 boolean bool = (shouldBePresent) ? obj != null : obj == null;
134                 return Boolean.valueOf(bool);
135             }
136         }, Boolean.TRUE);
137         Object JavaDoc obj = lookup.lookup(clazz);
138         if (shouldBePresent){
139             assertTrue("Object should be present in the lookup",obj!=null);
140         } else {
141             assertTrue("Object should NOT be present in the lookup",obj==null);
142         }
143     }
144     
145     private void checkLookupTemplate(final MimeLookup lookup, final Class JavaDoc clazz, final int instCount){
146         TestUtilities.waitMaxMilisForValue(WAIT_TIME, new TestUtilities.ValueResolver(){
147             public Object JavaDoc getValue(){
148                 Lookup.Result result = lookup.lookup(new Lookup.Template(clazz));
149                 boolean bool = result.allInstances().size() == instCount;
150                 return Boolean.valueOf(bool);
151             }
152         }, Boolean.TRUE);
153         Lookup.Result result = lookup.lookup(new Lookup.Template(clazz));
154         int size = result.allInstances().size();
155         boolean bool = (size == instCount);
156         assertTrue("Number of instances doesn't match. Found:"+size+". Should be presented:"+instCount+".", bool);
157     }
158
159 }
160
Popular Tags