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.83 by jsr166, Fri May 27 19:29:59 2011 UTC vs.
Revision 1.94 by jsr166, Mon Jan 21 19:32:19 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 283 | Line 288 | public class JSR166TestCase extends Test
288       * milliseconds in the future.
289       */
290      Date delayedDate(long delayMillis) {
291 <        return new Date(new Date().getTime() + delayMillis);
291 >        return new Date(System.currentTimeMillis() + delayMillis);
292      }
293  
294      /**
# Line 307 | Line 312 | public class JSR166TestCase extends Test
312      }
313  
314      /**
315 +     * Extra checks that get done for all test cases.
316 +     *
317       * Triggers test case failure if any thread assertions have failed,
318       * by rethrowing, in the test harness thread, any exception recorded
319       * earlier by threadRecordFailure.
320 +     *
321 +     * Triggers test case failure if interrupt status is set in the main thread.
322       */
323      public void tearDown() throws Exception {
324          Throwable t = threadFailure.getAndSet(null);
# Line 327 | Line 336 | public class JSR166TestCase extends Test
336                  throw afe;
337              }
338          }
339 +
340 +        if (Thread.interrupted())
341 +            throw new AssertionFailedError("interrupt status set in main thread");
342      }
343  
344      /**
# Line 521 | Line 533 | public class JSR166TestCase extends Test
533      }
534  
535      /**
536 +     * Checks that the threads do not terminate within the default
537 +     * millisecond delay of {@code timeoutMillis()}.
538 +     */
539 +    void assertThreadsStayAlive(Thread... threads) {
540 +        assertThreadsStayAlive(timeoutMillis(), threads);
541 +    }
542 +
543 +    /**
544 +     * Checks that the threads do not terminate within the given millisecond delay.
545 +     */
546 +    void assertThreadsStayAlive(long millis, Thread... threads) {
547 +        try {
548 +            // No need to optimize the failing case via Thread.join.
549 +            delay(millis);
550 +            for (Thread thread : threads)
551 +                assertTrue(thread.isAlive());
552 +        } catch (InterruptedException ie) {
553 +            fail("Unexpected InterruptedException");
554 +        }
555 +    }
556 +
557 +    /**
558       * Checks that future.get times out, with the default timeout of
559       * {@code timeoutMillis()}.
560       */
# Line 594 | Line 628 | public class JSR166TestCase extends Test
628          SecurityManager sm = System.getSecurityManager();
629          if (sm == null) {
630              r.run();
631 +        }
632 +        runWithSecurityManagerWithPermissions(r, permissions);
633 +    }
634 +
635 +    /**
636 +     * Runs Runnable r with a security policy that permits precisely
637 +     * the specified permissions.  If there is no current security
638 +     * manager, a temporary one is set for the duration of the
639 +     * Runnable.  We require that any security manager permit
640 +     * getPolicy/setPolicy.
641 +     */
642 +    public void runWithSecurityManagerWithPermissions(Runnable r,
643 +                                                      Permission... permissions) {
644 +        SecurityManager sm = System.getSecurityManager();
645 +        if (sm == null) {
646              Policy savedPolicy = Policy.getPolicy();
647              try {
648                  Policy.setPolicy(permissivePolicy());
649                  System.setSecurityManager(new SecurityManager());
650 <                runWithPermissions(r, permissions);
650 >                runWithSecurityManagerWithPermissions(r, permissions);
651              } finally {
652                  System.setSecurityManager(null);
653                  Policy.setPolicy(savedPolicy);
# Line 646 | Line 695 | public class JSR166TestCase extends Test
695              return perms.implies(p);
696          }
697          public void refresh() {}
698 +        public String toString() {
699 +            List<Permission> ps = new ArrayList<Permission>();
700 +            for (Enumeration<Permission> e = perms.elements(); e.hasMoreElements();)
701 +                ps.add(e.nextElement());
702 +            return "AdjustablePolicy with permissions " + ps;
703 +        }
704      }
705  
706      /**
# Line 683 | Line 738 | public class JSR166TestCase extends Test
738      }
739  
740      /**
741 <     * Waits up to the specified number of milliseconds for the given
741 >     * Spin-waits up to the specified number of milliseconds for the given
742       * thread to enter a wait state: BLOCKED, WAITING, or TIMED_WAITING.
743       */
744      void waitForThreadToEnterWaitState(Thread thread, long timeoutMillis) {
745 <        long timeoutNanos = timeoutMillis * 1000L * 1000L;
691 <        long t0 = System.nanoTime();
745 >        long startTime = System.nanoTime();
746          for (;;) {
747              Thread.State s = thread.getState();
748              if (s == Thread.State.BLOCKED ||
# Line 697 | Line 751 | public class JSR166TestCase extends Test
751                  return;
752              else if (s == Thread.State.TERMINATED)
753                  fail("Unexpected thread termination");
754 <            else if (System.nanoTime() - t0 > timeoutNanos) {
754 >            else if (millisElapsedSince(startTime) > timeoutMillis) {
755                  threadAssertTrue(thread.isAlive());
756                  return;
757              }
# Line 898 | Line 952 | public class JSR166TestCase extends Test
952          }
953      }
954  
955 +    public void await(Semaphore semaphore) {
956 +        try {
957 +            assertTrue(semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS));
958 +        } catch (Throwable t) {
959 +            threadUnexpectedException(t);
960 +        }
961 +    }
962 +
963   //     /**
964   //      * Spin-waits up to LONG_DELAY_MS until flag becomes true.
965   //      */
# Line 1119 | Line 1181 | public class JSR166TestCase extends Test
1181      }
1182  
1183      /**
1184 <     * A CyclicBarrier that fails with AssertionFailedErrors instead
1185 <     * of throwing checked exceptions.
1184 >     * A CyclicBarrier that uses timed await and fails with
1185 >     * AssertionFailedErrors instead of throwing checked exceptions.
1186       */
1187      public class CheckedBarrier extends CyclicBarrier {
1188          public CheckedBarrier(int parties) { super(parties); }
1189  
1190          public int await() {
1191              try {
1192 <                return super.await();
1192 >                return super.await(2 * LONG_DELAY_MS, MILLISECONDS);
1193 >            } catch (TimeoutException e) {
1194 >                throw new AssertionFailedError("timed out");
1195              } catch (Exception e) {
1196                  AssertionFailedError afe =
1197                      new AssertionFailedError("Unexpected exception: " + e);
# Line 1164 | Line 1228 | public class JSR166TestCase extends Test
1228          }
1229      }
1230  
1231 <    @SuppressWarnings("unchecked")
1232 <    <T> T serialClone(T o) {
1231 >    void assertSerialEquals(Object x, Object y) {
1232 >        assertTrue(Arrays.equals(serialBytes(x), serialBytes(y)));
1233 >    }
1234 >
1235 >    void assertNotSerialEquals(Object x, Object y) {
1236 >        assertFalse(Arrays.equals(serialBytes(x), serialBytes(y)));
1237 >    }
1238 >
1239 >    byte[] serialBytes(Object o) {
1240          try {
1241              ByteArrayOutputStream bos = new ByteArrayOutputStream();
1242              ObjectOutputStream oos = new ObjectOutputStream(bos);
1243              oos.writeObject(o);
1244              oos.flush();
1245              oos.close();
1246 <            ByteArrayInputStream bin =
1247 <                new ByteArrayInputStream(bos.toByteArray());
1248 <            ObjectInputStream ois = new ObjectInputStream(bin);
1249 <            return (T) ois.readObject();
1246 >            return bos.toByteArray();
1247 >        } catch (Throwable t) {
1248 >            threadUnexpectedException(t);
1249 >            return new byte[0];
1250 >        }
1251 >    }
1252 >
1253 >    @SuppressWarnings("unchecked")
1254 >    <T> T serialClone(T o) {
1255 >        try {
1256 >            ObjectInputStream ois = new ObjectInputStream
1257 >                (new ByteArrayInputStream(serialBytes(o)));
1258 >            T clone = (T) ois.readObject();
1259 >            assertSame(o.getClass(), clone.getClass());
1260 >            return clone;
1261          } catch (Throwable t) {
1262              threadUnexpectedException(t);
1263              return null;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines