KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > registry > ThreadingTest


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.api.registry;
21
22 import junit.textui.TestRunner;
23 import org.netbeans.junit.NbTestCase;
24 import org.netbeans.junit.NbTestSuite;
25 import org.openide.modules.ModuleInfo;
26 import org.openide.util.Lookup;
27
28 public class ThreadingTest extends NbTestCase {
29     private boolean ok = false;
30
31     public ThreadingTest(String JavaDoc name) {
32         super (name);
33     }
34
35     public static void main(String JavaDoc[] args) {
36         TestRunner.run(new NbTestSuite(ThreadingTest.class));
37     }
38
39     protected void setUp () throws Exception JavaDoc {
40         Lookup.getDefault().lookup(ModuleInfo.class);
41     }
42     
43     public void testThreading() throws Exception JavaDoc {
44         Context subctx = getContext().createSubcontext ("threading");
45         Context subctx2 = subctx.createSubcontext ("hoy");
46         
47         // - this thread will grab read mutex and will read for a few seconds
48
// - inside ReadThread1 is started writing thread which must wait
49
// till the ReadThread1 is finished
50
// - inside WriteThread the ReadThread2 is started which must wait till
51
// the WriteThread is finished
52
ReadThread1 rt1 = new ReadThread1();
53         rt1.start();
54
55         // wait till the threads are done.
56
try {
57             Thread.sleep(4000);
58         } catch(InterruptedException JavaDoc ie) {
59             assertTrue("Some error"+ ie.toString(), false);
60         }
61
62         assertTrue("Something failed", ok);
63         
64         getContext().destroySubcontext("threading");
65     }
66
67     protected Context getContext() {
68         return Context.getDefault();
69     }
70
71     private class ReadThread1 extends Thread JavaDoc {
72         public void run() {
73             final Context ctx = Context.getDefault().getSubcontext("threading");
74             ctx.getMutex().readAccess(new Runnable JavaDoc() {
75                 public void run() {
76                     
77                     // this thread will try to delete the hoy context
78
WriteThread wt = new WriteThread();
79                     wt.start();
80                     
81                     try {
82                         sleep(1500);
83                     } catch(InterruptedException JavaDoc ie) {
84                         assertTrue("Some error"+ ie.toString(), false);
85                     }
86                     
87                     Context c = ctx.getSubcontext("hoy");
88                     assertTrue("The 'hoy' context must exist.", c != null);
89                 }
90             });
91         }
92     }
93     
94     private class WriteThread extends Thread JavaDoc {
95         public void run() {
96             final Context ctx = Context.getDefault();
97             ctx.getMutex().writeAccess(new Runnable JavaDoc() {
98                 public void run() {
99                     
100                     // this thread will try to the hoy context which should be deleted by write thread
101
ReadThread2 rt2 = new ReadThread2();
102                     rt2.start();
103                     
104                     try {
105                         sleep(1500);
106                     } catch(InterruptedException JavaDoc ie) {
107                         assertTrue("Some error"+ ie.toString(), false);
108                     }
109                     
110                     try {
111                         Context ctx2 = ctx.getSubcontext("threading");
112                         ctx2.destroySubcontext("hoy");
113                     } catch (ContextException ce) {
114                         assertTrue("Cannot delete subcontext "+ ce.toString(), false);
115                     }
116                 }
117             });
118         }
119     }
120     
121     private class ReadThread2 extends Thread JavaDoc {
122         public void run() {
123             final Context ctx = Context.getDefault().getSubcontext("threading");
124             ctx.getMutex().readAccess(new Runnable JavaDoc() {
125                 public void run() {
126                     Context c = ctx.getSubcontext("hoy");
127                     assertTrue("The 'hoy' context cannot exist.", c == null);
128                     ThreadingTest.this.ok = true;
129                 }
130             });
131         }
132     }
133     
134     
135 }
136
Popular Tags