2 * Copyright (C) 2014, United States Government, as represented by the
3 * Administrator of the National Aeronautics and Space Administration.
6 * The Java Pathfinder core (jpf-core) platform is licensed under the
7 * Apache License, Version 2.0 (the "License"); you may not use this file except
8 * in compliance with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0.
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 package gov.nasa.jpf.test.java.lang.ref;
21 import gov.nasa.jpf.util.test.TestJPF;
22 import gov.nasa.jpf.vm.Verify;
24 import java.lang.ref.WeakReference;
26 import org.junit.Test;
28 public class WeakReferenceTest extends TestJPF
31 public void testGCClearsRef()
33 WeakReference<Target> ref;
35 if (verifyNoPropertyViolation())
37 ref = new WeakReference<Target>(new Target());
41 assertNull(ref.get());
46 public void testStrongReferenceKeepsWeakReference()
48 WeakReference<Target> ref;
51 if (verifyNoPropertyViolation())
53 target = new Target();
54 ref = new WeakReference<Target>(target);
58 assertSame(target, ref.get());
62 /* ClassInfo.refClassInfo wasn't being set to null between JPF runs. Thus,
63 * refClassInfo wasn't being updated. Hence, the WeakReference below would
64 * be treated as a normal object in GC. Re-run testGCClearsRef() to
65 * reproduce the issue.
68 public void testClearClassInfoRefClassInfo()
73 private static void forceGC()
75 System.gc(); // Mark that GC is needed
76 Verify.breakTransition("testForceGC"); // Cause a state to be captured and hence GC to run
79 private static class Target // Make this object easy to find in JPF heap