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