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