ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/JSR166TestCase.java
(Generate patch)

Comparing jsr166/src/test/tck/JSR166TestCase.java (file contents):
Revision 1.207 by jsr166, Thu Nov 3 20:41:32 2016 UTC vs.
Revision 1.211 by jsr166, Mon Nov 14 23:52:22 2016 UTC

# Line 41 | Line 41 | import java.security.ProtectionDomain;
41   import java.security.SecurityPermission;
42   import java.util.ArrayList;
43   import java.util.Arrays;
44 + import java.util.Collection;
45   import java.util.Collections;
46   import java.util.Date;
47   import java.util.Enumeration;
# Line 1827 | Line 1828 | public class JSR166TestCase extends Test
1828          }
1829      }
1830  
1831 +    void assertImmutable(final Object o) {
1832 +        if (o instanceof Collection) {
1833 +            assertThrows(
1834 +                UnsupportedOperationException.class,
1835 +                new Runnable() { public void run() {
1836 +                        ((Collection) o).add(null);}});
1837 +        }
1838 +    }
1839 +
1840      @SuppressWarnings("unchecked")
1841      <T> T serialClone(T o) {
1842          try {
1843              ObjectInputStream ois = new ObjectInputStream
1844                  (new ByteArrayInputStream(serialBytes(o)));
1845              T clone = (T) ois.readObject();
1846 +            if (o == clone) assertImmutable(o);
1847              assertSame(o.getClass(), clone.getClass());
1848              return clone;
1849          } catch (Throwable fail) {
# Line 1841 | Line 1852 | public class JSR166TestCase extends Test
1852          }
1853      }
1854  
1855 +    /**
1856 +     * A version of serialClone that leaves error handling (for
1857 +     * e.g. NotSerializableException) up to the caller.
1858 +     */
1859 +    @SuppressWarnings("unchecked")
1860 +    <T> T serialClonePossiblyFailing(T o)
1861 +        throws ReflectiveOperationException, java.io.IOException {
1862 +        ByteArrayOutputStream bos = new ByteArrayOutputStream();
1863 +        ObjectOutputStream oos = new ObjectOutputStream(bos);
1864 +        oos.writeObject(o);
1865 +        oos.flush();
1866 +        oos.close();
1867 +        ObjectInputStream ois = new ObjectInputStream
1868 +            (new ByteArrayInputStream(bos.toByteArray()));
1869 +        T clone = (T) ois.readObject();
1870 +        if (o == clone) assertImmutable(o);
1871 +        assertSame(o.getClass(), clone.getClass());
1872 +        return clone;
1873 +    }
1874 +
1875 +    /**
1876 +     * If o implements Cloneable and has a public clone method,
1877 +     * returns a clone of o, else null.
1878 +     */
1879 +    @SuppressWarnings("unchecked")
1880 +    <T> T cloneableClone(T o) {
1881 +        if (!(o instanceof Cloneable)) return null;
1882 +        final T clone;
1883 +        try {
1884 +            clone = (T) o.getClass().getMethod("clone").invoke(o);
1885 +        } catch (NoSuchMethodException ok) {
1886 +            return null;
1887 +        } catch (ReflectiveOperationException unexpected) {
1888 +            throw new Error(unexpected);
1889 +        }
1890 +        assertNotSame(o, clone); // not 100% guaranteed by spec
1891 +        assertSame(o.getClass(), clone.getClass());
1892 +        return clone;
1893 +    }
1894 +
1895      public void assertThrows(Class<? extends Throwable> expectedExceptionClass,
1896                               Runnable... throwingActions) {
1897          for (Runnable throwingAction : throwingActions) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines