Thursday, October 28, 2010

Eclipse tips - Conditional Breakpoints

The traditional waydebug_educational_software_510925

Sometimes in debugging we want to trace the code at a breakpoint under certain conditions, so what we do is every time this breakpoint is hit we check manually if this the case we want to trace or not.
 

Eclipse way !

Eclipse has a neat feature called conditional breakpoint. This feature enables you to enable the breakpoint if certain conditions is met. Now each time the breakpoint is reached, the expression is evaluated in the context of the breakpoint execution, and the breakpoint is either ignored or honored.

Example

Suppose we have this sample code
   1:  public static void initialize() 
   2:  {
   3:      final List activities = getActivities();
   4:   
   5:     for (Iterator i = activities.iterator(); i.hasNext();) 
   6:     {
   7:        final NestedActivity nestedActivity=(NestedActivity) i.next();
   8:        final SmallActivities[] smallActivities=nestedActivity.
   9:                                getSmallActivities();
  10:   
  11:        doSomething(nestedActivity, smallActivities);
  12:     }
  13:  }



And we want to put a conditional breakpoint at line 11 before executing the doSomething(..) method to trace a nestedActivity called debugging, so here is what we do:

  • Create breakpoint at line
  • Right click at the breakpiont then breakpoint properties
  • The properties of the breakpoint window will open
image
  • Check Enable condition
  • Write In the Condition field the expression for the breakpoint condition (notice the code assistant)
  • Do one of the following:
    • If you want the breakpoint to stop every time the condition evaluates to true, select the condition is 'true' option.  The expression provided must be a Boolean expression.
    • If you want the breakpoint to stop only when the result of the condition changes, select the value of condition changes option.

Saturday, October 16, 2010

The code is breathing - ep1

2003-06-17

Test Coverage tools (Cobertura)

The test coverage tools are used to ensure that all code paths are tested. It calculates the percentage of code called by tests. It also gives reports of which parts aren’t tested.

Cobertura – Hello World

As an example we will take the Cobertura as an example. As it’s free, open source and integrated with JUnit and Ant.

Installing Cobertura

Running the example that comes with it

  • It came with an example of simple calculator that square and sum numbers
  • In order to run the example you should install the JUnit & Ant & add their values in the classpath and path
  • open the example’s directory from the command line and enter ant
  • Now all the magic happens!! it compiles, run the tests and generate the reports

Now let’s take a look at the reports

image

It shows the percentage of overall the packages and for each class alone. The best thing is yet to come. By clicking on the class name it shows the class code and highlight the path which didn’t get accessed by the test cases in red and which get accessed in green. It also shows a number beside it which shows how many times this line is executed by the test cases.

image

How it works?

  • The cobertura generate for each class an instrumented version of it which has instructions
  • These instrumented version is added to the compile code
  • Instead of running the normal compiled code, you run the instrumented version
  • When these instructions are encountered by the Java Virtual Machine, the inserted code increments various counters so that it is possible to tell which instructions have been encountered and which have not.