1 /*
   2  * Copyright (c) 2014, 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 import jdk.test.lib.Asserts;
  25 import jdk.test.lib.Platform;
  26 
  27 import java.lang.reflect.InvocationTargetException;
  28 import java.lang.reflect.Method;
  29 import java.util.Arrays;
  30 import java.util.Collections;
  31 import java.util.EnumSet;
  32 import java.util.HashSet;
  33 import java.util.List;
  34 import java.util.Set;
  35 
  36 /**
  37  * @test
  38  * @summary Verify that for each group of mutually exclusive predicates defined
  39  *          in jdk.test.lib.Platform one and only one predicate
  40  *          evaluates to true.
  41  * @library /test/lib
  42  * @modules java.base/jdk.internal.misc
  43  *          java.management
  44  * @run main TestMutuallyExclusivePlatformPredicates
  45  */
  46 public class TestMutuallyExclusivePlatformPredicates {
  47     private static enum MethodGroup {
  48         ARCH("isARM", "isPPC", "isSparc", "isX86", "isX64", "isAArch64"),
  49         BITNESS("is32bit", "is64bit"),
  50         OS("isAix", "isLinux", "isOSX", "isSolaris", "isWindows"),
  51         VM_TYPE("isClient", "isServer", "isGraal", "isMinimal", "isZero", "isEmbedded"),
  52         MODE("isInt", "isMixed", "isComp"),
  53         IGNORED("isDebugBuild", "isFastDebugBuild", "isSlowDebugBuild",
  54                 "shouldSAAttach", "canPtraceAttachLinux", "canAttachOSX",
  55                 "isTieredSupported");
  56 
  57         public final List<String> methodNames;
  58 
  59         private MethodGroup(String... methodNames) {
  60             this.methodNames = Collections.unmodifiableList(
  61                     Arrays.asList(methodNames));
  62         }
  63     }
  64 
  65     public static void main(String args[]) {
  66         EnumSet<MethodGroup> notIgnoredMethodGroups
  67                 = EnumSet.complementOf(EnumSet.of(MethodGroup.IGNORED));
  68 
  69         notIgnoredMethodGroups.forEach(
  70                 TestMutuallyExclusivePlatformPredicates::verifyPredicates);
  71 
  72         TestMutuallyExclusivePlatformPredicates.verifyCoverage();
  73     }
  74 
  75     /**
  76      * Verifies that one and only one predicate method defined in
  77      * {@link jdk.test.lib.Platform}, whose name included into
  78      * methodGroup will return {@code true}.
  79      * @param methodGroup The group of methods that should be tested.
  80      */
  81     private static void verifyPredicates(MethodGroup methodGroup) {
  82         System.out.println("Verifying method group: " + methodGroup.name());
  83         long truePredicatesCount = methodGroup.methodNames.stream()
  84                 .filter(TestMutuallyExclusivePlatformPredicates
  85                         ::evaluatePredicate)
  86                 .count();
  87 
  88         Asserts.assertEQ(truePredicatesCount, 1L, String.format(
  89                 "Only one predicate from group %s should be evaluated to true "
  90                         + "(Actually %d predicates were evaluated to true).",
  91                 methodGroup.name(), truePredicatesCount));
  92     }
  93 
  94     /**
  95      * Verifies that all predicates defined in
  96      * {@link jdk.test.lib.Platform} were either tested or
  97      * explicitly ignored.
  98      */
  99     private static void verifyCoverage() {
 100         Set<String> allMethods = new HashSet<>();
 101         for (MethodGroup group : MethodGroup.values()) {
 102             allMethods.addAll(group.methodNames);
 103         }
 104 
 105         for (Method m : Platform.class.getMethods()) {
 106             if (m.getParameterCount() == 0
 107                     && m.getReturnType() == boolean.class) {
 108                 Asserts.assertTrue(allMethods.contains(m.getName()),
 109                         "All Platform's methods with signature '():Z' should "
 110                                 + "be tested ");
 111             }
 112         }
 113     }
 114 
 115     /**
 116      * Evaluates predicate method with name {@code name} defined in
 117      * {@link jdk.test.lib.Platform}.
 118      *
 119      * @param name The name of a predicate to be evaluated.
 120      * @return evaluated predicate's value.
 121      * @throws java.lang.Error if predicate is not defined or could not be
 122      *                         evaluated.
 123      */
 124     private static boolean evaluatePredicate(String name) {
 125         try {
 126             System.out.printf("Trying to evaluate predicate with name %s%n",
 127                     name);
 128             boolean value
 129                     = (Boolean) Platform.class.getMethod(name).invoke(null);
 130             System.out.printf("Predicate evaluated to: %s%n", value);
 131             return value;
 132         } catch (NoSuchMethodException e) {
 133             throw new Error("Predicate with name " + name
 134                     + " is not defined in " + Platform.class.getName(), e);
 135         } catch (IllegalAccessException | InvocationTargetException e) {
 136             throw new Error("Unable to evaluate predicate " + name, e);
 137         }
 138     }
 139 }