1 /*
   2  * Copyright (c) 2014, 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 import com.oracle.testlibrary.jsr292.Helper;
  25 import java.lang.reflect.Method;
  26 import java.util.Collection;
  27 import java.util.function.Function;
  28 
  29 /**
  30  * Lambda forms caching test case class. Contains all necessary test routines to
  31  * test lambda forms caching in method handles returned by methods of
  32  * MethodHandles class.
  33  *
  34  * @author kshefov
  35  */
  36 public abstract class LambdaFormTestCase {
  37 
  38     private final static String METHOD_HANDLE_CLASS_NAME = "java.lang.invoke.MethodHandle";
  39     private final static String INTERNAL_FORM_METHOD_NAME = "internalForm";
  40 
  41     /**
  42      * Reflection link to {@code j.l.i.MethodHandle.internalForm} method. It is
  43      * used to get a lambda form from a method handle.
  44      */
  45     protected final static Method INTERNAL_FORM;
  46 
  47     static {
  48         try {
  49             Class mhClass = Class.forName(METHOD_HANDLE_CLASS_NAME);
  50             INTERNAL_FORM = mhClass.getDeclaredMethod(INTERNAL_FORM_METHOD_NAME);
  51             INTERNAL_FORM.setAccessible(true);
  52         } catch (Exception ex) {
  53             throw new Error("Unexpected exception: ", ex);
  54         }
  55     }
  56 
  57     private final TestMethods testMethod;
  58 
  59     /**
  60      * Test case constructor. Generates test cases with random method types for
  61      * given methods form {@code j.l.i.MethodHandles} class.
  62      *
  63      * @param testMethod A method from {@code j.l.i.MethodHandles} class which
  64      * returns a {@code j.l.i.MethodHandle}.
  65      */
  66     protected LambdaFormTestCase(TestMethods testMethod) {
  67         this.testMethod = testMethod;
  68     }
  69 
  70     public TestMethods getTestMethod() {
  71         return testMethod;
  72     }
  73 
  74     /**
  75      * Routine that executes a test case.
  76      */
  77     public abstract void doTest();
  78 
  79     /**
  80      * Runs a number of test cases defined by the size of testCases list.
  81      *
  82      * @param ctor constructor of LambdaFormCachingTest or its child classes
  83      * object.
  84      * @param testMethods list of test methods
  85      */
  86     public static void runTests(Function<TestMethods, LambdaFormTestCase> ctor, Collection<TestMethods> testMethods) {
  87         boolean passed = true;
  88         int testCounter = 0;
  89         int failCounter = 0;
  90         long iterations = Math.max(1, Helper.TEST_LIMIT / testMethods.size());
  91         for (long i = 0; i < iterations; i++) {
  92             System.err.println(String.format("Iteration %d:", i));
  93             for (TestMethods testMethod : testMethods) {
  94                 LambdaFormTestCase testCase = ctor.apply(testMethod);
  95                 try {
  96                     System.err.printf("Tested LF caching feature with MethodHandles.%s method.%n",
  97                             testCase.getTestMethod().name);
  98                     testCase.doTest();
  99                     System.err.println("PASSED");
 100                 } catch (Throwable t) {
 101                     t.printStackTrace();
 102                     System.err.println("FAILED");
 103                     passed = false;
 104                     failCounter++;
 105                 }
 106                 testCounter++;
 107             }
 108         }
 109         if (!passed) {
 110             throw new Error(String.format("%d of %d test cases FAILED! %n"
 111                     + "Rerun the test with the same \"-Dseed=\" option as in the log file!",
 112                     failCounter, testCounter));
 113         } else {
 114             System.err.println(String.format("All %d test cases PASSED!", testCounter));
 115         }
 116     }
 117 }