Code Coverage for Java
When analyzing Java source code, Code Coverage can provide the following block coverage:
Statement Blocks
Statement Blocks and Decisions
Statement Blocks, Decisions, and Loops
Statement blocks are the Java method blocks, blocks introduced by control instructions:
THEN for IF and ELSE for IF, WHILE and DO ... WHILE blocks
non-empty blocks introduced by SWITCH CASE or DEFAULT statements
true and false outcomes of ternary expressions (<expr> ? <expr> : <expr>)
TRY blocks and any associated catch handler
blocks following a potentially terminal statement.
Example
public class StatementBlocks
{
public static void func( String _message )
throws UnsupportedOperationException
{
throw new UnsupportedOperationException(_message);
}
public static void main( String[] args )
throws Exception
{
try {
if ( false )
{
func( "Hello" );
}
else
{
throw new Exception("bad luck");
}
}
catch ( UnsupportedOperationException _E )
{
System.out.println( _E.toString() );
}
catch ( Exception _E )
{
System.out.println( _E.toString() );
throw _E ;
} //potentially terminal statement
return ; //sequence block
}
}
Each simple block is a branch. Every Java method contains at least one simple block corresponding to its main body.
Implicit blocks are introduced by IF statements without an ELSE statement, and a SWITCH statement without a DEFAULT statement.
Example
public class MathOp
{
static final int WHITE=0;
static final int LIGHTGRAY=1;
static final int RED=2;
static final int PINK=3;
static final int BLUE=4;
static final int GREEN=5;
// power of 10
public static int powerOf10( int _value, int _max )
{
int result = _value, i;
if( _value==0 ) return 0; //implicit else
for( i = 0; i < 10; i++ )
{
result = ( _max / 10 ) < result ? 10*result : _max ;
}
return result;
}
// Near color function
int nearColor( int _color )
{
switch( _color )
{
case WHITE:
case LIGHTGRAY:
return WHITE ;
case RED:
case PINK:
return RED;
//implicit default:
}
return _color ;
}
}
Each implicit block represents a branch.
Since the sum of all possible decision paths includes implicit blocks as well as simple blocks, reports provide the total number of simple and implicit blocks as a figure and a percentage after the term decisions.
Three branches are created in a FOR or WHILE loop:
The first branch is the simple block contained within the loop, and that is executed zero times (the entry condition is false from the start).
The second branch is the simple block executed exactly once (entry condition true, then false the next time).
The third branch is the simple block executed at least twice (entry condition true at least twice, and false at the end).
Two branches are created in a DO/WHILE loop, as the output condition is tested after the block has been executed:
The first branch is the simple block executed exactly once (output condition false the first time).
The second branch is the simple block executed at least twice (output condition false at least once, then true at the end).
Example
public class LogicalBlocks
{
public static int tryFiveTimes()
{
int result, i=0;
while ( ( ( result=resourcesAvailable() )<= 0)
&& ( ++i < 5 ) );
// while define 3 logical blocks
return result;
}
public static int resourcesAvailable()
{
return (_free_resources_++);
}
public static int _free_resources_=0;
public static void main( String[] argv )
{
//first call: '0 loop' block is reach
_free_resources_=1;
tryFiveTimes();
//second call: '1 loop' blocks are reach
_free_resources_=0;
tryFiveTimes();
//third call: '2 loops or more' blocks are reach
_free_resources_=-10;
tryFiveTimes();
}
}
Related Topics