Class ExecutableSequence
Sequence
with functionality for executing the sequence, via
methods execute(ExecutionVisitor, TestCheckGenerator)
and execute(ExecutionVisitor, TestCheckGenerator, boolean)
. It also lets the client add Check
s that check expected behaviors of the execution.
An ExecutableSequence augments a sequence with three additional pieces of data:
- Execution results. An ExecutableSequence can be executed, and the results of the execution (meaning the objects created during execution, and any exceptions thrown) are made available to clients or execution visitors to inspect.
- Checks. A check is an object representing an expected runtime behavior of the
sequence. Clients can add checks to specific indices of the sequence. For example, a client
might add a
NotNull
check to the ith index of a sequence to signify that the value returned by the statement at index i should not be null. - Check evaluation results. Corresponding to every check is a boolean value that represents whether the check passed or failed during the last execution of the sequence.
An ExecutableSequence only directly manages the execution results. Other data, including
checks and check evaluation results, are added or removed by the client of the
ExecutableSequence. One way of doing this is by implementing an ExecutionVisitor
and
passing it as an argument to the execute
method.
-
Field Summary
Modifier and TypeFieldDescriptionprivate TestChecks<?>
The checks for the last statement in this sequence.The subsequences that were concatenated to create this sequence.long
How long it took to execute this sequence in nanoseconds.private Execution
Contains the runtime objects created and exceptions thrown (if any) during execution of this sequence.long
How long it took to generate this sequence in nanoseconds, excluding execution time.private boolean
Flag to record whether execution of sequence has a null input.private static ByteArrayOutputStream
Captures output from the executed sequence.private static PrintStream
Used to populateoutput_buffer
.The underlying sequence.private IdentityMultiMap<Object,
Variable> Maps a value to the set of variables that hold it. -
Constructor Summary
ConstructorDescriptionExecutableSequence
(Sequence sequence) Create an executable sequence that executes the given sequence. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addCoveredClass
(Class<?> c) Adds a covered class to the most recent execution results of this sequence.private void
addReferenceValue
(Variable variable, Object value, Set<ReferenceValue> refValues) If the variable has a non-String reference type, add its value to the set and also add a mapping tovariableMap
.boolean
coversClass
(Class<?> c) Indicates whether the given class is covered by the most recent execution of this sequence.boolean
void
execute
(ExecutionVisitor visitor, TestCheckGenerator gen) Executes sequence, stopping on exceptions.private void
execute
(ExecutionVisitor visitor, TestCheckGenerator gen, boolean ignoreException) Execute this sequence, invoking the given visitor as the execution unfolds.private static void
executeStatement
(Sequence s, List<ExecutionOutcome> outcome, int index, Object[] inputVariables) Returns all values computed in the sequence.TestChecks<?>
Return the set of test checks for the most recent execution.private int
getExceptionIndex
(Class<?> exceptionClass) Returns the list of (reference type) values created and used by the last statement of this sequence.int
Returns the index i for a non-executed statement, or -1 if there is no such index.int
Return the operation from which this sequence was generated -- the operation of the last statement of this sequence.getResult
(int index) This method is typically used by ExecutionVisitors.private Object[]
getRuntimeInputs
(List<ExecutionOutcome> outcome, List<Variable> inputs) Object[]
getRuntimeInputs
(List<Variable> inputs) private static Object[]
getRuntimeValuesForVars
(List<Variable> vars, List<ExecutionOutcome> execution) static Object[]
getRuntimeValuesForVars
(List<Variable> vars, Execution execution) Returns the values for the given variables in theExecution
object.private Object
getValue
(int index) The result of executing the index-th element of the sequence.getVariable
(Object value) Returns some variable that has the given value in the outcome of executing this sequence.getVariables
(Object value) Returns the set of variables that have the given value in the outcome of executing this sequence.boolean
Indicate whether checks are failing or passing.int
hashCode()
boolean
Indicate whether there are any invalid checks.boolean
Returns whether the sequence contains a non-executed statement.boolean
Indicates whether the executed sequence has any null input values.boolean
private boolean
isNormalExecution
(int i) This method is typically used by ExecutionVisitors.private void
reset()
Reset this object to its initial state.int
size()
Return the number of statements in this sequence.statementToCodeString
(int i) Return the code representation of the i'th statement.boolean
throwsException
(Class<?> exceptionClass) Return true if an exception of the given class (or a class compatible with it) was thrown during this sequence's executionReturn this sequence as code.Return this sequence as code.toString()
-
Field Details
-
sequence
The underlying sequence. -
checks
The checks for the last statement in this sequence. -
executionResults
Contains the runtime objects created and exceptions thrown (if any) during execution of this sequence. Invariant: sequence.size() == executionResults.size(). Transient because it can contain arbitrary objects that may not be serializable. -
gentime
public long gentimeHow long it took to generate this sequence in nanoseconds, excluding execution time. Must be directly set by the generator that creates this object. No code in this class sets its value. -
exectime
public long exectimeHow long it took to execute this sequence in nanoseconds. Is -1 until the sequence completes execution. -
hasNullInput
private boolean hasNullInputFlag to record whether execution of sequence has a null input.TODO: This is wonky, it really belongs to execution.
-
output_buffer
Captures output from the executed sequence. -
output_buffer_stream
Used to populateoutput_buffer
. -
variableMap
Maps a value to the set of variables that hold it. -
componentSequences
The subsequences that were concatenated to create this sequence.
-
-
Constructor Details
-
ExecutableSequence
Create an executable sequence that executes the given sequence.- Parameters:
sequence
- the underlying sequence for this executable sequence
-
-
Method Details
-
reset
private void reset()Reset this object to its initial state. -
toString
-
toCodeLines
Return this sequence as code. Similar toSequence.toCodeString()
except includes the checks.If for a given statement there is a check of type
ExceptionCheck
, that check's pre-statement code is printed immediately before the statement, and its post-statement code is printed immediately after the statement.- Returns:
- the sequence as a string
-
toCodeString
Return this sequence as code. Similar toSequence.toCodeString()
except includes the checks.If for a given statement there is a check of type
ExceptionCheck
, that check's pre-statement code is printed immediately before the statement, and its post-statement code is printed immediately after the statement.- Returns:
- the sequence as a string
-
statementToCodeString
Return the code representation of the i'th statement.- Parameters:
i
- the statement index- Returns:
- the string representation of the statement
-
execute
Executes sequence, stopping on exceptions.- Parameters:
visitor
- theExecutionVisitor
that collects checks from resultsgen
- the check generator for tests- See Also:
-
execute
Execute this sequence, invoking the given visitor as the execution unfolds. For example, the visitor may decorate the sequence withCheck
s about the execution.This method operates as follows:
- Clear execution results and call
visitor.initialize(this)
. - For each statement in the sequence:
- call
visitor.visitBefore(this, i)
- execute the i-th statement, using reflection
- call
visitor.visitAfter(this, i)
- call
- For the last statement, check its specifications (pre-, post-, and throws-conditions).
- All statements in the sequences have been executed.
- A pre-condition for the final statement fails
- A statement's execution results in an exception and
ignoreException==false
. - A
null
input value is implicitly passed to the statement (i.e., not via explicit declaration like x = null) - After executing the i-th statement and calling the visitor's
visitAfter
method, aContractViolation
check is present at index i.
After invoking this method, the client can query the outcome of executing each statement via the method
getResult(int)
.- Parameters:
visitor
- theExecutionVisitor
gen
- the initial check generator, which this augments then usesignoreException
- if true, ignore exceptions thrown before the last statement- Throws:
Error
- if execution of the sequence throws an exception andignoreException==false
- Clear execution results and call
-
getRuntimeInputs
-
getRuntimeInputs
-
getRuntimeValuesForVars
Returns the values for the given variables in theExecution
object. The variables areVariable
objects in theSequence
of thisExecutableSequence
object.- Parameters:
vars
- a list ofVariable
objectsexecution
- the object representing outcome of executing this sequence- Returns:
- array of values corresponding to variables
-
getRuntimeValuesForVars
private static Object[] getRuntimeValuesForVars(List<Variable> vars, List<ExecutionOutcome> execution) -
executeStatement
private static void executeStatement(Sequence s, List<ExecutionOutcome> outcome, int index, Object[] inputVariables) -
getResult
This method is typically used by ExecutionVisitors.The result of executing the index-th element of the sequence.
- Parameters:
index
- the statement index- Returns:
- the outcome of the statement at index
-
getChecks
Return the set of test checks for the most recent execution.- Returns:
- the
TestChecks
generated from the most recent execution
-
getValue
The result of executing the index-th element of the sequence.- Parameters:
index
- which element to obtain- Returns:
- the result of executing the index-th element of the sequence, if that element's execution completed normally
-
getLastStatementValues
Returns the list of (reference type) values created and used by the last statement of this sequence. Null output values are not included.- Returns:
- the list of values created and used by the last statement of this sequence
-
addReferenceValue
If the variable has a non-String reference type, add its value to the set and also add a mapping tovariableMap
.- Parameters:
variable
- the variable to use as a value in variableMapvalue
- the Java value to use as a key in variableMaprefValues
- the set of all reference values; is side-effected by this method
-
getAllValues
Returns all values computed in the sequence.- Returns:
- the list of values computed in the sequence
-
getVariables
Returns the set of variables that have the given value in the outcome of executing this sequence.- Parameters:
value
- the value- Returns:
- the set of variables that have the given value, or null if none
-
getVariable
Returns some variable that has the given value in the outcome of executing this sequence.- Parameters:
value
- the value- Returns:
- a variable that has the given value
-
isNormalExecution
private boolean isNormalExecution(int i) This method is typically used by ExecutionVisitors.- Parameters:
i
- the statement index to test for normal execution- Returns:
- true if execution of the i-th statement terminated normally
-
getNonNormalExecutionIndex
public int getNonNormalExecutionIndex() -
isNormalExecution
public boolean isNormalExecution() -
getExceptionIndex
- Parameters:
exceptionClass
- the exception thrown- Returns:
- the index in the sequence at which an exception of the given class (or a class compatible with it) was thrown. If no such exception, returns -1.
-
throwsException
Return true if an exception of the given class (or a class compatible with it) was thrown during this sequence's execution- Parameters:
exceptionClass
- the exception class- Returns:
- true if an exception compatible with the given class was thrown during this sequence's execution
-
hasNonExecutedStatements
public boolean hasNonExecutedStatements()Returns whether the sequence contains a non-executed statement. That happens if some statement before the last one throws an exception.- Returns:
- true if this sequence has non-executed statements, false otherwise
-
getNonExecutedIndex
public int getNonExecutedIndex()Returns the index i for a non-executed statement, or -1 if there is no such index. Note that a statement is considered executed even if it throws an exception.- Returns:
- the index of a non-executed statement in this sequence
-
hashCode
public int hashCode() -
equals
-
hasNullInput
public boolean hasNullInput()Indicates whether the executed sequence has any null input values.- Returns:
- true if there is a null input value in this sequence, false otherwise
-
hasFailure
public boolean hasFailure()Indicate whether checks are failing or passing.- Returns:
- true if checks are all failing, or false if all passing
-
hasInvalidBehavior
public boolean hasInvalidBehavior()Indicate whether there are any invalid checks.- Returns:
- true if the test checks have been set and are for invalid behavior, false otherwise
-
addCoveredClass
Adds a covered class to the most recent execution results of this sequence.- Parameters:
c
- the class covered by the execution of this sequence
-
coversClass
Indicates whether the given class is covered by the most recent execution of this sequence.- Parameters:
c
- the class to be covered- Returns:
- true if the class is covered by the sequence, false otherwise
-
getOperation
Return the operation from which this sequence was generated -- the operation of the last statement of this sequence.- Returns:
- the operation of the last statement of this sequence
-
size
public int size()Return the number of statements in this sequence.- Returns:
- the number of statements in this sequence
-