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 org.apache.harmony.luni.tests.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                assertTrue("Remove failed.", ((Boolean) rq.poll().get())
66                                .booleanValue());
67	}
68
69	/**
70	 * @tests java.lang.ref.ReferenceQueue#remove()
71	 */
72	public void test_remove() throws Exception {
73		// store in a static so it won't be gc'ed because the jit
74		// optimized it out
75		b = new Boolean(true);
76		SoftReference sr = new SoftReference(b, rq);
77		sr.enqueue();
78                assertTrue("Remove failed.", ((Boolean) rq.remove().get())
79                                .booleanValue());
80	}
81
82	/**
83	 * @tests java.lang.ref.ReferenceQueue#remove(long)
84	 */
85	public void test_removeJ() throws Exception {
86                assertNull("Queue is empty.", rq.poll());
87                assertNull("Queue is empty.", rq.remove((long) 1));
88                Thread ct = new Thread(new ChildThread());
89                ct.start();
90                Reference ret = rq.remove(0L);
91                assertNotNull("Delayed remove failed.", ret);
92	}
93
94	/**
95	 * @tests java.lang.ref.ReferenceQueue#ReferenceQueue()
96	 */
97	public void test_Constructor() {
98		assertTrue("Used for testing.", true);
99	}
100
101	protected void setUp() {
102		rq = new ReferenceQueue();
103	}
104
105	protected void tearDown() {
106	}
107}
108