Unit testing with FlexMojos and displaying Cobertura coverage in Sonar

The great thing about running unit tests with FlexMojos 4 is that it can also generate code coverage report. See it here http://www.sonatype.com/people/2010/04/flex-test-coverage-kept-simple-with-flexmojos/

I just love that feature, because in the past you had to use a modified version of the Flex SDK with an external AIR Application to do that, but now it is very simplified.

I also love using Sonar to gather and display statistics for source code. I thought it would be great to build my project and run unit tests with FlexMojos and then reuse the reports created by it and show this information in Sonar. You are able to reuse the surefire reports using the flex plugin for Sonar, but however this was not possible for the cobertura coverage report . So what I did was to browse the code for this plugin and see how Olivier Gaudin and Evgeny Mandrikov, the guys written it, handle the surefire reports. And so I though that it was going to be easy using the code from the cobertura sonar plugin to implement this feature. And somehow I did.

I’ve added this patch in the Jira, so hopefully in the next version it will be included. Until the official version, you can use my modification (see bellow for download)

Here is how to use it. In your pom.xml you would have something like

    <param>xml <!-- param-->  

That snippet would build and test your project. Important here is to specify coverage and coverageReportFormat . The coverageReportFormat is xml, so it can be used later on.

And the properties in the pom.xml for the sonar plugin are


I think everything here is understandable, you specify to use the plugin for flex and reuse the generated reports for surefire and cobertura.

See the full pom.xml here

And to build all up run:

mvn clean install sonar:sonar -Pflex

Have in mind that you should use maven 3 for FlexMojos 4

See the result

So if you can’t wait to try this out, get this patched version, if not you could probably wait for an official release, I hope there will be one soon.

Download the patched version of flex plugin for sonar from here. (Again, this is my modification of the original version, it is not the version provided by Codehaus, Sonar)

Download the test Flex project with Unit tests from here

Improve your code using Sonar with Flex plugin

Bad code leads to bugs, hard understanding, hard maintenance. We should really learn to write good code, and with so much open source tools for code analysis we could improve it one step higher.

Sonar is a tool for code quality analysis. It helps improving software quality using static analysis tools. It targets Java code, but there are plugins for Flex, C, PHP, .Net and other languages. The Flex plug-in specifically uses FlexPMD, FlexMetrics, FlexCPD and FlexMojos.

In this post I will show you the features that FlexPMD gives us within Sonar. I think that this tool is not really known by the Flex developers, I admit that I also didn’t have an idea what it is until recently.
Have you used eclipse for Java developing? I really love when warnings like unused variables and not called methods appear. Well FlexPMD does this for us along with a lot more things. It even can be used inside of Eclipse to show you live reports. There are defined rules that FlexPMD follows to catch issues in the code and bad practices. Great thing is that you can also define your own rules using this flex app

Step 1. So first go and download Sonar. After that download the flex plugin for it. It is a jar file. Now extract the contents of the sonar archive in a desired location, and place the sonar-flex-plugin-x.x.x.jar in extensions\plugins directory of sonar. That’s pretty much what you need to start sonar. By default it uses an embeded Apache Derby database, that you can change to whatever you want in sonar.properties file in conf folder. So now start Sonar. I start it from bin\windows-x86-32\StartSonar.bat as I am a windows user, if you are on other operating system start it from bin\YOUR_SYSTEM\sonar.sh. Have in mind that it takes a while to start so have patience. To assure that it is started open a browser and navigate to http://localhost:9000/

Step 2. Create a flex project. I will create a small test project with one class written really ugly and bad and doesn’t actually do a thing it is here to show you what errors Sonar will find. Pff It doesn’t even deserve to be put it in a code tag and to be styled because I am embarrassed of it 🙂
Here it is:

public class MyTestComponent extends UIComponent  
 private var r:Number;  
 private var variableNotUsed:Object;  
 override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void  
  r = Math.random()* 85;  
  var child1:UIComponent = new UIComponent();  
 public function myFunctionWithVeryLongNameAndLotsOfParametersThatDoesNothing(p1:String, p2:Number, p3:Object, p4:Boolean, p5:int, p6:*, p7:Array):void  

Step 3. Run analysis built with Maven. For this step you should have Maven 2 on your computer (I think Maven 3 will also work, but I have tested it with 2). Note A lot of Flex developers think of Maven as the Black ninja, Java developers use for their projects. Maven is a tool that helps managing the building, documentation, testing and reporting for Java projects. With FlexMojos you can even use it for your Flex projects also. It is a lot useful for continuous integration. So my note to the Flex developers: don’t be afraid of Maven, it is here to help us 🙂

Assuming that you have Maven on your computer, as the documentation on the sonar site suggests, add

      <name>FlexPMD repository on opensource.adobe.com</name>  

this to the settings.xml file of maven located either in $M2_HOME/conf/settings.xml or ${user.home}/.m2/settings.xml

And now in the flex project folder add a new file named pom.xml

<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemalocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  <name>Test Sonar Project</name>  

Now open a console, navigate to the location of your flex project and type “mvn sonar:sonar -Pflex”.

Step 4. After some time of downloading the needed jars for the build and running the analysis task we can check what happened when we open http://localhost:9000/ There should be displayed our project with all the violations that were detected.
For my test project I have
Blocker 12
Critical 0
Major 9
Minor 0
Info 1

And when I select to see the Blocker issues for example I see:

Hmm strange, but it found most of the issues I think are present in this code 🙂

So tell me, do you use Sonar for static analysis of the code, or maybe some other tool? And I’m really interested on how do you use it? Did you integrated it in continuous build or do you check the code once in a while on your computer? Do you use it individually or your whole team uses it.

It is easy to code, but challenging to code right

I cannot say I have a lot of experience as a developer. I cannot say that I am ready to teach someone how to write code. I cannot say that what I am writing in this blog is always right.

What I can say is that I always have thirst to learn new things, new techniques. Willing to listen to new ideas, willing to discuss them and willing to share my experiences.

What I am saying is that I am always open to learn, open to try something new, and I think every developer should be. Sure I make mistakes, but making errors and then fixing them is making me better at what I do.

I said all of this, because I think it is time to step up my game. After few conversations with colleagues from work and college whether I am ready to guide other developers, give advices about what and why, and whether have the confidence to do it, I decided that I am not 🙂 But I won’t be ready even in 10 years if I don’t start from somewhere.

I have this idea for a posts about how to improve the quality of your code. Why? Being a developer at Obecto I am constantly observing the goodness of quality code and I’m practicing on writing one. What I realized is that the point is not always just to make things work. No. Very often you should write something that is less expose-able for bugs, something that is easy to maintain, something other developers could understand, that can extend and something that you can change later on and to still work.

I won’t step in into discussions of why and how to write good code, there are other books, papers and articles that cover that. In fact  you can check out this great article written by colleague of mine Vladimir Tsvetkov What’s wrong with the creative community of Flash? in which he writes about the problems of software engineers and how they can level up.

What I will do is explore and give my opinion on certain practices and tools to help us make better looking code.

I will try to make a series of posts on that topic, starting from Improve your code using Sonar with Flex plugin