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 
  24 /*
  25  * @test InlineMatcherTest
  26  * @bug 8074095
  27  * @modules java.base/jdk.internal.misc
  28  * @library /testlibrary /test/lib
  29  * @build sun.hotspot.WhiteBox
  30  * @run main ClassFileInstaller sun.hotspot.WhiteBox
  31  *                              sun.hotspot.WhiteBox$WhiteBoxPermission
  32  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI InlineMatcherTest
  33  * @summary Testing of compiler/InlineMatcher
  34  */
  35 
  36 import java.lang.reflect.Method;
  37 import java.util.ArrayList;
  38 import sun.hotspot.WhiteBox;
  39 
  40 public class InlineMatcherTest {
  41 
  42     /** Instance of WhiteBox */
  43     protected static final WhiteBox WHITE_BOX = WhiteBox.getWhiteBox();
  44 
  45     Method helper;
  46     Method getDate;
  47     Method inner;
  48     Method toString;
  49 
  50     final public static int FORCE_INLINE = 2;
  51     final public static int DONT_INLINE = 1;
  52     final public static int NO_MATCH = 0;
  53     final public static int PARSING_FAILURE = -1;
  54 
  55     public InlineMatcherTest() {
  56 
  57     }
  58 
  59     public void test() throws Exception {
  60         // instantiate before calling getMethod on innerHelper
  61         TestCases testCases = new TestCases();
  62 
  63         helper = getMethod(InlineMatcherTest.class, "helper");
  64 
  65         testCases.add(helper, "*.*", PARSING_FAILURE);
  66         testCases.add(helper, "+*.*", FORCE_INLINE);
  67         testCases.add(helper, "++*.*", NO_MATCH); // + is a valid part of the
  68                                                   // class name
  69         testCases.add(helper, "-*.*", DONT_INLINE);
  70         testCases.add(helper, "--*.*", NO_MATCH); // - is a valid part of the
  71                                                   // class name
  72 
  73         testCases.add(helper, "+InlineMatcherTest.*", FORCE_INLINE);
  74         testCases.add(helper, "+InlineMatcherTest.helper", FORCE_INLINE);
  75         testCases.add(helper, "+InlineMatcherTest.helper()", FORCE_INLINE);
  76         testCases.add(helper, "+InlineMatcherTest.helper()V", FORCE_INLINE);
  77         testCases.add(helper, "+InlineMatcherTest.helper(", FORCE_INLINE);
  78 
  79         testCases.add(helper, "-InlineMatcherTest.*", DONT_INLINE);
  80         testCases.add(helper, "-InlineMatcherTest.helper", DONT_INLINE);
  81         testCases.add(helper, "-InlineMatcherTest.helper()", DONT_INLINE);
  82         testCases.add(helper, "-InlineMatcherTest.helper()V", DONT_INLINE);
  83         testCases.add(helper, "-InlineMatcherTest.helper(", DONT_INLINE);
  84 
  85         testCases.add(helper, "+abc.*", NO_MATCH);
  86         testCases.add(helper, "+*.abc", NO_MATCH);
  87         testCases.add(helper, "-abc.*", NO_MATCH);
  88         testCases.add(helper, "-*.abcls ", NO_MATCH);
  89 
  90         int failures = 0;
  91 
  92         for (TestCase t : testCases) {
  93             System.out.println("Test case: " + t.pattern);
  94             if (!t.test()) {
  95                 failures++;
  96                 System.out.println(" * FAILED");
  97             }
  98         }
  99         if (failures != 0) {
 100             throw new Exception("There where " + failures + " failures in this test");
 101         }
 102     }
 103 
 104     public static void main(String... args) throws Exception {
 105         InlineMatcherTest test = new InlineMatcherTest();
 106         test.test();
 107     }
 108 
 109     public void helper() {
 110 
 111     }
 112 
 113     private static Method getMethod(Class klass, String name, Class<?>... parameterTypes) {
 114         try {
 115             return klass.getDeclaredMethod(name, parameterTypes);
 116         } catch (NoSuchMethodException | SecurityException e) {
 117             throw new RuntimeException("exception on getting method Helper." + name, e);
 118         }
 119     }
 120 
 121     class TestCase {
 122         String pattern;
 123         Method testTarget;
 124         int expectedResult;
 125 
 126         public TestCase(Method testTarget, String pattern, int expectedResult) {
 127             this.testTarget = testTarget;
 128             this.pattern = pattern;
 129             this.expectedResult = expectedResult;
 130         }
 131 
 132         public String resultAsStr(int errorCode) {
 133             switch (errorCode) {
 134             case PARSING_FAILURE:
 135                 return "Parsing failed";
 136             case NO_MATCH:
 137                 return "No match";
 138             case DONT_INLINE:
 139                 return "Dont Inline";
 140             case FORCE_INLINE:
 141                 return "Force Inline";
 142             default:
 143                 return "Unknown error";
 144             }
 145         }
 146 
 147         boolean test() {
 148             int result = WHITE_BOX.matchesInline(testTarget, pattern);
 149             if (result != expectedResult) {
 150                 System.out
 151                         .println("FAIL Wrong result, Got: " + resultAsStr(result) + "\n TestCase: " + this.toString());
 152                 return false;
 153             }
 154             return true;
 155         }
 156 
 157         @Override
 158         public String toString() {
 159             return "Method: '" + testTarget.toString() + "' Pattern: '" + pattern + "' Expected: "
 160                     + resultAsStr(expectedResult);
 161         }
 162 
 163         public void innerHelper() {
 164 
 165         }
 166     }
 167 
 168     class TestCases extends ArrayList<TestCase> {
 169         private static final long serialVersionUID = 1L;
 170 
 171         public boolean add(Method testTarget, String pattern, int expectedResult) {
 172             return super.add(new TestCase(testTarget, pattern, expectedResult));
 173         }
 174     }
 175 }