Skip to content
 

Cobertura on Google App Engine reloaded

In a previous article, I explained how to get the code coverage data generated by Cobertura, for integrations tests of an application deployed on Google App Engine.

Cobertura just released a new version (1.9.4), which is supposed to be up to 10 times faster (kudos for that), but is not backward compatible. I thus had to rewrite the method to get the project data. Here it is.

    public Resolution flushCobertura() throws ClassNotFoundException,
                                              SecurityException,
                                              NoSuchMethodException,
                                              IllegalArgumentException,
                                              IllegalAccessException,
                                              InvocationTargetException,
                                              IOException,
                                              InstantiationException {
        String projectDataClassName = "net.sourceforge.cobertura.coveragedata.ProjectData";
        Class< ?> projectDataClass = Class.forName(projectDataClassName);
        Object projectData = projectDataClass.newInstance();

        String touchCollectorClassName = "net.sourceforge.cobertura.coveragedata.TouchCollector";
        Class< ?> touchCollectorClass = Class.forName(touchCollectorClassName);

        String methodName = "applyTouchesOnProjectData";
        java.lang.reflect.Method applyTouchesOnProjectDataMethod =
            touchCollectorClass.getDeclaredMethod(methodName, new Class[] {projectDataClass});
        applyTouchesOnProjectDataMethod.invoke(null, projectData);

        getContext().getResponse().setContentType("application/octet-stream");
        OutputStream out = getContext().getResponse().getOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(out);
        try {
            oos.writeObject(projectData);
        }
        finally {
            oos.close();
        }

        return null;
    }