MethodMatcherTest.java revision 9032:0855eb2338ae
1/*
2 * Copyright (c) 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
24package compiler.compilercontrol.matcher;
25
26import jdk.test.lib.Pair;
27import compiler.compilercontrol.share.method.MethodDescriptor;
28import compiler.compilercontrol.share.method.MethodGenerator;
29import pool.PoolHelper;
30import sun.hotspot.WhiteBox;
31
32import java.lang.reflect.Executable;
33import java.util.ArrayList;
34import java.util.List;
35import java.util.concurrent.Callable;
36import java.util.regex.Matcher;
37import java.util.regex.Pattern;
38
39/*
40 * @test
41 * @bug 8135068
42 * @summary Tests CompilerCommand's method matcher
43 * @library /testlibrary /../../test/lib /compiler/whitebox ../share /
44 * @build MethodMatcherTest
45 * @run main ClassFileInstaller sun.hotspot.WhiteBox
46 *                              sun.hotspot.WhiteBox$WhiteBoxPermission
47 * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions
48 *           -XX:+WhiteBoxAPI compiler.compilercontrol.matcher.MethodMatcherTest
49 */
50public class MethodMatcherTest {
51    private static final WhiteBox WB = WhiteBox.getWhiteBox();
52    private static final PoolHelper POOL = new PoolHelper();
53    private static final List<Pair<Executable, Callable<?>>> METHODS =
54            POOL.getAllMethods();
55    private static final int AMOUNT = Integer.parseInt(System
56            .getProperty("test.amount", "25"));
57
58    public static void main(String[] args) {
59        MethodGenerator gen = new MethodGenerator();
60        List<Pair<Executable, Callable<?>>> testMethods =
61                POOL.getAllMethods(PoolHelper.METHOD_FILTER);
62        for (Pair<Executable, Callable<?>> pair : testMethods) {
63            for (int i = 0; i < AMOUNT; i++) {
64                MethodDescriptor md = gen.generateRandomDescriptor(pair.first);
65                check(md);
66            }
67        }
68    }
69
70    /**
71     * Check method matcher with given test case
72     *
73     * @param methodDescriptor method descriptor to check matcher's pattern
74     */
75    private static void check(MethodDescriptor methodDescriptor) {
76        System.out.println("Test case: " + methodDescriptor.getString());
77        System.out.println("Regex: " + methodDescriptor.getRegexp());
78        Pattern pattern = Pattern.compile(methodDescriptor.getRegexp());
79        boolean isValidDesc = methodDescriptor.isValid();
80        List<MethodDescriptor> failList = new ArrayList<>();
81        // walk through all methods in pool to check match with test pattern
82        for (Pair<Executable, Callable<?>> pair : METHODS) {
83            MethodDescriptor m = MethodGenerator.commandDescriptor(pair.first);
84            Matcher matcher = pattern.matcher(m.getCanonicalString());
85            // get expected result
86            MatcherResult expected;
87            if (isValidDesc) {
88                expected = matcher.matches() ?
89                        MatcherResult.MATCH : MatcherResult.NO_MATCH;
90            } else {
91                expected = MatcherResult.PARSING_FAILURE;
92            }
93            // get MethodMatcher's result
94            MatcherResult matchResult = MatcherResult.fromCode(WB.matchesMethod(
95                    pair.first, methodDescriptor.getString()));
96            // compare
97            if (matchResult != expected) {
98                System.out.printf("- Method: %s%n-- FAILED: result: %s, " +
99                                "but expected: %s%n", m.getCanonicalString(),
100                        matchResult, expected);
101                failList.add(m);
102            }
103        }
104        int size = failList.size();
105        if (size != 0) {
106            System.err.println("FAILED test case: " + methodDescriptor
107                    .getString());
108            if (size == METHODS.size()) {
109                System.err.println("-- All methods failed to match");
110            } else {
111                for (MethodDescriptor md : failList) {
112                    System.err.println("-- FAILED match: " + md.getString());
113                }
114            }
115            throw new AssertionError("FAIL: " + methodDescriptor.getString());
116        }
117        System.out.println("--PASSED");
118    }
119
120    /**
121     * Represents MethodMatcher's matching result
122     */
123    public enum MatcherResult {
124        PARSING_FAILURE(-1, "Parsing failed"),
125        NO_MATCH(0, "No match"),
126        MATCH(1, "Match");
127
128        public final int code;
129        private final String message;
130
131        private MatcherResult(int code, String message) {
132            this.code = code;
133            this.message = message;
134        }
135
136        public static MatcherResult fromCode(int code) {
137            switch (code) {
138                case -1: return PARSING_FAILURE;
139                case  0: return NO_MATCH;
140                case  1: return MATCH;
141                default:
142                    throw new IllegalArgumentException("MATCHER FAILURE:"
143                            + "Wrong code: " + code);
144            }
145        }
146
147        @Override
148        public String toString() {
149            return message;
150        }
151    }
152}
153