Skip Headers
SigTest User's Guide
Version 3.0
E19036-02
  Go To Table Of Contents
Contents
Go To Index
Index

Previous
Previous
 
Next
Next
 

B API Coverage Tool Quick Start Examples

This section contains step-by-step examples that show how to produce test coverage reports using the API Coverage tool. The examples are located in the following directory:

SIGTEST_HOME/examples/userguide/apicover

The appendix consists of the following examples. The examples build upon each other.

B.1 Example Procedures

The following procedures describes how to set up and use the API Coverage tool.

B.1.1 Set Up the API Coverage Tool Ant Build Script

  1. Make SIGTEST_HOME/examples/userguide/apicover the current directory.

  2. Make sure that the JAVA_HOME environment variable refers to JDK 5.0 or higher.

  3. Edit the following file:

    SIGTEST_HOME/examples/userguide/apicover/build.properties

    Set the junit.jar property to point to the junit.jar file. Obtain this file from version 3.7 or higher of the JUnit test harness product.

  4. Make sure Apache Ant 1.7 or higher is available and properly configured on your system.

B.1.2 Build API Coverage Tool Examples

  1. Run the ant command and specify the ”build” target.

    This command builds the following:

    • API classes

    • Example application (helloWorld.jar)

    • Test suite

    • API documentation (doc/ directory)

  2. Run the example application to verify that it built correctly.

    %java -jar helloWorld.jar

    The application's output should look like this:

    Good morning, Brazil!
    Good morning, USA!
    Good evening, China!
    Good afternoon, Czech Republic!
    Good afternoon, Russia!
    Good afternoon, United Kingdom!
    Good evening, India!
    Good afternoon, Mali!
    Hello, world!
    

B.1.3 Run the Example Test Suite

  1. Use the Ant ”test” target to run the test suite.

    %ant test

    The test suite runs and produces the following output:

    [junit] Running TestGreet
    [junit] Tests run: 4, Failures: 0, Errors: 1, Time elapsed: 0.219 sec
    [junit] Test TestGreet FAILED
    

B.1.4 Generate a Signature File for the Tested API

  1. Use the Ant ”sigtest” target to generate the signature file.

    %ant sigtest

    The ”sigtest” target executes the following command:

    java -jar sigtestdev.jar setup -filename reports/greetapi.sig -package com.sun.tdk.samples.helloworld.api -classpath helloWorld.jar:$JAVA_HOME/jre/lib/rt.jar

    This command generates the file reports/greetapi.sig. While the command runs you should see the following output:

    sigtest:
         [java] Class path: "helloWorld.jar:/opt/java/jdk1.5.0_16/jre/lib/rt.jar"
         [java] Constant checking: on
         [java] Found in total: 13217 classes
         [java] Selected by -Package: 5 classes
         [java] Written to sigfile: 8 classes
         [java] STATUS:Passed.
         [java] Java Result: 95
    

B.1.5 Use the API Coverage Tool to Calculate Test Coverage

  1. Use the Ant ”apicov” target to calculate static test coverage

    %ant apicov

B.1.6 Worst Case Mode

The script executes the following command:

java -jar apicover.jar -ts test_classes -api reports/greetapi.sig -apiinclude com.sun.tdk.samples.helloworld.api -mode w -report reports/cov-worst-case.txt

This mode reports that about of 37% of the API is covered by the tests. Some members that are defined in superclasses, such as finalize() or clone(), are marked as not covered. Some fields are also marked as not covered by the tests. The output written to a file named reports/cov-worst-case.txt and should appear as shown below.

Coverage Report

==========================================================================
Package                                 classes
     Class                                         members tested  %%
         Member
==========================================================================
com                                     4         33      14      42%
--------------------------------------------------------------------------
com.sun                                 4         33      14      42%
--------------------------------------------------------------------------
com.sun.tdk                             4         33      14      42%
--------------------------------------------------------------------------
com.sun.tdk.samples                     4         33      14      42%
--------------------------------------------------------------------------
com.sun.tdk.samples.helloworld          4         33      14      42%
--------------------------------------------------------------------------
com.sun.tdk.samples.helloworld.api      4         33      14      42%

    Greet                                         3       3       100%

    GreetFactory                                  6       1       16%
      - clone()
      - equals(Object)
      - finalize()
      - hashCode()
      - toString()

    Places                                        15      6       40%
      - getCountry()
      - toString()
      - valueOf(Class,String)
      - valueOf(String)
      - China
      - Czech
      - India
      - Mali
      - UK

    TimeOfDay                                      9      4       44%
      - valuOf(Class,String)
      - AFTERNOON
      - EVENING
      - MORNING
      - NIGHT
==========================================================================
Overall coverage                        4         33      14      42%
==========================================================================

Configuration
ts   test_classes
api  reports/greetapi.sig
mode w

B.1.7 Real World Mode

The script executes the following command:

java -jar apicover.jar -ts test_classes -api reports/greetapi.sig -apiinclude com.sun.tdk.samples.helloworld.api -mode r -report reports/cov-realworld.txt

This estimation does not take into account overridden members defined in superclasses. Overall coverage is 52%.

The report is named reports/cov-realworld.txt and is shown below.

Coverage Report

==========================================================================
Package                                 classes
    Class                                         members tested  %%
        Member
==========================================================================
com                                     4         25      14      56%
--------------------------------------------------------------------------
com.sun                                 4         25      14      56%
--------------------------------------------------------------------------
com.sun.tdk                             4         25      14      56%
--------------------------------------------------------------------------
com.sun.tdk.samples                     4         25      14      56%
--------------------------------------------------------------------------
com.sun.tdk.samples.helloworld          4         25      14      56%
--------------------------------------------------------------------------
com.sun.tdk.samples.helloworld.api      4         25      14      56%

    Greet                                         3       3       100%

    GreetFactory                                  1       1       100%

    Places                                        13      6       46%
      - getCountry()
      - valueOf(String)
      - Brazil
      - China
      - Czech
      - India
      - Mali
      - UK

    TimeOfDay                                     8       4       50%
      - AFTERNOON
      - EVENING
      - MORNING
      - NIGHT
==========================================================================
Overall coverage                        4         25      14      56%
==========================================================================

Configuration
ts   test_classes
api  reports/greetapi.sig
mode r

B.1.8 Real World Mode Without Fields and Enum Constants, detail Level 5

The script executes the following command:

java -jar apicover.jar -ts test_classes -api reports/greetapi.sig -apiinclude com.sun.tdk.samples.helloworld.api -mode r -excludeFields -detail 5 -report reports/cov-realworld-without-fields.txt

In this case test coverage is 84%.

The report is named reports/cov-realworld-without-fields.txt and is shown below.

Coverage Report

==========================================================================
Package                                 classes
    Class                                         members tested  %%
        Member                                                    count
==========================================================================
com                                     4         13      11      84%
--------------------------------------------------------------------------
com.sun                                 4         13      11      84%
--------------------------------------------------------------------------
com.sun.tdk                             4         13      11      84%
--------------------------------------------------------------------------
com.sun.tdk.samples                     4         13      11      84%
--------------------------------------------------------------------------
com.sun.tdk.samples.helloworld          4         13      11      84%
--------------------------------------------------------------------------
com.sun.tdk.samples.helloworld.api      4         13      11      84%

    Greet                                         3       3       100%
      + greetPlaces()                                               1
      + greetWorld()                                                1
      + setWriter(java.io.Writer)                                   1

    GreetFactory                                  1       1       100%
      + getInstance()                                               1

    Places                                        5       3       60%
      - getCountry()
      + getTz()                                                    1
      + shuffledValues()                                           1
      - valueOf(String)
      + values()                                                   2

    TimeOfDay                                     4       4       100%
      + getCurrentTime(java.util.TimeZone)                          1
      + toString()                                                  2
      + valueOf(String)                                             1
      + values()                                                    2
==========================================================================
Overall coverage                        4         13      11      84%
==========================================================================

Legend 
+ covered member
- uncovered member 

Configuration
ts            test_classes
api           reports/greetapi.sig
excludeFields yes
mode          r