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 com.sun.javafx.util; 27 28 import static org.junit.Assert.assertEquals; 29 import static org.junit.Assert.assertFalse; 30 import static org.junit.Assert.assertTrue; 31 32 import java.util.Iterator; 33 34 import org.junit.Test; 35 36 public class WeakReferenceQueueTest { 37 @Test 38 public void testAdd() { 39 WeakReferenceQueue q = new WeakReferenceQueue(); 40 String s = new String("Wow!"); 41 q.add(s); 42 assertEquals(1, q.size); 43 } 44 45 @Test 46 public void testRemove() { 47 WeakReferenceQueue q = new WeakReferenceQueue(); 48 String a = new String("a"); 49 q.add(a); 50 String b = new String("b"); 51 q.add(b); 52 String c = new String("c"); 53 q.add(c); 54 55 assertEquals(3, q.size); 56 q.remove(a); 57 q.remove(c); 58 assertEquals(1, q.size); 59 } 60 61 @Test 62 public void testCleanup() { 63 WeakReferenceQueue q = new WeakReferenceQueue(); 64 String a = new String("a"); 65 q.add(a); 66 String b = new String("b"); 67 q.add(b); 68 String c = new String("c"); 69 q.add(c); 70 71 assertEquals(3, q.size); 72 a = null; 73 c = null; 74 tryGCReallyHard(); 75 q.cleanup(); 76 assertEquals(1, q.size); 77 } 78 79 @Test 80 public void testIterator() { 81 WeakReferenceQueue q = new WeakReferenceQueue(); 82 String a = new String("a"); 83 q.add(a); 84 String b = new String("b"); 85 q.add(b); 86 String c = new String("c"); 87 q.add(c); 88 89 // This part of the test requires knowledge that iteration 90 // is from last to first 91 Iterator itr = q.iterator(); 92 assertTrue(itr.hasNext()); 93 assertEquals(c, itr.next()); 94 assertTrue(itr.hasNext()); 95 assertEquals(b, itr.next()); 96 assertTrue(itr.hasNext()); 97 assertEquals(a, itr.next()); 98 assertFalse(itr.hasNext()); 99 100 // and for good measure do it again without calling hasNext just 101 // to make sure calling hasNext isn't a requirement 102 itr = q.iterator(); 103 assertEquals(c, itr.next()); 104 assertEquals(b, itr.next()); 105 assertEquals(a, itr.next()); 106 } 107 108 @Test 109 public void testEmptyIterator() { 110 WeakReferenceQueue q = new WeakReferenceQueue(); 111 Iterator itr = q.iterator(); 112 assertFalse(itr.hasNext()); 113 } 114 115 @Test 116 public void testIteratorRemove() { 117 WeakReferenceQueue q = new WeakReferenceQueue(); 118 String a = new String("a"); 119 q.add(a); 120 String b = new String("b"); 121 q.add(b); 122 String c = new String("c"); 123 q.add(c); 124 125 Iterator itr = q.iterator(); 126 itr.next(); // gives me "c" 127 itr.remove(); 128 assertEquals(2, q.size); 129 itr.next(); // gives me "b" 130 itr.remove(); 131 assertEquals(1, q.size); 132 itr.next(); // gives me "a" 133 itr.remove(); 134 assertEquals(0, q.size); 135 136 q.add(a); 137 q.add(b); 138 q.add(c); 139 itr = q.iterator(); 140 itr.next(); 141 itr.next(); // gives me "b" 142 itr.remove(); 143 144 itr = q.iterator(); 145 assertEquals(c, itr.next()); 146 assertEquals(a, itr.next()); 147 } 148 149 @Test 150 public void testIteratingOverSparseQueue() { 151 WeakReferenceQueue q = new WeakReferenceQueue(); 152 String a = new String("a"); 153 q.add(a); 154 String b = new String("b"); 155 q.add(b); 156 String c = new String("c"); 157 q.add(c); 158 159 assertEquals(3, q.size); 160 a = null; 161 c = null; 162 tryGCReallyHard(); 163 q.cleanup(); 164 165 Iterator itr = q.iterator(); 166 assertEquals(b, itr.next()); 167 assertFalse(itr.hasNext()); 168 } 169 170 @Test 171 public void testIteratingOverSparseQueueWithoutCleanup() { 172 WeakReferenceQueue q = new WeakReferenceQueue(); 173 String a = new String("a"); 174 q.add(a); 175 String b = new String("b"); 176 q.add(b); 177 String c = new String("c"); 178 q.add(c); 179 180 assertEquals(3, q.size); 181 a = null; 182 c = null; 183 tryGCReallyHard(); 184 185 Iterator itr = q.iterator(); 186 assertEquals(b, itr.next()); 187 assertFalse(itr.hasNext()); 188 } 189 190 private void tryGCReallyHard() { 191 // produce some garbage to increase the need of performing gc 192 for (int i = 0; i < 100000; i++) { 193 String s = new String("GARBAGE"); 194 } 195 196 // now, yell at the VM to run gc 197 for (int i = 0; i < 10; i++) { 198 System.gc(); 199 System.gc(); 200 System.gc(); 201 } 202 203 // finally, give the VM some idle time to perform gc 204 try { Thread.sleep(100); } catch (InterruptedException e) {} 205 206 // hope that worked! 207 } 208 }