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