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