Renaming the class and adding code snippet boundaries
authorJaroslav Tulach <jtulach@netbeans.org>
Wed, 15 Oct 2008 21:39:35 +0200
changeset 2816befc59fc53c
parent 280 e619b35c903e
child 282 b762c8fb572e
Renaming the class and adding code snippet boundaries
samples/gc/test/org/apidesign/gc/CompilerSurprisesTest.java
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/samples/gc/test/org/apidesign/gc/CompilerSurprisesTest.java	Wed Oct 15 21:39:35 2008 +0200
     1.3 @@ -0,0 +1,75 @@
     1.4 +package org.apidesign.gc;
     1.5 +
     1.6 +import java.lang.ref.Reference;
     1.7 +import java.lang.ref.WeakReference;
     1.8 +import org.junit.Test;
     1.9 +import org.netbeans.junit.NbTestCase;
    1.10 +import static org.junit.Assert.*;
    1.11 +
    1.12 +// BEGIN: compiler.surprises.intro
    1.13 +public class CompilerSurprisesTest {
    1.14 +    Reference<String> cache;
    1.15 +
    1.16 +    public String factory() {
    1.17 +        String value = new String("Can I disappear?");
    1.18 +        cache = new WeakReference<String>(value);
    1.19 +        return value;
    1.20 +    }
    1.21 +
    1.22 +    @Test
    1.23 +    public void checkThatTheValueCanDisapper() {
    1.24 +        String retValue = factory();
    1.25 +        retValue = null;
    1.26 +        assertGC("Nobody holds the string value anymore. It can be GCed.", cache);
    1.27 +    }
    1.28 +// FINISH: compiler.surprises.intro
    1.29 +
    1.30 +// BEGIN: compiler.surprises.error
    1.31 +    @Test
    1.32 +    public void obviouslyWithoutClearingTheReferenceItCannotBeGCed() {
    1.33 +        String retValue = factory();
    1.34 +// commented out:        retValue = null;
    1.35 +        assertNotGC("The reference is still on stack. It cannot be GCed.", cache);
    1.36 +    }
    1.37 +// END: compiler.surprises.error
    1.38 +
    1.39 +
    1.40 +// BEGIN: compiler.surprises.surprise
    1.41 +    boolean yes = true;
    1.42 +    @Test
    1.43 +    public void canItBeGCedSurprisingly() {
    1.44 +        String retValue;
    1.45 +        if (yes) {
    1.46 +            retValue = factory();
    1.47 +        }
    1.48 +        assertGC("Can be GCed, as retValue is not on stack!!!!", cache);
    1.49 +    }
    1.50 +// END: compiler.surprises.surprise
    1.51 +
    1.52 +
    1.53 +// BEGIN: compiler.surprises.fix
    1.54 +    boolean ok = true;
    1.55 +    @Test
    1.56 +    public void canItBeGCedIfInitialized() {
    1.57 +        String retValue = null;
    1.58 +        if (ok) {
    1.59 +            retValue = factory();
    1.60 +        }
    1.61 +        assertNotGC("Cannot be GCed as retValue is not stack", cache);
    1.62 +    }
    1.63 +// END: compiler.surprises.fix
    1.64 +
    1.65 +    private static void assertGC(String msg, Reference<?> ref) {
    1.66 +        NbTestCase.assertGC(msg, ref);
    1.67 +    }
    1.68 +
    1.69 +    private static void assertNotGC(String msg, Reference<?> ref) {
    1.70 +        try {
    1.71 +            NbTestCase.assertGC("Cannot be GCed. " + msg, ref);
    1.72 +        } catch (Error ex) {
    1.73 +            // OK
    1.74 +            return;
    1.75 +        }
    1.76 +        fail(msg + ref.get());
    1.77 +    }
    1.78 +}
    1.79 \ No newline at end of file