Search in sources :

Example 21 with ReferenceQueue

use of java.lang.ref.ReferenceQueue in project robovm by robovm.

the class ReferenceTest method test_enqueue.

/**
     * java.lang.ref.Reference#enqueue()
     */
public void test_enqueue() {
    ReferenceQueue rq = new ReferenceQueue();
    obj = new Object();
    Reference ref = new SoftReference(obj, rq);
    assertTrue("Enqueue failed.", (!ref.isEnqueued()) && ((ref.enqueue()) && (ref.isEnqueued())));
    assertTrue("Not properly enqueued.", rq.poll().get() == obj);
    // This fails...
    assertTrue("Should remain enqueued.", !ref.isEnqueued());
    assertTrue("Can not enqueue twice.", (!ref.enqueue()) && (rq.poll() == null));
    rq = new ReferenceQueue();
    obj = new Object();
    ref = new WeakReference(obj, rq);
    assertTrue("Enqueue failed2.", (!ref.isEnqueued()) && ((ref.enqueue()) && (ref.isEnqueued())));
    assertTrue("Not properly enqueued2.", rq.poll().get() == obj);
    // This
    assertTrue("Should remain enqueued2.", !ref.isEnqueued());
    // fails.
    assertTrue("Can not enqueue twice2.", (!ref.enqueue()) && (rq.poll() == null));
    ref = new PhantomReference(obj, rq);
    assertTrue("Enqueue failed3.", (!ref.isEnqueued()) && ((ref.enqueue()) && (ref.isEnqueued())));
    assertNull("Not properly enqueued3.", rq.poll().get());
    // This
    assertTrue("Should remain enqueued3.", !ref.isEnqueued());
    // fails.
    assertTrue("Can not enqueue twice3.", (!ref.enqueue()) && (rq.poll() == null));
}
Also used : ReferenceQueue(java.lang.ref.ReferenceQueue) SoftReference(java.lang.ref.SoftReference) PhantomReference(java.lang.ref.PhantomReference) Reference(java.lang.ref.Reference) SoftReference(java.lang.ref.SoftReference) WeakReference(java.lang.ref.WeakReference) WeakReference(java.lang.ref.WeakReference) PhantomReference(java.lang.ref.PhantomReference)

Example 22 with ReferenceQueue

use of java.lang.ref.ReferenceQueue in project robovm by robovm.

the class ReferenceTest method test_clear.

/**
     * java.lang.ref.Reference#clear()
     */
public void test_clear() {
    tmpA = new Object();
    tmpB = new Object();
    tmpC = new Object();
    SoftReference sr = new SoftReference(tmpA, new ReferenceQueue());
    WeakReference wr = new WeakReference(tmpB, new ReferenceQueue());
    PhantomReference pr = new PhantomReference(tmpC, new ReferenceQueue());
    assertTrue("Start: Object not cleared.", (sr.get() != null) && (wr.get() != null));
    assertNull("Referent is not null.", pr.get());
    sr.clear();
    wr.clear();
    pr.clear();
    assertTrue("End: Object cleared.", (sr.get() == null) && (wr.get() == null));
    assertNull("Referent is not null.", pr.get());
    // Must reference tmpA and tmpB so the jit does not optimize them away
    assertTrue("should always pass", tmpA != sr.get() && tmpB != wr.get());
}
Also used : SoftReference(java.lang.ref.SoftReference) ReferenceQueue(java.lang.ref.ReferenceQueue) WeakReference(java.lang.ref.WeakReference) PhantomReference(java.lang.ref.PhantomReference)

Example 23 with ReferenceQueue

use of java.lang.ref.ReferenceQueue in project robovm by robovm.

the class ReferenceTest method test_subclass.

/**
     * Makes sure that overridden versions of clear() and enqueue()
     * get called, and that clear/enqueue/finalize happen in the
     * right order for WeakReferences.
     *
     * java.lang.ref.Reference#clear()
     * java.lang.ref.Reference#enqueue()
     * java.lang.Object#finalize()
     */
public void test_subclass() {
    error = null;
    testObjectFinalized = false;
    twr = null;
    class TestObject {

        public TestWeakReference testWeakReference = null;

        public void setTestWeakReference(TestWeakReference twr) {
            testWeakReference = twr;
        }

        protected void finalize() {
            testObjectFinalized = true;
        }
    }
    final ReferenceQueue rq = new ReferenceQueue();
    class TestThread extends Thread {

        public void run() {
            // Create the object in a separate thread to ensure it will be
            // gc'ed
            TestObject testObj = new TestObject();
            twr = new TestWeakReference(testObj, rq);
            testObj.setTestWeakReference(twr);
            testObj = null;
        }
    }
    Reference ref;
    try {
        Thread t = new TestThread();
        t.start();
        t.join();
        FinalizationTester.induceFinalization();
        // Give up after five seconds.
        ref = rq.remove(5000L);
        assertNotNull("Object not garbage collected.", ref);
        assertTrue("Unexpected reference.", ref == twr);
        assertNull("Object could not be reclaimed.", twr.get());
        //assertTrue("Overridden clear() should have been called.",
        //       twr.clearSeen);
        //assertTrue("Overridden enqueue() should have been called.",
        //        twr.enqueueSeen);
        assertTrue("finalize() should have been called.", testObjectFinalized);
    } catch (InterruptedException e) {
        fail("InterruptedException : " + e.getMessage());
    }
}
Also used : ReferenceQueue(java.lang.ref.ReferenceQueue) PhantomReference(java.lang.ref.PhantomReference) Reference(java.lang.ref.Reference) SoftReference(java.lang.ref.SoftReference) WeakReference(java.lang.ref.WeakReference)

Example 24 with ReferenceQueue

use of java.lang.ref.ReferenceQueue in project robovm by robovm.

the class ReferenceTest method test_get.

/**
     * java.lang.ref.Reference#get()
     */
public void test_get() {
    WeakReference ref = newWeakReference(null);
    FinalizationTester.induceFinalization();
    assertNull("get() doesn't return null after gc for WeakReference", ref.get());
    obj = new Object();
    ref = new WeakReference<Object>(obj, new ReferenceQueue<Object>());
    ref.clear();
    assertNull("get() doesn't return null after clear for WeakReference", ref.get());
}
Also used : ReferenceQueue(java.lang.ref.ReferenceQueue) WeakReference(java.lang.ref.WeakReference)

Example 25 with ReferenceQueue

use of java.lang.ref.ReferenceQueue in project robovm by robovm.

the class WeakReferenceTest method test_ConstructorLjava_lang_ObjectLjava_lang_ref_ReferenceQueue.

/**
     * java.lang.ref.WeakReference#WeakReference(java.lang.Object,
     *        java.lang.ref.ReferenceQueue)
     */
public void test_ConstructorLjava_lang_ObjectLjava_lang_ref_ReferenceQueue() {
    ReferenceQueue rq = new ReferenceQueue();
    bool = new Boolean(true);
    try {
        // Allow the finalizer to run to potentially enqueue
        WeakReference wr = new WeakReference(bool, rq);
        assertTrue("Initialization failed.", ((Boolean) wr.get()).booleanValue());
    } catch (Exception e) {
        fail("Exception during test : " + e.getMessage());
    }
    // need a reference to bool so the jit does not optimize it away
    assertTrue("should always pass", bool.booleanValue());
    boolean exception = false;
    try {
        new WeakReference(bool, null);
    } catch (NullPointerException e) {
        exception = true;
    }
    assertTrue("Should not throw NullPointerException", !exception);
}
Also used : ReferenceQueue(java.lang.ref.ReferenceQueue) WeakReference(java.lang.ref.WeakReference)

Aggregations

ReferenceQueue (java.lang.ref.ReferenceQueue)30 WeakReference (java.lang.ref.WeakReference)15 PhantomReference (java.lang.ref.PhantomReference)11 Reference (java.lang.ref.Reference)10 SoftReference (java.lang.ref.SoftReference)9 URLClassLoader (java.net.URLClassLoader)3 Test (org.junit.Test)3 ArrayList (java.util.ArrayList)2 ClassLoaders.inMemoryClassLoader (org.mockitoutil.ClassLoaders.inMemoryClassLoader)2 SideEffect (dalvik.annotation.SideEffect)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Field (java.lang.reflect.Field)1 SocketPermission (java.net.SocketPermission)1 MarshalledObject (java.rmi.MarshalledObject)1 Remote (java.rmi.Remote)1 RemoteException (java.rmi.RemoteException)1 AccessControlContext (java.security.AccessControlContext)1