Skip to end of metadata
Go to start of metadata
You are viewing an old version of this page. View the current version. Compare with Current ·  View Page History

Best practices

Developers of Sonar core should :

  • build with maven before commiting to subversion
  • commit frequently into subversion
  • never commit incomplete changes (Subversion is not a backup system)
  • never commit IDE files (.settings, .project, .classpath, .idea, *.iml, *.ipr, *.iws)
  • use meaningful comments when doing a subversion commit, always with the JIRA key and title (e.g. "SONAR-6 Apply license LGPL")
  • subscribe to the mailing list dev@sonar.codehaus.org
  • overuse unit tests (JUnit 4), with the help of hamcrest matchers and Mockito(mock objects)
  • test on MySql AND Derby
  • think IoC (Inversion of Control pattern)
  • overuse functional tests (Selenium). Tests are written in the Selenese format (HTML).
  • don't reinvent the wheel : use standard libraries like commons-lang, commons-io and google-collections (commons-collections is deprecated from Sonar 1.10)
  • use comments and javadocs only when necessary
  • do not forget the license header on all Java and Ruby files
  • respect the code formatting described below
  • eat your own food : analyze Sonar on Sonar.

Source code encoding

Source code encoding should be set to UTF-8 to prevent cross-platform encoding issues.

UTF-8 is specified in parent POMs, so it is used by Maven when building the projects. However, it may not be set by default in your prefered IDE. The best practice is to force it for all your source code.

  • For Eclipse : go to "Preferences -> General -> Workspace" and force the "Text file encoding" to "UTF-8"
  • For IntelliJ IDEA : go to "Settings -> File Encodings" and force the "IDE Encoding" to "UTF-8"

Code formatting

  • replace tabulations by 2 whitespaces
  • right margin at 140 columns
  • classes with acronyms are CamelCase : for example prefer PmdPlugin to PMDPlugin, or JavaNcssHandler to JavaNCSSHandler
  • follow this template :
  • replace :
    by :

The following sections show how to set up the code style for Sonar in IDEA and Eclipse. It is strongly preferred that patches use this style before they are supplied.

IntelliJ IDEA 4.5+

Download sonar-idea-codestyle.xml
Copy it to ~/.IntelliJIDEA/config/codestyles (on Windows, try C:\Documents and Settings\<username>\.IntelliJIDEA\config\codestyles).
After this, restart IDEA and open the settings to select the new code style.

Eclipse 3.2+

Download sonar-eclipse-codestyle.xml and sonar-eclipse-cleanup.xml
After this, select Window -> Preferences, and open up the configuration for Java -> Code Style:

  1. Open Formatter. Click on the button labeled "Import..." and select the first file you downloaded. Give the style a name, and click OK.
  2. Open Clean Up. Click on the button labeled "Import..." and select the second file you downloaded. Give the style a name, and click OK.

Tips and tricks

Connect to Derby from command line

  • download derby (the same version as used by sonar)
  • set the DERBY_HOME property
  • execute the command $DERBY_HOME/bin/ij
  • connect to the database :

Compare XML files in JUnit

Use XMLUnit. The dependency is already defined into maven.

Warning : There's a bug on the similar() method. Contrary to the documentation, it fails if nodes are not in the same order.

Override Object.equals(), hashCode() and toString()

You can use EqualsBuilder and HashCodeBuilder from commons-lang to simplify. Just define the fields to compare. Example on the Rule class :

Debug the Maven plugin from IDE

  • Two different ways :
    1. Execute Maven debug command line : mvnDebug
    2. Or edit $MAVEN_OPTS with :
          Then execute Maven. It should block on :
  • Attach your IDE to the remote process (the listening port is 8000).
    Example on Intellij Idea : Run -> Edit configurations -> Add new configuration -> Remote -> port 8000.
Labels
  • None