1 19 20 package org.netbeans.core.xml; 21 22 import java.io.IOException ; 23 import java.net.URL ; 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 40 public class FileEntityResolverDeadlock54971Test extends TestCase { 41 public FileEntityResolverDeadlock54971Test(String 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 { 50 Object lookup = Lookup.getDefault().getClass (); 51 assertEquals ("Our lookup registered", Lkp.class, lookup); 52 53 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 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 { 73 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 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 name) throws IOException { 91 FileObject set = FileUtil.createData (root, name); 92 93 FileLock lock = set.lock (); 94 java.io.PrintStream os = new java.io.PrintStream (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 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 Class ask = org.openide.cookies.InstanceCookie.class; 124 Object 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 obj) { 131 return obj == this; 132 } 133 134 public String getDisplayName() { 135 return getId (); 136 } 137 138 public String getId() { 139 return getType ().getName(); 140 } 141 142 public Object getInstance() { 143 return this; 144 } 145 146 public Class 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 instanceName() { 162 return getClass ().getName(); 163 } 164 165 public Object instanceCreate() throws IOException , ClassNotFoundException { 166 return this; 167 } 168 169 public Class instanceClass() throws IOException , ClassNotFoundException { 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 INSTANCE = ic; 198 199 for (int i = 0; i < 20; i++) { 201 Lkp.INSTANCE.add (new Integer (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 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 getURL(FileObject fo, int type) { 222 return null; 223 } 224 225 public FileObject[] getFileObjects(URL url) { 226 return null; 227 } 228 229 } 230 231 } 232 | Popular Tags |