• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.testng;
2 
3 /**
4  * The root exception for special skip handling. In case a @Test or @Configuration
5  * throws this exception the method will be considered a skip or a failure according to the
6  * return of {@link #isSkip()}.
7  * Users may provide extensions to this mechanism by extending this class.
8  *
9  * @author <a href='mailto:the[dot]mindstorm[at]gmail[dot]com'>Alex Popescu</a>
10  * @since 5.6
11  */
12 public class SkipException extends RuntimeException {
13   private static final long serialVersionUID = 4052142657885527260L;
14   private StackTraceElement[] m_stackTrace;
15   private volatile boolean m_stackReduced;
16 
SkipException(String skipMessage)17   public SkipException(String skipMessage) {
18     super(skipMessage);
19   }
20 
SkipException(String skipMessage, Throwable cause)21   public SkipException(String skipMessage, Throwable cause) {
22     super(skipMessage, cause);
23   }
24 
25   /**
26    * Flag if the current exception marks a skipped method (<tt>true</tt>)
27    * or a failure (<tt>false</tt>). By default Subclasses should override this method
28    * in order to provide smarter behavior.
29    *
30    * @return <tt>true</tt> if the method should be considered a skip,
31    *    <tt>false</tt> if the method should be considered failed. If not
32    *    overwritten it returns <tt>true</tt>
33    */
isSkip()34   public boolean isSkip() {
35     return true;
36   }
37 
38   /**
39    * Subclasses may use this method to reduce the printed stack trace.
40    * This method keeps only the last frame.
41    * <b>Important</b>: after calling this method the preserved internally
42    * and can be restored called {@link #restoreStackTrace}.
43    */
reduceStackTrace()44   protected void reduceStackTrace() {
45     if(!m_stackReduced) {
46       synchronized(this) {
47         StackTraceElement[] newStack= new StackTraceElement[1];
48         StackTraceElement[] originalStack= getStackTrace();
49         if(originalStack.length > 0) {
50           m_stackTrace= originalStack;
51           newStack[0]= getStackTrace()[0];
52           setStackTrace(newStack);
53         }
54         m_stackReduced= true;
55       }
56     }
57   }
58 
59   /**
60    * Restores the original exception stack trace after a
61    * previous call to {@link #reduceStackTrace()}.
62    *
63    */
restoreStackTrace()64   protected void restoreStackTrace() {
65     if(m_stackReduced && null != m_stackTrace) {
66       synchronized(this) {
67         setStackTrace(m_stackTrace);
68         m_stackReduced= false;
69       }
70     }
71   }
72 }
73