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.settings.storage;
44
import java.beans.PropertyChangeEvent;
45
import java.beans.PropertyChangeListener;
46
import java.io.IOException;
47
import java.lang.ref.WeakReference;
49
import java.util.Arrays;
50
import java.util.Collection;
52
import java.util.TreeSet;
53
import org.netbeans.junit.NbTestCase;
57
* @author Vita Stejskal
59
public class MimeTypesTrackerTest extends NbTestCase {
61
private static final String BASE = "Some/Folder/SomeWhere";
63
/** Creates a new instance of MimeTypesTrackerTest */
64
public MimeTypesTrackerTest(String name) {
68
protected void setUp() throws Exception {
71
EditorTestLookup.setLookup(
75
getClass().getClassLoader()
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();
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());
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();
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());
98
WeakReference<MimeTypesTracker> ref = new WeakReference<MimeTypesTracker>(mtt);
100
assertGC("Can't GC the tracker", ref);
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
116
for(String s : mimeTypes) {
117
TestUtilities.createFolder(BASE + "/" + s);
120
MimeTypesTracker mtt = new MimeTypesTracker(BASE, null);
121
Collection<String> recognized = mtt.getMimeTypes();
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));
128
public void testFoldersNotRecognized() throws IOException {
129
String [] mimeTypes = new String [] {
130
"NetBeans/Default", //NOI18N
132
"KeyBindings/whatever", //NOI18N
133
"Toolbars/Default", //NOI18N
134
"SideBar/MySideBar", //NOI18N
137
for(String s : mimeTypes) {
138
TestUtilities.createFolder(BASE + "/" + s);
141
MimeTypesTracker mtt = new MimeTypesTracker(BASE, null);
142
Collection<String> recognized = mtt.getMimeTypes();
144
assertNotNull("MimeTypes should not be null", recognized);
145
assertEquals("Wrong # of recognized mime types", 0, recognized.size());
148
public void testFilesNotRecognized() throws IOException {
149
TestUtilities.createFile(BASE + "/text/hello.xml");
150
TestUtilities.createFile(BASE + "/text");
151
TestUtilities.createFile(BASE + "/application/pdf");
153
MimeTypesTracker mtt = new MimeTypesTracker(BASE, null);
154
Collection<String> recognized = mtt.getMimeTypes();
156
assertNotNull("MimeTypes should not be null", recognized);
157
assertEquals("Wrong # of recognized mime types", 0, recognized.size());
160
public void testRecognition() throws IOException {
161
final MimeTypesTracker mtt = new MimeTypesTracker(BASE, null);
164
Collection<String> recognized = mtt.getMimeTypes();
165
assertNotNull("MimeTypes should not be null", recognized);
166
assertEquals("Wrong # of recognized mime types", 0, recognized.size());
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());
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());
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());
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());
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());
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());
211
public void testEvents() throws IOException {
212
final L listener = new L();
213
final MimeTypesTracker mtt = new MimeTypesTracker(BASE, null);
214
mtt.addPropertyChangeListener(listener);
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);
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());
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());
254
public void testEvents2() throws IOException {
255
final L listener = new L();
256
final MimeTypesTracker mtt = new MimeTypesTracker(BASE, null);
257
mtt.addPropertyChangeListener(listener);
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);
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);
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);
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);
296
private static final class L implements PropertyChangeListener {
298
public String lastEventName;
299
public Object lastEventOldValue;
300
public Object lastEventNewValue;
302
public void propertyChange(PropertyChangeEvent evt) {
304
lastEventName = evt.getPropertyName();
305
lastEventOldValue = evt.getOldValue();
306
lastEventNewValue = evt.getNewValue();
309
public void reset() {
311
lastEventName = null;
312
lastEventOldValue = null;
313
lastEventNewValue = null;