Archive

Archive for the ‘code metrics’ Category

What are the best practices for Java development team?

March 18, 2011 2 comments

At individual level
In Eclipse IDE use following files and share with team to maintain consistency.

PMD-rules-eclipse.xml
Eclipse-Clean-Config.xml
Eclipse-Code-Formatter.xml
Eclipse-Code-Template.xml
Eclipse-Import-Order.importorder

TestNG Test Cases
Emma plug in to check code coverage
Tie the memory and performance numbers to test cases

At team level
What happens in Hudson?
Configure following in Hudson, so that it is easy to check after each build.

Code Coverage Report
Open Tasks Report
PMD Report
Surefire Test Report

At organizational level
How to compare different projects / modules in organization?
Use SONAR and use maven plug in to push the data to Sonar server.

Code Coverage
Comments
Complexity
Lines of Code
PMD
Test Success

References:
http://java.net/projects/hudson/
http://www.sonarsource.org/
http://pmd.sourceforge.net/
http://emma.sourceforge.net/

Sonar (codehaus) Custom PMD Rules

It took some time to resolve this.

Problem Statement: How to add custom PMD rules to Sonar?

Solution:

Note: ABCD is project name or company name

Step 1:

ABCD-PMD-RULES-ECLIPSE.XML: This file can be used in Eclipse. Also we need to use same in Sonar to have consistent results. Zip/jar this file as ABCD-PMD-Rules.jar.

All .xml files must be under root folder.
Drop this .jar file under path \sonar-1.8\extensions\rules\pmd (Respective sonar folder.)
If using custom java classes, they need to be placed in jar file.

Important Change: Make rule name unique. Add project name or company name as prefix. Example: name=”pro_rulename”. Other wise it will create problem. Because these names are already in database and fails to import them. You can see this mysql exception in sonar.log file.


<?xml version="1.0" encoding="UTF-8"?>
<ruleset name="pmd-rules">
 <description>ABCD Ruleset</description>
 <rule class="net.sourceforge.pmd.rules.design.UseSingleton"
 message="All methods are static.  Consider using Singleton instead.  Alternatively, you could add a private constructor or make the class abstract to silence this warning."
 name="ABCD_UseSingleton">
 <description>
If you have a class that has nothing but static methods, consider making it a Singleton.
Note that this doesn't apply to abstract classes, since their subclasses may
well include non-static methods.  Also, if you want this class to be a Singleton,
remember to add a private constructor to prevent instantiation.
 </description>
 <example><![CDATA[

public class MaybeASingleton {
 public static void foo() {}
 public static void bar() {}
}

 ]]></example>
<priority>3</priority>
<properties/>
 </rule>

</ruleset>

Step 2:

Create ABCD_sonar_rules.xml file
Drop this .jar file under path \sonar-1.8\extensions\rules\pmd (Respective sonar folder.)
This file is specific to Sonar.
Read http://docs.codehaus.org/display/SONAR/Manage+quality+profiles for more information.


<rules>
 <rule key="ABCD_UseSingleton">
 <name>ABCD Use Singleton</name>
 <configKey>ABCD-PMD-RULES-ECLIPSE.XML/ABCD_UseSingleton</configKey>
 <!-- available categories : Reliability, Portability, Maintainability, Efficiency, Usability -->
 <category name="Reliability"/>
 <description><![CDATA[Test Description................ ]]></description>
 </rule>
</rules>

Step 3:

Login to Sonar Admin Console
You can see this rules under Sonar way
Copy this profile to Profile B
In profile B, remove all existing rules and keep only ABCD rules.
Make them mandatory
You can assign project to this rule set or we can make this as default rule set.

Step 4:

mvn sonar:sonar

This will generate new reports against new rules. Check the information on sonar site.

Notes: See this http://svn.codehaus.org/sonar/tags/1.4/sonar-rules-extensions/pmd-rules-extensions/pmd-rules-extensions.xml for more information

<pre><rules>
  <rule key="MaximumMethodsCountCheck">
    <name>Maximum Methods Count Check</name>
    <configKey>rulesets/extensions.xml/MaximumMethodsCountCheck</configKey>
    <category name="Usability"/>
    <description>Maximum number of methods authorised</description>
<param key="maxAuthorisedMethodsCount" type="i">
        <description>Maximum number of methods authorised</description>
        <defaultValue>2</defaultValue>
      </param>
  </rule>
  <rule key="AvoidIfWithoutBrace">
    <name>Avoid if without using brace</name>
    <configKey>rulesets/extensions.xml/AvoidIfWithoutBrace</configKey>
    <category name="Usability"/>
    <description>Avoid if without using brace</description>
  </rule>
 </rules></pre>

Note: If there is any errors, Sonar wont display exact error on screen. Check logs for exact information.

\sonar-1.8\logs\sonar.log

\sonar-1.8\logs\wrapper.log

Important Links: http://docs.codehaus.org/display/SONAR/Collect+data

-o-

Categories: code metrics Tags:

Java Code Metrics Report

November 4, 2008 Leave a comment

Java Code Metrics Report

This is the dream for Managers to have code report for each project. In Java we have tools like check style, PMD, CPD, Cobertura Test Coverage, ..etc. We need to have tool which can track all this results on day to day basis and helps to track down the progress in project. To achieve this we have following tools.

XRadar – http://xradar.sourceforge.net/

Maven Dashboard Plug-in – http://mojo.codehaus.org/dashboard-maven-plugin/

QA Lab – http://qalab.sourceforge.net/

Sonar – http://sonar.codehaus.org/

Hacky Stat – http://code.google.com/p/hackystat/

Strengths:

  1. It supports branches and multiple projects
  2. Nice time lines
  3. Tabular and graphical views
  4. Separate database to track huge past history.

JcReport – http://www.jcoderz.org/fawkez/wiki/JcReport

(Only with Ant…no maven support)


The best tool worked out for me is Sonar.

Here is the Sonar demo site http://nemo.sonar.codehaus.org

Problems faced in Sonar setup as on Nov-04-2008

Problem: sonar-1.4.3 is having issues and there is ticket http://jira.codehaus.org/browse/SONAR-413

Solution: Sonar 1.4.2 is working fine.

Problem: MySQL database in Windows with McAfee is having problem while using Windows/Temp folders

Embedded error: org.hibernate.exception.GenericJDBCException: could not get table metadata: rules_categories

Can’t create/write to file ‘C:\WINDOWS\TEMP\#sql_12a8_0.MYI’ (Errcode: 13)

Solution:

my.ini file in mysql folder

#Added to escape from McAfee

tmpdir=”C:/mysql_temp”

http://www.customware.net/repository/pages/viewpage.action?pageId=8093734

-o-

Categories: code metrics
Follow

Get every new post delivered to your Inbox.