Archive

Posts Tagged ‘maven’

Tutorial – Your first CXF web service in 10 minutes

December 16th, 2009 Karthikeyan C 1 comment

This post is for Java developers who wish to start developing web services using CXF.It is assumed you have Tomcat 6.0.x installed on your machine and an IDE that supports Maven projects.

There are 2 Maven projects involved.

  • Project 1 is the web application (deployed in Tomcat) which contains the CXF web service which accepts time in milliseconds and returns the related java.util.Date object.
  • Project 2 is the client side standalone Java code invoking this web service.

Please click here to browse/checkout the source code of the web application is available in Google code.

Please click here to browse/checkout the source code of the standalone Java client.

We start with the web service interface provided below which has the method to convert time in milli seconds to Date format.


package com.karthik;

import javax.jws.WebParam;
import javax.jws.WebService;

@WebService
public interface DateConversionService {
 java.util.Date convertDate(@WebParam(name="timeInMilliSecs") Long timeInMilliSecs);
}

The implementation class is as below.


package com.karthik;

import java.util.Date;
import javax.jws.WebService;

@WebService(endpointInterface="com.karthik.DateConversionService")
public class DateConversionServiceImpl implements DateConversionService{

 @Override
 public Date convertDate(Long timeInMilliSecs) {
 return new Date(timeInMilliSecs);
 }

}

We have to inform CXF to deploy the web service by providing the necessary details in a Spring Bean configuration file. The file name is cxf.xml and contains the following code (Few XML name spaces are not needed but provided so that we can reuse the same for future posts in CXF series).


<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:jaxws="http://cxf.apache.org/jaxws"
 xmlns:cxf="http://cxf.apache.org/core"
 xmlns:http-conf="http://cxf.apache.org/transports/http/configuration"
 xsi:schemaLocation
 ="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
 http://cxf.apache.org/jaxws
 http://cxf.apache.org/schemas/jaxws.xsd
 http://cxf.apache.org/core
 http://cxf.apache.org/schemas/core.xsd
 http://cxf.apache.org/transports/http/configuration
 http://cxf.apache.org/schemas/configuration/http-conf.xsd">

 <!-- Load CXF modules from cxf.jar -->
 <import resource="classpath:META-INF/cxf/cxf.xml" />
 <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
 <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

 <jaxws:endpoint id="dateconversion"  address="/DateConversionWS" implementor="com.karthik.DateConversionServiceImpl"/>

</beans>

Now in web.xml we configure the URL at which CXF servlet will be listening and also inform the location of the configuration file (that is the above cxf.xml).


<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
 xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 <displayname>karthikeyanc.com CXF example</displayname>
 <context-param>
 <param-name>contextConfigLocation</param-name>
 <param-value>classpath:cxf.xml</param-value>
 </context-param>
 <listener>
 <listener-class>
 org.springframework.web.context.ContextLoaderListener
 </listener-class>
 </listener>
 <servlet>
 <servlet-name>CXFServlet</servlet-name>
 <servlet-class>
 org.apache.cxf.transport.servlet.CXFServlet
 </servlet-class>
 </servlet>
 <servlet-mapping>
 <servlet-name>CXFServlet</servlet-name>
 <url-pattern>/services/*</url-pattern>
 </servlet-mapping>
 </web-app>

Note: The pom.xml of this web application uses Maven assembly plugin to pack the web service interface which will  be referred as a dependency from the standalone Java client. By doing this we do not duplicate the web service interface file.

Please navigate to the project root folder and run mvn install. Under Tomcat_Home/conf/Catalina/localhost, create file called convertdate.xml to configure the context for this web application (As convertdate) and point to the target directory of the maven project as below. (Please change the docBase accordingly). Then start Tomcat. The WSDL can be obtained by typing the following in the URL http://localhost:8080/convertdate/services/DateConversionWS?wsdl


<Context path="/convertdate" docBase="/home/pathtoprojectroot/cxfwebappdateconverter/target/cxfdate"/>

Running the standalone Java client:

Now you may open the stand alone client project in an IDE (I use NetBeans 6.8) and run DateConversionServiceClient.java. This is the only Java file in the client project. The web service interface comes from the distribution jar ( created when running mvn install command for the web application Maven project). The source code of DateConversionServiceClient.java is as below. (Please change port number accordingly).


package com.karthik.client;

import com.karthik.DateConversionService;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

public class DateConversionServiceClient {

 private static final String ADDRESS="http://localhost:8080/convertdate/services/DateConversionWS";
 public static void main(String[] args) {
 JaxWsProxyFactoryBean fb=new JaxWsProxyFactoryBean();
 fb.setAddress(ADDRESS);
 fb.setServiceClass(DateConversionService.class);
 DateConversionService service=(DateConversionService) fb.create();
 System.out.println("The date converted is :"+service.convertDate(System.currentTimeMillis()));

 }
}
  • Share/Bookmark
Categories: Development Tags: ,

Step By Step Guide – Setup Seam 2.2.0.GA based Integration testing with Maven TestNG Cobertura

December 7th, 2009 Karthikeyan C 4 comments

This step by step guide explains / contains the following

  • Configure a Seam 2.2.0.GA, Hibernate (as the JPA implementation provider), RichFaces, PrimeFaces based project to be built by Maven (tested on 2.0.9 plus).
  • A simple Seam based web application which allows us to save (create and update) Employee details (just name and email to keep it simple) in MySQL database.
  • Write Seam based Integration test cases that run in JBoss Embedded container that run with TestNG.
  • Code coverage report generation using Cobertura.
  • The given project in this post is deployed on Tomcat 6.0.x.

The executable project code is available in Google code and can be obtained by downloading  here. (Please note that I have excluded files that ship with embedded JBoss download. I have provided screen shots of the final folder structure to avoid any confusion)

  1. Choose a folder of your choice. This will be known as ROOT folder for the project. Create a folder called src under it. Also create an empty file called pom.xml
  2. Under this src folder, create two folders called main and test.
  3. Under main folder, we will create the following folders – java, resources, sql and webapp.
  4. Under test folder, we will create the following folders – java, resources and a empty file called testng.xml.

What have we done so far ?:

- We have almost created a Seam based Maven archetype. The main folder contains files that are bundled in the deployable web application. test folder contains files used in various testing (Unit, Integration etc).

The final folder structure is provided as screen shots.

mainfoldertestfolder

Let us start with the employee table in database.


CREATE TABLE employee (
 id INTEGER NOT NULL AUTO_INCREMENT,
 name varchar(100) NOT NULL,
 email varchar(70) NOT NULL UNIQUE,
 PRIMARY KEY  (id)
);

The related JPA entity class is as below. The package is com.karthik.entity (full source code available in the zip file that can be downloaded).[This file is under src/main/java/com/karthik/entity]


@Entity
@Table(name = "employee")
@NamedQueries({
 @NamedQuery(name = "Employee.findAll", query = "SELECT e FROM Employee e")})
public class Employee implements Serializable {
 private static final long serialVersionUID = 1L;
 @Id
 @GeneratedValue(strategy = GenerationType.IDENTITY)
 @Basic(optional = false)
 @Column(name = "id")
 private Integer id;
 @Basic(optional = false)
 @Column(name = "name")
 private String name;
 @Basic(optional = false)
 @Column(name = "email")
 private String email;

Now we need a Seam backing bean to save a new Employee data, update existing Employee data and retrieve all employees in database. The source code of the backing bean EmployeeSaveAction.java is provided below. [This file is under src/main/java/com/karthik/action]


package com.karthik.action;

import com.karthik.entity.Employee;
import java.io.Serializable;
import java.util.List;

import javax.persistence.EntityManager;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Create;
import org.jboss.seam.annotations.Factory;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.datamodel.DataModel;
import org.jboss.seam.annotations.datamodel.DataModelSelection;

@Name("employeeaction")
public class EmployeeSaveAction implements Serializable{

 @In
 EntityManager em;
 private Employee employee;
 @DataModel
 List<Employee> employeeList;
 @DataModelSelection("employeeList")
 Employee selectedEmployee;

 @Create
 public void init() {
 employee = new Employee();
 }

 @Factory("employeeList")
 public void populateEmployeeList(){
 employeeList=em.createNamedQuery("Employee.findAll").getResultList();
 }

 public void createEmployee() {
 em.persist(employee);
 //clear UI values
 employee = new Employee();
 populateEmployeeList();
 }

 public void update(){
 selectedEmployee=em.merge(selectedEmployee);
 populateEmployeeList();
 }

 public Employee getEmployee() {
 return employee;
 }

 public void setEmployee(Employee employee) {
 this.employee = employee;
 }

}

Now we focus on configuring Seam to do the necessary injection in the above backing bean. First comes components.xml (this is under src/main/webapp/WEB-INF)


<?xml version="1.0" encoding="UTF-8"?>
<components xmlns="http://jboss.com/products/seam/components"
 xmlns:core="http://jboss.com/products/seam/core"
 xmlns:persistence="http://jboss.com/products/seam/persistence"
 xmlns:transaction="http://jboss.com/products/seam/transaction"
 xmlns:web="http://jboss.com/products/seam/web"
 xmlns:security="http://jboss.com/products/seam/security"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:ui="http://jboss.com/products/seam/ui"
 xmlns:mail="http://jboss.com/products/seam/mail"
 xsi:schemaLocation=
 "http://jboss.com/products/seam/core http://jboss.com/products/seam/core-2.1.xsd
 http://jboss.com/products/seam/persistence http://jboss.com/products/seam/persistence-2.1.xsd
 http://jboss.com/products/seam/transaction http://jboss.com/products/seam/transaction-2.1.xsd
 http://jboss.com/products/seam/security http://jboss.com/products/seam/security-2.1.xsd
 http://jboss.com/products/seam/components http://jboss.com/products/seam/components-2.1.xsd">

 <core:manager conversation-timeout="120000"
 concurrent-request-timeout="2000"
 conversation-id-parameter="cid"
 />
  <transaction:entity-transaction entity-manager="#{em}"/>
  <persistence:entity-manager-factory name="exampleDatabase"/>
  <persistence:managed-persistence-context name="em"
 auto-create="true"
 entity-manager-factory="#{exampleDatabase}"/>
 </components>

Now we need a UI to create, update values. Part of the source code of employee.xhtml is provided below [The file is under src/main/webapp]. Complete source code is provided in the zip file that can be downloaded  from Google code.


  <h:form>
            <rich:messages/>
            <s:validateAll>

                <rich:panel id="homepanel">
                    <f:facet name="header">
                       Seam Example
                    </f:facet>
                    <h:panelGrid columns="2">
                        <h:outputText value="Employee Name:"/>
                        <h:inputText value="#{employeeaction.employee.name}" required="true"/>

                        <h:outputText value="Email:"/>
                        <h:inputText value="#{employeeaction.employee.email}" required="true"/>

                        <h:commandButton action="#{employeeaction.createEmployee}" value="Create Employee"/>
                    </h:panelGrid>
                    <br/>
                    List of Existing Employees
                    <br/>
                    <rich:dataTable value="#{employeeList}" var="emp">
                        <rich:column>
                            <f:facet name="header">
                               Name
                            </f:facet>
                            <h:inputText value="#{emp.name}"/>
                        </rich:column>
                        <rich:column>
                            <f:facet name="header">
                                Email
                            </f:facet>
                            <h:inputText value="#{emp.email}"/>
                        </rich:column>
                        <rich:column>
                            <f:facet name="header">
                                Update
                            </f:facet>
                            <h:commandButton action="#{employeeaction.update}" value="Update"/>
                        </rich:column>

                    </rich:dataTable>
                </rich:panel>
            </s:validateAll>
        </h:form>

The faces-config.xml file is provided below (File is under src/main/webapp/WEB-INF)

<?xml version="1.0" encoding="UTF-8"?>
<faces-config version="1.2"
   xmlns="http://java.sun.com/xml/ns/javaee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd">

    <!-- Facelets support -->
    <application>
        <view-handler>com.sun.facelets.FaceletViewHandler</view-handler>
    </application>

</faces-config>

We do not utilize pages.xml defining page navigation in Seam but for the sake of completeness it is provided below. (File is under src/main/webapp/WEB-INF)

<?xml version="1.0" encoding="UTF-8"?>
<pages xmlns="http://jboss.com/products/seam/pages"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://jboss.com/products/seam/pages http://jboss.com/products/seam/pages-2.1.xsd">
</pages>

And here comes web.xml (The file is under src/main/webapp/WEB-INF].

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <!-- Seam   -->

    <listener>
        <listener-class>org.jboss.seam.servlet.SeamListener</listener-class>
    </listener>

    <filter>
        <filter-name>Seam Filter</filter-name>
        <filter-class>org.jboss.seam.servlet.SeamFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>Seam Filter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <filter>
        <filter-name>richfaces</filter-name>
        <filter-class>org.ajax4jsf.Filter</filter-class>
        <init-param>
            <param-name>forceparser</param-name>
            <param-value>false</param-value>
        </init-param>
        <init-param>
            <param-name>enable-cache</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>richfaces</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <servlet>
        <servlet-name>Seam Resource Servlet</servlet-name>
        <servlet-class>org.jboss.seam.servlet.SeamResourceServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Seam Resource Servlet</servlet-name>
        <url-pattern>/seam/resource/*</url-pattern>
    </servlet-mapping>
    <servlet>
        <servlet-name>Document Store Servlet</servlet-name>
        <servlet-class>org.jboss.seam.document.DocumentStoreServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Document Store Servlet</servlet-name>
        <url-pattern>*.csv</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>Document Store Servlet</servlet-name>
        <url-pattern>*.xls</url-pattern>
    </servlet-mapping>
    <servlet>
        <servlet-name>Resource Servlet</servlet-name>
        <servlet-class>org.primefaces.resource.ResourceServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Resource Servlet</servlet-name>
        <url-pattern>/primefaces_resource/*</url-pattern>
    </servlet-mapping>
     <!-- Faces Servlet -->
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
     <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.seam</url-pattern>
    </servlet-mapping>
     <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.jsf</url-pattern>
    </servlet-mapping>

    <!-- JSF parameters -->
    <context-param>
        <param-name>javax.faces.DEFAULT_SUFFIX</param-name>
        <param-value>.xhtml</param-value>
    </context-param>
    <context-param>
        <param-name>facelets.DEVELOPMENT</param-name>
        <param-value>true</param-value>
    </context-param>
    <context-param>
        <param-name>org.richfaces.SKIN</param-name>
        <param-value>DEFAULT</param-value>
    </context-param>
    <context-param>
        <param-name>org.ajax4jsf.COMPRESS_SCRIPT</param-name>
        <param-value>true</param-value>
    </context-param>
    <session-config>
        <session-timeout>20</session-timeout>
    </session-config>
  </web-app>

Dont get bored :) , there are only two more files left under main folder that are part of our web application.
We need to have a marker file called seam.properties under src/main/resources folder (that will eventually be under WEB-INF/classes of your web application).
Seam will scan and register our Seam components only if this file is detected.

And the last one is persistence.xml (The file is under src/main/resources/META-INF)

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
  <persistence-unit name="exampleDatabase" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <!--Uncomment the below when you use Tomcat-->
   <jta-data-source>java:comp/env/jdbc/exampleDatabase</jta-data-source>
    <!--Uncomment the below when you run test cases in embedded server-->
    <!--<non-jta-data-source>java:/exampleDatabase</non-jta-data-source>-->

    <properties>
      <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
      <property name="hibernate.hbm2ddl.auto" value="validate"/>
      <property name="hibernate.show_sql" value="false"/>
    </properties>
  </persistence-unit>
</persistence>

Now we shift our focus to test folder. Let us start with testng.xml (TestNG configuration file which is under src/test)

<!DOCTYPE suite SYSTEM "http://beust.com/testng/testng-1.0.dtd" >
<suite name="KarthiksExampleSuite">
    <parameter name="datasourceJndiName" value="java:/exampleDatabase"/>
    <parameter name="database" value="MySQL" />
    <parameter name="binaryDir" value="img/" />
    <test name="Group Test">
        <packages>
            <package name="com.karthik.test" />
        </packages>
   </test>
</suite>

Next file is the Seam based TestNG test case which runs test cases to verify the creation of a new Employee in database. The file is under src/test/java/com/karthik/test
and the source code is provided below.


package com.karthik.test;

import com.karthik.entity.Employee;
import javax.faces.model.DataModel;
import org.jboss.seam.mock.DBUnitSeamTest;
import org.testng.annotations.Test;

public class EmployeeTest extends DBUnitSeamTest {

    private int numOfEmpBeforeAdd;
    private String name;
    private String email;

    @Test(groups = "embeddedcontainer")
    public void testEmployee() throws Exception {

        new FacesRequest("/employee.xhtml") {

            @Override
            protected void updateModelValues() throws Exception {
                name = "Name of employee" + System.nanoTime();
                email = "abc" + System.nanoTime() + "@xmail.com";

                setValue("#{employeeaction.employee.name}", name);
                setValue("#{employeeaction.employee.email}", email);
            }

            @Override
            protected void invokeApplication() {
                DataModel empList = (DataModel) getValue("#{employeeList}");
                numOfEmpBeforeAdd = empList.getRowCount();
                assert invokeAction("#{employeeaction.createEmployee}") == null;

            }

            @Override
            protected void renderResponse() {

                DataModel empList = (DataModel) getValue("#{employeeList}");
                assert empList.getRowCount() == (numOfEmpBeforeAdd + 1);
                empList.setRowIndex(numOfEmpBeforeAdd + 1);
                boolean isEmployeeAvailable = false;
                for (int ctr = 0; ctr < empList.getRowCount(); ctr++) {
                    empList.setRowIndex(ctr);
                    Employee employee = (Employee) empList.getRowData();

                    if (employee.getName().equals(name) && employee.getEmail().equals(email)) {
                        isEmployeeAvailable = true;
                    }

                }//for loop ends
                assert isEmployeeAvailable;
            }
        }.run();
    }

    @Override
    protected void prepareDBUnitOperations() {
    }
}

Now comes the most important part of configuring the embedded JBoss container. The configuration is involves 3 easy steps.
Step 1: Please download embedded-jboss-beta3.SP10.zip. Extract it to any temporary folder and copy the content of bootstrap folder (do not
copy bootstrap but only the content in it) and paste it under src/test/resources
Step 2: Configure the datasource for the embedded JBoss container. The file testmysql-ds.xml (The file is placed under src/test/resources/deploy)
is as below.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE datasources
    PUBLIC "-//JBoss//DTD JBOSS JCA Config 1.5//EN"
    "http://www.jboss.com/j2ee/dtd/jboss-ds_1_5.dtd">
<datasources>
   <local-tx-datasource>
      <jndi-name>exampleDatabase</jndi-name>
      <connection-url>jdbc:mysql://localhost:3306/ck</connection-url>
      <driver-class>com.mysql.jdbc.Driver</driver-class>
      <user-name>root</user-name>
      <password>password</password>
   </local-tx-datasource>
</datasources>

Step 3: In the above testng.xml configuration we had mentioned the binary folder as img. So create a folder called img under src/test/resources
and place an empty marker file called marker.txt (An empty marker file is needed so that Maven copies the empty folder also).

With that we complete the files under test folder. And now the most important file pom.xml which is under ROOT folder parallel to src folder.


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<properties>
<seam.version>2.2.0.GA</seam.version>
<richfaces.version>3.3.2.GA</richfaces.version>
</properties>
<modelVersion>4.0.0</modelVersion>
<groupId>com.karthik</groupId>
<artifactId>seamtest</artifactId>
<packaging>war</packaging>
<version>1.0</version>
<name>karthiks Seam TestNG Example</name>
<build>
<finalName>seamtest</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<configuration>
<artifactItems>
<artifactItem>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.1</version>
</artifactItem>
</artifactItems>
<outputDirectory>${project.build.testOutputDirectory}/endorsed</outputDirectory>
</configuration>
<id>download-jaxb-api</id>
<phase>process-test-resources</phase>
<goals>
<goal>copy</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>process-test-resources</phase>
<configuration>
<tasks>
<copy todir="${project.build.testOutputDirectory}">
<fileset dir="${basedir}/src/main/webapp"/>
</copy>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.4</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>src/test/testng.xml</suiteXmlFile>
</suiteXmlFiles>

<childDelegation>true</childDelegation>
<useSystemClassLoader>true</useSystemClassLoader>
<argLine>-Djava.endorsed.dirs=${project.build.testOutputDirectory}/endorsed -Dsun.lang.ClassLoader.allowArraySyntax=true</argLine>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.1</version>
<configuration>
<check>
<haltOnFailure>false</haltOnFailure>
<regexes>
<regex>
<pattern>com.karthik.*</pattern>
<branchRate>70</branchRate>
<lineRate>70</lineRate>
</regex>

</regexes>
</check>
<instrumentation>
<includes>
<include>com/**/*.class</include>

</includes>
</instrumentation>
</configuration>
<executions>
<execution>
<id>clean</id>
<phase>pre-site</phase>
<goals>
<goal>clean</goal>
</goals>
</execution>
<execution>
<id>instrument</id>
<phase>site</phase>
<goals>
<goal>instrument</goal>
<goal>cobertura</goal>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>repository.jboss.org</id>
<url>http://repository.jboss.org/maven2</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>repository.central.org</id>
<url>http://repo1.maven.org/maven2</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>primefaces.repo</id>
<name>Prime Technology Maven Repository</name>
<url>http://repository.prime.com.tr</url>
<layout>default</layout>
</repository>
</repositories>
<dependencies>
<!--Start of dependencies for embedded JBoss container required for Integration test cases-->
<dependency>
<groupId>org.jboss.embedded</groupId>
<artifactId>jboss-embedded-all</artifactId>
<version>beta3.SP10</version>
<exclusions>
<exclusion>
<groupId>org.jboss.embedded</groupId>
<artifactId>jboss-embedded</artifactId>
</exclusion>

<exclusion>
<groupId>org.jboss.microcontainer</groupId>
<artifactId>jboss-deployers-client-spi</artifactId>
</exclusion>
<exclusion>
<groupId>org.jboss.microcontainer</groupId>
<artifactId>jboss-deployers-core-spi</artifactId>
</exclusion>
</exclusions>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.embedded</groupId>
<artifactId>thirdparty-all</artifactId>
<version>beta3.SP10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.embedded</groupId>
<artifactId>jboss-embedded</artifactId>
<version>beta3.SP10</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.jboss.microcontainer</groupId>
<artifactId>jboss-deployers-client-spi</artifactId>
</exclusion>

</exclusions>
</dependency>
<!--End of dependencies for embedded JBoss container required for Integration test cases-->

<!--Start of TestNG dependency-->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>5.10</version>
<scope>test</scope>
<classifier>jdk15</classifier>
</dependency>
<!--el is required to parse the SeamTest setValue and invokeMethod stuff-->
<dependency>
<groupId>javax.el</groupId>
<artifactId>el-api</artifactId>
<version>1.2</version>
<scope>test</scope>
</dependency>
<!-- If you use any database other than MySQL please change the below dependency accordingly-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.dbunit</groupId>
<artifactId>dbunit</artifactId>
<version>2.2</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>xerces</groupId>
<artifactId>xercesImpl</artifactId>
</exclusion>
<exclusion>
<groupId>xerces</groupId>
<artifactId>xmlParserAPIs</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--End of TestNG dependency-->
<dependency>
<groupId>org.jboss.seam</groupId>
<artifactId>jboss-seam</artifactId>
<version>${seam.version}</version>
<exclusions>
<exclusion>
<groupId>javax.el</groupId>
<artifactId>el-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.jboss.seam</groupId>
<artifactId>jboss-seam-ui</artifactId>
<version>${seam.version}</version>
</dependency>
<dependency>
<groupId>org.jboss.seam</groupId>
<artifactId>jboss-seam-pdf</artifactId>
<version>${seam.version}</version>
</dependency>
<dependency>
<groupId>org.jboss.seam</groupId>
<artifactId>jboss-seam-mail</artifactId>
<version>${seam.version}</version>
</dependency>
<dependency>
<groupId>org.jboss.seam</groupId>
<artifactId>jboss-seam-ioc</artifactId>
<version>${seam.version}</version>
</dependency>
<dependency>
<groupId>org.jboss.seam</groupId>
<artifactId>jboss-seam-remoting</artifactId>
<version>${seam.version}</version>
</dependency>
<dependency>
<groupId>org.jboss.seam</groupId>
<artifactId>jboss-seam-rss</artifactId>
<version>${seam.version}</version>
</dependency>
<dependency>
<groupId>org.jboss.seam</groupId>
<artifactId>jboss-seam-excel</artifactId>
<version>${seam.version}</version>
</dependency>

<!-- Seam makes use of Hibernate which  depends on JTA and JPA-->
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>3.0.0.GA</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-commons-annotations</artifactId>
<version>3.1.0.GA</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>3.4.0.GA</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.4.0.GA</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-search</artifactId>
<version>3.1.0.GA</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.5.8</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.5.8</version>
</dependency>

<dependency>
<groupId>javax.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>1.2_12</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>1.2_12</version>
</dependency>
<dependency>
<groupId>com.sun.facelets</groupId>
<artifactId>jsf-facelets</artifactId>
<version>1.1.14</version>
</dependency>
<dependency>
<groupId>org.richfaces.framework</groupId>
<artifactId>richfaces-api</artifactId>
<version>${richfaces.version}</version>
</dependency>
<dependency>
<groupId>org.richfaces.framework</groupId>
<artifactId>richfaces-impl</artifactId>
<version>${richfaces.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate</artifactId>
<version>3.2.6.ga</version>
</dependency>
<dependency>
<groupId>org.richfaces.ui</groupId>
<artifactId>richfaces-ui</artifactId>
<version>${richfaces.version}</version>

</dependency>

<!--Java Mail Related dependencies-->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.1</version>
</dependency>

<!--Log4j-->
<dependency>
<groupId>apache-log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>
<dependency>
<groupId>jfree</groupId>
<artifactId>jfreechart</artifactId>
<version>1.0.8a</version>
</dependency>
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.8.0.GA</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>servlet-api</artifactId>
<version>6.0.16</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<version>1.0.0.RC</version>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>1.6.0</version>
</dependency>
</dependencies>
</project>

Things to consider:

To run the Integration test cases comment the Tomcat specific datasource configuration and uncomment JBoss specific datasource configuration as shown in the comment section of persistence.xml reproduced below. (You can avoid this ugly way of comment and uncomment by using Maven profiles – one profile for testing and the other for a deployable in Tomcat).


<!--Uncomment the below when you use Tomcat-->
<!--jta-data-source>java:comp/env/jdbc/exampleDatabase</jta-data-source-->
<!--Uncomment the below when you run test cases in embedded server-->
<non-jta-data-source>java:/exampleDatabase</non-jta-data-source>

The test cases are executed when we run a command like mvn install

To run cobertura and generate reports, the command is mvn cobertura:cobertura (The cobertura reports will be under target/site/cobertura. Please click on the index.html to view the report.)

coberturareport

To create a deployable, make the required change in persistence.xml (as explained in Things to consider) and run the command like mvn install -Dmaven.test.skip (As the test cases will not execute with a Tomcat specific data source configuration in persistence.xml. I will try to do more analysis on this and check if it is possible to pick up different files one for test and the other to be packaged in web application deployable).

Hope this post is of some use. Please provide your suggestions (to improve this configuration) as a comment.

Seam Example By Karthikeyan C Seam Example
List of Existing Employees
Name Email Update
  • Share/Bookmark

How to skip test execution phase in Maven

November 17th, 2009 Karthikeyan C No comments

Though it is considered not a good practice to skip tests execution, in few scenarios we may want to skip the execution of tests in Maven. [For example when the execution of tests need special setup which may not be available to the build machine.]

we can use the following command which mentions the argument maven.test.skip to skip test execution phase.

mvn -Dmaven.test.skip=true clean install
  • Share/Bookmark
Categories: Development Tags: