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