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 java.lang.reflect.InvocationTargetException; 27 28 import vm.runtime.defmeth.shared.Constants; 29 import vm.runtime.defmeth.shared.DefMethTest; 30 import vm.runtime.defmeth.shared.MemoryClassLoader; 31 import vm.runtime.defmeth.shared.data.Tester; 32 import java.util.*; 33 import nsk.share.Pair; 34 import static vm.runtime.defmeth.shared.Constants.*; 35 36 /** 37 * Run a single test with bytecode-generated asserts. 38 */ 39 public class GeneratedTest implements TestExecutor { 40 41 /** Assertions to check */ 42 private Collection<? extends Tester> tests; 43 44 /** Class loader to load all necessary classes for execution */ 45 private MemoryClassLoader cl; 46 47 private DefMethTest testInstance; 48 49 public GeneratedTest(MemoryClassLoader cl, DefMethTest testInstance, 50 Collection<? extends Tester> tests) { 51 this.cl = cl; 52 this.tests = tests; 53 this.testInstance = testInstance; 54 } 55 56 public GeneratedTest(MemoryClassLoader cl, DefMethTest testObj, 57 Tester... tests) { 58 this(cl, testObj, Arrays.asList(tests)); 59 } 60 61 @Override 62 public MemoryClassLoader getLoader() { 63 return cl; 64 } 65 66 /** 67 * Run individual assertion for the test by it's name. 68 * 69 * @param test 70 * @throws Throwable 71 */ 72 public void run(Tester test) throws Throwable { 73 try { 74 Class<?> clz = cl.loadClass(test.name()); 75 java.lang.reflect.Method m = clz.getMethod("test"); 76 m.invoke(null); 77 } catch (InvocationTargetException e) { 78 throw e.getCause(); 79 } 80 } 81 82 /** 83 * Check assertions from a test and return errors if any. 84 * 85 * @return 86 */ 87 public List<Pair<Tester,Throwable>> run() { 88 List<Pair<Tester,Throwable>> errors = new ArrayList<>(); 89 90 if (tests.isEmpty()) { 91 throw new IllegalStateException("No tests to run"); 92 } 93 94 for (Tester t : tests) { 95 StringBuilder msg = 96 new StringBuilder(String.format("\t%-30s: ", t.name())); 97 98 Throwable error = null; 99 try { 100 run(t); 101 102 msg.append("PASSED"); 103 } catch (Throwable e) { 104 error = e; 105 errors.add(Pair.of(t,e)); 106 msg.append("FAILED"); 107 } finally { 108 testInstance.getLog().info(msg.toString()); 109 if (error != null) { 110 testInstance.getLog().info("\t\t"+error.getMessage()); 111 if (PRINT_STACK_TRACE) { 112 error.printStackTrace(); 113 } 114 } 115 } 116 } 117 118 testInstance.addFailureCount(errors.isEmpty() ? 0 : 1); 119 return errors; 120 } 121 }