KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > options > BaseOptionsDeadlock92449Test


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.editor.options;
21
22 import java.util.Collection JavaDoc;
23 import javax.swing.text.EditorKit JavaDoc;
24 import org.netbeans.api.editor.mimelookup.MimeLookup;
25 import org.netbeans.api.editor.mimelookup.MimePath;
26 import org.netbeans.junit.NbTestCase;
27 import org.openide.filesystems.FileObject;
28 import org.openide.filesystems.Repository;
29 import org.openide.modules.ModuleInfo;
30 import org.openide.util.Lookup;
31 import org.openide.util.RequestProcessor;
32 import org.openide.util.Task;
33
34 /**
35  *
36  * @author vita
37  */

38 public class BaseOptionsDeadlock92449Test extends NbTestCase {
39     
40     /** Creates a new instance of BaseOptionsTest */
41     public BaseOptionsDeadlock92449Test(String JavaDoc name) {
42         super(name);
43     }
44     
45     public void testDeadlock92449() {
46         // Initialize the whole module system, it should load java module besides of other things
47
Collection JavaDoc modules = Lookup.getDefault().lookupAll(ModuleInfo.class);
48
49         // Check that the modules have been loaded properly
50
FileObject f = Repository.getDefault().getDefaultFileSystem().findResource("Editors/text/x-java/Settings.settings");
51         assertNotNull("Can't find Settings.settings for text/x-java", f);
52
53         final String JavaDoc mimeType = "text/x-java";
54         final MimePath mimePath = MimePath.parse(mimeType);
55
56         final BaseOptions [] baseOptions = new BaseOptions[1];
57         final Runnable JavaDoc runnableA = new Runnable JavaDoc() {
58             public void run() {
59                 baseOptions[0] = (BaseOptions) MimeLookup.getLookup(mimePath).lookup(BaseOptions.class);
60                 baseOptions[0].getAbbrevMap();
61             }
62         };
63
64         final EditorKit JavaDoc [] editorKit = new EditorKit JavaDoc[1];
65         final Runnable JavaDoc runnableB = new Runnable JavaDoc() {
66             public void run() {
67                 editorKit[0] = (EditorKit JavaDoc) MimeLookup.getLookup(mimePath).lookup(EditorKit JavaDoc.class);
68                 editorKit[0].getActions();
69             }
70         };
71
72         final Boolean JavaDoc [] stop = new Boolean JavaDoc[] { Boolean.FALSE };
73         final Runnable JavaDoc loadGenerator = new Runnable JavaDoc() {
74             public void run() {
75                 for( ; ; ) {
76                     if (stop[0].booleanValue()) {
77                         break;
78                     }
79                     
80                     int [] array = new int [1024000];
81                     for(int j = 0; j < array.length; j++) {
82                         array[j] = j;
83                     }
84                 }
85             }
86         };
87         
88         Task loadGeneratorTask = RequestProcessor.getDefault().post(loadGenerator);
89         Task taskA = RequestProcessor.getDefault().post(runnableA);
90         Task taskB = RequestProcessor.getDefault().post(runnableB);
91         
92         for(int i = 0; i < 50; i++) {
93             if (taskA.isFinished() && taskB.isFinished()) {
94                 break;
95             }
96             
97             try {
98                 Thread.sleep(1000);
99             } catch (InterruptedException JavaDoc e) {
100                 // ignore
101
}
102         }
103         
104         stop[0] = Boolean.TRUE;
105  
106         assertTrue("TaskA - lookup BaseOptions, has not finished. Possible deadlock", taskA.isFinished());
107         assertTrue("TaskB - lookup EditorKit, has not finished. Possible deadlock", taskB.isFinished());
108         
109         assertNotNull("Can't find BaseOptions for " + mimeType, baseOptions[0]);
110         assertEquals("Wrong mime type on BaseOptions", mimeType, baseOptions[0].getContentType());
111
112         assertNotNull("Can't find EditorKit for " + mimeType, editorKit[0]);
113         assertEquals("Wrong mime type on EditorKit", mimeType, editorKit[0].getContentType());
114     }
115 }
116
Popular Tags