Path: blob/master/test/jdk/java/lang/ClassLoader/IsParallelCapable.java
41149 views
/*1* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 8165793 816943526* @summary Test ClassLoader.isRegisteredAsParallelCapable() method27* @run main IsParallelCapable28*/2930import java.util.stream.Stream;3132public class IsParallelCapable {33public abstract static class TestCL extends ClassLoader {34static {35ClassLoader.registerAsParallelCapable();36}37public abstract boolean expectCapable();38public Class findClass(String name) throws ClassNotFoundException {39throw new ClassNotFoundException("Why are you using this?");40}41}4243public static class ParaCL extends TestCL {44static {45ClassLoader.registerAsParallelCapable();46}47@Override48public boolean expectCapable() { return true; }49}5051public static class NonParaCL extends TestCL {52@Override53public boolean expectCapable() {54// Doesn't call registerAsParallelCapable()55return false;56}57}5859public static class NonParaSubCL1 extends ParaCL {60@Override61public boolean expectCapable() {62// Doesn't call registerAsParallelCapable()63return false;64}65}6667public static class NonParaSubCL2 extends NonParaCL {68static {69ClassLoader.registerAsParallelCapable();70}71@Override72public boolean expectCapable() {73// Superclass is not parallel capable74return false;75}76}7778public static class ParaSubCL extends ParaCL {79static {80ClassLoader.registerAsParallelCapable();81}82@Override83public boolean expectCapable() { return true; }84}8586public static void main(String[] args) throws Exception {87if (!ClassLoader.getSystemClassLoader().isRegisteredAsParallelCapable()) {88throw new RuntimeException("System classloader not parallel capable!?");89}9091Stream.of(ParaCL.class,92NonParaCL.class,93NonParaSubCL1.class,94NonParaSubCL2.class,95ParaSubCL.class)96.forEach(IsParallelCapable::testClassLoaderClass);97}9899private static void testClassLoaderClass(Class<? extends TestCL> klazz) {100try {101TestCL cl = (TestCL)klazz.newInstance();102if (cl.expectCapable() != cl.isRegisteredAsParallelCapable()) {103throw new RuntimeException(klazz + " expectCapable: " +104cl.expectCapable() + ", isRegisteredAsParallelCapable: " +105cl.isRegisteredAsParallelCapable());106} else {107System.out.println(klazz + " passed");108}109} catch (InstantiationException | IllegalAccessException e) {110throw new RuntimeException(e);111}112}113}114115116