ReferenceQueueTest.java revision 2ad60cfc28e14ee8f0bb038720836a4696c478ad
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 java.lang.ref.Reference;
21import java.lang.ref.ReferenceQueue;
22import java.lang.ref.SoftReference;
23
24public class ReferenceQueueTest extends junit.framework.TestCase {
25    static Boolean b;
26
27    static Integer integer;
28
29    protected void doneSuite() {
30        b = null;
31        integer = null;
32    }
33
34    public class ChildThread implements Runnable {
35        public ChildThread() {
36        }
37
38        public void run() {
39            try {
40                rq.wait(1000);
41            } catch (Exception e) {
42            }
43            synchronized (rq) {
44                // store in a static so it won't be gc'ed because the jit
45                // optimized it out
46                integer = new Integer(667);
47                SoftReference sr = new SoftReference(integer, rq);
48                sr.enqueue();
49                rq.notify();
50            }
51        }
52    }
53
54    ReferenceQueue rq;
55
56    /**
57     * @tests java.lang.ref.ReferenceQueue#poll()
58     */
59    public void test_poll() {
60        // store in a static so it won't be gc'ed because the jit
61        // optimized it out
62        b = new Boolean(true);
63        SoftReference sr = new SoftReference(b, rq);
64        sr.enqueue();
65        try {
66            assertTrue("Remove failed.", ((Boolean) rq.poll().get())
67                    .booleanValue());
68        } catch (Exception e) {
69            fail("Exception during the test : " + e.getMessage());
70        }
71    }
72
73    /**
74     * @tests java.lang.ref.ReferenceQueue#remove()
75     */
76    public void test_remove() {
77        // store in a static so it won't be gc'ed because the jit
78        // optimized it out
79        b = new Boolean(true);
80        SoftReference sr = new SoftReference(b, rq);
81        sr.enqueue();
82        try {
83            assertTrue("Remove failed.", ((Boolean) rq.remove().get())
84                    .booleanValue());
85        } catch (Exception e) {
86            fail("Exception during the test : " + e.getMessage());
87        }
88    }
89
90    /**
91     * @tests java.lang.ref.ReferenceQueue#remove(long)
92     */
93    public void test_removeJ() {
94        try {
95            assertNull("Queue should be empty. (poll)", rq.poll());
96            assertNull("Queue should be empty. (remove(1))",
97                    rq.remove((long) 1));
98            Thread ct = new Thread(new ChildThread());
99            ct.start();
100            Reference ret = rq.remove(0L);
101            assertNotNull("Delayed remove failed.", ret);
102        } catch (InterruptedException e) {
103            fail("InterruptedExeException during test : " + e.getMessage());
104        }
105        catch (Exception e) {
106            fail("Exception during test : " + e.getMessage());
107        }
108    }
109
110    /**
111     * @tests java.lang.ref.ReferenceQueue#ReferenceQueue()
112     */
113    public void test_Constructor() {
114        assertTrue("Used for testing.", true);
115    }
116
117    protected void setUp() {
118        rq = new ReferenceQueue();
119    }
120
121    protected void tearDown() {
122    }
123}
124