KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > util > UtilitiesActiveQueueTest


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.util;
21
22 import java.lang.ref.Reference JavaDoc;
23 import java.lang.ref.ReferenceQueue JavaDoc;
24 import java.lang.ref.WeakReference JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.net.URLClassLoader JavaDoc;
27 import org.netbeans.junit.NbTestCase;
28
29 public class UtilitiesActiveQueueTest extends NbTestCase {
30
31     public UtilitiesActiveQueueTest(String JavaDoc testName) {
32         super(testName);
33     }
34
35     public void testRunnableReferenceIsExecuted () throws Exception JavaDoc {
36         Object JavaDoc obj = new Object JavaDoc ();
37         RunnableRef ref = new RunnableRef (obj);
38         synchronized (ref) {
39             obj = null;
40             assertGC ("Should be GCed quickly", ref);
41             ref.wait ();
42             assertTrue ("Run method has been executed", ref.executed);
43         }
44     }
45     
46     public void testRunnablesAreProcessedOneByOne () throws Exception JavaDoc {
47         Object JavaDoc obj = new Object JavaDoc ();
48         RunnableRef ref = new RunnableRef (obj);
49         ref.wait = true;
50         
51         
52         synchronized (ref) {
53             obj = null;
54             assertGC ("Is garbage collected", ref);
55             ref.wait ();
56             assertTrue ("Still not executed, it is blocked", !ref.executed);
57         }
58
59         RunnableRef after = new RunnableRef (new Object JavaDoc ());
60         synchronized (after) {
61             assertGC ("Is garbage collected", after);
62             after.wait (100); // will fail
63
assertTrue ("Even if GCed, still not processed", !after.executed);
64         }
65
66         synchronized (after) {
67             synchronized (ref) {
68                 ref.notify ();
69                 ref.wait ();
70                 assertTrue ("Processed", ref.executed);
71             }
72             after.wait ();
73             assertTrue ("Processed too", after.executed);
74         }
75     }
76     
77     public void testCallingPublicMethodsThrowsExceptions () {
78         try {
79             Utilities.activeReferenceQueue().poll();
80             fail ("One should not call public method from outside");
81         } catch (RuntimeException JavaDoc ex) {
82         }
83         try {
84             Utilities.activeReferenceQueue ().remove ();
85             fail ("One should not call public method from outside");
86         } catch (InterruptedException JavaDoc ex) {
87         }
88         try {
89             Utilities.activeReferenceQueue ().remove (10);
90             fail ("One should not call public method from outside");
91         } catch (InterruptedException JavaDoc ex) {
92         }
93     }
94     
95     public void testMemoryLeak() throws Exception JavaDoc {
96         final Class JavaDoc<?> u1 = Utilities.class;
97         class L extends URLClassLoader JavaDoc {
98             public L() {
99                 super(new URL JavaDoc[] {u1.getProtectionDomain().getCodeSource().getLocation()}, u1.getClassLoader().getParent());
100             }
101             protected Class JavaDoc<?> loadClass(String JavaDoc name, boolean resolve) throws ClassNotFoundException JavaDoc {
102                 if (name.equals(u1.getName()) || name.startsWith(u1.getName() + "$")) {
103                     Class JavaDoc c = findLoadedClass(name);
104                     if (c == null) {
105                         c = findClass(name);
106                     }
107                     if (resolve) {
108                         resolveClass(c);
109                     }
110                     return c;
111                 } else {
112                     return super.loadClass(name, resolve);
113                 }
114             }
115         }
116         ClassLoader JavaDoc l = new L JavaDoc();
117         Class JavaDoc<?> u2 = l.loadClass(u1.getName());
118         assertEquals(l, u2.getClassLoader());
119         Object JavaDoc obj = new Object JavaDoc();
120         @SuppressWarnings JavaDoc("unchecked")
121         ReferenceQueue JavaDoc<Object JavaDoc> q = (ReferenceQueue JavaDoc<Object JavaDoc>) u2.getMethod("activeReferenceQueue").invoke(null);
122         RunnableRef ref = new RunnableRef(obj, q);
123         synchronized (ref) {
124             obj = null;
125             assertGC("Ref should be GC'ed as usual", ref);
126             ref.wait();
127             assertTrue("Run method has been executed", ref.executed);
128         }
129         Reference JavaDoc<?> r = new WeakReference JavaDoc<Object JavaDoc>(u2);
130         q = null;
131         u2 = null;
132         l = null;
133         assertGC("#86625: Utilities.class can also be collected now", r);
134     }
135
136     
137     private static class RunnableRef extends WeakReference JavaDoc<Object JavaDoc>
138     implements Runnable JavaDoc {
139         public boolean wait;
140         public boolean entered;
141         public boolean executed;
142         
143         public RunnableRef (Object JavaDoc o) {
144             this(o, Utilities.activeReferenceQueue());
145         }
146         
147         public RunnableRef(Object JavaDoc o, ReferenceQueue JavaDoc<Object JavaDoc> q) {
148             super(o, q);
149         }
150         
151         public synchronized void run () {
152             entered = true;
153             if (wait) {
154                 // notify we are here
155
notify ();
156                 try {
157                     wait ();
158                 } catch (InterruptedException JavaDoc ex) {
159                 }
160             }
161             executed = true;
162             
163             notifyAll ();
164         }
165     }
166 }
167
Popular Tags