KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > text > ReloadDeadlockTest


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
21 package org.openide.text;
22
23
24 import java.io.File JavaDoc;
25 import java.io.IOException JavaDoc;
26 import javax.swing.text.BadLocationException JavaDoc;
27 import javax.swing.text.Position JavaDoc;
28 import javax.swing.text.StyledDocument JavaDoc;
29
30 import junit.textui.TestRunner;
31
32 import org.netbeans.junit.NbTestCase;
33 import org.netbeans.junit.NbTestSuite;
34
35 import org.openide.filesystems.FileObject;
36 import org.openide.filesystems.LocalFileSystem;
37 import org.openide.text.CloneableEditorSupport;
38
39
40 /**
41  * Test of deadlock described by issue #12557. Do not include between
42  * stable regression test bag until the issue is fixed
43  * (see <Netbeans>/openide/test/cfg-unit).
44  *
45  * How to run from IDE:
46  * 1. Mount jar: junit.jar
47  * 2. Mount dir: openide/src
48  * 3. Mount dir: openide/test/regr/src
49  * 4. Run class UnstableTest from dir openide/test/regr/src in internal execution
50  * (inside IDE VM - set execution type in Properties window)
51  * It will open new window in Editor. When deadlock is there IDE hangs.
52  * How to run from command line:
53  * In directory: <NetBeans>/openide/test/
54  * Command: ant -Dxtest.attributes=regr-unstable
55  *
56  * @author Peter Zavadsky
57  */

58 public class ReloadDeadlockTest extends NbTestCase {
59
60     /** Creates new TextTest */
61     public ReloadDeadlockTest(String JavaDoc s) {
62         super(s);
63     }
64     
65     /** Tests the deadlock described in #12557. */
66     public void testDeadlock() throws Exception JavaDoc {
67         System.out.println("START of deadlock test: see issue #12557.");
68
69         LocalFileSystem lfs = new LocalFileSystem();
70
71         File JavaDoc rootDir;
72         
73         int i = 1;
74         do {
75             rootDir = new File JavaDoc(getWorkDir(), "TestRootDir"+(i++));
76         } while(rootDir.exists() && !rootDir.isDirectory());
77         
78         System.err.println("root exists="+rootDir.exists());
79         
80         if(!rootDir.exists()) {
81             System.err.println("Created rootDir="+rootDir.mkdir());
82         }
83         
84         lfs.setRootDirectory(rootDir);
85         
86         FileObject root = lfs.getRoot();
87
88         FileObject fo = root.getFileObject("test", "txt");
89         
90         if(fo == null) {
91             fo = root.createData("test", "txt");
92         }
93
94         final SimpleCESHidden.Env env = new SimpleCESHidden.Env(fo);
95         final CloneableEditorSupport ces = new SimpleCESHidden(env);
96         env.setSupport(ces);
97
98         final StyledDocument JavaDoc doc = ces.openDocument();
99         
100         System.err.println("Creating PositionRef at 0 offset.");
101         final PositionRef posRef = ces.createPositionRef(0, Position.Bias.Forward);
102         
103         System.err.println("\nT1="+Thread.currentThread()); // TEMP
104
System.err.println("T1: Acquiring doc write lock.");
105
106         final Thread JavaDoc th2 = new Thread JavaDoc(new Runnable JavaDoc() {
107             public void run() {
108
109                 System.err.println("\nT2=" + Thread.currentThread()
110                 + "T2: Starting to reload doc.\n"
111                 + "T2: Acquiring locks in order: 1st CES lock, 2nd doc write lock.\n"
112                 + "T2: Thus will acquire CES lock and wait on doc write lock.");
113                 ces.reloadDocument().waitFinished();
114
115                 System.err.println("T2: document reloaded");
116             }
117         });
118
119         
120         NbDocument.runAtomic(doc, new Runnable JavaDoc() {
121             public void run() {
122                 System.err.println("T1: doc write lock acquired.");
123
124                 // Start the reloading thread.
125
th2.start();
126                 
127                 try {
128                     Thread.currentThread().sleep(3000);
129                 } catch(InterruptedException JavaDoc ie) {
130                     System.err.println("T1: interrupted");
131                     ie.printStackTrace();
132                 }
133                 
134                 System.err.println("\nT1: trying to get Position from PositionRef.\n"
135                 + "T1: Acquring CES lock. It simulates the issue condition,"
136                 + " this thread already holds doc write lock.\n"
137                 + "T1: After this the deadlock should occure!!");
138                 
139                 try {
140                     Position JavaDoc pos = posRef.getPosition();
141
142                     doc.insertString(pos.getOffset(), "New String Added", null);
143                     
144                     System.err.println("T1: Position="+pos);
145                     
146                     System.err.println("T1: Document after insert="+doc.getText(pos.getOffset(), doc.getLength()));
147                     
148                 } catch(IOException JavaDoc ioe) {
149                     ioe.printStackTrace();
150                 } catch(BadLocationException JavaDoc ble) {
151                     ble.printStackTrace();
152                 }
153                 
154                 System.err.println("T1: new string inserted, releasing doc write lock");
155             }
156         });
157
158         System.err.println("T1 waits for reloading thread (T2).");
159         th2.join();
160
161         System.out.println("END of deadlock test, see issue #12557.");
162         
163         System.err.println("Document after reload=\n" + doc.getText(0, doc.getLength()));
164         
165         rootDir.delete();
166     }
167     
168 }
169
Popular Tags