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