KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > xml > FileEntityResolverDeadlock54971Test


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.netbeans.core.xml;
21
22 import java.io.IOException JavaDoc;
23 import java.net.URL JavaDoc;
24 import junit.framework.TestCase;
25 import org.openide.cookies.InstanceCookie;
26 import org.openide.filesystems.FileLock;
27 import org.openide.filesystems.FileObject;
28 import org.openide.filesystems.FileUtil;
29 import org.openide.filesystems.Repository;
30 import org.openide.filesystems.URLMapper;
31 import org.openide.loaders.DataObject;
32 import org.openide.loaders.Environment;
33 import org.openide.util.Lookup;
34 import org.openide.util.lookup.Lookups;
35
36 /**
37  *
38  * @author Jaroslav Tulach
39  */

40 public class FileEntityResolverDeadlock54971Test extends TestCase {
41     public FileEntityResolverDeadlock54971Test(String JavaDoc testName) {
42         super(testName);
43     }
44     
45     static {
46         System.setProperty("org.openide.util.Lookup", "org.netbeans.core.xml.FileEntityResolverDeadlock54971Test$Lkp");
47     }
48     
49     protected void setUp() throws Exception JavaDoc {
50         Object JavaDoc lookup = Lookup.getDefault().getClass ();
51         assertEquals ("Our lookup registered", Lkp.class, lookup);
52         
53         // register Env as a handler for PublicIDs "-//NetBeans//Test//EN" which
54
// is will contain the settings file we create
55
FileObject root = Repository.getDefault().getDefaultFileSystem().getRoot();
56         FileObject register = FileUtil.createData (root, "/xml/lookups/NetBeans/Test.instance");
57         register.setAttribute("instanceCreate", Env.INSTANCE);
58         assertTrue (register.getAttribute("instanceCreate") instanceof Environment.Provider);
59         
60         
61         // prepare an object to ask him for cookie
62
FileObject fo = createSettings (root, "x.settings");
63         ComplexPair.obj = DataObject.find (fo);
64
65         assertNotNull ("correct resolver is registered", Lookup.getDefault().lookup (Environment.Provider.class));
66         assertEquals ("correct resolver is registered", FileEntityResolver.class, Lookup.getDefault().lookup (Environment.Provider.class).getClass ());
67         
68         Lookup l = org.openide.loaders.Environment.find (DataObject.find (fo.getParent()));
69         assertNotNull ("Previous call is done just to initialize the Environment.getProviders()", l);
70     }
71
72     public void testAskForEnvironmentFromLookup () throws Exception JavaDoc {
73         // this is going to clear the cache in URLMapper
74
Lkp.INSTANCE.add (new MyURLMapper ());
75
76         Lkp.query = true;
77         
78         Lookup.Result r = Lookup.getDefault().lookupResult(ComplexPair.class);
79         
80         java.util.Collection JavaDoc c = r.allInstances();
81         if (c.size () != 1 || ComplexPair.IC != c.iterator().next()) {
82             fail ("Wrong instances. Should be just " + ComplexPair.IC + " but was: " + c);
83         }
84         
85         assertEquals ("One request for environment. If this fails" +
86                 " then the obj.getCookie in ComplexPair failed and was not " +
87                 " fully executed, wrong it should be", 1, Env.howManyTimesWeHandledRequestForEnvironmentOfOurObject);
88     }
89     
90     static FileObject createSettings (FileObject root, String JavaDoc name) throws IOException JavaDoc {
91         FileObject set = FileUtil.createData (root, name);
92
93         FileLock lock = set.lock ();
94         java.io.PrintStream JavaDoc os = new java.io.PrintStream JavaDoc (set.getOutputStream (lock));
95         
96         os.println ("<?xml version=\"1.0\"?>");
97         os.println ("<!DOCTYPE settings PUBLIC \"-//NetBeans//Test//EN\" \"http://www.netbeans.org/dtds/sessionsettings-1_0.dtd\">");
98         os.println ("<lkp version=\"1.0\">");
99         os.println ("</lkp>");
100         
101         os.close ();
102         lock.releaseLock();
103         return set;
104     }
105     
106     
107     private static class ComplexPair extends org.openide.util.lookup.AbstractLookup.Pair {
108         public static ComplexPair IC;
109         public static DataObject obj;
110         
111         public ComplexPair () {
112         }
113
114         protected boolean instanceOf(Class JavaDoc c) {
115             if (c == getClass ()) {
116                 if (IC == null) {
117                     assertNull ("Just one instance allowed", IC);
118                     IC = this;
119                 }
120                 assertEquals (IC, this);
121                 
122                 // this used to print strange warnings from FileEntityResolver
123
Class JavaDoc ask = org.openide.cookies.InstanceCookie.class;
124                 Object JavaDoc ic = obj.getCookie (ask);
125                 assertNotNull ("Obj: " + obj + " Must have the InstanceCookie", ic);
126             }
127             return c.isAssignableFrom(getType ());
128         }
129
130         protected boolean creatorOf(Object JavaDoc obj) {
131             return obj == this;
132         }
133
134         public String JavaDoc getDisplayName() {
135             return getId ();
136         }
137
138         public String JavaDoc getId() {
139             return getType ().getName();
140         }
141
142         public Object JavaDoc getInstance() {
143             return this;
144         }
145
146         public Class JavaDoc getType() {
147             return getClass ();
148         }
149     }
150
151     
152     private static final class Env
153     implements InstanceCookie, org.openide.loaders.Environment.Provider {
154         public static int howManyTimesWeHandledRequestForEnvironmentOfOurObject;
155         public static final Env INSTANCE = new Env ();
156         
157         private Env () {
158             assertNull (INSTANCE);
159         }
160
161         public String JavaDoc instanceName() {
162             return getClass ().getName();
163         }
164
165         public Object JavaDoc instanceCreate() throws IOException JavaDoc, ClassNotFoundException JavaDoc {
166             return this;
167         }
168
169         public Class JavaDoc instanceClass() throws IOException JavaDoc, ClassNotFoundException JavaDoc {
170             return getClass ();
171         }
172
173         public Lookup getEnvironment(DataObject obj) {
174             if (obj == ComplexPair.obj) {
175                 howManyTimesWeHandledRequestForEnvironmentOfOurObject++;
176                 return Lookups.singleton(this);
177             } else {
178                 return null;
179             }
180         }
181         
182     }
183
184     public static final class Lkp extends org.openide.util.lookup.AbstractLookup {
185         public static org.openide.util.lookup.InstanceContent INSTANCE;
186         public static boolean query;
187         
188         public Lkp () {
189             this (new org.openide.util.lookup.InstanceContent ());
190         }
191         
192         private Lkp (org.openide.util.lookup.InstanceContent ic) {
193             super (ic);
194             ic.addPair(new ComplexPair ());
195             ic.add (new FileEntityResolver ());
196             // add yourself to the lookup as cookie
197
INSTANCE = ic;
198             
199             // add few instnaces to turn this into InheritanceTree storage lookup
200
for (int i = 0; i < 20; i++) {
201                 Lkp.INSTANCE.add (new Integer JavaDoc (i));
202             }
203
204         }
205
206         protected void beforeLookup(org.openide.util.Lookup.Template template) {
207             if (template.getType() == URLMapper.class) {
208                 if (query) {
209                     query = false;
210                     // this will throw exception
211
lookup (MyURLMapper.class);
212                     fail ("Never get here");
213                 }
214             }
215         }
216         
217         
218     }
219     
220     public static final class MyURLMapper extends URLMapper {
221         public URL JavaDoc getURL(FileObject fo, int type) {
222             return null;
223         }
224
225         public FileObject[] getFileObjects(URL JavaDoc url) {
226             return null;
227         }
228         
229     }
230     
231 }
232
Popular Tags