KickJava   Java API By Example, From Geeks To Geeks.

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


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.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Set JavaDoc;
26 import org.netbeans.junit.NbTestCase;
27
28 /**
29  *
30  * @author vita
31  */

32 public class ClassInfoStorageTest extends NbTestCase {
33
34     /** Creates a new instance of ClassPathsTest */
35     public ClassInfoStorageTest(String JavaDoc name) {
36         super(name);
37     }
38
39     protected void setUp() throws java.lang.Exception JavaDoc {
40         // Set up the default lookup, repository, etc.
41
EditorTestLookup.setLookup(new String JavaDoc[0], getWorkDir(), new Object JavaDoc[] {},
42             getClass().getClassLoader(),
43             null
44         );
45     }
46     
47     public void testNoMapper() {
48         ClassInfoStorage.Info info = ClassInfoStorage.getInstance().getInfo(DummySetting.class.getName());
49         assertTrue("There should be no mapper registered", isEmpty(info));
50     }
51     
52     public void testDummyMapper() throws Exception JavaDoc {
53         TestUtilities.createFile(getWorkDir(), "Services/org-netbeans-modules-editor-mimelookup-impl-DummyClass2LayerFolderWithInstanceProvider.instance");
54         TestUtilities.sleepForWhile();
55         
56         ClassInfoStorage.Info info = ClassInfoStorage.getInstance().getInfo(DummySetting.class.getName());
57         assertNotNull("The mapper not found", info);
58         assertEquals("Wrang mapper class", DummySetting.class.getName(), info.getClassName());
59         assertEquals("Wrong wrapper extra path", "DummyFolder", info.getExtraPath());
60         assertEquals("Wrong instance provider class", DummyInstanceProvider.class.getName(), info.getInstanceProviderClass());
61         assertNotNull("Instance provider should not be null", info.getInstanceProvider());
62         assertTrue("Wrong instance provider", info.getInstanceProvider() instanceof DummyInstanceProvider);
63     }
64
65     public void testAddingMapper() throws Exception JavaDoc {
66         ClassInfoStorage.Info info = ClassInfoStorage.getInstance().getInfo(DummySetting.class.getName());
67         assertTrue("There should be no mapper registered", isEmpty(info));
68         
69         L listener = new L();
70         ClassInfoStorage.getInstance().addPropertyChangeListener(listener);
71         try {
72             // Add the mapper
73
TestUtilities.createFile(getWorkDir(), "Services/org-netbeans-modules-editor-mimelookup-impl-DummyClass2LayerFolderWithInstanceProvider.instance");
74             TestUtilities.sleepForWhile();
75             
76             assertEquals("Wrong number of change events", 1, listener.changeEventsCnt);
77             assertNotNull("Invalid change event", listener.events.get(0));
78             assertEquals("Wrong change event",
79                 ClassInfoStorage.PROP_CLASS_INFO_ADDED, ((PropertyChangeEvent JavaDoc)listener.events.get(0)).getPropertyName());
80             
81             Set JavaDoc value = (Set JavaDoc) ((PropertyChangeEvent JavaDoc)listener.events.get(0)).getNewValue();
82             assertEquals("Invalid number of class in the change event", 1, value.size());
83             assertTrue("Wrong change event value", value.contains(DummySetting.class.getName()));
84         } finally {
85             ClassInfoStorage.getInstance().removePropertyChangeListener(listener);
86         }
87
88         info = ClassInfoStorage.getInstance().getInfo(DummySetting.class.getName());
89         checkInfo(info, "DummyFolder", DummySetting.class, DummyInstanceProvider.class);
90     }
91
92     public void testRemovingMapper() throws Exception JavaDoc {
93         TestUtilities.createFile(getWorkDir(), "Services/org-netbeans-modules-editor-mimelookup-impl-DummyClass2LayerFolderWithInstanceProvider.instance");
94         TestUtilities.sleepForWhile();
95         
96         ClassInfoStorage.Info info = ClassInfoStorage.getInstance().getInfo(DummySetting.class.getName());
97         checkInfo(info, "DummyFolder", DummySetting.class, DummyInstanceProvider.class);
98         
99         L listener = new L();
100         ClassInfoStorage.getInstance().addPropertyChangeListener(listener);
101         try {
102             // Remove the mapper
103
TestUtilities.deleteFile(getWorkDir(), "Services/org-netbeans-modules-editor-mimelookup-impl-DummyClass2LayerFolderWithInstanceProvider.instance");
104             TestUtilities.sleepForWhile();
105             
106             assertEquals("Wrong number of change events", 1, listener.changeEventsCnt);
107             assertNotNull("Invalid change event", listener.events.get(0));
108             assertEquals("Wrong change event",
109                 ClassInfoStorage.PROP_CLASS_INFO_REMOVED, ((PropertyChangeEvent JavaDoc)listener.events.get(0)).getPropertyName());
110             
111             Set JavaDoc value = (Set JavaDoc) ((PropertyChangeEvent JavaDoc)listener.events.get(0)).getNewValue();
112             assertEquals("Invalid number of class in the change event", 1, value.size());
113             assertTrue("Wrong change event value", value.contains(DummySetting.class.getName()));
114         } finally {
115             ClassInfoStorage.getInstance().removePropertyChangeListener(listener);
116         }
117
118         info = ClassInfoStorage.getInstance().getInfo(DummySetting.class.getName());
119         assertTrue("There should be no mapper registered", isEmpty(info));
120     }
121     
122     private void checkInfo(ClassInfoStorage.Info info, String JavaDoc extraPath, Class JavaDoc infoClass, Class JavaDoc instanceProviderClass) {
123         assertNotNull("The mapper not found", info);
124         assertEquals("Wrang mapper class", infoClass.getName(), info.getClassName());
125         assertEquals("Wrong wrapper extra path", extraPath, info.getExtraPath());
126         assertEquals("Wrong instance provider class", instanceProviderClass.getName(), info.getInstanceProviderClass());
127         assertNotNull("Instance provider should not be null", info.getInstanceProvider());
128         assertEquals("Wrong instance provider", instanceProviderClass, info.getInstanceProvider().getClass());
129     }
130
131     private boolean isEmpty(ClassInfoStorage.Info info) {
132         return info.getExtraPath().length() == 0 &&
133                 info.getInstanceProviderClass() == null &&
134                 info.getInstanceProvider() == null;
135     }
136     
137     private static class L implements PropertyChangeListener JavaDoc {
138         
139         public int changeEventsCnt = 0;
140         public ArrayList JavaDoc events = new ArrayList JavaDoc();
141         
142         public void propertyChange(PropertyChangeEvent JavaDoc evt) {
143             changeEventsCnt++;
144             events.add(evt);
145         }
146         
147         public void reset() {
148             changeEventsCnt = 0;
149             events.clear();
150         }
151     }
152 }
153
Popular Tags