1 /*
2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 /*
25  * @test
26  * @bug 8165793 8169435
27  * @summary Test ClassLoader.isRegisteredAsParallelCapable() method
28  * @run main IsParallelCapable
29  */
30 
31 import java.util.stream.Stream;
32 
33 public class IsParallelCapable {
34     public abstract static class TestCL extends ClassLoader {
35         static {
ClassLoader.registerAsParallelCapable()36             ClassLoader.registerAsParallelCapable();
37         }
expectCapable()38         public abstract boolean expectCapable();
findClass(String name)39         public Class findClass(String name) throws ClassNotFoundException {
40             throw new ClassNotFoundException("Why are you using this?");
41         }
42     }
43 
44     public static class ParaCL extends TestCL {
45         static {
ClassLoader.registerAsParallelCapable()46             ClassLoader.registerAsParallelCapable();
47         }
48         @Override
expectCapable()49         public boolean expectCapable() { return true; }
50     }
51 
52     public static class NonParaCL extends TestCL {
53         @Override
expectCapable()54         public boolean expectCapable() {
55             // Doesn't call registerAsParallelCapable()
56             return false;
57         }
58     }
59 
60     public static class NonParaSubCL1 extends ParaCL {
61         @Override
expectCapable()62         public boolean expectCapable() {
63             // Doesn't call registerAsParallelCapable()
64             return false;
65         }
66     }
67 
68     public static class NonParaSubCL2 extends NonParaCL {
69         static {
ClassLoader.registerAsParallelCapable()70             ClassLoader.registerAsParallelCapable();
71         }
72         @Override
expectCapable()73         public boolean expectCapable() {
74             // Superclass is not parallel capable
75             return false;
76         }
77     }
78 
79     public static class ParaSubCL extends ParaCL {
80         static {
ClassLoader.registerAsParallelCapable()81             ClassLoader.registerAsParallelCapable();
82         }
83         @Override
expectCapable()84         public boolean expectCapable() { return true; }
85     }
86 
main(String[] args)87     public static void main(String[] args) throws Exception {
88         if (!ClassLoader.getSystemClassLoader().isRegisteredAsParallelCapable()) {
89             throw new RuntimeException("System classloader not parallel capable!?");
90         }
91 
92         Stream.of(ParaCL.class,
93                   NonParaCL.class,
94                   NonParaSubCL1.class,
95                   NonParaSubCL2.class,
96                   ParaSubCL.class)
97                 .forEach(IsParallelCapable::testClassLoaderClass);
98     }
99 
testClassLoaderClass(Class<? extends TestCL> klazz)100     private static void testClassLoaderClass(Class<? extends TestCL> klazz) {
101         try {
102             TestCL cl = (TestCL)klazz.newInstance();
103             if (cl.expectCapable() != cl.isRegisteredAsParallelCapable()) {
104                 throw new RuntimeException(klazz + " expectCapable: " +
105                         cl.expectCapable() + ", isRegisteredAsParallelCapable: " +
106                         cl.isRegisteredAsParallelCapable());
107             } else {
108                 System.out.println(klazz + " passed");
109             }
110         } catch (InstantiationException |  IllegalAccessException e) {
111             throw new RuntimeException(e);
112         }
113     }
114 }
115