1 /*
   2  * Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package test.com.sun.javafx.util;
  27 
  28 import com.sun.javafx.util.WeakReferenceQueue;
  29 import com.sun.javafx.util.WeakReferenceQueueShim;
  30 import static org.junit.Assert.assertEquals;
  31 import static org.junit.Assert.assertFalse;
  32 import static org.junit.Assert.assertTrue;
  33 
  34 import java.util.Iterator;
  35 
  36 import org.junit.Test;
  37 
  38 public class WeakReferenceQueueTest {
  39     @Test
  40     public void testAdd() {
  41         WeakReferenceQueue q = new WeakReferenceQueue();
  42         String s = new String("Wow!");
  43         q.add(s);
  44         assertEquals(1, WeakReferenceQueueShim.size(q));
  45     }
  46     
  47     @Test
  48     public void testRemove() {
  49         WeakReferenceQueue q = new WeakReferenceQueue();
  50         String a = new String("a");
  51         q.add(a);
  52         String b = new String("b");
  53         q.add(b);
  54         String c = new String("c");
  55         q.add(c);
  56         
  57         assertEquals(3, WeakReferenceQueueShim.size(q));
  58         q.remove(a);
  59         q.remove(c);
  60         assertEquals(1, WeakReferenceQueueShim.size(q));
  61     }
  62     
  63     @Test
  64     public void testCleanup() {
  65         WeakReferenceQueue q = new WeakReferenceQueue();
  66         String a = new String("a");
  67         q.add(a);
  68         String b = new String("b");
  69         q.add(b);
  70         String c = new String("c");
  71         q.add(c);
  72         
  73         assertEquals(3, WeakReferenceQueueShim.size(q));
  74         a = null;
  75         c = null;
  76         tryGCReallyHard();
  77         q.cleanup();
  78         assertEquals(1, WeakReferenceQueueShim.size(q));
  79     }
  80     
  81     @Test
  82     public void testIterator() {
  83         WeakReferenceQueue q = new WeakReferenceQueue();
  84         String a = new String("a");
  85         q.add(a);
  86         String b = new String("b");
  87         q.add(b);
  88         String c = new String("c");
  89         q.add(c);
  90         
  91         // This part of the test requires knowledge that iteration
  92         // is from last to first
  93         Iterator itr = q.iterator();
  94         assertTrue(itr.hasNext());
  95         assertEquals(c, itr.next());
  96         assertTrue(itr.hasNext());
  97         assertEquals(b, itr.next());
  98         assertTrue(itr.hasNext());
  99         assertEquals(a, itr.next());
 100         assertFalse(itr.hasNext());
 101         
 102         // and for good measure do it again without calling hasNext just
 103         // to make sure calling hasNext isn't a requirement
 104         itr = q.iterator();
 105         assertEquals(c, itr.next());
 106         assertEquals(b, itr.next());
 107         assertEquals(a, itr.next());
 108     }
 109     
 110     @Test
 111     public void testEmptyIterator() {
 112         WeakReferenceQueue q = new WeakReferenceQueue();
 113         Iterator itr = q.iterator();
 114         assertFalse(itr.hasNext());
 115     }
 116     
 117     @Test
 118     public void testIteratorRemove() {
 119         WeakReferenceQueue q = new WeakReferenceQueue();
 120         String a = new String("a");
 121         q.add(a);
 122         String b = new String("b");
 123         q.add(b);
 124         String c = new String("c");
 125         q.add(c);
 126         
 127         Iterator itr = q.iterator();
 128         itr.next(); // gives me "c"
 129         itr.remove();
 130         assertEquals(2, WeakReferenceQueueShim.size(q));
 131         itr.next(); // gives me "b"
 132         itr.remove();
 133         assertEquals(1, WeakReferenceQueueShim.size(q));
 134         itr.next(); // gives me "a"
 135         itr.remove();
 136         assertEquals(0, WeakReferenceQueueShim.size(q));
 137         
 138         q.add(a);
 139         q.add(b);
 140         q.add(c);
 141         itr = q.iterator();
 142         itr.next();
 143         itr.next(); // gives me "b"
 144         itr.remove();
 145         
 146         itr = q.iterator();
 147         assertEquals(c, itr.next());
 148         assertEquals(a, itr.next());
 149     }
 150     
 151     @Test
 152     public void testIteratingOverSparseQueue() {
 153         WeakReferenceQueue q = new WeakReferenceQueue();
 154         String a = new String("a");
 155         q.add(a);
 156         String b = new String("b");
 157         q.add(b);
 158         String c = new String("c");
 159         q.add(c);
 160         
 161         assertEquals(3, WeakReferenceQueueShim.size(q));
 162         a = null;
 163         c = null;
 164         tryGCReallyHard();
 165         q.cleanup();
 166 
 167         Iterator itr = q.iterator();
 168         assertEquals(b, itr.next());
 169         assertFalse(itr.hasNext());
 170     }
 171 
 172     @Test
 173     public void testIteratingOverSparseQueueWithoutCleanup() {
 174         WeakReferenceQueue q = new WeakReferenceQueue();
 175         String a = new String("a");
 176         q.add(a);
 177         String b = new String("b");
 178         q.add(b);
 179         String c = new String("c");
 180         q.add(c);
 181 
 182         assertEquals(3, WeakReferenceQueueShim.size(q));
 183         a = null;
 184         c = null;
 185         tryGCReallyHard();
 186 
 187         Iterator itr = q.iterator();
 188         assertEquals(b, itr.next());
 189         assertFalse(itr.hasNext());
 190     }
 191 
 192     private void tryGCReallyHard() {
 193         // produce some garbage to increase the need of performing gc
 194         for (int i = 0; i < 100000; i++) {
 195             String s = new String("GARBAGE");
 196         }
 197 
 198         // now, yell at the VM to run gc
 199         for (int i = 0; i < 10; i++) {
 200             System.gc();
 201             System.gc();
 202             System.gc();
 203         }
 204 
 205         // finally, give the VM some idle time to perform gc
 206         try { Thread.sleep(100); } catch (InterruptedException e) {}
 207 
 208         // hope that worked!
 209     }
 210 }