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.90 by jsr166, Wed Jun 22 07:46:57 2011 UTC vs.
Revision 1.93 by jsr166, Sun Dec 16 17:22:42 2012 UTC

# Line 11 | Line 11 | import java.io.ByteArrayInputStream;
11   import java.io.ByteArrayOutputStream;
12   import java.io.ObjectInputStream;
13   import java.io.ObjectOutputStream;
14 + import java.util.ArrayList;
15   import java.util.Arrays;
16   import java.util.Date;
17 + import java.util.Enumeration;
18 + import java.util.List;
19   import java.util.NoSuchElementException;
20   import java.util.PropertyPermission;
21   import java.util.concurrent.*;
# Line 69 | Line 72 | import java.security.SecurityPermission;
72   *
73   * </ol>
74   *
75 < * <p> <b>Other notes</b>
75 > * <p><b>Other notes</b>
76   * <ul>
77   *
78   * <li> Usually, there is one testcase method per JSR166 method
# Line 623 | Line 626 | public class JSR166TestCase extends Test
626          SecurityManager sm = System.getSecurityManager();
627          if (sm == null) {
628              r.run();
629 +        }
630 +        runWithSecurityManagerWithPermissions(r, permissions);
631 +    }
632 +
633 +    /**
634 +     * Runs Runnable r with a security policy that permits precisely
635 +     * the specified permissions.  If there is no current security
636 +     * manager, a temporary one is set for the duration of the
637 +     * Runnable.  We require that any security manager permit
638 +     * getPolicy/setPolicy.
639 +     */
640 +    public void runWithSecurityManagerWithPermissions(Runnable r,
641 +                                                      Permission... permissions) {
642 +        SecurityManager sm = System.getSecurityManager();
643 +        if (sm == null) {
644              Policy savedPolicy = Policy.getPolicy();
645              try {
646                  Policy.setPolicy(permissivePolicy());
647                  System.setSecurityManager(new SecurityManager());
648 <                runWithPermissions(r, permissions);
648 >                runWithSecurityManagerWithPermissions(r, permissions);
649              } finally {
650                  System.setSecurityManager(null);
651                  Policy.setPolicy(savedPolicy);
# Line 675 | Line 693 | public class JSR166TestCase extends Test
693              return perms.implies(p);
694          }
695          public void refresh() {}
696 +        public String toString() {
697 +            List<Permission> ps = new ArrayList<Permission>();
698 +            for (Enumeration<Permission> e = perms.elements(); e.hasMoreElements();)
699 +                ps.add(e.nextElement());
700 +            return "AdjustablePolicy with permissions " + ps;
701 +        }
702      }
703  
704      /**
# Line 1202 | Line 1226 | public class JSR166TestCase extends Test
1226          }
1227      }
1228  
1229 <    @SuppressWarnings("unchecked")
1230 <    <T> T serialClone(T o) {
1229 >    void assertSerialEquals(Object x, Object y) {
1230 >        assertTrue(Arrays.equals(serialBytes(x), serialBytes(y)));
1231 >    }
1232 >
1233 >    void assertNotSerialEquals(Object x, Object y) {
1234 >        assertFalse(Arrays.equals(serialBytes(x), serialBytes(y)));
1235 >    }
1236 >
1237 >    byte[] serialBytes(Object o) {
1238          try {
1239              ByteArrayOutputStream bos = new ByteArrayOutputStream();
1240              ObjectOutputStream oos = new ObjectOutputStream(bos);
1241              oos.writeObject(o);
1242              oos.flush();
1243              oos.close();
1244 +            return bos.toByteArray();
1245 +        } catch (Throwable t) {
1246 +            threadUnexpectedException(t);
1247 +            return new byte[0];
1248 +        }
1249 +    }
1250 +
1251 +    @SuppressWarnings("unchecked")
1252 +    <T> T serialClone(T o) {
1253 +        try {
1254              ObjectInputStream ois = new ObjectInputStream
1255 <                (new ByteArrayInputStream(bos.toByteArray()));
1255 >                (new ByteArrayInputStream(serialBytes(o)));
1256              T clone = (T) ois.readObject();
1257              assertSame(o.getClass(), clone.getClass());
1258              return clone;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines