To determine code quality of an application,lot of tools are available such as PMD, FindBugs,etc. During the product development life-cycle there are various phases such as unit testing, code coverage analysis, performance testing ,and code quality analysis. SonarQube is one such open source tool that aggregates the aforementioned metrics into a single dashboard through which one can see, manage and iteratively improve upon such aspects. SonarQube has a plug-in based architecture and you can use this tool to analyze different languages within the same project and can maintain a central repository to track changes, assign and resolve issues.

In this post i will share an example of code analysis done by SonarQube for an ADF project, I have configured MySQL database as a repository for the SonarQube application. I have also extended the existing rule sets by adding a XPATH rule; This rule is based on PMD xpath template that flags the use of getRowSetIterator() method. The steps for running the analysis are mentioned below.
  1. Assuming sonar-runner in your classpath and SONAR_RUNNER_HOME  is configured, create sonar-project.properties in the application directory of your application as shown below :-
    # required metadata
    sonar.projectKey=com.blogspot.ramannanda.MasterDetail
    sonar.projectName=ADF Project
    sonar.projectVersion=1.0
     
    # optional description
    sonar.projectDescription=Master Detail Example
    
    #project modules
    sonar.modules=Model,ViewController
    
    # The value of the property must be the key of the language.
    sonar.language=java
    
    # Encoding of the source code
    sonar.sourceEncoding=UTF-8



  2. Create module specific sonar-project.properties files as shown below
    #For model project
    # required metadata
    sonar.projectName=Model
    sonar.projectVersion=1.0
     
    # path to source directories (required)
    sonar.sources=src
     
    # optional description
    sonar.projectDescription=Model for Master Detail
    
    #path to binaries
    sonar.binaries=classes 

    For viewcontroller project the configuration is similar as shown below

    # required metadata
    sonar.projectName=ViewController
    sonar.projectVersion=1.0
     
    # path to source directories (required)
    sonar.sources=src
    
    #optional description
    sonar.projectDescription=ViewController project for 
    
    #path to binaries
    sonar.binaries=classes 



  3. Just type the sonar-runner command in the application directory and analysis will be performed, after which you can open the application and view the analysis for the project


Extending the rule sets:-


Now i will provide an example of how to extend  the application with a custom XPATH rule.



  1. Go to Quality Profile menu and select a java profile


  2. Search for xpath rule; there might be two disabled rules. I have chosen the PMD xpath template rule, as it is currently the supported rule for java and expressions can easily be checked using PMDDesigner utility. The screenshot is shown below.SonarQube _Rule


  3. The xpath expression for the rule is shown in the below snippet.
    //PrimaryExpression/PrimaryPrefix/Name[contains(@Image,'getRowSetIterator')]



  4. Activate the rule and run the analysis with sonar-runner utility; After running the analysis we can see the rule violations for the project.


sonarqube_custom_pmd_rule





As can be seen from the screen-shot, we also have the option to assign the issue to an individual, browse the source code, see how long a issue has been opened, etc. So all in all this tool is a good place to maintain the metrics about the project. Probably, in future, people can extend the functionality further for ADF application by creating more rule sets applicable to ADF application.





References:-


http://www.sonarqube.org/


http://pmd.sourceforge.net/pmd-5.0.5/xpathruletutorial.html


Some Screens:


SonarQube - ADFProject


SonarQube_treemap