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.92 by jsr166, Sun Nov 18 18:03:11 2012 UTC vs.
Revision 1.105 by dl, Thu Mar 21 19:06:54 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 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 +                "CompletableFutureTest",
290 +                "ConcurrentHashMap8Test",
291 +                "CountedCompleterTest",
292 +                "DoubleAccumulatorTest",
293 +                "DoubleAdderTest",
294 +                "ForkJoinPool8Test",
295 +                "LongAccumulatorTest",
296 +                "LongAdderTest",
297 +                "StampedLockTest",
298 +            };
299 +            addNamedTestClasses(suite, java8TestClassNames);
300 +        }
301 +
302 +        return suite;
303      }
304  
305  
# Line 334 | Line 391 | public class JSR166TestCase extends Test
391  
392          if (Thread.interrupted())
393              throw new AssertionFailedError("interrupt status set in main thread");
394 +
395 +        checkForkJoinPoolThreadLeaks();
396 +    }
397 +
398 +    /**
399 +     * Find missing try { ... } finally { joinPool(e); }
400 +     */
401 +    void checkForkJoinPoolThreadLeaks() throws InterruptedException {
402 +        Thread[] survivors = new Thread[5];
403 +        int count = Thread.enumerate(survivors);
404 +        for (int i = 0; i < count; i++) {
405 +            Thread thread = survivors[i];
406 +            String name = thread.getName();
407 +            if (name.startsWith("ForkJoinPool-")) {
408 +                // give thread some time to terminate
409 +                thread.join(LONG_DELAY_MS);
410 +                if (!thread.isAlive()) continue;
411 +                thread.stop();
412 +                throw new AssertionFailedError
413 +                    (String.format("Found leaked ForkJoinPool thread test=%s thread=%s%n",
414 +                                   toString(), name));
415 +            }
416 +        }
417      }
418  
419      /**
# Line 507 | Line 587 | public class JSR166TestCase extends Test
587      }
588  
589      /**
590 +     * A debugging tool to print all stack traces, as jstack does.
591 +     */
592 +    static void printAllStackTraces() {
593 +        for (ThreadInfo info :
594 +                 ManagementFactory.getThreadMXBean()
595 +                 .dumpAllThreads(true, true))
596 +            System.err.print(info);
597 +    }
598 +
599 +    /**
600       * Checks that thread does not terminate within the default
601       * millisecond delay of {@code timeoutMillis()}.
602       */
# Line 623 | Line 713 | public class JSR166TestCase extends Test
713          SecurityManager sm = System.getSecurityManager();
714          if (sm == null) {
715              r.run();
716 +        }
717 +        runWithSecurityManagerWithPermissions(r, permissions);
718 +    }
719 +
720 +    /**
721 +     * Runs Runnable r with a security policy that permits precisely
722 +     * the specified permissions.  If there is no current security
723 +     * manager, a temporary one is set for the duration of the
724 +     * Runnable.  We require that any security manager permit
725 +     * getPolicy/setPolicy.
726 +     */
727 +    public void runWithSecurityManagerWithPermissions(Runnable r,
728 +                                                      Permission... permissions) {
729 +        SecurityManager sm = System.getSecurityManager();
730 +        if (sm == null) {
731              Policy savedPolicy = Policy.getPolicy();
732              try {
733                  Policy.setPolicy(permissivePolicy());
734                  System.setSecurityManager(new SecurityManager());
735 <                runWithPermissions(r, permissions);
735 >                runWithSecurityManagerWithPermissions(r, permissions);
736              } finally {
737                  System.setSecurityManager(null);
738                  Policy.setPolicy(savedPolicy);
# Line 675 | Line 780 | public class JSR166TestCase extends Test
780              return perms.implies(p);
781          }
782          public void refresh() {}
783 +        public String toString() {
784 +            List<Permission> ps = new ArrayList<Permission>();
785 +            for (Enumeration<Permission> e = perms.elements(); e.hasMoreElements();)
786 +                ps.add(e.nextElement());
787 +            return "AdjustablePolicy with permissions " + ps;
788 +        }
789      }
790  
791      /**

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines