KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > loaders > InstanceDataObjecIssue47707Test


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.openide.loaders;
21
22 import org.openide.filesystems.*;
23 import org.openide.filesystems.FileSystem; // override java.io.FileSystem
24
import org.openide.loaders.*;
25 import org.openide.cookies.*;
26 import org.openide.util.*;
27
28 import java.beans.*;
29 import java.io.*;
30 import java.util.*;
31
32 import org.netbeans.junit.*;
33
34 /** Simulate deadlock from issue 47707.
35  *
36  * @author Radek Matous, Jaroslav Tulach
37  */

38 public class InstanceDataObjecIssue47707Test extends NbTestCase {
39     /** folder to create instances in */
40     private DataObject inst;
41     /** filesystem containing created instances */
42     private FileSystem lfs;
43     
44     /** Creates new DataFolderTest */
45     public InstanceDataObjecIssue47707Test(String JavaDoc name) {
46         super (name);
47     }
48     
49     /** Setups variables.
50      */

51     protected void setUp () throws Exception JavaDoc {
52         System.setProperty ("org.openide.util.Lookup", "org.openide.loaders.InstanceDataObjecIssue47707Test$Lkp");
53         
54         String JavaDoc fsstruct [] = new String JavaDoc [] {
55             "A.settings",
56         };
57         
58         TestUtilHid.destroyLocalFileSystem (getName());
59         lfs = TestUtilHid.createLocalFileSystem (getWorkDir(), fsstruct);
60
61         FileObject bb = lfs.findResource("A.settings");
62         
63         inst = DataObject.find (bb);
64     }
65
66     public void testGetCookieCanBeCalledTwice () throws Exception JavaDoc {
67         Object JavaDoc cookie = inst.getCookie (org.openide.cookies.InstanceCookie.class);
68         
69         assertNotNull ("There is at least data object", cookie);
70         assertEquals ("Of right type", LkpForDO.class, cookie.getClass ());
71         
72     }
73     
74     public static final class Lkp extends org.openide.util.lookup.AbstractLookup
75     implements Environment.Provider {
76         public Lkp () {
77             this (new org.openide.util.lookup.InstanceContent ());
78         }
79         
80         private Lkp (org.openide.util.lookup.InstanceContent ic) {
81             super (ic);
82             ic.add (this);
83         }
84         
85         public org.openide.util.Lookup getEnvironment (DataObject obj) {
86             return new LkpForDO (new org.openide.util.lookup.InstanceContent (), obj);
87         }
88     } // end of Lkp
89

90     public static final class LkpForDO extends org.openide.util.lookup.AbstractLookup
91     implements org.openide.cookies.InstanceCookie, Runnable JavaDoc {
92         private boolean triedToDeadlock;
93         private DataObject obj;
94         
95         private LkpForDO (org.openide.util.lookup.InstanceContent ic, DataObject obj) {
96             super (ic);
97             ic.add (this);
98             this.obj = obj;
99         }
100         
101         public void run () {
102             // tries to query instance data object from other thread
103
Object JavaDoc o = obj.getCookie (InstanceCookie.class);
104             assertNotNull ("Cookie is there", o);
105         }
106
107         protected void beforeLookup(Template template) {
108             if (!triedToDeadlock) {
109                 triedToDeadlock = true;
110                 org.openide.util.RequestProcessor.getDefault ().post (this).waitFinished ();
111             }
112         }
113         
114         
115         public String JavaDoc instanceName () {
116             return getClass ().getName ();
117         }
118
119         public Class JavaDoc instanceClass ()
120         throws java.io.IOException JavaDoc, ClassNotFoundException JavaDoc {
121             return getClass ();
122         }
123
124         public Object JavaDoc instanceCreate ()
125         throws java.io.IOException JavaDoc, ClassNotFoundException JavaDoc {
126             return this;
127         }
128
129         public String JavaDoc toString() {
130             return getClass().getName();
131         }
132     } // end LkpForDO
133

134 }
135
Popular Tags