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