tests/libjava-mauve/src/gnu/testlet/java/lang/ref/WeakReference/weakref.java
branchjk_new_structure
changeset 1541 75c2e24dea9a
parent 1540 92ac284961c1
child 1542 be11db817bcf
equal deleted inserted replaced
1540:92ac284961c1 1541:75c2e24dea9a
     1 // Test of WeakReference
       
     2 
       
     3 // Copyright (C) 2001 Red Hat, Inc.
       
     4 
       
     5 // This file is part of Mauve.
       
     6 
       
     7 // Mauve is free software; you can redistribute it and/or modify
       
     8 // it under the terms of the GNU General Public License as published by
       
     9 // the Free Software Foundation; either version 2, or (at your option)
       
    10 // any later version.
       
    11 
       
    12 // Mauve is distributed in the hope that it will be useful,
       
    13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    15 // GNU General Public License for more details.
       
    16 
       
    17 // You should have received a copy of the GNU General Public License
       
    18 // along with Mauve; see the file COPYING.  If not, write to
       
    19 // the Free Software Foundation, 59 Temple Place - Suite 330,
       
    20 // Boston, MA 02111-1307, USA.
       
    21 
       
    22 // Tags: JDK1.2
       
    23 
       
    24 // In this test we make some assumptions about how the GC operates
       
    25 // that are probably not quite sound.  In particular we assume
       
    26 // System.gc() will collect everything.
       
    27 
       
    28 package gnu.testlet.java.lang.ref.WeakReference;
       
    29 
       
    30 import gnu.testlet.Testlet;
       
    31 import gnu.testlet.TestHarness;
       
    32 import java.lang.ref.*;
       
    33 
       
    34 public class weakref implements Testlet
       
    35 {
       
    36 
       
    37   static class Reffer extends Thread
       
    38   {
       
    39     ReferenceQueue q;
       
    40     TestHarness harness;
       
    41     WeakReference wr;
       
    42     Integer twt;
       
    43 
       
    44     public Reffer (ReferenceQueue q, TestHarness harness)
       
    45     {
       
    46       this.q = q;
       
    47       this.harness = harness;
       
    48     }
       
    49 
       
    50     public void run()
       
    51     {
       
    52       twt = new Integer (23);
       
    53       wr = new WeakReference (twt, q);
       
    54 
       
    55       System.gc ();
       
    56 
       
    57       Reference r = q.poll ();
       
    58       harness.check (r, null, "live reference");
       
    59       harness.check (wr.get (), twt);
       
    60     }
       
    61   }
       
    62 
       
    63   public void test (TestHarness harness)
       
    64   {
       
    65     ReferenceQueue q = new ReferenceQueue ();
       
    66 
       
    67     // Create reference in a separate thread so no inadvertent references
       
    68     // to the contained object are left on the stack, which causes VM's that
       
    69     // do conservative stack GC scans to report false negatives for this test.
       
    70     Reffer reffer = new Reffer(q, harness); 
       
    71     reffer.start();
       
    72     try {
       
    73       reffer.join();
       
    74     } catch (InterruptedException e) {
       
    75       throw new RuntimeException(e);
       
    76     }
       
    77     WeakReference wr = reffer.wr;
       
    78 
       
    79     // Weak reference "should" get cleared here
       
    80     reffer.twt = null;
       
    81     System.gc ();
       
    82 
       
    83     Reference r = null;
       
    84     try
       
    85       {
       
    86 	r = q.remove (5 * 1000); // 5 seconds.
       
    87       }
       
    88     catch (InterruptedException _)
       
    89       {
       
    90 	harness.debug (_);
       
    91       }
       
    92 
       
    93     harness.check (r, wr, "unreachable");
       
    94     harness.check (wr.get (), null, "contents of weak reference");
       
    95   }
       
    96 }