KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > masterfs > Deadlock54741


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.netbeans.modules.masterfs;
21
22 import java.io.File JavaDoc;
23 import junit.framework.Test;
24 import org.netbeans.junit.NbTestCase;
25 import org.netbeans.junit.NbTestSuite;
26 import org.openide.filesystems.FileAttributeEvent;
27 import org.openide.filesystems.FileChangeListener;
28 import org.openide.filesystems.FileEvent;
29 import org.openide.filesystems.FileObject;
30 import org.openide.filesystems.FileRenameEvent;
31 import org.openide.filesystems.FileSystem;
32 import org.openide.filesystems.FileUtil;
33
34 /**
35  * @author pzajac
36  */

37 public class Deadlock54741 extends NbTestCase {
38     
39     private static class DelFileChangeListener implements FileChangeListener {
40         public void fileAttributeChanged(FileAttributeEvent fe) {
41         }
42
43         public void fileChanged(FileEvent fe) {
44         }
45
46         public void fileDataCreated(FileEvent fe) {
47         }
48
49         public void fileDeleted(FileEvent fe) {
50             try {
51                 synchronized (this) {
52                     wait();
53                 }
54             } catch (InterruptedException JavaDoc e) {
55                 e.printStackTrace();
56             }
57         }
58
59         public void fileFolderCreated(FileEvent fe) {
60         }
61
62         public void fileRenamed(FileRenameEvent fe) {
63         }
64         
65     }
66     
67     private static class DeleteRunnable implements Runnable JavaDoc {
68         FileObject fo;
69         public DeleteRunnable(FileObject fo) {
70             this.fo = fo;
71         }
72         public void run() {
73             System.out.println("start delete");
74             try {
75                fo.getFileSystem().addFileChangeListener(new DelFileChangeListener());
76                FileSystem fs = fo.getFileSystem();
77                FileUtil.toFile(fo).delete();
78                fs.refresh(true);
79             } catch (Exception JavaDoc e) {
80                 e.printStackTrace();
81             }
82             System.out.println("end delete");
83         }
84     }
85     
86     static {
87         System.setProperty("org.openide.util.Lookup", MasterFileSystemTest.TestLookup.class.getName());
88     }
89     
90     public Deadlock54741(String JavaDoc testName) {
91         super(testName);
92     }
93     
94     public void testDeadLock () throws Exception JavaDoc {
95         File JavaDoc f = File.createTempFile("fff","fsdfsd");
96         FileObject tmpFo = FileUtil.toFileObject(f.getParentFile());
97         assertNotNull(tmpFo);
98       
99         FileObject fo = null;
100         fo = tmpFo.createData("ssss");
101         Runnable JavaDoc deleteRunnable = new DeleteRunnable(fo);
102         Thread JavaDoc thread = new Thread JavaDoc(deleteRunnable);
103         thread.start();
104             
105         try {
106             Thread.currentThread().sleep(2000);
107             boolean isDeadlock [] = new boolean[1];
108              makeDeadlock(tmpFo,f, isDeadlock);
109             Thread.sleep(2000);
110             boolean isD = isDeadlock[0];
111             // finish -> unlock thread
112
deleteRunnable.notify();
113             assertFalse("deadlock!!!",isD);
114         } catch (InterruptedException JavaDoc ie) {
115             ie.printStackTrace();
116         }
117         
118     }
119     private Thread JavaDoc makeDeadlock(final FileObject fo, final File JavaDoc f,final boolean isDeadLock[]) {
120         isDeadLock[0] = true;
121         Thread JavaDoc t = new Thread JavaDoc () {
122             public void run() {
123                    fo.getFileObject(f.getName());
124                    isDeadLock[0] = false;
125         }};
126         t.start();
127         return t;
128     }
129         
130     public static Test suite() {
131         NbTestSuite suite = new NbTestSuite();
132         suite.addTestSuite(Deadlock54741.class);
133          
134         return new MasterFileSystemTest(suite);
135     }
136 }
137
Popular Tags