diff --git a/test/functional/VM_Test/build.xml b/test/functional/VM_Test/build.xml index 82c927b2f42..67d598d9f92 100644 --- a/test/functional/VM_Test/build.xml +++ b/test/functional/VM_Test/build.xml @@ -41,6 +41,7 @@ + @@ -86,6 +87,11 @@ + + + + + @@ -95,10 +101,15 @@ + + + + + @@ -106,10 +117,15 @@ + + + + + diff --git a/test/functional/VM_Test/src/j9vm/test/clone/CloneWeakReferenceTest.java b/test/functional/VM_Test/src/j9vm/test/clone/CloneWeakReferenceTest.java index 265317cf64a..02b06de1137 100644 --- a/test/functional/VM_Test/src/j9vm/test/clone/CloneWeakReferenceTest.java +++ b/test/functional/VM_Test/src/j9vm/test/clone/CloneWeakReferenceTest.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2001, 2012 IBM Corp. and others + * Copyright (c) 2001, 2018 IBM Corp. and others * * This program and the accompanying materials are made available under * the terms of the Eclipse Public License 2.0 which accompanies this @@ -22,6 +22,7 @@ package j9vm.test.clone; import java.lang.ref.WeakReference; +import org.openj9.test.util.VersionCheck; /** * Test case to ensure that the VM is properly cloning weak references. @@ -41,38 +42,52 @@ public static void main(String[] args) throws CloneNotSupportedException { Object referent = new String("foobar"); SubWeakReference reference = new SubWeakReference(referent); - SubWeakReference clone = (SubWeakReference) reference.clone(); - - if (clone.get() == referent) { - System.out.println("original and cloned weak references using the same referent (expected)"); - } else { - throw new RuntimeException("original and cloned weak references are not using the same referent!"); - } - - referent = null; - - System.gc(); - - /* At this point the weak references may not have been cleared. In metronome, if a GC cycle - * is ongoing when System.gc is called, the cycle will be finished. This GC is a - * snapshot-at-the-beginning collector. If the GC cycle was started BEFORE referent was set - * to null, then String object will not be collected and the weak references will not be - * cleared. - */ - if (reference.get() != clone.get()) { - throw new RuntimeException("Referents not identical. reference.get(): " + reference.get() + " clone.get(): " + clone.get()); - } - - System.gc(); - - if (reference.get() != null) { - throw new RuntimeException("Reference referent not cleared: " + reference + " " + reference.get()); + if (VersionCheck.major() >= 11) { + try { + SubWeakReference clone = (SubWeakReference) reference.clone(); + // fail if CloneNotSupportedException is not thrown + throw new RuntimeException( + "CloneNotSupportedException is expected, but it is not thrown for JDK version: " + + VersionCheck.major()); + } catch (CloneNotSupportedException e) { + // CloneNotSupportedException is expected for JDK version >= 11 + } + } else { + SubWeakReference clone = (SubWeakReference) reference.clone(); + + if (clone.get() == referent) { + System.out.println("original and cloned weak references using the same referent (expected)"); + } else { + throw new RuntimeException("original and cloned weak references are not using the same referent!"); + } + + referent = null; + + System.gc(); + + /* At this point the weak references may not have been cleared. In metronome, if a GC cycle + * is ongoing when System.gc is called, the cycle will be finished. This GC is a + * snapshot-at-the-beginning collector. If the GC cycle was started BEFORE referent was set + * to null, then String object will not be collected and the weak references will not be + * cleared. + */ + + if (reference.get() != clone.get()) { + throw new RuntimeException("Referents not identical. reference.get(): " + reference.get() + " clone.get(): " + clone.get()); + } + + System.gc(); + + if (reference.get() != null) { + throw new RuntimeException("Reference referent not cleared: " + reference + " " + reference.get()); + } + + if (clone.get() != null) { + throw new RuntimeException("Clone referent not cleared: " + clone + " " + clone.get()); + } } - if (clone.get() != null) { - throw new RuntimeException("Clone referent not cleared: " + clone + " " + clone.get()); - } } static class SubWeakReference extends WeakReference implements Cloneable { @@ -80,7 +95,7 @@ static class SubWeakReference extends WeakReference implements Cloneable { public SubWeakReference(Object referent) { super(referent); } - + public Object clone() throws CloneNotSupportedException { return super.clone(); }