2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
42
package org.netbeans.modules.editor.mimelookup.impl;
44
import java.beans.PropertyChangeEvent;
45
import java.beans.PropertyChangeListener;
46
import java.util.ArrayList;
48
import org.netbeans.junit.NbTestCase;
54
public class ClassInfoStorageTest extends NbTestCase {
56
public ClassInfoStorageTest(String name) {
60
protected void setUp() throws Exception {
62
// Set up the default lookup, repository, etc.
63
EditorTestLookup.setLookup(new String[0], getWorkDir(), new Object[] {},
64
getClass().getClassLoader(),
69
public void testNoMapper() {
70
ClassInfoStorage.Info info = ClassInfoStorage.getInstance().getInfo(DummySetting.class.getName());
71
assertTrue("There should be no mapper registered", isEmpty(info));
74
public void testDummyMapper() throws Exception {
75
TestUtilities.createFile(getWorkDir(), "Services/org-netbeans-modules-editor-mimelookup-impl-DummyClass2LayerFolderWithInstanceProvider.instance");
76
TestUtilities.sleepForWhile();
78
ClassInfoStorage.Info info = ClassInfoStorage.getInstance().getInfo(DummySetting.class.getName());
79
assertNotNull("The mapper not found", info);
80
assertEquals("Wrang mapper class", DummySetting.class.getName(), info.getClassName());
81
assertEquals("Wrong wrapper extra path", "DummyFolder", info.getExtraPath());
82
assertEquals("Wrong instance provider class", DummyInstanceProvider.class.getName(), info.getInstanceProviderClass());
83
assertNotNull("Instance provider should not be null", info.getInstanceProvider());
84
assertTrue("Wrong instance provider", info.getInstanceProvider() instanceof DummyInstanceProvider);
87
public void testAddingMapper() throws Exception {
88
ClassInfoStorage.Info info = ClassInfoStorage.getInstance().getInfo(DummySetting.class.getName());
89
assertTrue("There should be no mapper registered: " + info, isEmpty(info));
92
ClassInfoStorage.getInstance().addPropertyChangeListener(listener);
95
TestUtilities.createFile(getWorkDir(), "Services/org-netbeans-modules-editor-mimelookup-impl-DummyClass2LayerFolderWithInstanceProvider.instance");
96
TestUtilities.sleepForWhile();
98
assertEquals("Wrong number of change events", 1, listener.changeEventsCnt);
99
assertNotNull("Invalid change event", listener.events.get(0));
100
assertEquals("Wrong change event",
101
ClassInfoStorage.PROP_CLASS_INFO_ADDED, ((PropertyChangeEvent)listener.events.get(0)).getPropertyName());
103
Set value = (Set) ((PropertyChangeEvent)listener.events.get(0)).getNewValue();
104
assertEquals("Invalid number of class in the change event", 1, value.size());
105
assertTrue("Wrong change event value", value.contains(DummySetting.class.getName()));
107
ClassInfoStorage.getInstance().removePropertyChangeListener(listener);
110
info = ClassInfoStorage.getInstance().getInfo(DummySetting.class.getName());
111
checkInfo(info, "DummyFolder", DummySetting.class, DummyInstanceProvider.class);
114
public void testRemovingMapper() throws Exception {
115
TestUtilities.createFile(getWorkDir(), "Services/org-netbeans-modules-editor-mimelookup-impl-DummyClass2LayerFolderWithInstanceProvider.instance");
116
TestUtilities.sleepForWhile();
118
ClassInfoStorage.Info info = ClassInfoStorage.getInstance().getInfo(DummySetting.class.getName());
119
checkInfo(info, "DummyFolder", DummySetting.class, DummyInstanceProvider.class);
121
L listener = new L();
122
ClassInfoStorage.getInstance().addPropertyChangeListener(listener);
125
TestUtilities.deleteFile(getWorkDir(), "Services/org-netbeans-modules-editor-mimelookup-impl-DummyClass2LayerFolderWithInstanceProvider.instance");
126
TestUtilities.sleepForWhile();
128
assertEquals("Wrong number of change events", 1, listener.changeEventsCnt);
129
assertNotNull("Invalid change event", listener.events.get(0));
130
assertEquals("Wrong change event",
131
ClassInfoStorage.PROP_CLASS_INFO_REMOVED, ((PropertyChangeEvent)listener.events.get(0)).getPropertyName());
133
Set value = (Set) ((PropertyChangeEvent)listener.events.get(0)).getNewValue();
134
assertEquals("Invalid number of class in the change event", 1, value.size());
135
assertTrue("Wrong change event value", value.contains(DummySetting.class.getName()));
137
ClassInfoStorage.getInstance().removePropertyChangeListener(listener);
140
info = ClassInfoStorage.getInstance().getInfo(DummySetting.class.getName());
141
assertTrue("There should be no mapper registered: " + info, isEmpty(info));
144
private void checkInfo(ClassInfoStorage.Info info, String extraPath, Class infoClass, Class instanceProviderClass) {
145
assertNotNull("The mapper not found", info);
146
assertEquals("Wrang mapper class", infoClass.getName(), info.getClassName());
147
assertEquals("Wrong wrapper extra path", extraPath, info.getExtraPath());
148
assertEquals("Wrong instance provider class", instanceProviderClass.getName(), info.getInstanceProviderClass());
149
assertNotNull("Instance provider should not be null", info.getInstanceProvider());
150
assertEquals("Wrong instance provider", instanceProviderClass, info.getInstanceProvider().getClass());
153
private boolean isEmpty(ClassInfoStorage.Info info) {
154
return info.getExtraPath().length() == 0 &&
155
info.getInstanceProviderClass() == null &&
156
info.getInstanceProvider() == null;
159
private static class L implements PropertyChangeListener {
161
public int changeEventsCnt = 0;
162
public ArrayList events = new ArrayList();
164
public void propertyChange(PropertyChangeEvent evt) {
169
public void reset() {