author | Jaroslav Tulach <jtulach@netbeans.org> |
Wed, 15 Oct 2008 21:48:18 +0200 | |
changeset 282 | b762c8fb572e |
parent 281 | 6befc59fc53c |
child 283 | f6369ddf822c |
permissions | -rw-r--r-- |
jtulach@281 | 1 |
package org.apidesign.gc; |
jtulach@281 | 2 |
|
jtulach@281 | 3 |
import java.lang.ref.Reference; |
jtulach@281 | 4 |
import java.lang.ref.WeakReference; |
jtulach@281 | 5 |
import org.junit.Test; |
jtulach@281 | 6 |
import org.netbeans.junit.NbTestCase; |
jtulach@281 | 7 |
import static org.junit.Assert.*; |
jtulach@281 | 8 |
|
jtulach@281 | 9 |
// BEGIN: compiler.surprises.intro |
jtulach@281 | 10 |
public class CompilerSurprisesTest { |
jtulach@281 | 11 |
Reference<String> cache; |
jtulach@281 | 12 |
|
jtulach@281 | 13 |
public String factory() { |
jtulach@281 | 14 |
String value = new String("Can I disappear?"); |
jtulach@281 | 15 |
cache = new WeakReference<String>(value); |
jtulach@281 | 16 |
return value; |
jtulach@281 | 17 |
} |
jtulach@281 | 18 |
|
jtulach@281 | 19 |
@Test |
jtulach@281 | 20 |
public void checkThatTheValueCanDisapper() { |
jtulach@281 | 21 |
String retValue = factory(); |
jtulach@281 | 22 |
retValue = null; |
jtulach@282 | 23 |
assertGC("Nobody holds the string value anymore." + |
jtulach@282 | 24 |
"It can be GCed.", cache); |
jtulach@281 | 25 |
} |
jtulach@281 | 26 |
// FINISH: compiler.surprises.intro |
jtulach@281 | 27 |
|
jtulach@281 | 28 |
// BEGIN: compiler.surprises.error |
jtulach@281 | 29 |
@Test |
jtulach@281 | 30 |
public void obviouslyWithoutClearingTheReferenceItCannotBeGCed() { |
jtulach@281 | 31 |
String retValue = factory(); |
jtulach@281 | 32 |
// commented out: retValue = null; |
jtulach@282 | 33 |
assertNotGC("The reference is still on stack." + |
jtulach@282 | 34 |
"It cannot be GCed.", cache); |
jtulach@281 | 35 |
} |
jtulach@281 | 36 |
// END: compiler.surprises.error |
jtulach@281 | 37 |
|
jtulach@281 | 38 |
|
jtulach@281 | 39 |
// BEGIN: compiler.surprises.surprise |
jtulach@281 | 40 |
boolean yes = true; |
jtulach@281 | 41 |
@Test |
jtulach@281 | 42 |
public void canItBeGCedSurprisingly() { |
jtulach@281 | 43 |
String retValue; |
jtulach@281 | 44 |
if (yes) { |
jtulach@281 | 45 |
retValue = factory(); |
jtulach@281 | 46 |
} |
jtulach@281 | 47 |
assertGC("Can be GCed, as retValue is not on stack!!!!", cache); |
jtulach@281 | 48 |
} |
jtulach@281 | 49 |
// END: compiler.surprises.surprise |
jtulach@281 | 50 |
|
jtulach@281 | 51 |
|
jtulach@281 | 52 |
// BEGIN: compiler.surprises.fix |
jtulach@281 | 53 |
boolean ok = true; |
jtulach@281 | 54 |
@Test |
jtulach@281 | 55 |
public void canItBeGCedIfInitialized() { |
jtulach@281 | 56 |
String retValue = null; |
jtulach@281 | 57 |
if (ok) { |
jtulach@281 | 58 |
retValue = factory(); |
jtulach@281 | 59 |
} |
jtulach@281 | 60 |
assertNotGC("Cannot be GCed as retValue is not stack", cache); |
jtulach@281 | 61 |
} |
jtulach@281 | 62 |
// END: compiler.surprises.fix |
jtulach@281 | 63 |
|
jtulach@281 | 64 |
private static void assertGC(String msg, Reference<?> ref) { |
jtulach@281 | 65 |
NbTestCase.assertGC(msg, ref); |
jtulach@281 | 66 |
} |
jtulach@281 | 67 |
|
jtulach@281 | 68 |
private static void assertNotGC(String msg, Reference<?> ref) { |
jtulach@281 | 69 |
try { |
jtulach@281 | 70 |
NbTestCase.assertGC("Cannot be GCed. " + msg, ref); |
jtulach@281 | 71 |
} catch (Error ex) { |
jtulach@281 | 72 |
// OK |
jtulach@281 | 73 |
return; |
jtulach@281 | 74 |
} |
jtulach@281 | 75 |
fail(msg + ref.get()); |
jtulach@281 | 76 |
} |
jtulach@281 | 77 |
} |