Package randoop.test

Class ExceptionCheck

java.lang.Object
randoop.test.ExceptionCheck
All Implemented Interfaces:
Check
Direct Known Subclasses:
EmptyExceptionCheck, ExpectedExceptionCheck, InvalidExceptionCheck

public abstract class ExceptionCheck extends Object implements Check
An ExceptionCheck indicates that an exception is expected at a particular statement in a sequence.

When test code is generated in ExecutableSequence.toCodeString(), the methods toCodeStringPreStatement() and toCodeStringPostStatement() wrap the statement in a try-catch block for the exception, while the implementing classes define appendTryBehavior(StringBuilder) and appendCatchBehavior(StringBuilder) which handle differences in whether assertions are generated to enforce the expectation of the exception.

  • Field Details

    • exception

      protected final Throwable exception
      The thrown exception.
    • statementIndex

      final int statementIndex
      Indicates which statement results in the given exception.
    • catchClassName

      private String catchClassName
      The name of exception to be caught. This might be a supertype of exception's class C, for instance because C is private or because the method is declared to throw a superclass of C.
  • Constructor Details

    • ExceptionCheck

      protected ExceptionCheck(Throwable exception, int statementIndex, String catchClassName)
      Creates an exception check for the statement at the statement index. The generated code for this check will include a try-catch block with behaviors determined by implementing sub-classes.
      Parameters:
      exception - the exception expected at the statement index
      statementIndex - the position of the statement in a sequence
      catchClassName - the name of exception to be caught
  • Method Details

    • equals

      public boolean equals(Object o)
      Determines if two ExceptionCheck objects are equal. Assumes that implementing classes have no state.
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • toCodeStringPreStatement

      public final String toCodeStringPreStatement()
      Returns a string of Java source code to be emitted before a statement containing this check.

      The pre-statement prefix of the try-catch wrapper.

      Specified by:
      toCodeStringPreStatement in interface Check
      Returns:
      the string to be included before the statement
    • toCodeStringPostStatement

      public final String toCodeStringPostStatement()
      Returns a string of Java source code to be emitted after a statement containing this check.

      Returns the post-statement portion of the try-catch wrapper. Starts with post-statement try-behavior as determined by a subclass implementation of appendTryBehavior(java.lang.StringBuilder), and then closes with the catch clause with the body determined by the sub-class implementation of appendCatchBehavior(StringBuilder). Catches this exception or the closest public superclass of the exception.

      Specified by:
      toCodeStringPostStatement in interface Check
      Returns:
      the post-statement code text for the expected exception
    • appendCatchBehavior

      protected abstract void appendCatchBehavior(StringBuilder b)
      Appends code for catch block behavior corresponding to expected exception.
      Parameters:
      b - the string builder to which code text is to be added
    • appendTryBehavior

      protected abstract void appendTryBehavior(StringBuilder b)
      Appends code to follow the statement throwing expected exception in try block.
      Parameters:
      b - the string builder to which code text is added
    • getExceptionName

      public String getExceptionName()
      Returns the name of the exception class.
      Returns:
      the canonical name of the exception class
    • getException

      public Throwable getException()
      Returns the exception.
      Returns:
      the exception in this check