samples/gc/test/org/apidesign/gc/CompilerSurprisesTest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Wed, 15 Oct 2008 21:39:35 +0200
changeset 281 6befc59fc53c
child 282 b762c8fb572e
permissions -rw-r--r--
Renaming the class and adding code snippet boundaries
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@281
    23
        assertGC("Nobody holds the string value anymore. It can be GCed.", cache);
jtulach@281
    24
    }
jtulach@281
    25
// FINISH: compiler.surprises.intro
jtulach@281
    26
jtulach@281
    27
// BEGIN: compiler.surprises.error
jtulach@281
    28
    @Test
jtulach@281
    29
    public void obviouslyWithoutClearingTheReferenceItCannotBeGCed() {
jtulach@281
    30
        String retValue = factory();
jtulach@281
    31
// commented out:        retValue = null;
jtulach@281
    32
        assertNotGC("The reference is still on stack. It cannot be GCed.", cache);
jtulach@281
    33
    }
jtulach@281
    34
// END: compiler.surprises.error
jtulach@281
    35
jtulach@281
    36
jtulach@281
    37
// BEGIN: compiler.surprises.surprise
jtulach@281
    38
    boolean yes = true;
jtulach@281
    39
    @Test
jtulach@281
    40
    public void canItBeGCedSurprisingly() {
jtulach@281
    41
        String retValue;
jtulach@281
    42
        if (yes) {
jtulach@281
    43
            retValue = factory();
jtulach@281
    44
        }
jtulach@281
    45
        assertGC("Can be GCed, as retValue is not on stack!!!!", cache);
jtulach@281
    46
    }
jtulach@281
    47
// END: compiler.surprises.surprise
jtulach@281
    48
jtulach@281
    49
jtulach@281
    50
// BEGIN: compiler.surprises.fix
jtulach@281
    51
    boolean ok = true;
jtulach@281
    52
    @Test
jtulach@281
    53
    public void canItBeGCedIfInitialized() {
jtulach@281
    54
        String retValue = null;
jtulach@281
    55
        if (ok) {
jtulach@281
    56
            retValue = factory();
jtulach@281
    57
        }
jtulach@281
    58
        assertNotGC("Cannot be GCed as retValue is not stack", cache);
jtulach@281
    59
    }
jtulach@281
    60
// END: compiler.surprises.fix
jtulach@281
    61
jtulach@281
    62
    private static void assertGC(String msg, Reference<?> ref) {
jtulach@281
    63
        NbTestCase.assertGC(msg, ref);
jtulach@281
    64
    }
jtulach@281
    65
jtulach@281
    66
    private static void assertNotGC(String msg, Reference<?> ref) {
jtulach@281
    67
        try {
jtulach@281
    68
            NbTestCase.assertGC("Cannot be GCed. " + msg, ref);
jtulach@281
    69
        } catch (Error ex) {
jtulach@281
    70
            // OK
jtulach@281
    71
            return;
jtulach@281
    72
        }
jtulach@281
    73
        fail(msg + ref.get());
jtulach@281
    74
    }
jtulach@281
    75
}