1 /* 2 * Copyright (c) 2017, 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 jdk.test.lib.combo; 25 26 import javax.tools.StandardJavaFileManager; 27 import java.util.Optional; 28 29 /** 30 * This class is the common superclass of all combo test instances. It defines few helper methods 31 * to build compilation tasks using the shared context object, as well as entry points for 32 * signalling test failures. 33 */ 34 public abstract class ComboInstance<X extends ComboInstance<X>> { 35 36 /** The test instance result status. */ 37 private ResultStatus resultStatus = ResultStatus.PASSED; 38 39 /** The test instance execution environment. */ 40 private ComboTestHelper<X>.Env env; 41 42 /** 43 * Entry point for executing a combo test instance; first, the test environment is saved 44 * in the corresponding field, then the instance is run (see {@link ComboInstance#doWork()}. 45 * During execution, the result status will be updated to match the test outcome. 46 */ 47 final void run(ComboTestHelper<X>.Env env) { 48 try { 49 this.env = env; 50 doWork(); 51 if (resultStatus.isSuccess()) { 52 env.info().passCount++; 53 } 54 } catch (Throwable ex) { 55 resultStatus = ResultStatus.ERROR; 56 env.info().errCount++; 57 env.info().lastError = Optional.of(ex); 58 } finally { 59 this.env = null; 60 } 61 } 62 63 /** 64 * Retrieve a unique ID associated with this test instance. 65 */ 66 public int id() { 67 return env.info().comboCount; 68 } 69 70 /** 71 * Retrieve shared file manager. 72 */ 73 public StandardJavaFileManager fileManager() { 74 return env.fileManager(); 75 } 76 77 /** 78 * Create a new compilation task using shared compilation context. 79 */ 80 protected ComboTask newCompilationTask() { 81 return new ComboTask(env); 82 } 83 84 /** 85 * Main test execution entry point; subclasses must implement this method to define the test 86 * logic. 87 */ 88 protected abstract void doWork() throws Throwable; 89 90 /** 91 * Report a test failure. 92 */ 93 protected void fail() { 94 //dump some default info (such as dimension bindings) 95 fail("Combo instance failed; " + env.bindings); 96 } 97 98 /** 99 * Report a test failure with corresponding failure message. 100 */ 101 protected void fail(String msg) { 102 resultStatus = ResultStatus.FAILED; 103 env.info().failCount++; 104 env.info().lastFailure = Optional.of(msg); 105 } 106 107 /** 108 * The status associated with this test instance execution. 109 */ 110 enum ResultStatus { 111 /** Test passed. */ 112 PASSED(true), 113 /** Test failed. */ 114 FAILED(false), 115 /** Test thrown unexpected error/exception. */ 116 ERROR(false); 117 118 boolean success; 119 120 ResultStatus(boolean success) { 121 this.success = success; 122 } 123 124 boolean isSuccess() { 125 return success; 126 } 127 } 128 }