~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to editor/mimelookup/impl/test/unit/src/org/netbeans/modules/editor/mimelookup/impl/ClassInfoStorageTest.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
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]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
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.
 
29
 *
 
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.
 
40
 */
 
41
 
 
42
package org.netbeans.modules.editor.mimelookup.impl;
 
43
 
 
44
import java.beans.PropertyChangeEvent;
 
45
import java.beans.PropertyChangeListener;
 
46
import java.util.ArrayList;
 
47
import java.util.Set;
 
48
import org.netbeans.junit.NbTestCase;
 
49
 
 
50
/**
 
51
 *
 
52
 * @author vita
 
53
 */
 
54
public class ClassInfoStorageTest extends NbTestCase {
 
55
 
 
56
    public ClassInfoStorageTest(String name) {
 
57
        super(name);
 
58
    }
 
59
 
 
60
    protected void setUp() throws Exception {
 
61
        clearWorkDir();
 
62
        // Set up the default lookup, repository, etc.
 
63
        EditorTestLookup.setLookup(new String[0], getWorkDir(), new Object[] {},
 
64
            getClass().getClassLoader(),
 
65
            null
 
66
        );
 
67
    }
 
68
    
 
69
    public void testNoMapper() {
 
70
        ClassInfoStorage.Info info = ClassInfoStorage.getInstance().getInfo(DummySetting.class.getName());
 
71
        assertTrue("There should be no mapper registered", isEmpty(info));
 
72
    }
 
73
    
 
74
    public void testDummyMapper() throws Exception {
 
75
        TestUtilities.createFile(getWorkDir(), "Services/org-netbeans-modules-editor-mimelookup-impl-DummyClass2LayerFolderWithInstanceProvider.instance");
 
76
        TestUtilities.sleepForWhile();
 
77
        
 
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);
 
85
    }
 
86
 
 
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));
 
90
        
 
91
        L listener = new L();
 
92
        ClassInfoStorage.getInstance().addPropertyChangeListener(listener);
 
93
        try {
 
94
            // Add the mapper
 
95
            TestUtilities.createFile(getWorkDir(), "Services/org-netbeans-modules-editor-mimelookup-impl-DummyClass2LayerFolderWithInstanceProvider.instance");
 
96
            TestUtilities.sleepForWhile();
 
97
            
 
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());
 
102
            
 
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()));
 
106
        } finally {
 
107
            ClassInfoStorage.getInstance().removePropertyChangeListener(listener);
 
108
        }
 
109
 
 
110
        info = ClassInfoStorage.getInstance().getInfo(DummySetting.class.getName());
 
111
        checkInfo(info, "DummyFolder", DummySetting.class, DummyInstanceProvider.class);
 
112
    }
 
113
 
 
114
    public void testRemovingMapper() throws Exception {
 
115
        TestUtilities.createFile(getWorkDir(), "Services/org-netbeans-modules-editor-mimelookup-impl-DummyClass2LayerFolderWithInstanceProvider.instance");
 
116
        TestUtilities.sleepForWhile();
 
117
        
 
118
        ClassInfoStorage.Info info = ClassInfoStorage.getInstance().getInfo(DummySetting.class.getName());
 
119
        checkInfo(info, "DummyFolder", DummySetting.class, DummyInstanceProvider.class);
 
120
        
 
121
        L listener = new L();
 
122
        ClassInfoStorage.getInstance().addPropertyChangeListener(listener);
 
123
        try {
 
124
            // Remove the mapper
 
125
            TestUtilities.deleteFile(getWorkDir(), "Services/org-netbeans-modules-editor-mimelookup-impl-DummyClass2LayerFolderWithInstanceProvider.instance");
 
126
            TestUtilities.sleepForWhile();
 
127
            
 
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());
 
132
            
 
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()));
 
136
        } finally {
 
137
            ClassInfoStorage.getInstance().removePropertyChangeListener(listener);
 
138
        }
 
139
 
 
140
        info = ClassInfoStorage.getInstance().getInfo(DummySetting.class.getName());
 
141
        assertTrue("There should be no mapper registered: " + info, isEmpty(info));
 
142
    }
 
143
    
 
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());
 
151
    }
 
152
 
 
153
    private boolean isEmpty(ClassInfoStorage.Info info) {
 
154
        return  info.getExtraPath().length() == 0 && 
 
155
                info.getInstanceProviderClass() == null &&
 
156
                info.getInstanceProvider() == null;
 
157
    }
 
158
    
 
159
    private static class L implements PropertyChangeListener {
 
160
        
 
161
        public int changeEventsCnt = 0;
 
162
        public ArrayList events = new ArrayList();
 
163
        
 
164
        public void propertyChange(PropertyChangeEvent evt) {
 
165
            changeEventsCnt++;
 
166
            events.add(evt);
 
167
        }
 
168
        
 
169
        public void reset() {
 
170
            changeEventsCnt = 0;
 
171
            events.clear();
 
172
        }
 
173
    }
 
174
}