KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Collection JavaDoc;
23 import org.netbeans.junit.NbTestCase;
24 import org.netbeans.spi.editor.mimelookup.Class2LayerFolder;
25 import org.openide.util.Lookup;
26 import org.openide.util.LookupEvent;
27 import org.openide.util.LookupListener;
28
29 /**
30  *
31  * @author vita
32  */

33 public class FolderPathLookupTest extends NbTestCase {
34
35     /** Creates a new instance of FolderPathLookupTest */
36     public FolderPathLookupTest(String JavaDoc name) {
37         super(name);
38     }
39
40     protected void setUp() throws java.lang.Exception JavaDoc {
41         // Set up the default lookup, repository, etc.
42
EditorTestLookup.setLookup(new String JavaDoc[0], getWorkDir(), new Object JavaDoc[] {},
43             getClass().getClassLoader(),
44             null
45         );
46     }
47     
48     protected void tearDown() {
49         TestUtilities.gc();
50     }
51     
52     public void testSimple() throws Exception JavaDoc {
53         TestUtilities.createFile(getWorkDir(), "Tmp/A/B/C/D/org-netbeans-modules-editor-mimelookup-impl-DummyClass2LayerFolder.instance");
54         
55         // Create lookup over an existing folder
56
Lookup lookup = new FolderPathLookup(new String JavaDoc [] { "Tmp/A/B/C/D" });
57         Collection JavaDoc instances = lookup.lookupAll(Class2LayerFolder.class);
58         
59         assertEquals("Wrong number of instances", 1, instances.size());
60         assertEquals("Wrong instance", DummyClass2LayerFolder.class, instances.iterator().next().getClass());
61         
62         // Now create lookup over a non-existing folder
63
lookup = new FolderPathLookup(new String JavaDoc [] { "Tmp/X/Y/Z" });
64         instances = lookup.lookupAll(Object JavaDoc.class);
65         
66         assertEquals("Wrong number of instances", 0, instances.size());
67     }
68
69     public void testAddingFolders() throws Exception JavaDoc {
70         // Create lookup over a non-existing folder
71
Lookup lookup = new FolderPathLookup(new String JavaDoc [] { "Tmp/A/B/C/D" });
72         Collection JavaDoc instances = lookup.lookupAll(Class2LayerFolder.class);
73         
74         assertEquals("Wrong number of instances", 0, instances.size());
75
76         // Create the folder and the instance
77
TestUtilities.createFile(getWorkDir(), "Tmp/A/B/C/D/org-netbeans-modules-editor-mimelookup-impl-DummyClass2LayerFolder.instance");
78         
79         instances = lookup.lookupAll(Class2LayerFolder.class);
80         assertEquals("Wrong number of instances", 1, instances.size());
81         assertEquals("Wrong instance", DummyClass2LayerFolder.class, instances.iterator().next().getClass());
82     }
83
84     public void testRemovingFolders() throws Exception JavaDoc {
85         TestUtilities.createFile(getWorkDir(), "Tmp/A/B/C/D/org-netbeans-modules-editor-mimelookup-impl-DummyClass2LayerFolder.instance");
86         
87         // Create lookup over an existing folder
88
Lookup lookup = new FolderPathLookup(new String JavaDoc [] { "Tmp/A/B/C/D" });
89         Collection JavaDoc instances = lookup.lookupAll(Class2LayerFolder.class);
90         
91         assertEquals("Wrong number of instances", 1, instances.size());
92         assertEquals("Wrong instance", DummyClass2LayerFolder.class, instances.iterator().next().getClass());
93
94         // Delete the folders
95
TestUtilities.deleteFile(getWorkDir(), "Tmp");
96         
97         instances = lookup.lookupAll(Class2LayerFolder.class);
98         assertEquals("Wrong number of instances", 0, instances.size());
99     }
100     
101     public void testChangeEvents() throws Exception JavaDoc {
102         Lookup.Result lr = new FolderPathLookup(new String JavaDoc [] { "Tmp/A/B/C/D" }).lookupResult(Class2LayerFolder.class);
103         L listener = new L();
104         lr.addLookupListener(listener);
105
106         Collection JavaDoc instances = lr.allInstances();
107         assertEquals("Wrong number of instances", 0, instances.size());
108
109         // Create the folder and the instance
110
TestUtilities.createFile(getWorkDir(), "Tmp/A/B/C/D/org-netbeans-modules-editor-mimelookup-impl-DummyClass2LayerFolder.instance");
111
112         assertEquals("Wrong number of events", 1, listener.resultChangedCnt);
113         
114         instances = lr.allInstances();
115         assertEquals("Wrong number of instances", 1, instances.size());
116         assertEquals("Wrong instance", DummyClass2LayerFolder.class, instances.iterator().next().getClass());
117
118         // Reset the listener
119
listener.resultChangedCnt = 0;
120
121         // Delete the folders
122
TestUtilities.deleteFile(getWorkDir(), "Tmp");
123
124         assertEquals("Wrong number of events", 1, listener.resultChangedCnt);
125         
126         instances = lr.allInstances();
127         assertEquals("Wrong number of instances", 0, instances.size());
128     }
129     
130     private static final class L implements LookupListener {
131         public int resultChangedCnt = 0;
132         public void resultChanged(LookupEvent ev) {
133             resultChangedCnt++;
134         }
135     }
136 }
137
Popular Tags