KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > filesystems > StreamPool50137Test


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.filesystems;
21
22
23 import java.io.InputStream JavaDoc;
24 import java.io.OutputStream JavaDoc;
25 import org.netbeans.junit.*;
26 import org.openide.util.Task;
27
28 /**
29  * Simulate deadlock from issue 50137.
30  *
31  * There is expected not to call any foreign code from
32  * StreamPool.
33  *
34  * @author Radek Matous
35  */

36 public class StreamPool50137Test extends NbTestCase {
37     /**
38      * filesystem containing created instances
39      */

40     private FileSystem lfs;
41     private FileObject testFo;
42
43     public StreamPool50137Test(String JavaDoc name) {
44         super(name);
45     }
46
47     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
48         junit.textui.TestRunner.run(new NbTestSuite(StreamPool50137Test.class));
49     }
50
51     /**
52      * Setups variables.
53      */

54     protected void setUp() throws Exception JavaDoc {
55
56         TestUtilHid.destroyLocalFileSystem(getName());
57         lfs = new TestFileSystem ((LocalFileSystem)TestUtilHid.createLocalFileSystem(getName(), new String JavaDoc[]{"TestDeadlock" }));
58         testFo = lfs.findResource("TestDeadlock");
59         assertNotNull(testFo);
60     }
61
62
63     public void testStreamPoolDeadlock () throws Exception JavaDoc {
64         FileLock fLock = testFo.lock();
65         OutputStream JavaDoc os = null;
66         InputStream JavaDoc is = null;
67         
68         try {
69             os = testFo.getOutputStream(fLock);
70             os.close(); os = null;
71             is = testFo.getInputStream();
72             is.close(); is = null;
73         } finally {
74             if (fLock != null) fLock.releaseLock();
75             if (os != null) os.close();
76             if (is != null) is.close();
77         }
78     }
79     
80     private static final class TestFileSystem extends LocalFileSystem {
81         TestFileSystem (LocalFileSystem lfs) throws Exception JavaDoc {
82             super ();
83             this.info = new TestImplInfo (this);
84             setRootDirectory(lfs.getRootDirectory());
85         }
86     }
87
88     private static final class TestImplInfo extends LocalFileSystem.Impl {
89         private final TestFileSystem tfs;
90         TestImplInfo (TestFileSystem tfs) {
91             super (tfs);
92             this.tfs = tfs;
93             
94         }
95         
96         public OutputStream JavaDoc outputStream(String JavaDoc name) throws java.io.IOException JavaDoc {
97             OutputStream JavaDoc retValue = super.outputStream(name);
98             deadlockSimulation ();
99             return retValue;
100         }
101
102         public InputStream JavaDoc inputStream(String JavaDoc name) throws java.io.FileNotFoundException JavaDoc {
103             InputStream JavaDoc retValue = super.inputStream(name);
104             deadlockSimulation ();
105             return retValue;
106         }
107         
108         
109         private void deadlockSimulation() {
110             Task task = org.openide.util.RequestProcessor.getDefault().post(new Runnable JavaDoc () {
111                 public void run() {
112                     StreamPool.find(tfs);
113                 }
114             });
115             task.waitFinished();
116         }
117
118     }
119
120 }
121   
122   
123   
Popular Tags