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