--- jsr166/src/test/tck/JSR166TestCase.java 2016/11/03 20:41:32 1.207 +++ jsr166/src/test/tck/JSR166TestCase.java 2016/11/05 16:09:50 1.208 @@ -41,6 +41,7 @@ import java.security.ProtectionDomain; import java.security.SecurityPermission; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collection; import java.util.Collections; import java.util.Date; import java.util.Enumeration; @@ -1827,12 +1828,22 @@ public class JSR166TestCase extends Test } } + void assertImmutable(Object o) { + if (o instanceof Collection) { + assertThrows( + UnsupportedOperationException.class, + new Runnable() { public void run() { + ((Collection) o).add(null);}}); + } + } + @SuppressWarnings("unchecked") T serialClone(T o) { try { ObjectInputStream ois = new ObjectInputStream (new ByteArrayInputStream(serialBytes(o))); T clone = (T) ois.readObject(); + if (o == clone) assertImmutable(o); assertSame(o.getClass(), clone.getClass()); return clone; } catch (Throwable fail) { @@ -1841,6 +1852,26 @@ public class JSR166TestCase extends Test } } + /** + * If o implements Cloneable and has a public clone method, + * returns a clone of o, else null. + */ + @SuppressWarnings("unchecked") + T cloneableClone(T o) { + if (!(o instanceof Cloneable)) return null; + final T clone; + try { + clone = (T) o.getClass().getMethod("clone").invoke(o); + } catch (NoSuchMethodException ok) { + return null; + } catch (ReflectiveOperationException unexpected) { + throw new Error(unexpected); + } + assertNotSame(o, clone); // not 100% guaranteed by spec + assertSame(o.getClass(), clone.getClass()); + return clone; + } + public void assertThrows(Class expectedExceptionClass, Runnable... throwingActions) { for (Runnable throwingAction : throwingActions) {