modules/graphics/src/test/java/test/com/sun/javafx/util/WeakReferenceQueueTest.java

Print this page
rev 9250 : 8134762: Refactor Javafx graphics module tests for clear separation of tests
Reviewed-by:


   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());


 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();


   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());


 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();