WeakReferenceTest.java revision 89c1feb0a69a7707b271086e749975b3f7acacf7
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 tests.api.java.lang.ref; 19 20import dalvik.annotation.TestInfo; 21import dalvik.annotation.TestLevel; 22import dalvik.annotation.TestTarget; 23import dalvik.annotation.TestTargetClass; 24 25import java.lang.ref.ReferenceQueue; 26import java.lang.ref.WeakReference; 27 28@TestTargetClass(WeakReference.class) 29public class WeakReferenceTest extends junit.framework.TestCase { 30 static Boolean bool; 31 32 protected void doneSuite() { 33 bool = null; 34 } 35 36 /** 37 * @tests java.lang.ref.WeakReference#WeakReference(java.lang.Object, 38 * java.lang.ref.ReferenceQueue) 39 */ 40 @TestInfo( 41 level = TestLevel.COMPLETE, 42 purpose = "", 43 targets = { 44 @TestTarget( 45 methodName = "WeakReference", 46 methodArgs = {Object.class, java.lang.ref.ReferenceQueue.class} 47 ) 48 }) 49 public void test_ConstructorLjava_lang_ObjectLjava_lang_ref_ReferenceQueue() { 50 ReferenceQueue rq = new ReferenceQueue(); 51 bool = new Boolean(true); 52 try { 53 // Allow the finalizer to run to potentially enqueue 54 WeakReference wr = new WeakReference(bool, rq); 55 assertTrue("Initialization failed.", ((Boolean) wr.get()) 56 .booleanValue()); 57 } catch (Exception e) { 58 fail("Exception during test : " + e.getMessage()); 59 } 60 // need a reference to bool so the jit does not optimize it away 61 assertTrue("should always pass", bool.booleanValue()); 62 63 boolean exception = false; 64 try { 65 new WeakReference(bool, null); 66 } catch (NullPointerException e) { 67 exception = true; 68 } 69 assertTrue("Should not throw NullPointerException", !exception); 70 } 71 72 /** 73 * @tests java.lang.ref.WeakReference#WeakReference(java.lang.Object) 74 */ 75 @TestInfo( 76 level = TestLevel.COMPLETE, 77 purpose = "", 78 targets = { 79 @TestTarget( 80 methodName = "WeakReference", 81 methodArgs = {Object.class} 82 ) 83 }) 84 public void test_ConstructorLjava_lang_Object() { 85 bool = new Boolean(true); 86 try { 87 WeakReference wr = new WeakReference(bool); 88 // Allow the finalizer to run to potentially enqueue 89 Thread.sleep(1000); 90 assertTrue("Initialization failed.", ((Boolean) wr.get()) 91 .booleanValue()); 92 } catch (Exception e) { 93 fail("Exception during test : " + e.getMessage()); 94 } 95 // need a reference to bool so the jit does not optimize it away 96 assertTrue("should always pass", bool.booleanValue()); 97 } 98 99 protected void setUp() { 100 } 101 102 protected void tearDown() { 103 } 104} 105