Active Application Model
WP
Active Application Model (aka Trace Director)
T2 allows an
application model to be specified. Such a model express assumptions about the proper use of a target class C by its environment (e.g. application, hence the name 'application model'). An application model can be encoded as a predicate, and is inserted into the class invariant. Using a predicate to express it is more abstract, but we may have a problem in generating sequences that satisfy that predicate.
So, as an alternative T2 allows an application model to be specified 'imperatively'. In such a specification we specify explicitly what are the set of allowed methods to call at a particular state. This is called
active application model or, as it is called in T2, a
trace director. It is activated by the --customtracedir option. We of course have to additionally write the director itself.
Here is a simple example. We define a class. Its trace director is encoded in the private method
customTraceDirector which has to return a list of
Method objects. Whenever T2 wants to generate a test step, it will call the director; so the director decides what choices are for the next method to call. If there is more that one choice, T2 will select one randomly.
In the example below, the trace director will impose that the two methods of
Example will be called alternatingly, something which is hard to achieve by relying on pure random generation.
import java.lang.reflect.* ;
import java.util.* ;
public class Example{
public void m1(int x) { ... }
public void m2(boolean b) { ... }
// Variables to hold meta-representations of m1 and m2:
static private Method META_m1 ;
static private Method META_m2 ;
{
try {
META_m1 = Example.class.getDeclaredMethod("m1", new Class[] { Integer.TYPE } ) ;
META_m2 = Example.class.getDeclaredMethod("m2", new Class[] { Boolean.TYPE } ) ;
}
catch (Exception e) { }
}
private boolean m1turn = true ;
private LinkedList<Method> nextsteps = new LinkedList<Method>();
// This custor director will enforce m1 and m2 to be called alternatingly
private List<Method> customTraceDirector() {
nextsteps.clear() ;
if (m1turn) nextsteps.add(META_m1) ;
else nextsteps.add(META_m2) ;
m1turn = !m1turn ;
return nextsteps;
}
}