1 /* 2 * Copyright (c) 2007, 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 package org.jemmy.action; 24 25 import org.jemmy.JemmyException; 26 27 28 /** 29 * 30 * @author shura, KAM 31 */ 32 public abstract class Action { 33 34 private boolean interrupted = false; 35 private long startTime = -1, endTime = 0, allowedTime = 0; 36 private Throwable throwable = null; 37 38 /** 39 * Executes {@linkplain #run(java.lang.Object[]) run()} method of this 40 * Action, saving the duration of its execution and storing any 41 * RuntimeException and Error which may occur during its work. 42 * @param parameters Parameters to pass to {@linkplain 43 * #run(java.lang.Object[]) run()} method 44 * @see #getThrowable() 45 * @see #failed() 46 */ 47 public final void execute(Object... parameters) { 48 startTime = System.currentTimeMillis(); 49 try { 50 run(parameters); 51 } catch (Error e) { 52 throwable = e; 53 throw e; 54 } catch (RuntimeException e) { 55 throwable = e; 56 throw e; 57 } catch (Exception e) { 58 throwable = e; 59 throw new JemmyException("Exception in action " + this.toString(), e); 60 } finally { 61 endTime = System.currentTimeMillis(); 62 } 63 } 64 65 /** 66 * 67 * @return 68 */ 69 public long getEndTime() { 70 return endTime; 71 } 72 73 /** 74 * 75 * @return 76 */ 77 public long getStartTime() { 78 return startTime; 79 } 80 81 /** 82 * Should be used from {@linkplain #run(java.lang.Object[]) run()) method 83 * to check whether execution time is withing allowed time 84 * @return true if difference between current time and start time is less 85 * then allowed time; false otherwice 86 */ 87 protected boolean withinAllowedTime() { 88 return System.currentTimeMillis() - startTime < allowedTime; 89 } 90 91 /** 92 * 93 * @return 94 */ 95 public long getAllowedTime() { 96 return allowedTime; 97 } 98 99 /** 100 * 101 * @param allowedTime 102 */ 103 public void setAllowedTime(long allowedTime) { 104 this.allowedTime = allowedTime; 105 } 106 107 /** 108 * 109 * @param parameters 110 */ 111 public abstract void run(Object... parameters) throws Exception; 112 113 /** 114 * 115 * @return 116 */ 117 public boolean isInterrupted() { 118 return interrupted; 119 } 120 121 /** 122 * 123 */ 124 public void interrupt() { 125 this.interrupted = true; 126 } 127 128 /** 129 * Returns throwable that occurred during run() invocation. 130 * @return Error or RuntimeException. 131 */ 132 public Throwable getThrowable() { 133 return throwable; 134 } 135 136 /** 137 * Indicates whether action invocation failed. 138 * @return true if some exception occurred during run() invocation. 139 */ 140 public boolean failed() { 141 return throwable != null; 142 } 143 144 /** 145 * Override this method to provide action description which 146 * will be printed into output. 147 * @return null If nothing should be printed into output. 148 */ 149 @Override 150 public String toString() { 151 return super.toString(); 152 } 153 }