use of java.lang.ref.PhantomReference in project robovm by robovm.
the class PhantomReferenceTest method test_get.
/**
* java.lang.ref.PhantomReference#get()
*/
public void test_get() {
ReferenceQueue rq = new ReferenceQueue();
bool = new Boolean(false);
PhantomReference pr = new PhantomReference(bool, rq);
assertNull("get() should return null.", pr.get());
pr.enqueue();
assertNull("get() should return null.", pr.get());
pr.clear();
assertNull("get() should return null.", pr.get());
}
use of java.lang.ref.PhantomReference in project robovm by robovm.
the class ReferenceQueueTest method test_removeJ.
/**
* java.lang.ref.ReferenceQueue#remove(long)
*/
public void test_removeJ() {
try {
assertNull("Queue should be empty. (poll)", rq.poll());
assertNull("Queue should be empty. (remove(1))", rq.remove((long) 1));
Thread ct = new Thread(new ChildThread());
ct.start();
Reference ret = rq.remove(0L);
assertNotNull("Delayed remove failed.", ret);
} catch (InterruptedException e) {
fail("InterruptedExeException during test : " + e.getMessage());
} catch (Exception e) {
fail("Exception during test : " + e.getMessage());
}
Object obj = new Object();
WeakReference wr = new WeakReference(obj, rq);
Boolean b = new Boolean(true);
SoftReference sr = new SoftReference(b, rq);
String str = "Test";
PhantomReference pr = new PhantomReference(str, rq);
pr.enqueue();
wr.enqueue();
sr.enqueue();
try {
Reference result = rq.remove(1L);
assertTrue((Boolean) result.get());
result = rq.remove(1L);
assertEquals(obj, result.get());
result = rq.remove(1L);
assertNull(result.get());
} catch (IllegalArgumentException e1) {
fail("IllegalArgumentException was thrown.");
} catch (InterruptedException e1) {
fail("InterruptedException was thrown.");
}
rq = new ReferenceQueue();
isThrown = false;
assertNull(rq.poll());
class RemoveThread extends Thread {
public void run() {
try {
rq.remove(1000L);
} catch (InterruptedException ie) {
isThrown = true;
}
}
}
RemoveThread rt = new RemoveThread();
rt.start();
try {
Thread.sleep(10);
} catch (InterruptedException ie) {
}
rt.interrupt();
try {
Thread.sleep(10);
} catch (InterruptedException ie) {
}
assertTrue(isThrown);
assertNull(rq.poll());
try {
rq.remove(-1);
fail("IllegalArgumentException expected.");
} catch (IllegalArgumentException iae) {
//expected
} catch (InterruptedException e) {
fail("Unexpected InterruptedException.");
}
}
use of java.lang.ref.PhantomReference 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.PhantomReference 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.PhantomReference in project mockito by mockito.
the class TypeCachingMockBytecodeGeneratorTest method ensure_cache_returns_same_instance.
@Test
public void ensure_cache_returns_same_instance() throws Exception {
// given
ClassLoader classloader_with_life_shorter_than_cache = inMemoryClassLoader().withClassDefinition("foo.Bar", makeMarkerInterface("foo.Bar")).build();
TypeCachingBytecodeGenerator cachingMockBytecodeGenerator = new TypeCachingBytecodeGenerator(new SubclassBytecodeGenerator(), true);
Class<?> the_mock_type = cachingMockBytecodeGenerator.mockClass(withMockFeatures(classloader_with_life_shorter_than_cache.loadClass("foo.Bar"), Collections.<Class<?>>emptySet(), SerializableMode.NONE));
Class<?> other_mock_type = cachingMockBytecodeGenerator.mockClass(withMockFeatures(classloader_with_life_shorter_than_cache.loadClass("foo.Bar"), Collections.<Class<?>>emptySet(), SerializableMode.NONE));
assertThat(other_mock_type).isSameAs(the_mock_type);
ReferenceQueue<Object> referenceQueue = new ReferenceQueue<Object>();
Reference<Object> typeReference = new PhantomReference<Object>(the_mock_type, referenceQueue);
// when
classloader_with_life_shorter_than_cache = is_no_more_referenced();
the_mock_type = is_no_more_referenced();
other_mock_type = is_no_more_referenced();
System.gc();
ensure_gc_happened();
// then
assertThat(referenceQueue.poll()).isEqualTo(typeReference);
}
Aggregations