KickJava   Java API By Example, From Geeks To Geeks.

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


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.beans.PropertyChangeListener JavaDoc;
23 import java.lang.ref.WeakReference JavaDoc;
24 import java.lang.reflect.Method JavaDoc;
25 import java.util.Arrays JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.Vector JavaDoc;
28 import junit.framework.*;
29 import org.netbeans.junit.*;
30
31 public class WeakSetTest extends NbTestCase {
32
33     public WeakSetTest(java.lang.String JavaDoc testName) {
34         super(testName);
35     }
36
37     public static void main(java.lang.String JavaDoc[] args) {
38         junit.textui.TestRunner.run(new NbTestSuite(WeakSetTest.class));
39     }
40
41     public void testToArrayMayContainNullsIssue42271 () {
42         class R implements Runnable JavaDoc {
43             Object JavaDoc[] arr;
44             Object JavaDoc last;
45             
46             public R () {
47                 int cnt = 10;
48                 arr = new Object JavaDoc[cnt];
49                 for (int i = 0; i < cnt; i++) {
50                     arr[i] = new Integer JavaDoc (i);
51                 }
52             }
53             
54             
55             public void run () {
56                 
57                 WeakReference JavaDoc r = new WeakReference JavaDoc (last);
58                 for (int i = 0; i < arr.length; i++) {
59                     arr[i] = null;
60                 }
61                 arr = null;
62                 last = null;
63                 assertGC ("Last item has to disappear", r);
64             }
65             
66             public void putToSet (NotifyWhenIteratedSet s) {
67                 for (int i = 0; i < arr.length; i++) {
68                     s.add (arr[i]);
69                 }
70                 assertEquals (arr.length, s.size ());
71                 Iterator JavaDoc it = s.superIterator ();
72                 Object JavaDoc prev = it.next ();
73                 while (it.hasNext ()) {
74                     prev = it.next ();
75                 }
76                 last = prev;
77             }
78         }
79         R r = new R ();
80         
81         
82         
83         NotifyWhenIteratedSet ws = new NotifyWhenIteratedSet (r, 1);
84         
85         r.putToSet (ws);
86         
87         Object JavaDoc[] arr = ws.toArray ();
88         for (int i = 0; i < arr.length; i++) {
89             assertNotNull (i + "th index should not be null", arr[i]);
90         }
91     }
92     
93     private static final class NotifyWhenIteratedSet extends WeakSet {
94         private Runnable JavaDoc run;
95         private int cnt;
96         
97         public NotifyWhenIteratedSet (Runnable JavaDoc run, int cnt) {
98             this.run = run;
99             this.cnt = cnt;
100         }
101         
102         public Iterator JavaDoc superIterator () {
103             return super.iterator ();
104         }
105         
106         public Iterator JavaDoc iterator () {
107             final Iterator JavaDoc it = super.iterator ();
108             class I implements Iterator JavaDoc {
109                 public boolean hasNext() {
110                     return it.hasNext ();
111                 }
112
113                 public Object JavaDoc next() {
114                     if (--cnt == 0) {
115                         run.run ();
116                     }
117                     return it.next ();
118                 }
119
120                 public void remove() {
121                     it.remove();
122                 }
123             }
124             return new I ();
125         }
126     }
127 }
128
Popular Tags