SoftReferenceTest.java revision cc05ad238516f1303687aba4a978e24e57c0c07a
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 */ 17package tests.api.java.lang.ref; 18 19import dalvik.annotation.TestTargets; 20import dalvik.annotation.TestLevel; 21import dalvik.annotation.TestTargetNew; 22import dalvik.annotation.TestTargetClass; 23 24import java.lang.ref.Reference; 25import java.lang.ref.ReferenceQueue; 26import java.lang.ref.SoftReference; 27import java.util.Vector; 28 29@TestTargetClass(SoftReference.class) 30public class SoftReferenceTest extends junit.framework.TestCase { 31 static Boolean bool; 32 SoftReference r; 33 34 protected void doneSuite() { 35 bool = null; 36 } 37 38 /** 39 * @tests java.lang.ref.SoftReference#SoftReference(java.lang.Object, 40 * java.lang.ref.ReferenceQueue) 41 */ 42 @TestTargetNew( 43 level = TestLevel.COMPLETE, 44 notes = "", 45 method = "SoftReference", 46 args = {java.lang.Object.class, java.lang.ref.ReferenceQueue.class} 47 ) 48 public void test_ConstructorLjava_lang_ObjectLjava_lang_ref_ReferenceQueue() { 49 ReferenceQueue rq = new ReferenceQueue(); 50 bool = new Boolean(true); 51 try { 52 SoftReference sr = new SoftReference(bool, rq); 53 assertTrue("Initialization failed.", ((Boolean) sr.get()) 54 .booleanValue()); 55 } catch (Exception e) { 56 fail("Exception during test : " + e.getMessage()); 57 } 58 59 boolean exception = false; 60 try { 61 new SoftReference(bool, null); 62 } catch (NullPointerException e) { 63 exception = true; 64 } 65 assertTrue("Should not throw NullPointerException", !exception); 66 } 67 68 /** 69 * @tests java.lang.ref.SoftReference#SoftReference(java.lang.Object) 70 */ 71 @TestTargetNew( 72 level = TestLevel.COMPLETE, 73 notes = "", 74 method = "SoftReference", 75 args = {java.lang.Object.class} 76 ) 77 public void test_ConstructorLjava_lang_Object() { 78 bool = new Boolean(true); 79 try { 80 SoftReference sr = new SoftReference(bool); 81 assertTrue("Initialization failed.", ((Boolean) sr.get()) 82 .booleanValue()); 83 } catch (Exception e) { 84 fail("Exception during test : " + e.getMessage()); 85 } 86 } 87 88 /** 89 * @tests java.lang.ref.SoftReference#get() 90 */ 91 @TestTargetNew( 92 level = TestLevel.PARTIAL, 93 notes = "Doesn't verified that get() can return null.", 94 method = "get", 95 args = {} 96 ) 97 public void test_get() { 98 bool = new Boolean(false); 99 SoftReference sr = new SoftReference(bool); 100 assertTrue("Same object not returned.", bool == sr.get()); 101 } 102 103 @TestTargetNew( 104 level = TestLevel.COMPLETE, 105 notes = "", 106 method = "get", 107 args = {} 108 ) 109 public void test_get_SoftReference() { 110 111 class TestObject { 112 public boolean finalized; 113 public TestObject() { 114 finalized = false; 115 } 116 117 protected void finalize() { 118 finalized = true; 119 } 120 } 121 122 final ReferenceQueue rq = new ReferenceQueue(); 123 124 class TestThread extends Thread { 125 public void run() { 126 Object testObj = new TestObject(); 127 r = new SoftReference(testObj, rq); 128 } 129 } 130 Reference ref; 131 try { 132 TestThread t = new TestThread(); 133 t.start(); 134 t.join(); 135 Vector<StringBuffer> v = new Vector<StringBuffer>(); 136 try { 137 while(true) { 138 v.add(new StringBuffer(10000)); 139 } 140 } catch(OutOfMemoryError ofme) { 141 v = null; 142 } 143 } catch (InterruptedException e) { 144 fail("InterruptedException : " + e.getMessage()); 145 } 146 147 assertNull("get() should return null " + 148 "if OutOfMemoryError is thrown.", r.get()); 149 150 try { 151 TestThread t = new TestThread(); 152 t.start(); 153 t.join(); 154 System.gc(); 155 System.runFinalization(); 156 ref = rq.poll(); 157 assertNotNull("Object not garbage collected.", ref); 158 assertNull("Object is not null.", ref.get()); 159 assertNotNull("Object could not be reclaimed.", r.get()); 160 } catch (Exception e) { 161 fail("Exception : " + e.getMessage()); 162 } 163 } 164 165 protected void setUp() { 166 } 167 168 protected void tearDown() { 169 } 170} 171