1 /* 2 * Copyright (c) 2013, 2018, 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 package vm.runtime.defmeth.shared.executor; 25 26 import nsk.share.Pair; 27 import vm.runtime.defmeth.shared.Constants; 28 import vm.runtime.defmeth.shared.DefMethTest; 29 import vm.runtime.defmeth.shared.MemoryClassLoader; 30 import vm.runtime.defmeth.shared.Util; 31 import vm.runtime.defmeth.shared.builder.TestBuilder; 32 import vm.runtime.defmeth.shared.data.Clazz; 33 import vm.runtime.defmeth.shared.data.ParamValueExtractor; 34 import vm.runtime.defmeth.shared.data.Tester; 35 import vm.runtime.defmeth.shared.data.method.param.Param; 36 37 import java.util.ArrayList; 38 import java.util.Collection; 39 import java.util.List; 40 41 /** 42 * Commmon ancestor for reflection-based tests (Method.invoke & MethodHandle.invokeWithArguments). 43 * Encapsulates all necessary state for test execution and contains some utility methods. 44 */ 45 public abstract class AbstractReflectionTest implements TestExecutor { 46 protected MemoryClassLoader cl; 47 protected Collection<? extends Tester> tests; 48 protected DefMethTest testInstance; 49 50 public AbstractReflectionTest(DefMethTest testInstance, MemoryClassLoader cl, Collection<? extends Tester> tests) { 51 this.testInstance = testInstance; 52 this.cl = cl; 53 this.tests = tests; 54 } 55 56 @Override 57 public MemoryClassLoader getLoader() { 58 return cl; 59 } 60 61 protected Class[] paramType(String desc) throws ClassNotFoundException { 62 Pair<String[],String> p = Util.parseDesc(desc); 63 Class[] ptypes = new Class[p.first.length]; 64 for (int i = 0; i < ptypes.length; i++) { 65 ptypes[i] = Util.decodeClass(p.first[i], getLoader()); 66 } 67 return ptypes; 68 } 69 70 public Class resolve(Clazz clazz) { 71 try { 72 return cl.loadClass(clazz.name()); 73 } catch (ClassNotFoundException e) { 74 throw new Error(e); 75 } 76 } 77 78 protected Object[] values(Param[] params) { 79 Object[] result = new Object[params.length]; 80 for (int i = 0; i < result.length; i++) { 81 result[i] = new ParamValueExtractor(this, params[i]).value(); 82 } 83 return result; 84 } 85 86 public List<Pair<Tester,Throwable>> run() { 87 List<Pair<Tester,Throwable>> errors = new ArrayList<>(); 88 89 if (tests.isEmpty()) { 90 throw new IllegalStateException("No tests to run"); 91 } 92 93 for (Tester t : tests) { 94 StringBuilder msg = 95 new StringBuilder(String.format("\t%-30s: ", t.name())); 96 97 Throwable error = null; 98 try { 99 run(t); 100 101 msg.append("PASSED"); 102 } catch (Throwable e) { 103 error = e; 104 errors.add(Pair.of(t,e)); 105 msg.append("FAILED"); 106 } finally { 107 testInstance.getLog().info(msg.toString()); 108 if (error != null) { 109 //testInstance.getLog().info("\t\t"+error.getMessage()); 110 testInstance.getLog().info("\t\t"+error); 111 if (Constants.PRINT_STACK_TRACE) { 112 error.printStackTrace(); 113 } 114 } 115 } 116 } 117 118 testInstance.addFailureCount(errors.isEmpty() ? 0 : 1); 119 return errors; 120 } 121 }