1 /* 2 * Copyright (C) 2005 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.common.base; 18 19 import com.google.common.base.internal.Finalizer; 20 import com.google.common.testing.GcFinalization; 21 import java.lang.ref.ReferenceQueue; 22 import java.lang.ref.WeakReference; 23 import java.net.URL; 24 import java.net.URLClassLoader; 25 import java.util.Arrays; 26 import java.util.Collections; 27 import junit.framework.TestCase; 28 29 /** 30 * Unit test for {@link FinalizableReferenceQueue}. 31 * 32 * @author Bob Lee 33 */ 34 public class FinalizableReferenceQueueTest extends TestCase { 35 36 private FinalizableReferenceQueue frq; 37 38 @Override tearDown()39 protected void tearDown() throws Exception { 40 frq = null; 41 } 42 43 testFinalizeReferentCalled()44 public void testFinalizeReferentCalled() { 45 final MockReference reference = new MockReference(frq = new FinalizableReferenceQueue()); 46 47 GcFinalization.awaitDone( 48 new GcFinalization.FinalizationPredicate() { 49 public boolean isDone() { 50 return reference.finalizeReferentCalled; 51 } 52 }); 53 } 54 55 static class MockReference extends FinalizableWeakReference<Object> { 56 57 volatile boolean finalizeReferentCalled; 58 MockReference(FinalizableReferenceQueue frq)59 MockReference(FinalizableReferenceQueue frq) { 60 super(new Object(), frq); 61 } 62 63 @Override finalizeReferent()64 public void finalizeReferent() { 65 finalizeReferentCalled = true; 66 } 67 } 68 69 /** 70 * Keeps a weak reference to the underlying reference queue. When this reference is cleared, we 71 * know that the background thread has stopped and released its strong reference. 72 */ 73 private WeakReference<ReferenceQueue<Object>> queueReference; 74 75 testThatFinalizerStops()76 public void testThatFinalizerStops() { 77 weaklyReferenceQueue(); 78 GcFinalization.awaitClear(queueReference); 79 } 80 81 /** If we don't keep a strong reference to the reference object, it won't be enqueued. */ 82 FinalizableWeakReference<Object> reference; 83 84 /** Create the FRQ in a method that goes out of scope so that we're sure it will be reclaimed. */ weaklyReferenceQueue()85 private void weaklyReferenceQueue() { 86 frq = new FinalizableReferenceQueue(); 87 queueReference = new WeakReference<>(frq.queue); 88 89 /* 90 * Queue and clear a reference for good measure. We test later on that 91 * the finalizer thread stopped, but we should test that it actually 92 * started first. 93 */ 94 reference = 95 new FinalizableWeakReference<Object>(new Object(), frq) { 96 @Override 97 public void finalizeReferent() { 98 reference = null; 99 frq = null; 100 } 101 }; 102 } 103 104 @AndroidIncompatible // no concept of separate ClassLoaders testDecoupledLoader()105 public void testDecoupledLoader() { 106 FinalizableReferenceQueue.DecoupledLoader decoupledLoader = 107 new FinalizableReferenceQueue.DecoupledLoader() { 108 @Override 109 URLClassLoader newLoader(URL base) { 110 return new DecoupledClassLoader(new URL[] {base}); 111 } 112 }; 113 114 Class<?> finalizerCopy = decoupledLoader.loadFinalizer(); 115 116 assertNotNull(finalizerCopy); 117 assertNotSame(Finalizer.class, finalizerCopy); 118 119 assertNotNull(FinalizableReferenceQueue.getStartFinalizer(finalizerCopy)); 120 } 121 122 static class DecoupledClassLoader extends URLClassLoader { 123 DecoupledClassLoader(URL[] urls)124 public DecoupledClassLoader(URL[] urls) { 125 super(urls); 126 } 127 128 @Override loadClass(String name, boolean resolve)129 protected synchronized Class<?> loadClass(String name, boolean resolve) 130 throws ClassNotFoundException { 131 // Force Finalizer to load from this class loader, not its parent. 132 if (name.equals(Finalizer.class.getName())) { 133 Class<?> clazz = findClass(name); 134 if (resolve) { 135 resolveClass(clazz); 136 } 137 return clazz; 138 } 139 140 return super.loadClass(name, resolve); 141 } 142 } 143 144 @AndroidIncompatible // TODO(cpovirk): How significant is this failure? testGetFinalizerUrl()145 public void testGetFinalizerUrl() { 146 assertNotNull(getClass().getResource("internal/Finalizer.class")); 147 } 148 testFinalizeClassHasNoNestedClasses()149 public void testFinalizeClassHasNoNestedClasses() throws Exception { 150 // Ensure that the Finalizer class has no nested classes. 151 // See https://code.google.com/p/guava-libraries/issues/detail?id=1505 152 assertEquals(Collections.emptyList(), Arrays.asList(Finalizer.class.getDeclaredClasses())); 153 } 154 } 155