KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * FileEntityResolver66438Test.java
3  * JUnit based test
4  *
5  * Created on 23. listopad 2005, 9:39
6  */

7
8 package org.netbeans.core.xml;
9
10 import java.io.IOException JavaDoc;
11 import java.util.Date JavaDoc;
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 /** Checks race condition in the Lkp.beforeLookup
24  *
25  * @author Jaroslav Tulach
26  */

27 public class FileEntityResolver66438Test extends LoggingTestCaseHid {
28     
29     public FileEntityResolver66438Test(String JavaDoc testName) {
30         super(testName);
31     }
32
33     public void testRaceCondition() throws Exception JavaDoc {
34         registerIntoLookup(new ErrManager());
35         
36         // register Env as a handler for PublicIDs "-//NetBeans//Test//EN" which
37
// is will contain the settings file we create
38
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         // prepare an object to ask him for cookie
45
FileObject fo = FileEntityResolverDeadlock54971Test.createSettings (root, "x.settings");
46         final DataObject obj = DataObject.find (fo);
47
48         class QueryIC implements Runnable JavaDoc {
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 JavaDoc attachAnnotations(Throwable JavaDoc t, ErrorManager.Annotation[] arr) {
73             return null;
74         }
75
76         public ErrorManager.Annotation[] findAnnotations(Throwable JavaDoc t) {
77             return null;
78         }
79
80         public Throwable JavaDoc annotate(Throwable JavaDoc t, int severity, String JavaDoc message, String JavaDoc localizedMessage, Throwable JavaDoc stackTrace, Date JavaDoc date) {
81             return null;
82         }
83
84         public void notify(int severity, Throwable JavaDoc t) {
85         }
86
87         public void log(int severity, String JavaDoc 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 JavaDoc ex) {
94                     ex.printStackTrace();
95                 }
96                 ErrorManager.getDefault().log("Done sleeping");
97             }
98         }
99
100         public ErrorManager getInstance(String JavaDoc 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 JavaDoc instanceName() {
121             return getClass ().getName();
122         }
123
124         public Object JavaDoc instanceCreate() throws IOException JavaDoc, ClassNotFoundException JavaDoc {
125             return this;
126         }
127
128         public Class JavaDoc instanceClass() throws IOException JavaDoc, ClassNotFoundException JavaDoc {
129             return getClass ();
130         }
131
132         public Lookup getEnvironment(DataObject obj) {
133             return Lookups.singleton(this);
134         }
135         
136     }
137     
138 }
139
Popular Tags