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  * @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 
 117     /**
 118      * Override this method to provide action description which
 119      * will be printed into output.
 120      * @return null If nothing should be printed into output.
 121      */
 122     @Override
 123     public String toString() {
 124         return super.toString();
 125     }
 126 }