~ubuntu-branches/ubuntu/trusty/netbeans/trusty

« back to all changes in this revision

Viewing changes to editor/settings/storage/test/unit/src/org/netbeans/modules/editor/settings/storage/MimeTypesTrackerTest.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.settings.storage;
 
43
 
 
44
import java.beans.PropertyChangeEvent;
 
45
import java.beans.PropertyChangeListener;
 
46
import java.io.IOException;
 
47
import java.lang.ref.WeakReference;
 
48
import java.net.URL;
 
49
import java.util.Arrays;
 
50
import java.util.Collection;
 
51
import java.util.Map;
 
52
import java.util.TreeSet;
 
53
import org.netbeans.junit.NbTestCase;
 
54
 
 
55
/**
 
56
 *
 
57
 * @author Vita Stejskal
 
58
 */
 
59
public class MimeTypesTrackerTest extends NbTestCase {
 
60
 
 
61
    private static final String BASE = "Some/Folder/SomeWhere";
 
62
    
 
63
    /** Creates a new instance of MimeTypesTrackerTest */
 
64
    public MimeTypesTrackerTest(String name) {
 
65
        super(name);
 
66
    }
 
67
 
 
68
    protected void setUp() throws Exception {
 
69
        super.setUp();
 
70
    
 
71
        EditorTestLookup.setLookup(
 
72
            new URL[] { },
 
73
            getWorkDir(),
 
74
            new Object[] {},
 
75
            getClass().getClassLoader()
 
76
        );
 
77
    }
 
78
    
 
79
    public void testBasic() throws Exception {
 
80
        TestUtilities.createFolder(BASE + "/text/plain");
 
81
        MimeTypesTracker mtt = new MimeTypesTracker(BASE, null);
 
82
        Collection<String> mimeTypes = mtt.getMimeTypes();
 
83
        
 
84
        assertNotNull("MimeTypes should not be null", mimeTypes);
 
85
        assertEquals("Wrong # of recognized mime types", 1, mimeTypes.size());
 
86
        assertEquals("Wrong mime type", "text/plain", mimeTypes.iterator().next());
 
87
    }
 
88
 
 
89
    public void testGC() throws Exception {
 
90
        TestUtilities.createFolder(BASE + "/text/plain");
 
91
        MimeTypesTracker mtt = new MimeTypesTracker(BASE, null);
 
92
        Collection<String> mimeTypes = mtt.getMimeTypes();
 
93
        
 
94
        assertNotNull("MimeTypes should not be null", mimeTypes);
 
95
        assertEquals("Wrong # of recognized mime types", 1, mimeTypes.size());
 
96
        assertEquals("Wrong mime type", "text/plain", mimeTypes.iterator().next());
 
97
        
 
98
        WeakReference<MimeTypesTracker> ref = new WeakReference<MimeTypesTracker>(mtt);
 
99
        mtt = null;
 
100
        assertGC("Can't GC the tracker", ref);
 
101
    }
 
102
 
 
103
    public void testWellKnownMimeTypes() throws Exception {
 
104
        // See http://www.iana.org/assignments/media-types/ for details
 
105
        String [] mimeTypes = new String [] {
 
106
            "application/pdf", //NOI18N
 
107
            "audio/mpeg", //NOI18N
 
108
            "image/jpeg", //NOI18N
 
109
            "message/news", //NOI18N
 
110
            "model/vrml", //NOI18N
 
111
            "multipart/mixed", //NOI18N
 
112
            "text/plain", //NOI18N
 
113
            "video/mpeg" //NOI18N
 
114
        };
 
115
        
 
116
        for(String s : mimeTypes) {
 
117
            TestUtilities.createFolder(BASE + "/" + s);
 
118
        }
 
119
        
 
120
        MimeTypesTracker mtt = new MimeTypesTracker(BASE, null);
 
121
        Collection<String> recognized = mtt.getMimeTypes();
 
122
        
 
123
        assertNotNull("MimeTypes should not be null", recognized);
 
124
        assertEquals("Wrong # of recognized mime types", mimeTypes.length, recognized.size());
 
125
        assertEquals("Wrong mime types", new TreeSet<String>(Arrays.asList(mimeTypes)), new TreeSet<String>(recognized));
 
126
    }
 
127
    
 
128
    public void testFoldersNotRecognized() throws IOException {
 
129
        String [] mimeTypes = new String [] {
 
130
            "NetBeans/Default", //NOI18N
 
131
            "Default", //NOI18N
 
132
            "KeyBindings/whatever", //NOI18N
 
133
            "Toolbars/Default", //NOI18N
 
134
            "SideBar/MySideBar", //NOI18N
 
135
        };
 
136
        
 
137
        for(String s : mimeTypes) {
 
138
            TestUtilities.createFolder(BASE + "/" + s);
 
139
        }
 
140
        
 
141
        MimeTypesTracker mtt = new MimeTypesTracker(BASE, null);
 
142
        Collection<String> recognized = mtt.getMimeTypes();
 
143
        
 
144
        assertNotNull("MimeTypes should not be null", recognized);
 
145
        assertEquals("Wrong # of recognized mime types", 0, recognized.size());
 
146
    }
 
147
 
 
148
    public void testFilesNotRecognized() throws IOException {
 
149
        TestUtilities.createFile(BASE + "/text/hello.xml");
 
150
        TestUtilities.createFile(BASE + "/text");
 
151
        TestUtilities.createFile(BASE + "/application/pdf");
 
152
        
 
153
        MimeTypesTracker mtt = new MimeTypesTracker(BASE, null);
 
154
        Collection<String> recognized = mtt.getMimeTypes();
 
155
        
 
156
        assertNotNull("MimeTypes should not be null", recognized);
 
157
        assertEquals("Wrong # of recognized mime types", 0, recognized.size());
 
158
    }
 
159
    
 
160
    public void testRecognition() throws IOException {
 
161
        final MimeTypesTracker mtt = new MimeTypesTracker(BASE, null);
 
162
 
 
163
        {
 
164
        Collection<String> recognized = mtt.getMimeTypes();
 
165
        assertNotNull("MimeTypes should not be null", recognized);
 
166
        assertEquals("Wrong # of recognized mime types", 0, recognized.size());
 
167
        }
 
168
        
 
169
        {
 
170
        TestUtilities.createFolder(BASE);
 
171
        Collection<String> recognized = mtt.getMimeTypes();
 
172
        assertNotNull("MimeTypes should not be null", recognized);
 
173
        assertEquals("Wrong # of recognized mime types", 0, recognized.size());
 
174
        }
 
175
        
 
176
        {
 
177
        TestUtilities.createFolder(BASE + "/text");
 
178
        Collection<String> recognized = mtt.getMimeTypes();
 
179
        assertNotNull("MimeTypes should not be null", recognized);
 
180
        assertEquals("Wrong # of recognized mime types", 0, recognized.size());
 
181
        }
 
182
        
 
183
        {
 
184
        TestUtilities.createFolder(BASE + "/text/x-java");
 
185
        Collection<String> recognized = mtt.getMimeTypes();
 
186
        assertNotNull("MimeTypes should not be null", recognized);
 
187
        assertEquals("Wrong # of recognized mime types", 1, recognized.size());
 
188
        assertEquals("Wrong mime type recognized", "text/x-java", recognized.iterator().next());
 
189
        }
 
190
 
 
191
        {
 
192
        TestUtilities.delete(BASE + "/text/x-java");
 
193
        Collection<String> recognized = mtt.getMimeTypes();
 
194
        assertNotNull("MimeTypes should not be null", recognized);
 
195
        assertEquals("Wrong # of recognized mime types", 0, recognized.size());
 
196
        }
 
197
        {
 
198
        TestUtilities.delete(BASE + "/text");
 
199
        Collection<String> recognized = mtt.getMimeTypes();
 
200
        assertNotNull("MimeTypes should not be null", recognized);
 
201
        assertEquals("Wrong # of recognized mime types", 0, recognized.size());
 
202
        }
 
203
        {
 
204
        TestUtilities.delete(BASE);
 
205
        Collection<String> recognized = mtt.getMimeTypes();
 
206
        assertNotNull("MimeTypes should not be null", recognized);
 
207
        assertEquals("Wrong # of recognized mime types", 0, recognized.size());
 
208
        }
 
209
    }
 
210
    
 
211
    public void testEvents() throws IOException {
 
212
        final L listener = new L();
 
213
        final MimeTypesTracker mtt = new MimeTypesTracker(BASE, null);
 
214
        mtt.addPropertyChangeListener(listener);
 
215
        
 
216
        {
 
217
        Collection<String> recognized = mtt.getMimeTypes();
 
218
        assertNotNull("MimeTypes should not be null", recognized);
 
219
        assertEquals("Wrong # of recognized mime types", 0, recognized.size());
 
220
        assertEquals("Wrong # of events fired", 0, listener.events);
 
221
        }
 
222
        
 
223
        {
 
224
        TestUtilities.createFolder(BASE + "/text/x-java");
 
225
        Collection<String> recognized = mtt.getMimeTypes();
 
226
        assertNotNull("MimeTypes should not be null", recognized);
 
227
        assertEquals("Wrong # of recognized mime types", 1, recognized.size());
 
228
        assertEquals("Wrong mime type recognized", "text/x-java", recognized.iterator().next());
 
229
        assertEquals("Wrong # of events fired", 1, listener.events);
 
230
        assertEquals("Wrong change event name", MimeTypesTracker.PROP_MIME_TYPES, listener.lastEventName);
 
231
        assertTrue("Wrong change event old value", listener.lastEventOldValue instanceof Map);
 
232
        assertEquals("Wrong change event old value contents", 0, ((Map) listener.lastEventOldValue).size());
 
233
        assertTrue("Wrong change event new value", listener.lastEventNewValue instanceof Map);
 
234
        assertEquals("Wrong change event new value contents", 1, ((Map) listener.lastEventNewValue).size());
 
235
        assertEquals("Wrong change event new value mime type", "text/x-java", ((Map) listener.lastEventNewValue).keySet().iterator().next());
 
236
        }
 
237
 
 
238
        {
 
239
        listener.reset();
 
240
        TestUtilities.delete(BASE);
 
241
        Collection<String> recognized = mtt.getMimeTypes();
 
242
        assertNotNull("MimeTypes should not be null", recognized);
 
243
        assertEquals("Wrong # of recognized mime types", 0, recognized.size());
 
244
        assertEquals("Wrong # of events fired", 1, listener.events);
 
245
        assertEquals("Wrong change event name", MimeTypesTracker.PROP_MIME_TYPES, listener.lastEventName);
 
246
        assertTrue("Wrong change event old value", listener.lastEventOldValue instanceof Map);
 
247
        assertEquals("Wrong change event old value contents", 1, ((Map) listener.lastEventOldValue).size());
 
248
        assertEquals("Wrong change event old value mime type", "text/x-java", ((Map) listener.lastEventOldValue).keySet().iterator().next());
 
249
        assertTrue("Wrong change event new value", listener.lastEventNewValue instanceof Map);
 
250
        assertEquals("Wrong change event new value contents", 0, ((Map) listener.lastEventNewValue).size());
 
251
        }
 
252
    }
 
253
 
 
254
    public void testEvents2() throws IOException {
 
255
        final L listener = new L();
 
256
        final MimeTypesTracker mtt = new MimeTypesTracker(BASE, null);
 
257
        mtt.addPropertyChangeListener(listener);
 
258
        
 
259
        {
 
260
        Collection<String> recognized = mtt.getMimeTypes();
 
261
        assertNotNull("MimeTypes should not be null", recognized);
 
262
        assertEquals("Wrong # of recognized mime types", 0, recognized.size());
 
263
        assertEquals("Wrong # of events fired", 0, listener.events);
 
264
        }
 
265
        
 
266
        {
 
267
        TestUtilities.createFolder(BASE + "/text/plain");
 
268
        Collection<String> recognized = mtt.getMimeTypes();
 
269
        assertNotNull("MimeTypes should not be null", recognized);
 
270
        assertEquals("Wrong # of recognized mime types", 1, recognized.size());
 
271
        assertEquals("Wrong mime type recognized", "text/plain", recognized.iterator().next());
 
272
        assertEquals("Wrong # of events fired", 1, listener.events);
 
273
        }
 
274
 
 
275
        {
 
276
        listener.reset();
 
277
        TestUtilities.createFolder(BASE + "/NetBeans");
 
278
        Collection<String> recognized = mtt.getMimeTypes();
 
279
        assertNotNull("MimeTypes should not be null", recognized);
 
280
        assertEquals("Wrong # of recognized mime types", 1, recognized.size());
 
281
        assertEquals("Wrong mime type recognized", "text/plain", recognized.iterator().next());
 
282
        assertEquals("Wrong # of events fired", 0, listener.events);
 
283
        }
 
284
        
 
285
        {
 
286
        listener.reset();
 
287
        TestUtilities.createFolder(BASE + "/text/plain/NetBeans");
 
288
        Collection<String> recognized = mtt.getMimeTypes();
 
289
        assertNotNull("MimeTypes should not be null", recognized);
 
290
        assertEquals("Wrong # of recognized mime types", 1, recognized.size());
 
291
        assertEquals("Wrong mime type recognized", "text/plain", recognized.iterator().next());
 
292
        assertEquals("Wrong # of events fired", 0, listener.events);
 
293
        }
 
294
    }
 
295
    
 
296
    private static final class L implements PropertyChangeListener {
 
297
        public int events;
 
298
        public String lastEventName;
 
299
        public Object lastEventOldValue;
 
300
        public Object lastEventNewValue;
 
301
        
 
302
        public void propertyChange(PropertyChangeEvent evt) {
 
303
            events++;
 
304
            lastEventName = evt.getPropertyName();
 
305
            lastEventOldValue = evt.getOldValue();
 
306
            lastEventNewValue = evt.getNewValue();
 
307
        }
 
308
        
 
309
        public void reset() {
 
310
            events = 0;
 
311
            lastEventName = null;
 
312
            lastEventOldValue = null;
 
313
            lastEventNewValue = null;
 
314
        }
 
315
    }
 
316
}