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.99 by jsr166, Tue Feb 5 03:39:34 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.lang.management.ManagementFactory;
15 + import java.lang.management.ThreadInfo;
16 + import java.lang.reflect.Method;
17 + import java.util.ArrayList;
18   import java.util.Arrays;
19   import java.util.Date;
20 + import java.util.Enumeration;
21 + import java.util.List;
22   import java.util.NoSuchElementException;
23   import java.util.PropertyPermission;
24   import java.util.concurrent.*;
# Line 69 | Line 75 | import java.security.SecurityPermission;
75   *
76   * </ol>
77   *
78 < * <p> <b>Other notes</b>
78 > * <p><b>Other notes</b>
79   * <ul>
80   *
81   * <li> Usually, there is one testcase method per JSR166 method
# Line 142 | Line 148 | public class JSR166TestCase extends Test
148      }
149  
150      /**
151 <     * Runs all JSR166 unit tests using junit.textui.TestRunner
151 >     * Runs all JSR166 unit tests using junit.textui.TestRunner.
152 >     * Optional command line arg provides the number of iterations to
153 >     * repeat running the tests.
154       */
155      public static void main(String[] args) {
156          if (useSecurityManager) {
# Line 174 | Line 182 | public class JSR166TestCase extends Test
182          return suite;
183      }
184  
185 +    public static void addNamedTestClasses(TestSuite suite,
186 +                                           String... testClassNames) {
187 +        for (String testClassName : testClassNames) {
188 +            try {
189 +                Class<?> testClass = Class.forName(testClassName);
190 +                Method m = testClass.getDeclaredMethod("suite",
191 +                                                       new Class<?>[0]);
192 +                suite.addTest(newTestSuite((Test)m.invoke(null)));
193 +            } catch (Exception e) {
194 +                throw new Error("Missing test class", e);
195 +            }
196 +        }
197 +    }
198 +
199 +    public static final double JAVA_CLASS_VERSION;
200 +    static {
201 +        try {
202 +            JAVA_CLASS_VERSION = java.security.AccessController.doPrivileged(
203 +                new java.security.PrivilegedAction<Double>() {
204 +                public Double run() {
205 +                    return Double.valueOf(System.getProperty("java.class.version"));}});
206 +        } catch (Throwable t) {
207 +            throw new Error(t);
208 +        }
209 +    }
210 +
211 +    public static boolean atLeastJava6() { return JAVA_CLASS_VERSION >= 50.0; }
212 +    public static boolean atLeastJava7() { return JAVA_CLASS_VERSION >= 51.0; }
213 +    public static boolean atLeastJava8() { return JAVA_CLASS_VERSION >= 52.0; }
214 +
215      /**
216       * Collects all JSR166 unit tests as one suite.
217       */
218      public static Test suite() {
219 <        return newTestSuite(
219 >        // Java7+ test classes
220 >        TestSuite suite = newTestSuite(
221              ForkJoinPoolTest.suite(),
222              ForkJoinTaskTest.suite(),
223              RecursiveActionTest.suite(),
# Line 243 | Line 282 | public class JSR166TestCase extends Test
282              TreeSetTest.suite(),
283              TreeSubMapTest.suite(),
284              TreeSubSetTest.suite());
285 +
286 +        // Java8+ test classes
287 +        if (atLeastJava8()) {
288 +            String[] java8TestClassNames = {
289 +                "StampedLockTest",
290 +                "ForkJoinPool8Test",
291 +            };
292 +            addNamedTestClasses(suite, java8TestClassNames);
293 +        }
294 +
295 +        return suite;
296      }
297  
298  
# Line 507 | Line 557 | public class JSR166TestCase extends Test
557      }
558  
559      /**
560 +     * A debugging tool to print all stack traces, as jstack does.
561 +     */
562 +    static void printAllStackTraces() {
563 +        for (ThreadInfo info :
564 +                 ManagementFactory.getThreadMXBean()
565 +                 .dumpAllThreads(true, true))
566 +            System.err.print(info);
567 +    }
568 +
569 +    /**
570       * Checks that thread does not terminate within the default
571       * millisecond delay of {@code timeoutMillis()}.
572       */
# Line 528 | Line 588 | public class JSR166TestCase extends Test
588      }
589  
590      /**
591 +     * Checks that the threads do not terminate within the default
592 +     * millisecond delay of {@code timeoutMillis()}.
593 +     */
594 +    void assertThreadsStayAlive(Thread... threads) {
595 +        assertThreadsStayAlive(timeoutMillis(), threads);
596 +    }
597 +
598 +    /**
599 +     * Checks that the threads do not terminate within the given millisecond delay.
600 +     */
601 +    void assertThreadsStayAlive(long millis, Thread... threads) {
602 +        try {
603 +            // No need to optimize the failing case via Thread.join.
604 +            delay(millis);
605 +            for (Thread thread : threads)
606 +                assertTrue(thread.isAlive());
607 +        } catch (InterruptedException ie) {
608 +            fail("Unexpected InterruptedException");
609 +        }
610 +    }
611 +
612 +    /**
613       * Checks that future.get times out, with the default timeout of
614       * {@code timeoutMillis()}.
615       */
# Line 601 | Line 683 | public class JSR166TestCase extends Test
683          SecurityManager sm = System.getSecurityManager();
684          if (sm == null) {
685              r.run();
686 +        }
687 +        runWithSecurityManagerWithPermissions(r, permissions);
688 +    }
689 +
690 +    /**
691 +     * Runs Runnable r with a security policy that permits precisely
692 +     * the specified permissions.  If there is no current security
693 +     * manager, a temporary one is set for the duration of the
694 +     * Runnable.  We require that any security manager permit
695 +     * getPolicy/setPolicy.
696 +     */
697 +    public void runWithSecurityManagerWithPermissions(Runnable r,
698 +                                                      Permission... permissions) {
699 +        SecurityManager sm = System.getSecurityManager();
700 +        if (sm == null) {
701              Policy savedPolicy = Policy.getPolicy();
702              try {
703                  Policy.setPolicy(permissivePolicy());
704                  System.setSecurityManager(new SecurityManager());
705 <                runWithPermissions(r, permissions);
705 >                runWithSecurityManagerWithPermissions(r, permissions);
706              } finally {
707                  System.setSecurityManager(null);
708                  Policy.setPolicy(savedPolicy);
# Line 653 | Line 750 | public class JSR166TestCase extends Test
750              return perms.implies(p);
751          }
752          public void refresh() {}
753 +        public String toString() {
754 +            List<Permission> ps = new ArrayList<Permission>();
755 +            for (Enumeration<Permission> e = perms.elements(); e.hasMoreElements();)
756 +                ps.add(e.nextElement());
757 +            return "AdjustablePolicy with permissions " + ps;
758 +        }
759      }
760  
761      /**
# Line 690 | Line 793 | public class JSR166TestCase extends Test
793      }
794  
795      /**
796 <     * Waits up to the specified number of milliseconds for the given
796 >     * Spin-waits up to the specified number of milliseconds for the given
797       * thread to enter a wait state: BLOCKED, WAITING, or TIMED_WAITING.
798       */
799      void waitForThreadToEnterWaitState(Thread thread, long timeoutMillis) {
800 <        long timeoutNanos = timeoutMillis * 1000L * 1000L;
698 <        long t0 = System.nanoTime();
800 >        long startTime = System.nanoTime();
801          for (;;) {
802              Thread.State s = thread.getState();
803              if (s == Thread.State.BLOCKED ||
# Line 704 | Line 806 | public class JSR166TestCase extends Test
806                  return;
807              else if (s == Thread.State.TERMINATED)
808                  fail("Unexpected thread termination");
809 <            else if (System.nanoTime() - t0 > timeoutNanos) {
809 >            else if (millisElapsedSince(startTime) > timeoutMillis) {
810                  threadAssertTrue(thread.isAlive());
811                  return;
812              }
# Line 905 | Line 1007 | public class JSR166TestCase extends Test
1007          }
1008      }
1009  
1010 +    public void await(Semaphore semaphore) {
1011 +        try {
1012 +            assertTrue(semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS));
1013 +        } catch (Throwable t) {
1014 +            threadUnexpectedException(t);
1015 +        }
1016 +    }
1017 +
1018   //     /**
1019   //      * Spin-waits up to LONG_DELAY_MS until flag becomes true.
1020   //      */
# Line 1173 | Line 1283 | public class JSR166TestCase extends Test
1283          }
1284      }
1285  
1286 <    @SuppressWarnings("unchecked")
1287 <    <T> T serialClone(T o) {
1286 >    void assertSerialEquals(Object x, Object y) {
1287 >        assertTrue(Arrays.equals(serialBytes(x), serialBytes(y)));
1288 >    }
1289 >
1290 >    void assertNotSerialEquals(Object x, Object y) {
1291 >        assertFalse(Arrays.equals(serialBytes(x), serialBytes(y)));
1292 >    }
1293 >
1294 >    byte[] serialBytes(Object o) {
1295          try {
1296              ByteArrayOutputStream bos = new ByteArrayOutputStream();
1297              ObjectOutputStream oos = new ObjectOutputStream(bos);
1298              oos.writeObject(o);
1299              oos.flush();
1300              oos.close();
1301 <            ByteArrayInputStream bin =
1302 <                new ByteArrayInputStream(bos.toByteArray());
1303 <            ObjectInputStream ois = new ObjectInputStream(bin);
1304 <            return (T) ois.readObject();
1301 >            return bos.toByteArray();
1302 >        } catch (Throwable t) {
1303 >            threadUnexpectedException(t);
1304 >            return new byte[0];
1305 >        }
1306 >    }
1307 >
1308 >    @SuppressWarnings("unchecked")
1309 >    <T> T serialClone(T o) {
1310 >        try {
1311 >            ObjectInputStream ois = new ObjectInputStream
1312 >                (new ByteArrayInputStream(serialBytes(o)));
1313 >            T clone = (T) ois.readObject();
1314 >            assertSame(o.getClass(), clone.getClass());
1315 >            return clone;
1316          } catch (Throwable t) {
1317              threadUnexpectedException(t);
1318              return null;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines