KickJava   Java API By Example, From Geeks To Geeks.

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


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
25 /**
26  * URL mapper is often invoked from inside the lookup. That is why
27  * it needs to be ready to survive strange states.
28  *
29  * Trying to mimic IZ 44365.
30  *
31  * @author Jaroslav Tulach
32  */

33 public class URLMapperLookupTest extends NbTestCase {
34
35     public URLMapperLookupTest(String JavaDoc name) {
36         super(name);
37     }
38
39     protected void setUp() throws Exception JavaDoc {
40         System.setProperty("org.openide.util.Lookup", "org.openide.filesystems.URLMapperLookupTest$Lkp");
41         
42         super.setUp();
43         
44         assertEquals ("Our lookup is registered", Lkp.class, org.openide.util.Lookup.getDefault().getClass());
45     }
46     
47     public void testIfIAskForAnItemThatAsksURLMapperAndThenAskOnceMoreAllMappersAreAsked ()
48     throws Exception JavaDoc {
49         Object JavaDoc found = org.openide.util.Lookup.getDefault().lookup (QueryingPair.class);
50         assertNotNull (found);
51         
52         MyUM.queried = null;
53         java.net.URL JavaDoc url = new java.net.URL JavaDoc ("http://www.netbeans.org");
54         URLMapper.findFileObject(url);
55         
56         assertEquals ("Really got the query thru", url, MyUM.queried);
57     }
58
59     /** This is a pair that as a part of its instanceOf method queries the URL resolver.
60      */

61     private static class QueryingPair extends org.openide.util.lookup.AbstractLookup.Pair {
62         public boolean beBroken;
63         
64         public java.lang.String JavaDoc getId() {
65             return getType ().toString();
66         }
67
68         public java.lang.String JavaDoc getDisplayName() {
69             return getId ();
70         }
71
72         public java.lang.Class JavaDoc getType() {
73             return getClass ();
74         }
75
76        protected boolean creatorOf(java.lang.Object JavaDoc obj) {
77             return obj == this;
78         }
79
80         protected boolean instanceOf(java.lang.Class JavaDoc c) {
81             if (beBroken) {
82                 beBroken = false;
83                 try {
84                     assertNull ("is still null", MyUM.queried);
85                     java.net.URL JavaDoc url = new java.net.URL JavaDoc ("http://www.netbeans.org");
86                     URLMapper.findFileObject(url);
87                     assertNull ("This query did not get thru", MyUM.queried);
88                 } catch (java.net.MalformedURLException JavaDoc ex) {
89                     ex.printStackTrace();
90                     fail ("No exceptions: " + ex.getMessage ());
91                 }
92             }
93             return c.isAssignableFrom(getType ());
94         }
95
96         public java.lang.Object JavaDoc getInstance() {
97             return this;
98         }
99     }
100     
101     private static final class MyUM extends URLMapper {
102         public static java.net.URL JavaDoc queried;
103         
104         public org.openide.filesystems.FileObject[] getFileObjects(java.net.URL JavaDoc url) {
105             queried = url;
106             return null;
107         }
108
109         public java.net.URL JavaDoc getURL(org.openide.filesystems.FileObject fo, int type) {
110             return null;
111         }
112     }
113      
114
115     public static final class Lkp extends org.openide.util.lookup.AbstractLookup {
116         private static org.openide.util.lookup.InstanceContent ic;
117         
118         public Lkp () {
119             this (new org.openide.util.lookup.InstanceContent ());
120         }
121         
122         private Lkp (org.openide.util.lookup.InstanceContent ic) {
123             super (ic);
124             this.ic = ic;
125         }
126
127         protected void initialize() {
128             // a small trick to make the InheritanceTree storage to be used
129
// because if the amount of elements in small, the ArrayStorage is
130
// used and it does not have the same problems like InheritanceTree
131
for (int i = 0; i < 1000; i++) {
132                 ic.add (new Integer JavaDoc (i));
133             }
134
135             QueryingPair qp = new QueryingPair();
136             ic.addPair (qp);
137             ic.add (new MyUM ());
138
139             
140             qp.beBroken = true;
141         }
142
143     } // end of Lkp
144
}
145
Popular Tags