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 | 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 * @author shura, KAM 32 */ 33 public abstract class Action { 34 35 private boolean interrupted = false; 36 private long startTime = -1, endTime = 0, allowedTime = 0; 37 private Throwable throwable = null; 38 39 /** 40 * Executes {@linkplain #run(java.lang.Object[]) run()} method of this 41 * Action, saving the duration of its execution and storing any 42 * RuntimeException and Error which may occur during its work. 43 * @param parameters Parameters to pass to {@linkplain #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 public long getEndTime() { 66 return endTime; 67 } 68 69 public long getStartTime() { 70 return startTime; 71 } 72 73 /** 74 * Should be used from {@linkplain #run(java.lang.Object[]) run()} method 75 * to check whether execution time is withing allowed time 76 * @return true if difference between current time and start time is less 77 * then allowed time; false otherwice 78 */ 79 protected boolean withinAllowedTime() { 80 return System.currentTimeMillis() - startTime < allowedTime; 81 } 82 83 public long getAllowedTime() { 84 return allowedTime; 85 } 86 87 public void setAllowedTime(long allowedTime) { 88 this.allowedTime = allowedTime; 89 } 90 91 public abstract void run(Object... parameters) throws Exception; 92 93 public boolean isInterrupted() { 94 return interrupted; 95 } 96 97 public void interrupt() { 98 this.interrupted = true; 99 } 100 101 /** 102 * Returns throwable that occurred during run() invocation. 103 * @return Error or RuntimeException. 104 */ 105 public Throwable getThrowable() { 106 return throwable; 107 } 108 109 /** 110 * Indicates whether action invocation failed. 111 * @return true if some exception occurred during run() invocation. 112 */ 113 public boolean failed() { 114 return throwable != null; 115 } 116 |