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.86 by jsr166, Mon May 30 22:42:22 2011 UTC vs.
Revision 1.95 by jsr166, Mon Jan 21 19:43:52 2013 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 142 | Line 145 | public class JSR166TestCase extends Test
145      }
146  
147      /**
148 <     * Runs all JSR166 unit tests using junit.textui.TestRunner
148 >     * Runs all JSR166 unit tests using junit.textui.TestRunner.
149 >     * Optional command line arg provides the number of iterations to
150 >     * repeat running the tests.
151       */
152      public static void main(String[] args) {
153          if (useSecurityManager) {
# Line 507 | Line 512 | public class JSR166TestCase extends Test
512      }
513  
514      /**
515 +     * A debugging tool to print all stack traces, as jstack does.
516 +     */
517 +    void printAllStackTraces() {
518 +        System.err.println(
519 +            Arrays.toString(
520 +                java.lang.management.ManagementFactory.getThreadMXBean()
521 +                .dumpAllThreads(true, true)));
522 +    }
523 +
524 +    /**
525       * Checks that thread does not terminate within the default
526       * millisecond delay of {@code timeoutMillis()}.
527       */
# Line 528 | Line 543 | public class JSR166TestCase extends Test
543      }
544  
545      /**
546 +     * Checks that the threads do not terminate within the default
547 +     * millisecond delay of {@code timeoutMillis()}.
548 +     */
549 +    void assertThreadsStayAlive(Thread... threads) {
550 +        assertThreadsStayAlive(timeoutMillis(), threads);
551 +    }
552 +
553 +    /**
554 +     * Checks that the threads do not terminate within the given millisecond delay.
555 +     */
556 +    void assertThreadsStayAlive(long millis, Thread... threads) {
557 +        try {
558 +            // No need to optimize the failing case via Thread.join.
559 +            delay(millis);
560 +            for (Thread thread : threads)
561 +                assertTrue(thread.isAlive());
562 +        } catch (InterruptedException ie) {
563 +            fail("Unexpected InterruptedException");
564 +        }
565 +    }
566 +
567 +    /**
568       * Checks that future.get times out, with the default timeout of
569       * {@code timeoutMillis()}.
570       */
# Line 601 | Line 638 | public class JSR166TestCase extends Test
638          SecurityManager sm = System.getSecurityManager();
639          if (sm == null) {
640              r.run();
641 +        }
642 +        runWithSecurityManagerWithPermissions(r, permissions);
643 +    }
644 +
645 +    /**
646 +     * Runs Runnable r with a security policy that permits precisely
647 +     * the specified permissions.  If there is no current security
648 +     * manager, a temporary one is set for the duration of the
649 +     * Runnable.  We require that any security manager permit
650 +     * getPolicy/setPolicy.
651 +     */
652 +    public void runWithSecurityManagerWithPermissions(Runnable r,
653 +                                                      Permission... permissions) {
654 +        SecurityManager sm = System.getSecurityManager();
655 +        if (sm == null) {
656              Policy savedPolicy = Policy.getPolicy();
657              try {
658                  Policy.setPolicy(permissivePolicy());
659                  System.setSecurityManager(new SecurityManager());
660 <                runWithPermissions(r, permissions);
660 >                runWithSecurityManagerWithPermissions(r, permissions);
661              } finally {
662                  System.setSecurityManager(null);
663                  Policy.setPolicy(savedPolicy);
# Line 653 | Line 705 | public class JSR166TestCase extends Test
705              return perms.implies(p);
706          }
707          public void refresh() {}
708 +        public String toString() {
709 +            List<Permission> ps = new ArrayList<Permission>();
710 +            for (Enumeration<Permission> e = perms.elements(); e.hasMoreElements();)
711 +                ps.add(e.nextElement());
712 +            return "AdjustablePolicy with permissions " + ps;
713 +        }
714      }
715  
716      /**
# Line 690 | Line 748 | public class JSR166TestCase extends Test
748      }
749  
750      /**
751 <     * Waits up to the specified number of milliseconds for the given
751 >     * Spin-waits up to the specified number of milliseconds for the given
752       * thread to enter a wait state: BLOCKED, WAITING, or TIMED_WAITING.
753       */
754      void waitForThreadToEnterWaitState(Thread thread, long timeoutMillis) {
755 <        long timeoutNanos = timeoutMillis * 1000L * 1000L;
698 <        long t0 = System.nanoTime();
755 >        long startTime = System.nanoTime();
756          for (;;) {
757              Thread.State s = thread.getState();
758              if (s == Thread.State.BLOCKED ||
# Line 704 | Line 761 | public class JSR166TestCase extends Test
761                  return;
762              else if (s == Thread.State.TERMINATED)
763                  fail("Unexpected thread termination");
764 <            else if (System.nanoTime() - t0 > timeoutNanos) {
764 >            else if (millisElapsedSince(startTime) > timeoutMillis) {
765                  threadAssertTrue(thread.isAlive());
766                  return;
767              }
# Line 905 | Line 962 | public class JSR166TestCase extends Test
962          }
963      }
964  
965 +    public void await(Semaphore semaphore) {
966 +        try {
967 +            assertTrue(semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS));
968 +        } catch (Throwable t) {
969 +            threadUnexpectedException(t);
970 +        }
971 +    }
972 +
973   //     /**
974   //      * Spin-waits up to LONG_DELAY_MS until flag becomes true.
975   //      */
# Line 1173 | Line 1238 | public class JSR166TestCase extends Test
1238          }
1239      }
1240  
1241 <    @SuppressWarnings("unchecked")
1242 <    <T> T serialClone(T o) {
1241 >    void assertSerialEquals(Object x, Object y) {
1242 >        assertTrue(Arrays.equals(serialBytes(x), serialBytes(y)));
1243 >    }
1244 >
1245 >    void assertNotSerialEquals(Object x, Object y) {
1246 >        assertFalse(Arrays.equals(serialBytes(x), serialBytes(y)));
1247 >    }
1248 >
1249 >    byte[] serialBytes(Object o) {
1250          try {
1251              ByteArrayOutputStream bos = new ByteArrayOutputStream();
1252              ObjectOutputStream oos = new ObjectOutputStream(bos);
1253              oos.writeObject(o);
1254              oos.flush();
1255              oos.close();
1256 <            ByteArrayInputStream bin =
1257 <                new ByteArrayInputStream(bos.toByteArray());
1258 <            ObjectInputStream ois = new ObjectInputStream(bin);
1259 <            return (T) ois.readObject();
1256 >            return bos.toByteArray();
1257 >        } catch (Throwable t) {
1258 >            threadUnexpectedException(t);
1259 >            return new byte[0];
1260 >        }
1261 >    }
1262 >
1263 >    @SuppressWarnings("unchecked")
1264 >    <T> T serialClone(T o) {
1265 >        try {
1266 >            ObjectInputStream ois = new ObjectInputStream
1267 >                (new ByteArrayInputStream(serialBytes(o)));
1268 >            T clone = (T) ois.readObject();
1269 >            assertSame(o.getClass(), clone.getClass());
1270 >            return clone;
1271          } catch (Throwable t) {
1272              threadUnexpectedException(t);
1273              return null;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines