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