Integration Test Coverage with Sonar

I love to develop test driven, but even more I love code analysis tools that show me what to improve. Sonar is quite “popular” and common for Java developers and I’m using it already for years in different projects.

If you want to see integration test coverage, you have to set the jacoco maven plugin together with the fail safe plugin in your maven config (pom.xml):

    <build>
       <plugins>
            <!-- integration tests -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>integration-test</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <!-- for integration test coverage in sonar -->
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>jacoco-initialize</id>
                        <goals>
                            <goal>prepare-agent-integration</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <properties>
      <!-- needs to be set for jacoco's forked process -->
        <argLine>${env.MAVEN_OPTS}</argLine>
    </properties>

argLine is used as additional param in the javaagent command line section jacoco uses (needs to be set, otherwise the maven opts are not set for the forked process – OutOfMemoryException and so on…). See jacoco’s documentation for details.

The result in Sonar is pretty amazing:

sonar-jacoco

Advertisements

Sonar: PicoLifecycleException and NonUniqueResultException during build

Still with sonar 2.14 the old problem occurs, that with parallel runs/analysis on the same project (with different build versions?) a wrong state is created in the DB.

For postgres the statement for analysis of the problem is:

select project_id, count(project_id) from snapshots
where islast is true
group by project_id
having count(islast) > 1

This statement fixes the problem:

update snapshots old_snap
set islast=false
where islast=true and exists(select * from snapshots new_snap where new_snap.created_at > old_snap.created_at and new_snap.project_id = old_snap.project_id and new_snap.islast=true)

 

see also SONAR-2329 NonUniqueResultException occurs when analyzing both a maven module and its parent

and Sonar 2.11 Embedded error: PicoLifecycleException

 

Stacktrace for this error is (Jenkins Sonar Plugin)

[INFO] [14:02:05.667] Initializing Hibernate
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD ERROR
[INFO] ------------------------------------------------------------------------
[INFO] Can not execute Sonar

Embedded error: PicoLifecycleException: method 'public void org.sonar.batch.ProjectTree.start() throws java.io.IOException', instance 'org.sonar.batch.ProjectTree@74f9a042, javax.persistence.NonUniqueResultException: Expected single result, but got : [Snapshot[resourceId=3818,buildDate=2012-09-13 13:43:58.638,createdAt=2012-09-13 13:43:58.545,version=WH-3.09-SNAPSHOT,last=true,status=P,purgeStatus=1,scope=PRJ,path=477617.,depth=1,qualifier=BRC,rootId=477617,parentId=477617,rootProjectId=1,period1Mode=previous_analysis,period2Mode=days,period3Mode=days,period4Mode=<null>,period5Mode=<null>,period1Param=2012-09-13,period2Param=5,period3Param=30,period4Param=<null>,period5Param=<null>,period1Date=2012-09-13 13:43:55.266,period2Date=2012-09-08 13:43:58.545,period3Date=2012-08-14 13:43:58.545,period4Date=<null>,period5Date=<null>,id=477626], Snapshot[resourceId=3818,buildDate=2012-09-13 13:49:13.894,createdAt=2012-09-13 13:49:13.737,version=WH-3.09-SNAPSHOT,last=true,status=P,purgeStatus=<null>,scope=PRJ,path=,depth=0,qualifier=TRK,rootId=<null>,parentId=<null>,rootProjectId=3818,period1Mode=previous_analysis,period2Mode=days,period3Mode=days,period4Mode=<null>,period5Mode=<null>,period1Param=2012-09-13,period2Param=5,period3Param=30,period4Param=<null>,period5Param=<null>,period1Date=2012-09-13 13:43:55.266,period2Date=2012-09-08 13:49:13.737,period3Date=2012-08-14 13:49:13.737,period4Date=<null>,period5Date=<null>,id=480765]]
[INFO] ------------------------------------------------------------------------
[INFO] Trace
org.apache.maven.lifecycle.LifecycleExecutionException: Can not execute Sonar
	at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(DefaultLifecycleExecutor.java:719)
	at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeStandaloneGoal(DefaultLifecycleExecutor.java:569)
	at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoal(DefaultLifecycleExecutor.java:539)
	at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalAndHandleFailures(DefaultLifecycleExecutor.java:387)
	at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeTaskSegments(DefaultLifecycleExecutor.java:284)
	at org.apache.maven.lifecycle.DefaultLifecycleExecutor.execute(DefaultLifecycleExecutor.java:180)
	at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:328)
	at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:138)
	at org.apache.maven.cli.MavenCli.main(MavenCli.java:362)
	at org.apache.maven.cli.compat.CompatibleMain.main(CompatibleMain.java:60)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at org.codehaus.classworlds.Launcher.launchEnhanced(Launcher.java:315)
	at org.codehaus.classworlds.Launcher.launch(Launcher.java:255)
	at org.codehaus.classworlds.Launcher.mainWithExitCode(Launcher.java:430)
	at org.codehaus.classworlds.Launcher.main(Launcher.java:375)
Caused by: org.apache.maven.plugin.MojoExecutionException: Can not execute Sonar
	at org.codehaus.mojo.sonar.Bootstraper.executeMojo(Bootstraper.java:103)
	at org.codehaus.mojo.sonar.Bootstraper.start(Bootstraper.java:79)
	at org.codehaus.mojo.sonar.SonarMojo.execute(SonarMojo.java:88)
	at org.apache.maven.plugin.DefaultPluginManager.executeMojo(DefaultPluginManager.java:490)
	at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(DefaultLifecycleExecutor.java:694)
	... 17 more
Caused by: org.picocontainer.PicoLifecycleException: PicoLifecycleException: method 'public void org.sonar.batch.ProjectTree.start() throws java.io.IOException', instance 'org.sonar.batch.ProjectTree@74f9a042, javax.persistence.NonUniqueResultException: Expected single result, but got : [Snapshot[resourceId=3818,buildDate=2012-09-13 13:43:58.638,createdAt=2012-09-13 13:43:58.545,version=WH-3.09-SNAPSHOT,last=true,status=P,purgeStatus=1,scope=PRJ,path=477617.,depth=1,qualifier=BRC,rootId=477617,parentId=477617,rootProjectId=1,period1Mode=previous_analysis,period2Mode=days,period3Mode=days,period4Mode=<null>,period5Mode=<null>,period1Param=2012-09-13,period2Param=5,period3Param=30,period4Param=<null>,period5Param=<null>,period1Date=2012-09-13 13:43:55.266,period2Date=2012-09-08 13:43:58.545,period3Date=2012-08-14 13:43:58.545,period4Date=<null>,period5Date=<null>,id=477626], Snapshot[resourceId=3818,buildDate=2012-09-13 13:49:13.894,createdAt=2012-09-13 13:49:13.737,version=WH-3.09-SNAPSHOT,last=true,status=P,purgeStatus=<null>,scope=PRJ,path=,depth=0,qualifier=TRK,rootId=<null>,parentId=<null>,rootProjectId=3818,period1Mode=previous_analysis,period2Mode=days,period3Mode=days,period4Mode=<null>,period5Mode=<null>,period1Param=2012-09-13,period2Param=5,period3Param=30,period4Param=<null>,period5Param=<null>,period1Date=2012-09-13 13:43:55.266,period2Date=2012-09-08 13:49:13.737,period3Date=2012-08-14 13:49:13.737,period4Date=<null>,period5Date=<null>,id=480765]]
	at org.picocontainer.monitors.NullComponentMonitor.lifecycleInvocationFailed(NullComponentMonitor.java:77)
	at org.picocontainer.lifecycle.ReflectionLifecycleStrategy.monitorAndThrowReflectionLifecycleException(ReflectionLifecycleStrategy.java:132)
	at org.picocontainer.lifecycle.ReflectionLifecycleStrategy.invokeMethod(ReflectionLifecycleStrategy.java:115)
	at org.picocontainer.lifecycle.ReflectionLifecycleStrategy.start(ReflectionLifecycleStrategy.java:89)
	at org.picocontainer.injectors.AbstractInjectionFactory$LifecycleAdapter.start(AbstractInjectionFactory.java:84)
	at org.picocontainer.behaviors.AbstractBehavior.start(AbstractBehavior.java:169)
	at org.picocontainer.behaviors.Stored$RealComponentLifecycle.start(Stored.java:132)
	at org.picocontainer.behaviors.Stored.start(Stored.java:110)
	at org.picocontainer.DefaultPicoContainer.potentiallyStartAdapter(DefaultPicoContainer.java:1009)
	at org.picocontainer.DefaultPicoContainer.startAdapters(DefaultPicoContainer.java:1002)
	at org.picocontainer.DefaultPicoContainer.start(DefaultPicoContainer.java:760)
	at org.sonar.api.platform.ComponentContainer.startComponents(ComponentContainer.java:70)
	at org.sonar.batch.bootstrap.Module.start(Module.java:82)
	at org.sonar.batch.bootstrap.BootstrapModule.doStart(BootstrapModule.java:102)
	at org.sonar.batch.bootstrap.Module.start(Module.java:83)
	at org.sonar.batch.bootstrapper.Batch.startBatch(Batch.java:71)
	at org.sonar.batch.bootstrapper.Batch.execute(Batch.java:58)
	at org.sonar.maven.SonarMojo.execute(SonarMojo.java:151)
	at org.apache.maven.plugin.DefaultPluginManager.executeMojo(DefaultPluginManager.java:490)
	at org.codehaus.mojo.sonar.Bootstraper.executeMojo(Bootstraper.java:98)
	... 21 more
Caused by: javax.persistence.NonUniqueResultException: Expected single result, but got : [Snapshot[resourceId=3818,buildDate=2012-09-13 13:43:58.638,createdAt=2012-09-13 13:43:58.545,version=WH-3.09-SNAPSHOT,last=true,status=P,purgeStatus=1,scope=PRJ,path=477617.,depth=1,qualifier=BRC,rootId=477617,parentId=477617,rootProjectId=1,period1Mode=previous_analysis,period2Mode=days,period3Mode=days,period4Mode=<null>,period5Mode=<null>,period1Param=2012-09-13,period2Param=5,period3Param=30,period4Param=<null>,period5Param=<null>,period1Date=2012-09-13 13:43:55.266,period2Date=2012-09-08 13:43:58.545,period3Date=2012-08-14 13:43:58.545,period4Date=<null>,period5Date=<null>,id=477626], Snapshot[resourceId=3818,buildDate=2012-09-13 13:49:13.894,createdAt=2012-09-13 13:49:13.737,version=WH-3.09-SNAPSHOT,last=true,status=P,purgeStatus=<null>,scope=PRJ,path=,depth=0,qualifier=TRK,rootId=<null>,parentId=<null>,rootProjectId=3818,period1Mode=previous_analysis,period2Mode=days,period3Mode=days,period4Mode=<null>,period5Mode=<null>,period1Param=2012-09-13,period2Param=5,period3Param=30,period4Param=<null>,period5Param=<null>,period1Date=2012-09-13 13:43:55.266,period2Date=2012-09-08 13:49:13.737,period3Date=2012-08-14 13:49:13.737,period4Date=<null>,period5Date=<null>,id=480765]]
	at org.sonar.jpa.session.JpaDatabaseSession.getSingleResult(JpaDatabaseSession.java:195)
	at org.sonar.jpa.session.JpaDatabaseSession.getSingleResult(JpaDatabaseSession.java:213)
	at org.sonar.batch.ProjectConfigurator.isLatestAnalysis(ProjectConfigurator.java:84)
	at org.sonar.batch.ProjectConfigurator.configure(ProjectConfigurator.java:71)
	at org.sonar.batch.ProjectTree.doStart(ProjectTree.java:89)
	at org.sonar.batch.ProjectTree.start(ProjectTree.java:66)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at org.picocontainer.lifecycle.ReflectionLifecycleStrategy.invokeMethod(ReflectionLifecycleStrategy.java:110)
	... 38 more
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5 seconds
[INFO] Finished at: Thu Sep 13 14:02:07 CEST 2012
[INFO] Final Memory: 44M/141M
[INFO] ------------------------------------------------------------------------
Sonar analysis completed: FAILURE
Build step 'Sonar' changed build result to FAILURE
Build step 'Sonar' marked build as failure
Finished: FAILURE