KickJava   Java API By Example, From Geeks To Geeks.

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


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.netbeans.junit.NbTestCase;
23 import org.openide.filesystems.FileAttributeEvent;
24 import org.openide.filesystems.FileChangeListener;
25 import org.openide.filesystems.FileEvent;
26 import org.openide.filesystems.FileObject;
27 import org.openide.filesystems.FileRenameEvent;
28 import org.openide.filesystems.FileSystem;
29 import org.openide.filesystems.FileUtil;
30 import org.openide.filesystems.Repository;
31 import org.openide.util.RequestProcessor;
32
33 /** MDR listens on FileChanges and wants to acquire its lock then.
34  * Also it does rename under holding its lock in other thread.
35  *
36  * @author Jaroslav Tulach
37  */

38 public class Deadlock59522Test extends NbTestCase implements FileChangeListener {
39     FileObject toolbars;
40     FileSystem fs;
41     DataFolder toolbarsFolder;
42     DataFolder anotherFolder;
43     DataObject obj;
44     DataObject anotherObj;
45     
46     
47     Exception JavaDoc assigned;
48     boolean called;
49     boolean ok;
50     
51     private Object JavaDoc BIG_MDR_LOCK = new Object JavaDoc();
52     
53     public Deadlock59522Test(String JavaDoc testName) {
54         super (testName);
55     }
56     
57     protected void setUp() throws Exception JavaDoc {
58         fs = Repository.getDefault ().getDefaultFileSystem ();
59         FileObject root = fs.getRoot ();
60         toolbars = FileUtil.createFolder (root, "Toolbars");
61         toolbarsFolder = DataFolder.findFolder (toolbars);
62         FileObject[] arr = toolbars.getChildren ();
63         for (int i = 0; i < arr.length; i++) {
64             arr[i].delete ();
65         }
66         FileObject fo = FileUtil.createData (root, "Ahoj.txt");
67         obj = DataObject.find (fo);
68         fo = FileUtil.createFolder (root, "Another");
69         anotherFolder = DataFolder.findFolder (fo);
70         fo = FileUtil.createData (root, "Another.txt");
71         anotherObj = DataObject.find (fo);
72         
73         fs.addFileChangeListener (this);
74     }
75
76     protected void tearDown() throws Exception JavaDoc {
77         fs.removeFileChangeListener (this);
78         
79         assertTrue ("The doRenameAObjectWhileHoldingMDRLock must be called", called);
80         
81         FileObject[] arr = toolbars.getChildren ();
82         for (int i = 0; i < arr.length; i++) {
83             arr[i].delete ();
84         }
85         
86         if (assigned != null) {
87             throw assigned;
88         }
89     }
90     private static int cnt = 0;
91     private void startRename() throws Exception JavaDoc {
92         synchronized (BIG_MDR_LOCK) {
93             RequestProcessor.getDefault ().post (new Runnable JavaDoc () {
94                 public void run () {
95                     synchronized (BIG_MDR_LOCK) {
96                         try {
97                             called = true;
98                             BIG_MDR_LOCK.notify();
99                             BIG_MDR_LOCK.wait(); // for notification
100
// in some thread try to rename some object while holding mdr lock
101
anotherObj.rename ("mynewname" + cnt++);
102                             ok = true;
103                         } catch (Exception JavaDoc ex) {
104                             assigned = ex;
105                         } finally {
106                             // end this all
107
BIG_MDR_LOCK.notifyAll();
108                         }
109                     }
110                 }
111             });
112             BIG_MDR_LOCK.wait();
113         }
114     }
115     
116     private boolean wasHereOnce;
117     private void lockMdr() {
118         if (wasHereOnce) {
119             return;
120         }
121         
122         wasHereOnce = true;
123         // no more callbacks
124
fs.removeFileChangeListener(this);
125         
126         synchronized (BIG_MDR_LOCK) {
127             BIG_MDR_LOCK.notify(); // notified from herer
128
try {
129                 BIG_MDR_LOCK.wait();
130             } catch (InterruptedException JavaDoc ex) {
131                 ex.printStackTrace();
132                 fail ("No InterruptedExceptions");
133             }
134             assertTrue ("Rename finished ok", ok);
135         }
136     }
137     
138
139     public void testMove () throws Exception JavaDoc {
140         synchronized (BIG_MDR_LOCK) {
141             startRename();
142             obj.move (anotherFolder);
143         }
144     }
145
146     public void testCopy () throws Exception JavaDoc {
147         synchronized (BIG_MDR_LOCK) {
148             startRename();
149             obj.copy (anotherFolder);
150         }
151     }
152     
153     public void testRename () throws Exception JavaDoc {
154         synchronized (BIG_MDR_LOCK) {
155             startRename();
156             obj.rename ("NewName.txt");
157         }
158     }
159     
160     public void testCreateShadow () throws Exception JavaDoc {
161         synchronized (BIG_MDR_LOCK) {
162             startRename();
163             obj.createShadow (anotherFolder);
164         }
165     }
166     
167     public void testTemplate () throws Exception JavaDoc {
168         synchronized (BIG_MDR_LOCK) {
169             startRename();
170             obj.createFromTemplate (anotherFolder);
171         }
172     }
173
174     public void testTemplate2 () throws Exception JavaDoc {
175         synchronized (BIG_MDR_LOCK) {
176             startRename();
177             obj.createFromTemplate (anotherFolder, "AhojVole.txt");
178         }
179     }
180
181     //
182
// Listener triggers creation of the node
183
//
184

185     public void fileRenamed (FileRenameEvent fe) {
186         lockMdr ();
187     }
188
189     public void fileAttributeChanged (FileAttributeEvent fe) {
190     }
191
192     public void fileFolderCreated (FileEvent fe) {
193         lockMdr ();
194     }
195
196     public void fileDeleted (FileEvent fe) {
197         lockMdr ();
198     }
199
200     public void fileDataCreated (FileEvent fe) {
201         lockMdr ();
202     }
203
204     public void fileChanged (FileEvent fe) {
205         lockMdr ();
206     }
207     
208 }
209
Popular Tags