Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/test/testlibrary_tests/TestMutuallyExclusivePlatformPredicates.java
32278 views
/*1* Copyright (c) 2014, 2015, 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*/2223import com.oracle.java.testlibrary.Asserts;24import com.oracle.java.testlibrary.Platform;2526import java.lang.reflect.InvocationTargetException;27import java.lang.reflect.Method;28import java.util.Arrays;29import java.util.Collections;30import java.util.EnumSet;31import java.util.HashSet;32import java.util.List;33import java.util.Set;3435/**36* @test37* @summary Verify that for each group of mutually exclusive predicates defined38* in com.oracle.java.testlibrary.Platform one and only one predicate39* evaluates to true.40* @library /testlibrary41* @run main TestMutuallyExclusivePlatformPredicates42*/43public class TestMutuallyExclusivePlatformPredicates {44private static enum MethodGroup {45ARCH("isARM", "isPPC", "isSparc", "isX86", "isX64", "isAArch64"),46BITNESS("is32bit", "is64bit"),47OS("isAix", "isLinux", "isSolaris", "isWindows", "isOSX"),48VM_TYPE("isClient", "isServer", "isGraal", "isMinimal"),49IGNORED("isEmbedded", "isDebugBuild", "shouldSAAttach",50"canPtraceAttachLinux", "canAttachOSX");5152public final List<String> methodNames;5354private MethodGroup(String... methodNames) {55this.methodNames = Collections.unmodifiableList(56Arrays.asList(methodNames));57}58}5960public static void main(String args[]) {61EnumSet<MethodGroup> notIgnoredMethodGroups62= EnumSet.complementOf(EnumSet.of(MethodGroup.IGNORED));6364notIgnoredMethodGroups.forEach(65TestMutuallyExclusivePlatformPredicates::verifyPredicates);6667TestMutuallyExclusivePlatformPredicates.verifyCoverage();68}6970/**71* Verifies that one and only one predicate method defined in72* {@link com.oracle.java.testlibrary.Platform}, whose name included into73* methodGroup will return {@code true}.74* @param methodGroup The group of methods that should be tested.75*/76private static void verifyPredicates(MethodGroup methodGroup) {77System.out.println("Verifying method group: " + methodGroup.name());78long truePredicatesCount = methodGroup.methodNames.stream()79.filter(TestMutuallyExclusivePlatformPredicates80::evaluatePredicate)81.count();8283Asserts.assertEQ(truePredicatesCount, 1L, String.format(84"Only one predicate from group %s should be evaluated to true "85+ "(Actually %d predicates were evaluated to true).",86methodGroup.name(), truePredicatesCount));87}8889/**90* Verifies that all predicates defined in91* {@link com.oracle.java.testlibrary.Platform} were either tested or92* explicitly ignored.93*/94private static void verifyCoverage() {95Set<String> allMethods = new HashSet<>();96for (MethodGroup group : MethodGroup.values()) {97allMethods.addAll(group.methodNames);98}99100for (Method m : Platform.class.getMethods()) {101if (m.getParameterCount() == 0102&& m.getReturnType() == boolean.class) {103Asserts.assertTrue(allMethods.contains(m.getName()),104"All Platform's methods with signature '():Z' should "105+ "be tested ");106}107}108}109110/**111* Evaluates predicate method with name {@code name} defined in112* {@link com.oracle.java.testlibrary.Platform}.113*114* @param name The name of a predicate to be evaluated.115* @return evaluated predicate's value.116* @throws java.lang.Error if predicate is not defined or could not be117* evaluated.118*/119private static boolean evaluatePredicate(String name) {120try {121System.out.printf("Trying to evaluate predicate with name %s%n",122name);123boolean value124= (Boolean) Platform.class.getMethod(name).invoke(null);125System.out.printf("Predicate evaluated to: %s%n", value);126return value;127} catch (NoSuchMethodException e) {128throw new Error("Predicate with name " + name129+ " is not defined in " + Platform.class.getName(), e);130} catch (IllegalAccessException | InvocationTargetException e) {131throw new Error("Unable to evaluate predicate " + name, e);132}133}134}135136137