Wednesday 28 August 2013

Sonar Set Up


Sonar is an open source web-based application to manage code quality which covers seven axes of code quality as: 
  • Architecture and design
  • comments
  • duplications
  • unit tests
  • complexity
  • potential bugs and coding rules. 
Sonar is Developed in Java. It can cover projects in Java, Flex, PHP, PL/SQL, Cobol and Visual Basic 6. 
This post will cover sonar installation and how to use it:

Sonar Installation:

1. Download sonar server from http://dist.sonar.codehaus.org/.  Let us install sonar of 2.11 version
sonar-2.11

Running sonar server with default derby database:
Start the server :<path/to sonar/installtion/directory/>/bin/linux-x86-32 : ./sonar.sh start
eg.: /home/impadmin/sws/sonar-2.11/bin/linux-x86-32 : ./sonar.sh start
By default this server starts on 9000 port.Verify the same on UI : http://localhost:9000 

Running sonar server using mysql database:
In mysql prompt, fire below queries:
CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci;
CREATE USER 'sonar' IDENTIFIED BY 'sonar';
GRANT ALL ON sonar.* TO 'sonar'@'%' IDENTIFIED BY 'sonar';
GRANT ALL ON sonar.* TO 'sonar'@'localhost' IDENTIFIED BY 'sonar';
FLUSH PRIVILEGES; 

Once it is done edit sonar.properties file:
Path of file: <path/to sonar/installtion/directory>/conf/sonar.properties
eg: /home/impadmin/sws/sonar-2.11/conf/sonar.properties

Comment the properties related to derby;
#sonar.jdbc.url: jdbc:derby://localhost:1527/sonar;create=true
#sonar.jdbc.driverClassName: org.apache.derby.jdbc.ClientDriver
#sonar.jdbc.validationQuery: values(1)

Uncomment the properties related to Mysql
#----- MySQL 5.x/6.x
# Comment the embedded database and uncomment the following properties to use MySQL. The validation query is optional.
sonar.jdbc.url: jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8
sonar.jdbc.driverClassName: com.mysql.jdbc.Driver
sonar.jdbc.validationQuery: select 1 

Now stop and start the sonar server:
Stop the server : <sonar/installation/directory>/bin/linux-x86-32 : ./sonar.sh stop
Start the server : <sonar/installation/directory>/bin/linux-x86-32 : ./sonar.sh start
By default this server starts on 9000 port.Verify the same on UI : http://localhost:9000
Verify Apporx 43 tables will be created in sonar database in mysql

 
Sample code to show how to use this: Now sonar is up and running perfectly. Let us create sample application that will show the usage of sonar

1. Create a mavenized project say TestSonar in eclipse
2. Create a package with name test.sonar in project
3. Copy the One.java and OneTest.java in test.sonar package
4. Replace pom.xml content with below mentioned pom.xml content
5. From terminal, Go to project path . Run below command:
mvn clean install -Psonar sonar:sonar

One.java:

public class One {
      String message = "foo";
      String message2 = "toto";

      public String foo() {
        return message;
      }

      public String toto() {
        return message2;
      }

      public void uncoveredMethod() {
        System.out.println(foo());
      }
    }

OneTest.java: Create a junit with name OneTest

import static org.junit.Assert.*;

import org.junit.Test;

public class OneTest {

    @Test
      public void testFoo() throws Exception {
        One one = new One();
        assertEquals("foo", one.foo());
      }

      @Test
      public void testBoth() throws Exception {
        One one = new One();
        assertEquals("toto", one.toto());
        assertEquals("foo", one.foo());
      }

}
 
pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.codehaus.sonar</groupId>
  <artifactId>example-ut-maven-jacoco-runTests</artifactId>
  <version>1.0-SNAPSHOT</version>

  <!-- <name>UT coverage with Maven and JaCoCo running tests</name>-->
  <name>Code coverage with Maven and Sonar running tests</name>


  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <sonar.language>java</sonar.language>

    <!-- Tells Sonar to run the unit tests -->
    <sonar.dynamicAnalysis>true</sonar.dynamicAnalysis>
    <!-- Tells Sonar to use JaCoCo as the code coverage tool -->
    <sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <!-- Minimal supported version is 4.7 -->
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.5</source>
          <target>1.5</target>
        </configuration>
      </plugin>
    </plugins>
  </build>

 
  <!-- BEGIN: Specific to mapping unit tests and covered code -->
  <profiles>
   <profile>
            <id>sonar</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <!-- SERVER ON A REMOTE HOST -->
               <sonar.jdbc.url>jdbc:mysql://localhost:3306/sonar?useUnicode=true&amp;characterEncoding=utf8</sonar.jdbc.url>
                <sonar.jdbc.driverClassName>com.mysql.jdbc.Driver</sonar.jdbc.driverClassName>
                <sonar.jdbc.username>sonar</sonar.jdbc.username>
                <sonar.jdbc.password>sonar</sonar.jdbc.password>
                <sonar.host.url>http://localhost:9000</sonar.host.url>
            </properties>
 </profile>

    <profile>
      <id>coverage-per-test</id>
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <!-- Minimal supported version is 2.4 -->
            <version>2.13</version>
            <configuration>
              <properties>
                <property>
                  <name>listener</name>
                  <value>org.sonar.java.jacoco.JUnitListener</value>
                </property>
              </properties>
            </configuration>
          </plugin>
        </plugins>
      </build>

      <dependencies>
        <dependency>
          <groupId>org.codehaus.sonar-plugins.java</groupId>
          <artifactId>sonar-jacoco-listeners</artifactId>
          <version>1.2</version>
          <scope>test</scope>
        </dependency>
      </dependencies>
    </profile>
  </profiles>
  <!-- END: Specific to mapping unit tests and covered code -->
 
</project>

Once the build will be created successfully, Verify one project with name "Code coverage with Maven and Sonar running tests" will be displayed in sonar UI. Opening the project will show something like below:


OneTest.java has two test cases. You can check the time taken by each test cases
Uncomment or add another testcases to see furthur behaviour


1 comment: