001: /*
002: * FileEntityResolver66438Test.java
003: * JUnit based test
004: *
005: * Created on 23. listopad 2005, 9:39
006: */
007:
008: package org.netbeans.core.xml;
009:
010: import java.io.IOException;
011: import java.util.Date;
012: import org.netbeans.core.LoggingTestCaseHid;
013: import org.openide.ErrorManager;
014: import org.openide.filesystems.FileUtil;
015: import org.openide.filesystems.FileObject;
016: import org.openide.filesystems.Repository;
017: import org.openide.loaders.*;
018: import org.openide.cookies.InstanceCookie;
019: import org.openide.util.Lookup;
020: import org.openide.util.RequestProcessor;
021: import org.openide.util.lookup.Lookups;
022:
023: /** Checks race condition in the Lkp.beforeLookup
024: *
025: * @author Jaroslav Tulach
026: */
027: public class FileEntityResolver66438Test extends LoggingTestCaseHid {
028:
029: public FileEntityResolver66438Test(String testName) {
030: super (testName);
031: }
032:
033: public void testRaceCondition() throws Exception {
034: registerIntoLookup(new ErrManager());
035:
036: // register Env as a handler for PublicIDs "-//NetBeans//Test//EN" which
037: // is will contain the settings file we create
038: FileObject root = Repository.getDefault()
039: .getDefaultFileSystem().getRoot();
040: FileObject register = FileUtil.createData(root,
041: "/xml/lookups/NetBeans/Test.instance");
042: register.setAttribute("instanceCreate", Env.INSTANCE);
043: assertTrue(register.getAttribute("instanceCreate") instanceof Environment.Provider);
044:
045: // prepare an object to ask him for cookie
046: FileObject fo = FileEntityResolverDeadlock54971Test
047: .createSettings(root, "x.settings");
048: final DataObject obj = DataObject.find(fo);
049:
050: class QueryIC implements Runnable {
051: public InstanceCookie ic;
052:
053: public void run() {
054: ic = (InstanceCookie) obj
055: .getCookie(InstanceCookie.class);
056: }
057: }
058:
059: QueryIC i1 = new QueryIC();
060: QueryIC i2 = new QueryIC();
061:
062: RequestProcessor.Task t1 = new RequestProcessor("t1").post(i1);
063: RequestProcessor.Task t2 = new RequestProcessor("t2").post(i2);
064:
065: t1.waitFinished();
066: t2.waitFinished();
067:
068: assertEquals("First has cookie", Env.INSTANCE, i1.ic);
069: assertEquals("Second has cookie", Env.INSTANCE, i2.ic);
070: }
071:
072: private static final class ErrManager extends ErrorManager {
073: private boolean block;
074:
075: public Throwable attachAnnotations(Throwable t,
076: ErrorManager.Annotation[] arr) {
077: return null;
078: }
079:
080: public ErrorManager.Annotation[] findAnnotations(Throwable t) {
081: return null;
082: }
083:
084: public Throwable annotate(Throwable t, int severity,
085: String message, String localizedMessage,
086: Throwable stackTrace, Date date) {
087: return null;
088: }
089:
090: public void notify(int severity, Throwable t) {
091: }
092:
093: public void log(int severity, String s) {
094: if (block && s.indexOf("change the lookup") >= 0) {
095: block = false;
096: ErrorManager.getDefault().log("Going to sleep");
097: try {
098: Thread.sleep(200);
099: } catch (InterruptedException ex) {
100: ex.printStackTrace();
101: }
102: ErrorManager.getDefault().log("Done sleeping");
103: }
104: }
105:
106: public ErrorManager getInstance(String name) {
107: if (name.equals("org.netbeans.core.xml.FileEntityResolver")) {
108: ErrManager e = new ErrManager();
109: e.block = true;
110: return e;
111: }
112: return this ;
113: }
114:
115: }
116:
117: private static final class Env implements InstanceCookie,
118: org.openide.loaders.Environment.Provider {
119: public static int howManyTimesWeHandledRequestForEnvironmentOfOurObject;
120: public static final Env INSTANCE = new Env();
121:
122: private Env() {
123: assertNull(INSTANCE);
124: }
125:
126: public String instanceName() {
127: return getClass().getName();
128: }
129:
130: public Object instanceCreate() throws IOException,
131: ClassNotFoundException {
132: return this ;
133: }
134:
135: public Class instanceClass() throws IOException,
136: ClassNotFoundException {
137: return getClass();
138: }
139:
140: public Lookup getEnvironment(DataObject obj) {
141: return Lookups.singleton(this);
142: }
143:
144: }
145:
146: }
|