KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > filesystems > MIMESupport49418Test


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.openide.filesystems;
21
22 import org.netbeans.junit.NbTestCase;
23 import org.openide.util.Lookup;
24 import org.openide.util.lookup.AbstractLookup.Pair;
25
26 /**
27  * Trying to mimic IZ 49418.
28  *
29  * @author Radek Matous
30  */

31 public class MIMESupport49418Test extends NbTestCase {
32     private FileSystem lfs;
33     private static FileObject mimeFo;
34     private static final String JavaDoc MIME_TYPE = "text/x-opqr";
35
36     public MIMESupport49418Test(String JavaDoc name) {
37         super(name);
38     }
39
40     protected void setUp() throws Exception JavaDoc {
41         System.setProperty("org.openide.util.Lookup", "org.openide.filesystems.MIMESupport49418Test$Lkp");
42         super.setUp();
43         assertEquals("Our lookup is registered", Lkp.class, Lookup.getDefault().getClass());
44         lfs = TestUtilHid.createLocalFileSystem(getName(), new String JavaDoc[]{"A.opqr", });
45         mimeFo = lfs.findResource("A.opqr");
46         assertNotNull(mimeFo);
47     }
48
49
50     public void testMIMEResolution()
51             throws Exception JavaDoc {
52         assertNull(Lookup.getDefault().lookup(Runnable JavaDoc.class));
53         assertEquals(MIME_TYPE, mimeFo.getMIMEType());
54
55     }
56
57     /**
58      * This is a pair that as a part of its instanceOf method queries the URL resolver.
59      */

60     @SuppressWarnings JavaDoc("unchecked")
61     private static class QueryingPair extends Pair {
62         public boolean beBroken;
63
64         public String JavaDoc getId() {
65             return getType().toString();
66         }
67
68         public String JavaDoc getDisplayName() {
69             return getId();
70         }
71
72         public Class JavaDoc getType() {
73             return getClass();
74         }
75
76         protected boolean creatorOf(Object JavaDoc obj) {
77             return obj == this;
78         }
79
80         protected boolean instanceOf(Class JavaDoc c) {
81             if (beBroken) {
82                 beBroken = false;
83                 assertEquals("content/unknown", mimeFo.getMIMEType());
84
85             }
86             return c.isAssignableFrom(getType());
87         }
88
89         public Object JavaDoc getInstance() {
90             return this;
91         }
92     }
93
94
95     public static final class Lkp extends org.openide.util.lookup.AbstractLookup {
96         private static org.openide.util.lookup.InstanceContent ic;
97
98         public Lkp() {
99             this(new org.openide.util.lookup.InstanceContent());
100         }
101
102         private Lkp(org.openide.util.lookup.InstanceContent ic) {
103             super(ic);
104             this.ic = ic;
105         }
106
107         protected void initialize() {
108             // a small trick to make the InheritanceTree storage to be used
109
// because if the amount of elements in small, the ArrayStorage is
110
// used and it does not have the same problems like InheritanceTree
111
for (int i = 0; i < 1000; i++) {
112                 ic.add(new Integer JavaDoc(i));
113             }
114
115             QueryingPair qp = new QueryingPair();
116             ic.addPair(qp);
117             ic.add(new MIMEResolver() {
118                 public String JavaDoc findMIMEType(FileObject fo) {
119                     return MIME_TYPE;
120                 }
121             });
122
123
124             qp.beBroken = true;
125         }
126
127     } // end of Lkp
128
}
129
Popular Tags