Menu

#39 Finally clause is not instrumented by Cobertura

open
nobody
None
5
2005-11-21
2005-11-21
No

The finally clause in our code does not seem to be
instrumented. Example code:
try {
c = JDBCHelper.getConnection();
...

} catch (SQLException e) {
throw new DatabaseException(e, "...");
} finally {
JDBCHelper.close(c, stmt, rs);
}

Instrumented result (decompiled):

c = JDBCHelper.getConnection();
ProjectData.getGlobalProjectData
().getOrCreateClassData("...").touch(787);
stmt = c.prepareStatement("<SQL text>");
...
SQLException e;
e;
throw new DatabaseException(e, "...");
ProjectData.getGlobalProjectData
().getOrCreateClassData("...").touch(799);
Exception exception;
exception;
goto _L2
_L3:
throw exception;
_L2:
JDBCHelper.close(c, stmt, rs);
if(true) goto _L3; else goto _L1
_L1:
ProjectData.getGlobalProjectData
().getOrCreateClassData("...").touch(802);
return new Integer(0);

As you can see there's no touch before or after the
JDBCHelper clause... So it shows up in the coverage
report as not executed, which is wrong.

Discussion

  • Alan Harder

    Alan Harder - 2007-10-15

    Logged In: YES
    user_id=70034
    Originator: NO

    I hit this too.. I found if the finally clause has 2 lines, the second line is OK but the first line is not counted. A one line finally clause is not counted at all, as mentioned above. I'm using Cobertura 1.9.

     
  • Alan Harder

    Alan Harder - 2007-10-16

    Logged In: YES
    user_id=70034
    Originator: NO

    I ran some tests, and determined that in my case the bug only occurs with Java 1.4.0. Even a version as old as 1.4.2 doesn't show this behavior. Test case below (sorry for long output.. I'm not able to add an attachment). Set some paths at the top of build.xml and run ant.. with java140.bin selected, the "i = 2" line in test1 and test2 shows as not executed. With 1.4.2 or 1.5, it's ok. I threw in test3 which shows some odd behavior in java 1.4.x and 1.5.

    --------------------- test.java -------------------------------------
    public class test
    {
    public static void main(String[] args) throws Exception
    {
    test1();
    test2();
    test3();
    }

    static void test1() {
    int i;
    try {
    i = 1;
    }
    finally {
    i = 2;
    }
    }

    static void test2() {
    int i;
    try {
    i = 1;
    }
    finally {
    i = 2;
    i = 3;
    }
    }

    static void test3() {
    try
    {
    int i = 1;
    }
    finally
    {
    try
    {
    throw new NullPointerException();
    }
    catch (Exception ex)
    {
    }
    }
    }
    }

    ------------------------- build.xml --------------------------------
    <project name="cobertura_finally_test" default="report">
    <property name="java140.bin" value="/path/to/java,v1.4.0/bin"/>
    <property name="java142.bin" value="/path/to/java,v1.4.2/bin"/>
    <property name="java.bin" value="${java140.bin}"/>
    <path id="cobertura.classpath">
    <fileset dir="/path/to/cobertura-jars" includes="*.jar"/>
    </path>
    <target name="compile">
    <javac srcdir="." includes="test.java" debug="on"
    fork="yes" executable="${java.bin}/javac"/>
    </target>
    <target name="instrument" depends="compile">
    <delete file="cobertura.ser"/>
    <mkdir dir="cobertura"/>
    <taskdef resource="tasks.properties" classpathref="cobertura.classpath"/>
    <cobertura-instrument todir="cobertura" datafile="cobertura.ser">
    <fileset dir="." includes="test.class"/>
    </cobertura-instrument>
    </target>
    <target name="run" depends="instrument">
    <java classname="test" classpath="cobertura" fork="yes" jvm="${java.bin}/java">
    <classpath refid="cobertura.classpath"/>
    </java>
    </target>
    <target name="report" depends="run">
    <mkdir dir="cobertura"/>
    <cobertura-report srcdir="." destdir="cobertura" datafile="cobertura.ser"/>
    </target>
    <target name="clean">
    <delete file="test.class"/>
    <delete file="cobertura.ser"/>
    <delete dir="cobertura"/>
    </target>
    </project>

     
  • Jiri Mares

    Jiri Mares - 2007-10-16

    Logged In: YES
    user_id=819956
    Originator: NO

    Please, can you send me the translated classes and sources ... you can send it to the cobertura mailing list or directly to me ... jiramares at gmail.com. Thanks a lot (it is not necessary to comment both bug reports)

     
  • Alan Harder

    Alan Harder - 2007-10-18

    Logged In: YES
    user_id=70034
    Originator: NO

    I sent a zip file yesterday (to the gmail address).. let me know if you need anything else. Thanks!

     

Log in to post a comment.