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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package org.jemmy.action; 26 27 import org.jemmy.JemmyException; 28 29 30 /** 31 * 32 * @author shura, KAM 33 */ 34 public abstract class Action { 35 36 private boolean interrupted = false; 37 private long startTime = -1, endTime = 0, allowedTime = 0; 38 private Throwable throwable = null; 39 40 /** 41 * Executes {@linkplain #run(java.lang.Object[]) run()} method of this 42 * Action, saving the duration of its execution and storing any 43 * RuntimeException and Error which may occur during its work. 44 * @param parameters Parameters to pass to {@linkplain 45 * #run(java.lang.Object[]) run()} method 46 * @see #getThrowable() 47 * @see #failed() 48 */ 49 public final void execute(Object... parameters) { 50 startTime = System.currentTimeMillis(); 51 try { 52 run(parameters); 53 } catch (Error e) { 54 throwable = e; 55 throw e; 56 } catch (RuntimeException e) { 57 throwable = e; 58 throw e; 59 } catch (Exception e) { 60 throwable = e; 61 throw new JemmyException("Exception in action " + this.toString(), e); 62 } finally { 63 endTime = System.currentTimeMillis(); 64 } 65 } 66 67 /** 68 * 69 * @return 70 */ 71 public long getEndTime() { 72 return endTime; 73 } 74 75 /** 76 * 77 * @return 78 */ 79 public long getStartTime() { 80 return startTime; 81 } 82 83 /** 84 * Should be used from {@linkplain #run(java.lang.Object[]) run()) method 85 * to check whether execution time is withing allowed time 86 * @return true if difference between current time and start time is less 87 * then allowed time; false otherwice 88 */ 89 protected boolean withinAllowedTime() { 90 return System.currentTimeMillis() - startTime < allowedTime; 91 } 92 93 /** 94 * 95 * @return 96 */ 97 public long getAllowedTime() { 98 return allowedTime; 99 } 100 101 /** 102 * 103 * @param allowedTime 104 */ 105 public void setAllowedTime(long allowedTime) { 106 this.allowedTime = allowedTime; 107 } 108 109 /** 110 * 111 * @param parameters 112 */ 113 public abstract void run(Object... parameters) throws Exception; 114 115 /** 116 * 117 * @return 118 */ 119 public boolean isInterrupted() { 120 return interrupted; 121 } 122 123 /** 124 * 125 */ 126 public void interrupt() { 127 this.interrupted = true; 128 } 129 130 /** 131 * Returns throwable that occurred during run() invocation. 132 * @return Error or RuntimeException. 133 */ 134 public Throwable getThrowable() { 135 return throwable; 136 } 137 138 /** 139 * Indicates whether action invocation failed. 140 * @return true if some exception occurred during run() invocation. 141 */ 142 public boolean failed() { 143 return throwable != null; 144 } 145 146 /** 147 * Override this method to provide action description which 148 * will be printed into output. 149 * @return null If nothing should be printed into output. 150 */ 151 @Override 152 public String toString() { 153 return super.toString(); 154 } 155 }