1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18package org.apache.harmony.tests.java.lang.ref; 19 20import java.lang.ref.ReferenceQueue; 21import java.lang.ref.WeakReference; 22 23public class WeakReferenceTest extends junit.framework.TestCase { 24 static Boolean bool; 25 26 protected void doneSuite() { 27 bool = null; 28 } 29 30 /** 31 * java.lang.ref.WeakReference#WeakReference(java.lang.Object, 32 * java.lang.ref.ReferenceQueue) 33 */ 34 public void test_ConstructorLjava_lang_ObjectLjava_lang_ref_ReferenceQueue() { 35 ReferenceQueue rq = new ReferenceQueue(); 36 bool = new Boolean(true); 37 try { 38 // Allow the finalizer to run to potentially enqueue 39 WeakReference wr = new WeakReference(bool, rq); 40 assertTrue("Initialization failed.", ((Boolean) wr.get()) 41 .booleanValue()); 42 } catch (Exception e) { 43 fail("Exception during test : " + e.getMessage()); 44 } 45 // need a reference to bool so the jit does not optimize it away 46 assertTrue("should always pass", bool.booleanValue()); 47 48 boolean exception = false; 49 try { 50 new WeakReference(bool, null); 51 } catch (NullPointerException e) { 52 exception = true; 53 } 54 assertTrue("Should not throw NullPointerException", !exception); 55 } 56 57 /** 58 * java.lang.ref.WeakReference#WeakReference(java.lang.Object) 59 */ 60 public void test_ConstructorLjava_lang_Object() { 61 bool = new Boolean(true); 62 try { 63 WeakReference wr = new WeakReference(bool); 64 // Allow the finalizer to run to potentially enqueue 65 Thread.sleep(1000); 66 assertTrue("Initialization failed.", ((Boolean) wr.get()) 67 .booleanValue()); 68 } catch (Exception e) { 69 fail("Exception during test : " + e.getMessage()); 70 } 71 // need a reference to bool so the jit does not optimize it away 72 assertTrue("should always pass", bool.booleanValue()); 73 } 74 75 protected void setUp() { 76 } 77 78 protected void tearDown() { 79 } 80} 81