001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.core.xml;
043:
044: import java.io.IOException;
045: import java.net.URL;
046: import junit.framework.TestCase;
047: import org.openide.cookies.InstanceCookie;
048: import org.openide.filesystems.FileLock;
049: import org.openide.filesystems.FileObject;
050: import org.openide.filesystems.FileUtil;
051: import org.openide.filesystems.Repository;
052: import org.openide.filesystems.URLMapper;
053: import org.openide.loaders.DataObject;
054: import org.openide.loaders.Environment;
055: import org.openide.util.Lookup;
056: import org.openide.util.lookup.AbstractLookup;
057: import org.openide.util.lookup.Lookups;
058:
059: /**
060: *
061: * @author Jaroslav Tulach
062: */
063: public class FileEntityResolverDeadlock54971Test extends TestCase {
064: public FileEntityResolverDeadlock54971Test(String testName) {
065: super (testName);
066: }
067:
068: static {
069: System
070: .setProperty("org.openide.util.Lookup",
071: "org.netbeans.core.xml.FileEntityResolverDeadlock54971Test$Lkp");
072: }
073:
074: protected void setUp() throws Exception {
075: Object lookup = Lookup.getDefault().getClass();
076: assertEquals("Our lookup registered", Lkp.class, lookup);
077:
078: // register Env as a handler for PublicIDs "-//NetBeans//Test//EN" which
079: // is will contain the settings file we create
080: FileObject root = Repository.getDefault()
081: .getDefaultFileSystem().getRoot();
082: FileObject register = FileUtil.createData(root,
083: "/xml/lookups/NetBeans/Test.instance");
084: register.setAttribute("instanceCreate", Env.INSTANCE);
085: assertTrue(register.getAttribute("instanceCreate") instanceof Environment.Provider);
086:
087: // prepare an object to ask him for cookie
088: FileObject fo = createSettings(root, "x.settings");
089: ComplexPair.obj = DataObject.find(fo);
090:
091: assertNotNull("correct resolver is registered", Lookup
092: .getDefault().lookup(Environment.Provider.class));
093: assertEquals("correct resolver is registered",
094: FileEntityResolver.class, Lookup.getDefault().lookup(
095: Environment.Provider.class).getClass());
096:
097: Lookup l = org.openide.loaders.Environment.find(DataObject
098: .find(fo.getParent()));
099: assertNotNull(
100: "Previous call is done just to initialize the Environment.getProviders()",
101: l);
102: }
103:
104: public void testAskForEnvironmentFromLookup() throws Exception {
105: // this is going to clear the cache in URLMapper
106: Lkp.INSTANCE.add(new MyURLMapper());
107:
108: Lkp.query = true;
109:
110: Lookup.Result r = Lookup.getDefault().lookupResult(
111: ComplexPair.class);
112:
113: java.util.Collection c = r.allInstances();
114: if (c.size() != 1 || ComplexPair.IC != c.iterator().next()) {
115: fail("Wrong instances. Should be just " + ComplexPair.IC
116: + " but was: " + c);
117: }
118:
119: assertEquals(
120: "One request for environment. If this fails"
121: + " then the obj.getCookie in ComplexPair failed and was not "
122: + " fully executed, wrong it should be",
123: 1,
124: Env.howManyTimesWeHandledRequestForEnvironmentOfOurObject);
125: }
126:
127: static FileObject createSettings(FileObject root, String name)
128: throws IOException {
129: FileObject set = FileUtil.createData(root, name);
130:
131: FileLock lock = set.lock();
132: java.io.PrintStream os = new java.io.PrintStream(set
133: .getOutputStream(lock));
134:
135: os.println("<?xml version=\"1.0\"?>");
136: os
137: .println("<!DOCTYPE settings PUBLIC \"-//NetBeans//Test//EN\" \"http://www.netbeans.org/dtds/sessionsettings-1_0.dtd\">");
138: os.println("<lkp version=\"1.0\">");
139: os.println("</lkp>");
140:
141: os.close();
142: lock.releaseLock();
143: return set;
144: }
145:
146: private static class ComplexPair extends
147: AbstractLookup.Pair<ComplexPair> {
148: public static ComplexPair IC;
149: public static DataObject obj;
150:
151: public ComplexPair() {
152: }
153:
154: protected boolean instanceOf(Class<?> c) {
155: if (c == getClass()) {
156: if (IC == null) {
157: assertNull("Just one instance allowed", IC);
158: IC = this ;
159: }
160: assertEquals(IC, this );
161:
162: // this used to print strange warnings from FileEntityResolver
163: assertNotNull("Obj: " + obj
164: + " Must have the InstanceCookie", obj
165: .getCookie(InstanceCookie.class));
166: }
167: return c.isAssignableFrom(getType());
168: }
169:
170: protected boolean creatorOf(Object obj) {
171: return obj == this ;
172: }
173:
174: public String getDisplayName() {
175: return getId();
176: }
177:
178: public String getId() {
179: return getType().getName();
180: }
181:
182: public ComplexPair getInstance() {
183: return this ;
184: }
185:
186: public Class<? extends ComplexPair> getType() {
187: return getClass();
188: }
189: }
190:
191: private static final class Env implements InstanceCookie,
192: org.openide.loaders.Environment.Provider {
193: public static int howManyTimesWeHandledRequestForEnvironmentOfOurObject;
194: public static final Env INSTANCE = new Env();
195:
196: private Env() {
197: assertNull(INSTANCE);
198: }
199:
200: public String instanceName() {
201: return getClass().getName();
202: }
203:
204: public Object instanceCreate() throws IOException,
205: ClassNotFoundException {
206: return this ;
207: }
208:
209: public Class instanceClass() throws IOException,
210: ClassNotFoundException {
211: return getClass();
212: }
213:
214: public Lookup getEnvironment(DataObject obj) {
215: if (obj == ComplexPair.obj) {
216: howManyTimesWeHandledRequestForEnvironmentOfOurObject++;
217: return Lookups.singleton(this );
218: } else {
219: return null;
220: }
221: }
222:
223: }
224:
225: public static final class Lkp extends
226: org.openide.util.lookup.AbstractLookup {
227: public static org.openide.util.lookup.InstanceContent INSTANCE;
228: public static boolean query;
229:
230: public Lkp() {
231: this (new org.openide.util.lookup.InstanceContent());
232: }
233:
234: private Lkp(org.openide.util.lookup.InstanceContent ic) {
235: super (ic);
236: ic.addPair(new ComplexPair());
237: ic.add(new FileEntityResolver());
238: // add yourself to the lookup as cookie
239: INSTANCE = ic;
240:
241: // add few instnaces to turn this into InheritanceTree storage lookup
242: for (int i = 0; i < 20; i++) {
243: Lkp.INSTANCE.add(new Integer(i));
244: }
245:
246: }
247:
248: protected void beforeLookup(
249: org.openide.util.Lookup.Template template) {
250: if (template.getType() == URLMapper.class) {
251: if (query) {
252: query = false;
253: // this will throw exception
254: lookup(MyURLMapper.class);
255: fail("Never get here");
256: }
257: }
258: }
259:
260: }
261:
262: public static final class MyURLMapper extends URLMapper {
263: public URL getURL(FileObject fo, int type) {
264: return null;
265: }
266:
267: public FileObject[] getFileObjects(URL url) {
268: return null;
269: }
270:
271: }
272:
273: }
|