/* * JBoss, Home of Professional Open Source. * Copyright 2014 Red Hat, Inc., and individual contributors * as indicated by the @author tags. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.jboss.modules.ref; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; import java.lang.ref.ReferenceQueue; import java.util.ArrayList; import java.util.Collection; import org.jboss.modules.ref.Reference.Type; import org.jboss.modules.ref.util.TestReaper; import org.junit.Test; /** * Test for PhantomReference. * * @author Flavia Rainone * @see PhantomReference */ public class PhantomReferenceTestCase extends AbstractReapableReferenceTest { @Test public void plainPhantomReference() { final Reference reference = new PhantomReference("referent", "attachment", (ReferenceQueue) null); assertReference(reference, null, "attachment", null); } @Test public void nullPhantomReference() throws Exception { final Reference reference = new PhantomReference(null, Integer.valueOf(0), new ReferenceQueue()); assertReference(reference, null, Integer.valueOf(0), null); } @Test public void phantomReferenceWithReferenceQueue() throws Exception { final ReferenceQueue> referenceQueue = new ReferenceQueue>(); Collection collection = new ArrayList(); final Reference, String> reference = new PhantomReference, String>(collection, "collection", referenceQueue); assertReference(reference, null, "collection", null); collection = null; System.gc(); assertSame(reference, referenceQueue.remove(300)); } @Test public void phantomReferenceWithReaper() throws Exception { Thing thing = new Thing(); final TestReaper reaper = new TestReaper(); final Reference reference = new PhantomReference(thing, null, reaper); assertReference(reference, null, null, reaper); thing = null; System.gc(); assertSame(reference, reaper.getReapedReference()); } @Override @Test public void clearReference() { final Reference reference = createReference(Boolean.TRUE, "attachment for true"); assertNull(reference.get()); assertEquals("attachment for true", reference.getAttachment()); reference.clear(); assertNull(reference.get()); assertEquals("attachment for true", reference.getAttachment()); } @Override Reference createReference(T value, A attachment) { return new PhantomReference(value, attachment, new TestReaper()); } @Override Type getTestedReferenceType() { return Type.PHANTOM; } }