http://www.developer.com/

Back to article

Code Coverage for Free: How to Use EMMA


May 16, 2007

In this article you will learn how to configure Open Source framework EMMA to work with a web-based application.

Why Do You Need Code Coverage?

How do you know if the code you have written is being executed during regression or unit testing? One way is to use a debugger. Another way is to log every line of code. Yet another way is to compare the expected results of the test with the actual results. The latter usually suffices. All three methods are "somewhat" potent, but nonetheless, all three methods are not enough.

The first case will work in a small, non-repeatable test. The second method clutters your code and the verification process is labor intensive and ineffective. Only the third method is generally accepted by the IT and user community. The third method relies on the test author to cover ALL conditions and prepare test data in such a way that all test conditions are met. You put your trust into the test author's hands. Now, consider the following situation:

You are testing a method that accepts two parameters. It returns false if the first parameter is 1 or the second parameter is 1. Otherwise, the method returns true. The test author creates three test cases to cover three possible scenarios:

  1. A is 1; the expected result is false.
  2. B is 1; the expected result is false.
  3. Both A and B are not 1; the expected result is true.

Figure 1

For the test data, the test author creates three records corresponding to each case:

  1. A = 1, B = 0
  2. A = 0, B = 1
  3. A = 2, B = 2

The above test data in conjunction with three test cases covers all exit points of the returnSomething () method. Now, suppose the test author made a mistake and created the following test data:

  1. A = 1, B = 0
  2. A = 1, B = 1
  3. A = 2, B = 1

What happens now? The second test case return false, which matches our expected result, but Line 4 had never been executed. This is precisely why you need a code coverage tool.

Why EMMA?

I have chosen EMMA as a code coverage tool of choice because:

  1. It is open-source 100% Java.
  2. It has a large market share compared with the other open source coverage tools.
  3. Note: See other open source coverage tools here: http://java-source.net/open-source/code-coverage.
  4. It is easy to use (once you figure out how to use it).
  5. It has most of the features that one needs from the code coverage tool:
    1. It has multiple report type formats.
    2. It has class instrumentation that can be done offline and on the fly.
    3. It can be used in an enterprise environment.
    4. It has ANT support.
    5. It includes the ability to drill down to class, method, and lines of code.
  6. It is free for both open-source and commercial development.

Emma's home page is http://emma.sourceforge.net/.

How-To Overview

This overview explains how to configure EMMA for a web-based Java application. It entails:

  1. Recompilation of Java classes using the debug="on" option.
  2. Off-line instrumentation of application-specific Java classes by using the EMMA <instr> command.
  3. Extracting Java coverage dump from JVM by using the <ctl> ANT command.
  4. Building code coverage report by using the EMMA <report> command.

All development for this how-to is done within the IBM Rational Software Development Platform 6.0.1 and Websphere 5.1 test environment.

Get JAR Files

Emma jar files (emma.jar and emma_ant.jar) can be downloaded here: http://sourceforge.net/project/showfiles.php?group_id=108932&package_id=117768. You want to get version 2.1.5320 just to be on board with my example.

Import Emmarun.zip into Your Workspace

You can find a link to the download of EmmaRun.zip at the end of this article. The zip file contains one servlet (EmmaTestServlet.java), deployment descriptor (web.xml), one jsp file (input.jsp), and ANT files for running EMMA tools (1_emma_compile.xml, 2_emma-instument.xml, 3_emma_copyfiles.xml, 4_emma-extract-covarage.xml, and 5_emma_report.xml).

Go to FILE → IMPORT → ZIP FILE and follow further instructions on the screen.

Figure 2

Add JAR Files to Your Application Server Classpath

Add Emma.jar and emma_ant.jar to application server classpath Locate Application Server for your web application → Select Environment Tab → under Class Path, click on "Add External Jars", and add emma.jar and emma_ant.jar.

Figure 3

Recompile Classes for Code Coverage

Locate the 1_emma-compile.xml Ant script and execute it. The output should look like this:

Figure 4

This script compiles your EmmaTestServlet.java and places the class file into a emma_classes directory under WebContent/WEB-INF/. The only thing to note in this script is that you do compile with the debug="on" option.

Instrument Classes for Code Coverage

Locate the 2_emma-instument.xml Ant script and execute it. The output should look like this:

Figure 5

This script performs the following steps:

  1. It resets this demo project to a clean state.
  2. It turns on EMMA instrumentation.
  3. It runs the EMMA instrumentation <instr> by taking the class files produced by 1_emma-compile.xml ANT script and producing metadata.em instrumentation file and EMMA-instrumented class files.

Copy Classes

Locate the 3_emma-copyfiles.xml Ant script and execute it. This script copies instrumented class files from the "emma_intr_classes" directory to the "classes" directory. This is the output of the script:

Figure 6

Execute Your Example

Now that you have recompiled, instrumented, and copied class files to the classes directory, you can start executing your application. In this step, you will call your application several times. As I mentioned earlier, your test application consists of one servlet, EmmaTestServlet.java, and one jsp, input.jsp. The servlet accepts two parameters from the JSP and, based on these parameters, returns true or false. Repeat the same steps you saw at the beginning of the article:

  1. Parameter 1 = 1, Parameter 2 = 0
  2. Parameter 1 = 0, Parameter 2 = 1
  3. Parameter 1 = 2, Parameter 2 = 2

Figure 7

Extract Coverage Dump

Locate the 4_emma-extract-covarage.xml ANT Script and execute it. By using the <CTL> command, this script communicates with a JVM running EMMA-instrumented classes via a TCP socket. It downloads current coverage data from a JVM at a given TCP address and saves it into a local file, 'mycoverage.ec'. The output of the script should look like this:

Figure 8

Locate the 5_emma_report.xml ANT Script and execute it. This script executes a <report> EMMA command- offline coverage report generator. It reads in a 'metadata.em' class metadata (produced by the instrumentation step) and runtime 'mycoverage.ec' coverage data (produced by extracting the coverage dump step) and generates a coverage report. The execution output should look like this:

Figure 9

View Report

You finally can open the generated index.html file in the browser to review your results. The report shows that 100% of your code has been executed.

Figure 10 (overall coverage summary)

Figure 11 (coverage summary for source file [EmmaTestServlet.java])

Conclusion

The example that is explained above reveals only a part of EMMA's full functionality, but I hope it will help you get started with this great framework. The framework provides many customization options that you can read about in the help files provided by the author of the framework.

Download

Download the source code for this article here.

References

About the Author

Aleksey Shevchenko has been working with object-oriented languages for over seven years. For the past four years, he has served as a technical lead and a project manager. Aleksey has been implementing Enterprise IT solutions for Wall Street and the manufacturing and publishing industries.

Sitemap | Contact Us

Thanks for your registration, follow us on our social networks to keep up-to-date