Home | History | Annotate | Download | only in ref
      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 package org.apache.harmony.tests.java.lang.ref;
     18 
     19 import java.lang.ref.Reference;
     20 import java.lang.ref.ReferenceQueue;
     21 import java.lang.ref.SoftReference;
     22 import java.util.Vector;
     23 import libcore.java.lang.ref.FinalizationTester;
     24 
     25 public class SoftReferenceTest extends junit.framework.TestCase {
     26     static Boolean bool;
     27     SoftReference r;
     28 
     29     protected void doneSuite() {
     30         bool = null;
     31     }
     32 
     33     /**
     34      * java.lang.ref.SoftReference#SoftReference(java.lang.Object,
     35      *        java.lang.ref.ReferenceQueue)
     36      */
     37     public void test_ConstructorLjava_lang_ObjectLjava_lang_ref_ReferenceQueue() {
     38         ReferenceQueue rq = new ReferenceQueue();
     39         bool = new Boolean(true);
     40         try {
     41             SoftReference sr = new SoftReference(bool, rq);
     42             assertTrue("Initialization failed.", ((Boolean) sr.get())
     43                     .booleanValue());
     44         } catch (Exception e) {
     45             fail("Exception during test : " + e.getMessage());
     46         }
     47 
     48         boolean exception = false;
     49         try {
     50             new SoftReference(bool, null);
     51         } catch (NullPointerException e) {
     52             exception = true;
     53         }
     54         assertTrue("Should not throw NullPointerException", !exception);
     55     }
     56 
     57     /**
     58      * java.lang.ref.SoftReference#SoftReference(java.lang.Object)
     59      */
     60     public void test_ConstructorLjava_lang_Object() {
     61         bool = new Boolean(true);
     62         try {
     63             SoftReference sr = new SoftReference(bool);
     64             assertTrue("Initialization failed.", ((Boolean) sr.get())
     65                     .booleanValue());
     66         } catch (Exception e) {
     67             fail("Exception during test : " + e.getMessage());
     68         }
     69     }
     70 
     71     /**
     72      * java.lang.ref.SoftReference#get()
     73      */
     74     public void test_get() {
     75         bool = new Boolean(false);
     76         SoftReference sr = new SoftReference(bool);
     77         assertTrue("Same object not returned.", bool == sr.get());
     78     }
     79 
     80     // SideEffect: Causes OutOfMemoryError to test finalization
     81     public void test_get_SoftReference() {
     82 
     83         class TestObject {
     84             public boolean finalized;
     85                 public TestObject() {
     86                     finalized = false;
     87                 }
     88 
     89                 protected void finalize() {
     90                     finalized = true;
     91                 }
     92         }
     93 
     94         final ReferenceQueue rq = new ReferenceQueue();
     95 
     96         class TestThread extends Thread {
     97             public void run() {
     98                 Object testObj = new TestObject();
     99                 r = new SoftReference(testObj, rq);
    100             }
    101         }
    102         Reference ref;
    103         try {
    104             TestThread t = new TestThread();
    105             t.start();
    106             t.join();
    107             Vector<StringBuffer> v = new Vector<StringBuffer>();
    108             try {
    109                 while(true) {
    110                     v.add(new StringBuffer(10000));
    111                 }
    112             } catch(OutOfMemoryError ofme) {
    113                 v = null;
    114             }
    115         } catch (InterruptedException e) {
    116             fail("InterruptedException : " + e.getMessage());
    117         }
    118 
    119         assertNull("get() should return null " +
    120                 "if OutOfMemoryError is thrown.", r.get());
    121 
    122         try {
    123             TestThread t = new TestThread();
    124             t.start();
    125             t.join();
    126             FinalizationTester.induceFinalization();
    127             ref = rq.poll();
    128             assertNotNull("Object not garbage collected.", ref);
    129             assertNull("Object is not null.", ref.get());
    130             assertNotNull("Object could not be reclaimed.", r.get());
    131         } catch (Exception e) {
    132             fail("Exception : " + e.getMessage());
    133         }
    134     }
    135 
    136     protected void setUp() {
    137     }
    138 
    139     protected void tearDown() {
    140     }
    141 }
    142