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));
}
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());
}
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());
}
}
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());
}
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);
}
Aggregations