001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.openide.util;
043:
044: import java.beans.PropertyChangeListener;
045: import java.lang.ref.WeakReference;
046: import java.lang.reflect.Method;
047: import java.util.Arrays;
048: import java.util.Iterator;
049: import java.util.Vector;
050: import junit.framework.*;
051: import org.netbeans.junit.*;
052:
053: public class WeakSetTest extends NbTestCase {
054:
055: public WeakSetTest(java.lang.String testName) {
056: super (testName);
057: }
058:
059: public static void main(java.lang.String[] args) {
060: junit.textui.TestRunner.run(new NbTestSuite(WeakSetTest.class));
061: }
062:
063: public void testToArrayMayContainNullsIssue42271() {
064: class R implements Runnable {
065: Object[] arr;
066: Object last;
067:
068: public R() {
069: int cnt = 10;
070: arr = new Object[cnt];
071: for (int i = 0; i < cnt; i++) {
072: arr[i] = new Integer(i);
073: }
074: }
075:
076: public void run() {
077:
078: WeakReference r = new WeakReference(last);
079: for (int i = 0; i < arr.length; i++) {
080: arr[i] = null;
081: }
082: arr = null;
083: last = null;
084: assertGC("Last item has to disappear", r);
085: }
086:
087: public void putToSet(NotifyWhenIteratedSet s) {
088: for (int i = 0; i < arr.length; i++) {
089: s.add(arr[i]);
090: }
091: assertEquals(arr.length, s.size());
092: Iterator it = s.super Iterator();
093: Object prev = it.next();
094: while (it.hasNext()) {
095: prev = it.next();
096: }
097: last = prev;
098: }
099: }
100: R r = new R();
101:
102: NotifyWhenIteratedSet ws = new NotifyWhenIteratedSet(r, 1);
103:
104: r.putToSet(ws);
105:
106: Object[] arr = ws.toArray();
107: for (int i = 0; i < arr.length; i++) {
108: assertNotNull(i + "th index should not be null", arr[i]);
109: }
110: }
111:
112: private static final class NotifyWhenIteratedSet extends WeakSet {
113: private Runnable run;
114: private int cnt;
115:
116: public NotifyWhenIteratedSet(Runnable run, int cnt) {
117: this .run = run;
118: this .cnt = cnt;
119: }
120:
121: public Iterator super Iterator() {
122: return super .iterator();
123: }
124:
125: public Iterator iterator() {
126: final Iterator it = super .iterator();
127: class I implements Iterator {
128: public boolean hasNext() {
129: return it.hasNext();
130: }
131:
132: public Object next() {
133: if (--cnt == 0) {
134: run.run();
135: }
136: return it.next();
137: }
138:
139: public void remove() {
140: it.remove();
141: }
142: }
143: return new I();
144: }
145: }
146: }
|