PhantomReferenceTest.java revision 561ee011997c6c2f1befbfaa9d5f0a99771c1d63
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.PhantomReference;
21import java.lang.ref.ReferenceQueue;
22
23public class PhantomReferenceTest extends junit.framework.TestCase {
24	static Boolean bool;
25
26	protected void doneSuite() {
27		bool = null;
28	}
29
30	/**
31	 * @tests java.lang.ref.PhantomReference#get()
32	 */
33	public void test_get() {
34		ReferenceQueue rq = new ReferenceQueue();
35		bool = new Boolean(false);
36		PhantomReference pr = new PhantomReference(bool, rq);
37		assertNull("Same object returned.", pr.get());
38	}
39
40	/**
41	 * @tests java.lang.ref.PhantomReference#PhantomReference(java.lang.Object,
42	 *        java.lang.ref.ReferenceQueue)
43	 */
44	public void test_ConstructorLjava_lang_ObjectLjava_lang_ref_ReferenceQueue() throws Exception {
45		ReferenceQueue rq = new ReferenceQueue();
46		bool = new Boolean(true);
47                PhantomReference pr = new PhantomReference(bool, rq);
48                // Allow the finalizer to run to potentially enqueue
49                Thread.sleep(1000);
50                assertTrue("Initialization failed.", !pr.isEnqueued());
51
52                // need a reference to bool so the jit does not optimize it away
53		assertTrue("should always pass", bool.booleanValue());
54
55		boolean exception = false;
56		try {
57			new PhantomReference(bool, null);
58		} catch (NullPointerException e) {
59			exception = true;
60		}
61		assertTrue("Should not throw NullPointerException", !exception);
62	}
63
64	protected void setUp() {
65	}
66
67	protected void tearDown() {
68	}
69}
70