http://dgdsbygo8mp3h.cloudfront.net/sites/default/files/imagecache/videoproduct_large/bookretailers/5729OS_Video.jpg

JSF static analysis in your build with Maven

apache_maven

Static analysis is in the verification of properties of software used in safety-critical computer systems and locating potentially vulnerable/buggy code. it is desirable to make your build fails at compile/test phases to detect faults earlier. Thanks to JSFUnit and Maven, you’ll be able to plug a JSF checker in your build with no effort.

JSFUnit is a test framework for JSF applications. It is designed to allow complete integration testing and unit testing of JSF applications using a simplified API. JSFUnit tests run inside the container, which provides the developer full access to managed beans, the FacesContext, EL Expressions, and the internal JSF component tree. At the same time, you also have access to parsed HTML output of each client request.

JSFUnit provides a set of unit tests for static analysis of JSF applications. Compare to JSFUnit, you can run these tests without any container, in Maven phase “test” like any regular Unit Test

Views tests (JSFUnitStaticAnalysisViewTest.java)

  • Do any of your facelets templates or well formed JSPs reference nonexistent managed beans?
  • Do any of your templates or JSPs have EL expressions for nonexistent managed bean actions or action listeners?

Faces-configurations tests (JSFUnitStaticAnalysisFacesConfigTest.java)

  • Are all of your session and application scoped beans Serializable?
  • Invalid Managed Bean Scope?
  • Missing Managed Bean Class?
  • Faces Configurations Class Inheritance?
  • Missing Setter Methods?
  • Duplicate Managed Bean Names?

TLD tests (JSFUnitStaticAnalysisFacesConfigTest.java)

  • Correct Tag Attribute Types?
  • Unique Tag Names?
  • Correct Tag Inheritance?
  • Unique Tag Attributes?

Install

Put the following in your web project pom.xml (the pom.xml with <packaging>war</packaging>) between <dependencies> .. </dependencies>, Note that this dependency is only available in scope “test”

<dependencies>
    <dependency>
        <groupId>org.jboss.jsfunit</groupId>
        <artifactId>jboss-jsfunit-analysis</artifactId>
        <version>1.0.0.GA</version>
        <scope>test</scope>
    </dependency>
    <!-- TLD test  dependencies  below, for 
         View and facesConfig not needed-->
    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.1.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>maven-taglib</groupId>
        <artifactId>maven-taglib-plugin</artifactId>
        <version>1.4.2</version>
        <scope>test</scope>
    </dependency>
</dependencies>

Add the following repository in your pom, settings.xml or your Maven proxy repository (Artifactory for example).

<Repositories>
    <Repository>
        <id>jboss</id>
        <url>http://repository.jboss.com/maven2/</url>
    </Repository>
</Repositories>
and
<PluginRepositories>
    <PluginRepository>
        <id>taglib</id>
        <url>http://maven-taglib.sourceforge.net/maven</url>
    </PluginRepository>
</PluginRepositories>

Create now 3 test classes in /src/test/java

JSFUnitStaticAnalysisViewTest.java

package com.waltercedric.jsfunit;

import java.util.HashSet;
import java.util.Set;
import org.jboss.jsfunit.analysis.AbstractViewTestCase;

public class JSFUnitStaticAnalysisViewTest extends AbstractViewTestCase {
  private static Set absoluteViewPaths = new HashSet<String>() {
    {
      // add("C:/work/project/src/home.xhtml");
    }
  };
  private static Set recursiveViewPaths = new HashSet<String>() {
    {
      add("src/main/resources/pages");
      add("src/main/resources/bottom");
      add("src/main/resources/top");
      add("src/main/resources/menu");
    }
  };
  public JSFUnitStaticAnalysisViewTest() {
    super(absoluteViewPaths, recursiveViewPaths,
    "src/main/resources/META-INF/faces-config.xml");
  }
}

JSFUnitStaticAnalysisFacesConfigTest.java

package com.waltercedric.jsfunit;

import java.util.HashSet;
import java.util.Set;
import org.jboss.jsfunit.analysis.AbstractFacesConfigTestCase;

public class JSFUnitStaticAnalysisFacesConfigTest extends AbstractFacesConfigTestCase {
  private static Set<String> paths = new HashSet<String>() {
    {
      add("src/main/resources/META-INF/faces-config.xml");
    }
  };
  public JSFUnitStaticAnalysisFacesConfigTest() {
    super(paths);
  }
}

JSFUnitStaticAnalysisTldTest.java

package com.waltercedric.jsfunit;

import java.util.HashSet;
import java.util.Set;
import org.jboss.jsfunit.analysis.AbstractTldTestCase;

public class JSFUnitStaticAnalysisTldTest extends AbstractTldTestCase {
  private static Set<String> paths = new HashSet<String>() {
    {
      add("src/main/resources/META-INF/facelets.core.taglib.xml");
    }
  };
  public TldTestCase() {
    super(paths);
  }
}
 

References

Related Posts

Review: Getting Started with Apache Maven by Russell Gold
Some time ago I was asked if I would like to write a review about one of the new video courses from Packt Publishing. It was "Getting Started with Apache Maven" http://bit.ly/1fycmpP by Russell Gold and since I have been using Maven for some years now (since 2007) and did publish...
193 days ago
Update Maven pom version on GIT checkout in TeamCity
Here is a solution to the following problems Deriving maven artifact version from GIT branch, Update pom version on GIT checkout automatically, Add the ability to use Pull request with Apache Maven. You have a workflow requirement that require you to have the artifact version of a...
198 days ago
Easily Compress Web Application Resources with EhCache
Resources such as JavaScript and CSS files can be compressed before being sent to the browser, improving network efficiencies and application load time in certain case. If you are not using Apache with mod_deflate or nginx in front of your web application, you may need to implement resources...
680 days ago
Tomcat 7 and Apache Maven
Here is 3 different way to control the lifetime a local Tomcat 7 container using Apache Maven. A typical scenario would be to start a servlet container prior to running integration tests (Selenium, SAHI or using any other framework you can think of ) With the following examples, you will be...
680 days ago
Apache Maven copy local file to a remote server server using SSH
I will show you in an Apache Maven configuration file how to copy files to server each time the package phase is executed. Solution with Ant SCP task This snippet of code is a ready to use code that make use of Apache Ant task scp, Just put this snippet of code in your Maven module where the...
868 days ago
Apache M2Eclipse: Get rid of Duplicate resources when opening resources and types
In this small post, I’ll show you how to remove duplicated resources in the Open Resource view of Eclipse Eclipse – M2Eclipse – Subversive ...
874 days ago
Apache Maven 3 Cookbook
  First a big thanks to Packt Publishing for having sent me this book to review! I did enjoy going through this book, while I did not learn a lot of new stuff (I am using Apache Maven daily since 2006!), I found it to be concise and would recommend it anytime to any of my colleagues....
1016 days ago
Apache Maven 3 Cookbook Review
Thanks to Packt Publishing for having sent me this book to review. I will publish a review in the next coming days Grasp the fundamentals and extend Apache Maven 3 to meet your needs Implement engineering practices in your application development process with Apache Maven ...
1062 days ago
blog comments powered by Disqus

Donations

Thank You for supporting my work