nz.ac.waikato.modeljunit.coverage
Class AbstractCoverage

java.lang.Object
  extended by nz.ac.waikato.modeljunit.AbstractListener
      extended by nz.ac.waikato.modeljunit.coverage.AbstractCoverage
All Implemented Interfaces:
CoverageMetric, ModelListener
Direct Known Subclasses:
ActionCoverage, StateCoverage, TransitionCoverage, TransitionPairCoverage

public abstract class AbstractCoverage
extends AbstractListener
implements CoverageMetric

A partial implementation of CoverageMetric. This maintains the map from Object to Integer, and calculates the coverage metrics from that. Note that currCoverage_ is the number of keys in the coverage_ map that have non-zero values. (The addItem and incrementItem methods maintain this invariant.)

Subclasses must implement the setGraph method so that it calls addItem for every item in the FSM graph. After doing this, it should also set maxCoverage_ to coverage_.size(). Subclasses must also implement doneTransition (and perhaps doneReset) so that it calls incrementCount(item) each time a coverage item is covered. Of course, they must also implement getName() and getDescription() so that this metric has a meaningful name and some documentation.


Field Summary
protected  java.util.Map<java.lang.Object,java.lang.Integer> coverage_
          Records the number of times each item has been covered.
protected  int currCoverage_
          The number of items that have been 'covered'.
protected  int maxCoverage_
          The maximum number of coverage items.
 
Fields inherited from class nz.ac.waikato.modeljunit.AbstractListener
model_
 
Constructor Summary
AbstractCoverage()
          This default constructor sets up the coverage_ map, sets maxCoverage=-1 (meaning unknown), and sets currCoverage_=0.
 
Method Summary
protected  void addItem(java.lang.Object item)
          Add a coverage item to the coverage_ map.
 void clear()
          Reset all coverage data.
 void doneReset(java.lang.String reason, boolean testing)
          The Model calls this after each reset(boolean) action.
 int getCoverage()
          The number of 'items' covered so far.
 java.util.Map<java.lang.Object,java.lang.Integer> getDetails()
          Details of which items have been covered and how many times.
 int getMaximum()
          The maximum number of items that can be covered.
 float getPercentage()
          The current coverage percentage.
protected  void incrementItem(java.lang.Object item)
          Increments the count of item in the coverage_ map.
 void setGraph(InspectableGraph model, java.util.Map<java.lang.Object,Vertex> state2vertex)
          A default implementation that does nothing.
 java.lang.String toString()
           
static Transition transition(Edge e, InspectableGraph model)
          A convenience method for converting a graph edge into a Transition.
 
Methods inherited from class nz.ac.waikato.modeljunit.AbstractListener
doneGuard, doneTransition, failure, getModel, setModel, startAction
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface nz.ac.waikato.modeljunit.coverage.CoverageMetric
getDescription
 
Methods inherited from interface nz.ac.waikato.modeljunit.ModelListener
doneGuard, doneTransition, failure, getModel, getName, setModel, startAction
 

Field Detail

coverage_

protected java.util.Map<java.lang.Object,java.lang.Integer> coverage_
Records the number of times each item has been covered. If possible, all changes to this field should be done via the methods reset, addItem and incrementItem, because they preserve the invariant that currCoverage_ is the number of non-zero entries in the coverage_ map.


maxCoverage_

protected int maxCoverage_
The maximum number of coverage items. This is typically -1 (which means unknown) until setModel is called, then it is equal to the number of items in the coverage_ map.


currCoverage_

protected int currCoverage_
The number of items that have been 'covered'. This is equal to the number of non-zero entries in coverage_.

Constructor Detail

AbstractCoverage

public AbstractCoverage()
This default constructor sets up the coverage_ map, sets maxCoverage=-1 (meaning unknown), and sets currCoverage_=0.

Method Detail

clear

public void clear()
Reset all coverage data. After calling this method, getCoverage() will return 0. Resetting the coverage does not imply that the model has changed, so the result of getMaximum() should be unchanged.

The default implementation iterates through the coverage_ map and resets all counts to zero, and also resets currCoverage_ to zero, to maintain the invariant.

Specified by:
clear in interface CoverageMetric

addItem

protected void addItem(java.lang.Object item)
Add a coverage item to the coverage_ map. If the item is already in the map, its value is left unchanged. If the item is new, its value is set to zero.

Parameters:
item - The object that is to be added to the coverage_ map.

incrementItem

protected void incrementItem(java.lang.Object item)
Increments the count of item in the coverage_ map. This should be called by doneTransition (and perhaps doneReset) whenever a coverage item is covered. If item was not already in the map, then it is added to the map and its value is set to one.

Parameters:
item - The object that has just been 'covered'.

getCoverage

public int getCoverage()
Description copied from interface: CoverageMetric
The number of 'items' covered so far. The definition of 'item' depends upon the kind of coverage that is being counted. For example, it could be states, or actions, or transitions, or transition-pairs, or steps, or test sequences etc.

Specified by:
getCoverage in interface CoverageMetric

getMaximum

public int getMaximum()
The maximum number of items that can be covered. For some coverage metrics, this may return -1 until the FSM graph is completely built.

Specified by:
getMaximum in interface CoverageMetric

getPercentage

public float getPercentage()
Description copied from interface: CoverageMetric
The current coverage percentage. This is equivalent to (100.0 * getCoverage()) / getMaximum(). So the result will be a large negative number before setModel is called, or for coverage metrics that have no maximum coverage.

Specified by:
getPercentage in interface CoverageMetric

getDetails

public java.util.Map<java.lang.Object,java.lang.Integer> getDetails()
Description copied from interface: CoverageMetric
Details of which items have been covered and how many times. Coverage metrics that cannot provide this level of detail will return null.

The type of objects in the domain of the result map will depend upon the kind of coverage (Action, Transition, TransitionPair etc.). However, all of them should provide a useful toString() method, so that you can print coverage results. A typical use of this method is to iterate through the result map and print all the objects that map to zero, because they have not been covered.

Specified by:
getDetails in interface CoverageMetric
Returns:
Map of how many times each object has been covered, or null.

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

transition

public static Transition transition(Edge e,
                                    InspectableGraph model)
A convenience method for converting a graph edge into a Transition.


doneReset

public void doneReset(java.lang.String reason,
                      boolean testing)
The Model calls this after each reset(boolean) action.

The default implementation does nothing.

Specified by:
doneReset in interface ModelListener
Overrides:
doneReset in class AbstractListener
Parameters:
reason - An adjective that describe why the reset was done.
testing - The parameter that was passed to the FsmModel reset call.

setGraph

public void setGraph(InspectableGraph model,
                     java.util.Map<java.lang.Object,Vertex> state2vertex)
A default implementation that does nothing.

Specified by:
setGraph in interface CoverageMetric


Copyright © 2009 ModelJUnit Project. All Rights Reserved.