1 7 8 package org.netbeans.core.xml; 9 10 import java.io.IOException ; 11 import java.util.Date ; 12 import org.netbeans.core.LoggingTestCaseHid; 13 import org.openide.ErrorManager; 14 import org.openide.filesystems.FileUtil; 15 import org.openide.filesystems.FileObject; 16 import org.openide.filesystems.Repository; 17 import org.openide.loaders.*; 18 import org.openide.cookies.InstanceCookie; 19 import org.openide.util.Lookup; 20 import org.openide.util.RequestProcessor; 21 import org.openide.util.lookup.Lookups; 22 23 27 public class FileEntityResolver66438Test extends LoggingTestCaseHid { 28 29 public FileEntityResolver66438Test(String testName) { 30 super(testName); 31 } 32 33 public void testRaceCondition() throws Exception { 34 registerIntoLookup(new ErrManager()); 35 36 FileObject root = Repository.getDefault().getDefaultFileSystem().getRoot(); 39 FileObject register = FileUtil.createData (root, "/xml/lookups/NetBeans/Test.instance"); 40 register.setAttribute("instanceCreate", Env.INSTANCE); 41 assertTrue (register.getAttribute("instanceCreate") instanceof Environment.Provider); 42 43 44 FileObject fo = FileEntityResolverDeadlock54971Test.createSettings (root, "x.settings"); 46 final DataObject obj = DataObject.find (fo); 47 48 class QueryIC implements Runnable { 49 public InstanceCookie ic; 50 51 public void run() { 52 ic = (InstanceCookie)obj.getCookie(InstanceCookie.class); 53 } 54 } 55 56 QueryIC i1 = new QueryIC(); 57 QueryIC i2 = new QueryIC(); 58 59 RequestProcessor.Task t1 = new RequestProcessor("t1").post(i1); 60 RequestProcessor.Task t2 = new RequestProcessor("t2").post(i2); 61 62 t1.waitFinished(); 63 t2.waitFinished(); 64 65 assertEquals("First has cookie", Env.INSTANCE, i1.ic); 66 assertEquals("Second has cookie", Env.INSTANCE, i2.ic); 67 } 68 69 private static final class ErrManager extends ErrorManager { 70 private boolean block; 71 72 public Throwable attachAnnotations(Throwable t, ErrorManager.Annotation[] arr) { 73 return null; 74 } 75 76 public ErrorManager.Annotation[] findAnnotations(Throwable t) { 77 return null; 78 } 79 80 public Throwable annotate(Throwable t, int severity, String message, String localizedMessage, Throwable stackTrace, Date date) { 81 return null; 82 } 83 84 public void notify(int severity, Throwable t) { 85 } 86 87 public void log(int severity, String s) { 88 if (block && s.indexOf("change the lookup") >= 0) { 89 block = false; 90 ErrorManager.getDefault().log("Going to sleep"); 91 try { 92 Thread.sleep(200); 93 } catch (InterruptedException ex) { 94 ex.printStackTrace(); 95 } 96 ErrorManager.getDefault().log("Done sleeping"); 97 } 98 } 99 100 public ErrorManager getInstance(String name) { 101 if (name.equals("org.netbeans.core.xml.FileEntityResolver")) { 102 ErrManager e = new ErrManager(); 103 e.block = true; 104 return e; 105 } 106 return this; 107 } 108 109 } 110 111 private static final class Env 112 implements InstanceCookie, org.openide.loaders.Environment.Provider { 113 public static int howManyTimesWeHandledRequestForEnvironmentOfOurObject; 114 public static final Env INSTANCE = new Env (); 115 116 private Env () { 117 assertNull (INSTANCE); 118 } 119 120 public String instanceName() { 121 return getClass ().getName(); 122 } 123 124 public Object instanceCreate() throws IOException , ClassNotFoundException { 125 return this; 126 } 127 128 public Class instanceClass() throws IOException , ClassNotFoundException { 129 return getClass (); 130 } 131 132 public Lookup getEnvironment(DataObject obj) { 133 return Lookups.singleton(this); 134 } 135 136 } 137 138 } 139 | Popular Tags |