How to test your Java Application using PMD, and Jenkins

In the previous articles, Previous whilst working on various projects at IDR Solutions I went through the process of how to build your own Java Application with Apache Ant and also how to set up Jenkins. In this tutorial i will show you how to perform automated static analysis test on your source code.
I will suggest you see the previous article before proceeding if you haven’t. This will make it easier to follow.

What is PMD

It is a static rule-set based Java source code analyser that identifies potential problems in your source code. There are other static analysers like FindBugs. You can get PMD from here

From our previous AntProject, open the jbuild.xml file. you can get the code here if you do not have it. Now in the jbuild file, copy and paste this code before the closing project tag. Also change the pmd.dir property value to where u installed you pmd

<target name ="copy">
    <copy todir="${staging.dir}/src/">
            <fileset dir="${src.dir}" />
        </copy>
</target>
 
 <!-- ================== PMD : static analysis checks on code =================== -->
    <target name="pmd" depends="main, copy">
 
        <property name="pmd.dir" value="/path/to/pmd/" />
        <path id="pmd.classpath">
            <fileset dir="${pmd.dir}\lib"> 
                <include name="*.jar" /> 
            </fileset>
        </path>
        <taskdef name="pmd" classname="net.sourceforge.pmd.ant.PMDTask" classpathref="pmd.classpath" />
 
        <pmd rulesetfiles="pmdRules.xml" failOnRuleViolation="yes" >
            <formatter type="html" toFile="pmd_report.html" toConsole="true" />
            <fileset dir="${staging.dir}/src/">
                <include name="**/*.java"/>
            </fileset>
        </pmd>
    </target>

Now if running on MAC/LINUX, create a bash script called testSA.bash, copy the script below, change the ANT_HOME to where you installed ant paste.

#!/bin/bash
#1.4
 
export ANT_HOME=/PDFdata/library/apache_ant/
export PATH=$ANT_HOME/bin:$PATH
 
 
echo "Build PMD"
ant  -buildfile jbuild.xml pmd
if [ $? -eq 1 ]; then
echo "Failed on testSA"
exit 1
fi

On Windows, make sure your ANT_HONE Environment variable is set, create a batch file the copy and paste the code below

@ECHO OFF
 
echo "Build PMD"
ant  -buildfile jbuild.xml pmd
IF ERRORLEVEL 1 (
echo "Failed on testSA"
exit 1
)

Now we need to create the a file that will have all the pmd rules we want o check. Create a file called pmdRules.xml in the AntProject and paste this code

<?xml version="1.0" encoding="UTF-8"?>
 
<ruleset name="Custom ruleset"
xmlns="http://pmd.sf.net/ruleset/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0 http://pmd.sf.net/ruleset_xml_schema.xsd"
xsi:noNamespaceSchemaLocation="http://pmd.sf.net/ruleset_xml_schema.xsd">
 
<description>This ruleset checks my code for bad stuff</description>
 
      <!-- Checking for unused imports -->
   <rule ref="rulesets/java/imports.xml/UnusedImports"/>
 
        <!-- Check for Empty Catch blocks -->
   <rule ref="rulesets/java/empty.xml/EmptyCatchBlock"/>
 
       <!-- Check for Empty try blocks -->
   <rule ref="rulesets/java/empty.xml/EmptyTryBlock"/>
 
</ruleset>

In the xml file above, we are only checking for unused import and empty try and catch blocks.You can find more rules here. Your project should now be looking like this.

Screen Shot 2015-07-15 at 16.26.19
Now open your command line, navigate to your ant project and run the bat/bash script.You should see something like this

Screen Shot 2015-07-15 at 16.33.32
Time to break the build. Now that it successfully builds, open your java class, add an import statement and empty try and catch block.

Screen Shot 2015-07-15 at 16.44.14
Open command line and run testSA again, this time it should fail.

Screen Shot 2015-07-15 at 16.52.55
Head over to NetBeans, in your project you should see a new file called pmd_report.html. Right click on it and select view, this should tell you why the test is failing.

Screen Shot 2015-07-15 at 16.58.55
Screen Shot 2015-07-15 at 16.57.20
Now Let’s test with Jenkin.
Refer to previous article to set up Jenkins task.
To do that I am going to push my go to GitHub and manually run it for the first time.

Screen Shot 2015-07-15 at 17.29.58

As you can see it pulled from my GitHub repo and was started by me.

Now i will push a code that will break the test.

Screen Shot 2015-07-15 at 17.50.42

As you can see this one was performed by SCM change. so each time you push to GitHub the test automatically runs and sends you an email if it failed.

Screen Shot 2015-07-15 at 17.58.43
Hopefully you have found this quick guide useful.

If you’re a first-time reader, or simply want to be notified when we post new articles and updates, you can keep up to date by social media (Twitter, Facebook and Google+) or the Blog RSS.

Related Posts:

The following two tabs change content below.

Ernest Duodu

Developer at IDRSolutions
Ernest is a developer at IDRSolutions. He focuses mainly on our JavaFX PDF Viewer implementation. He was a speaker at JavaOne 2014, where he did a joint session titled "Lessons learnt developing a NetBeans PDF viewer plugin in JavaFX". Aside programming, he also enjoys a wide variety of hobbies which includes sky-diving, photography, exercising and listening to music.
Ernest

About Ernest Duodu

Ernest is a developer at IDRSolutions. He focuses mainly on our JavaFX PDF Viewer implementation. He was a speaker at JavaOne 2014, where he did a joint session titled “Lessons learnt developing a NetBeans PDF viewer plugin in JavaFX”. Aside programming, he also enjoys a wide variety of hobbies which includes sky-diving, photography, exercising and listening to music.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>