Showing posts with label Maven2. Show all posts
Showing posts with label Maven2. Show all posts

Saturday, November 28, 2009

Selenium test reporting with maven surefire report plugin

If you are familiar with Junit, you may have already known the maven surefire plugin which enables running unit test inside maven build cycles. Normally, surefire plugin generates the XML test reports at /target/surefire-reports directory. If you use selenium RC integrated with your build system or as a separate test framework, you may use maven-selenium-plugin coupled with the surefire plugin to launch your tests.
As far as I know, there are two ways to generate test reports when selenium is coupled with maven.

1. Use LoggingSelenium java library to generate HTML test reports with screenshots

2. Use maven-surefire-report plugin

This post is about the latter one, maven-surefire-report plugin.

The default configuration of surefire-report-plugin is trivial. You just need to add a <reporting> element to projects' pom.xml as follows.

<reporting>
<plugins>
<plugin>

<groupId>org.apache.maven.plugins</groupId> <
<configuration>

<outputDirectory>${basedir}/target/wso2qa</outputDirectory>

</configuration>

</plugin>
</plugins>

</reporting>


When you run mvn surefire-report:report goal after running the tests, HTML based descriptive report will be created at the specified output directory. In this mechanism, you do not use anything specific to selenium,but get the advantage of surefire report plugin in test reporting.

Friday, October 16, 2009

How to pass system properties to maven surefire plugin test run

While developing WSO2 QA test framework (which is based on selenium RC), we wanted to give users the choice of selecting test cases without running whole suite at once. Our plan was to pass a system property during maven test run. However, passing a system property directly through command line does not work since the surefire plugin is run under a different JVM instance that is launched by Maven.
In order to overcome this, maven surefire plugin can be configured as follows by specifying the required system property.

<systemProperties>
<property>
<name>test.suite</name>
<value>${test.suite}</value>
</property>
</systemProperties>

According to this example configuration, we can run test suite with passing a system property as follows.

'mvn clean install -Dtest.suite=test-case'

Saturday, April 4, 2009

How to add FindBugs maven plugin to your project

FindBugs is a very useful static analyzer which inspects java bytecode for bug patterns. Static code inspection is important element in a good continuous integration process. Integrating FindBugs to your Maven build system is extremely simple as follows.

1. Add FindBugs maven2 plugin to the root pom of your maven project. You can add the plugin configuration element as a child of <reporting> element.

<reporting>

<plugins>

<plugin>

<groupId>org.codehaus.mojo</groupId>

<artifactId>findbugs-maven-plugin</artifactId>

<version>2.0</version>

<configuration>

<xmlOutput>true</xmlOutput>

<xmlOutputDirectory>C:\projects</xmlOutputDirectory>

</configuration>

</plugin>

</plugins>

</reporting>

Note: The xmlOutputDiectory is hard coded intentionally for demonstration purpose.

2. Go to the directory where the above pom.xml is placed and issue the following command.
mvn findbugs:findbugs

This will generate an XML report in the specified output directory. Here is an excerpt from such report.

<file classname="org.test.ExampleService">

<BugInstance type="OBL_UNSATISFIED_OBLIGATION" priority="Normal" category="EXPERIMENTAL" message="OBL: Method org.test.ExampleService.archiveFile(String, String) may fail to clean up stream or resource of type java.io.InputStream" lineNumber="69" />

<BugInstance type="OBL_UNSATISFIED_OBLIGATION" priority="Normal" category="EXPERIMENTAL" message="OBL: Method org.test.utils.ArchiveManipulator.extract(String, String) may fail to clean up stream or resource of type java.io.InputStream" lineNumber="114" />

<BugInstance type="OS_OPEN_STREAM" priority="Normal" category="BAD_PRACTICE" message="OS: org.test.utils.ArchiveManipulator.extractFromStream(InputStream, String) may fail to close stream" lineNumber="148" />

-------

<BugInstance type="RV_RETURN_VALUE_IGNORED_BAD_PRACTICE" priority="Normal" category="BAD_PRACTICE" message="RV: org.test.utils.ArchiveManipulator.extractFromStream(InputStream, String) ignores exceptional return value of java.io.File.mkdirs()" lineNumber="124" />

</file>

Thats all! For more information about plugin usage and configuration parameters, havea look at the plugin home page.

Sunday, November 2, 2008

Axis2 java2wsdl maven plugin

I demonstrated the usage of Maven2 WSDL2Code plugin in a previous post. Apache Axis2 provides with a Java2WSDL maven2 plugin as well. Maven2 Java2Wsdl plugin can be used to generate WSDL from a java class. The following steps will help you to create a wsdl from a java class using Axis2 java2wsdl maven plugin.

Step 1
Create a mavan project (See step 1 of ).
Create a java class in the source directory of your maven project. (i.e:- Create Calculator.java class at \src\main\java\com\test directory)


Step 2

Update the pom.xml of your maven project as follows.

<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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>calculator</artifactId>
<version>1.0-SNAPSHOT</version>
<name>calculator</name>
<url>http://maven.apache.org</url>
<build>
<plugins>
<plugin>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-java2wsdl-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>java2wsdl</goal>
</goals>
</execution>
</executions>
<configuration>
<className>com.test.Calculator</className>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2</artifactId>
<version>1.4</version>
</dependency>
</dependencies>
</project>

Note the highlighted elements in the above pom. First we added a new <plugin> to use java2wsdl goal. This goal accepts a set of parameters as explained in Axis2 online documentation.
In this example we used the simplest configuration parameter, <className>, which defines the fully qualified name of class from which the WSDL is generated.

Also, make sure to add a dependency to Axis2 jars in your pom.xml.

Step 3

Go to the root directory of your project structure and run the following command.

mvn clean axis2-java2wsdl:java2wsdl

You could find the generated wsdl at target\generated-resources\java2wsdl\ directory.

Tuesday, October 21, 2008

How to use Maven2 WSDL2Code plugin in Axis2

Apache Axis2 ships with a lot of useful tools to make web service developer's life easier. Maven2 WSDL2Code plugin is one of them which can be used to generate server side skeletons or client stubs from a given WSDL using a maven pom.xml.
Lets see how this plugin can be used.

Pre-requistes:
Apache Maven2

Step1

Create a maven project using maven archetype template (You may ignore this step and use an existing project if you are familiar with maven)

mvn archetype:create -DgroupId=com.test -DartifactId=calculator

This will create a maven project structure as follows.



Step 2

Create a directory (i.e:- resources) at src\main and copy your WSDL file there.
Now, remove the existing contents of the auto-generated pom.xml (calculator\pom.xml) and add the following configuration.

<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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>calculator</artifactId>
<version>1.0-SNAPSHOT</version>
<name>calculator</name>
<url>http://maven.apache.org</url>
<build>
<plugins>
<plugin>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-wsdl2code-maven-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<goals>
<goal>wsdl2code</goal>
</goals>
</execution>
</executions>
<configuration>
<packageName>org.charitha</packageName>
<wsdlFile>src/main/resources/calculator.wsdl</wsdlFile>
<databindingName>adb</databindingName>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2</artifactId>
<version>1.4</version>
</dependency>
</dependencies>
</project>

Note the highlighted elements in the above pom. First we added a new <plugin> to use wsdl2code goal. The WSDL2Code goal takes a set of input parameters as explained here.
In this example, we use 3 configuration parameters.
<packageName> - The generated source will be added to this package
<wsdlFile> - The location of the input wsdl file
<databindingName> - Databinding mechanism used for code generation

Also, we need to add a dependency to Axis2 jars.

Step 3

Go to the root directory of your project structure (i.e:- calculator directory where pom.xml exists) and run the following command.

mvn clean axis2-wsdl2code:wsdl2code

You could find the generated classes at target\generated-sources\axis2\wsdl2code directory.

Note: The sample wsdl used for the above example can be found at http://ww2.wso2.org/~charitha/calculator.wsdl

Sunday, October 12, 2008

How to make an OSGI bundle using maven bundle plugin

OSGI (Open systems Gateway Initiative) specification defines a complete and dynamic component model which can be remotely installed, started, updated, stopped and uninstalled without restarting JVM. I'm not going to discuss the specification details of OSGI. You can find a lot of information from www.osgi.org
The objective of this post is to create a simple OSGI bundle using Apache Felix maven bundle plugin.

Pre-requisites:
Apache Maven2

Step 1
First we need to create a simple java class and which must be included in a proper directory structure therefore we can use Maven2 to build the source.

Create the following directory structure in your file system.

Create the following interface.

package org.wso2;

public interface GreetingService {
public void sayGreeting(String s);
}

Create an Impl directory under wso2 and add the following class in there.

package org.wso2.Impl;

import org.wso2.GreetingService;

public class GreetingServiceImpl implements GreetingService {

public void sayGreeting(String s){
System.out.println("Welcome " +s);

}
}

Step 2

We have created an interface and its implementation. We have not done anything new for you so far.
Since we are going to create an OSGI bundle, we need to add a BundleActivator though it is not mandatory to have. Bundle Activator class is used to start and stop the bundle when it is installed on an OSGI framework such as Knopflerfish.
Lets create an implementation of BundleActivator at the same package where GreetingServiceImpl is placed.

package org.wso2.Impl;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.wso2.Impl.GreetingServiceImpl;

public class Activator implements BundleActivator {
public void start(BundleContext bundleContext) throws Exception {
GreetingServiceImpl greetingServiceImpl = new GreetingServiceImpl();
greetingServiceImpl.sayGreeting("Charitha");

}

public void stop(BundleContext bundleContext) throws Exception {

}
}

Step3
Now we can create the pom.xml to compile above classes and make an OSGI bundle (Basically we need to generate manifest headers in MANIFEST.MF of the generated bundle jar)
Here is the complete pom.xml. Note the highlighted section.

<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">

<modelVersion>4.0.0</modelVersion>


<groupId>org</groupId>
<artifactId>wso2</artifactId>
<packaging>bundle</packaging>
<version>1.0</version>

<dependencies>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.osgi.core</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>

<name>Simple OSGI example</name>
<description>A bundle to demonstrate maven bundle plugin.</description>


<properties>
<bundle.namespace>${pom.groupId}.${pom.artifactId}</bundle.namespace>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>1.4.0</version>
<extensions>true</extensions>
<configuration>
<instructions>
<Private-Package>${bundle.namespace},${bundle.namespace}.Impl</Private-Package>
<Export-Package>${bundle.namespace}</Export-Package>
<Bundle-Activator>${bundle.namespace}.Impl.Activator</Bundle-Activator>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
</project>

The <Export-Package> element tells the plugin which of the available packages to copy into the bundle and export. The <Private-Package> instruction indicates which of the available packages to copy into the bundle but not export. In other words, private packages are not exported by the bundle.
Here, we define our impl package, org.wso2.Impl as a private package.
These elements will be added as manifest headers in resulting bundle jar as we will see shortly.

Step 4
Save pom.xml at the root directory of the above folder tree. (e.g:- example directory) and run it with mvn clean install

Bundle will be created at the target directory. Open it and look at META-INF/MANIFEST.MF

Manifest-Version: 1.0

Built-By: Charitha

Created-By: Apache Maven Bundle Plugin

Bundle-Activator: org.wso2.Impl.Activator

Import-Package: org.osgi.framework;version="1.3",org.wso2

Bnd-LastModified: 1223869521406

Export-Package: org.wso2

Bundle-Version: 1.0

Bundle-Name: Simple OSGI example

Bundle-Description: A bundle to demonstrate maven bundle plugin.

Build-Jdk: 1.5.0_14

Private-Package: org.wso2.Impl

Bundle-ManifestVersion: 2

Bundle-SymbolicName: org.wso2

Tool: Bnd-0.0.238


Have a look at the Export-Package and Private-Package headers which were added to the manifest as we defined them in pom.xml.
Now our bundle is ready to deploy in an OSGI framework. We will see how this bundle can be deployed on Knopflerfish in a future blog post.

Friday, September 5, 2008

How to avoid OutofMemory error when building projects with Maven2

You may have encountered outofMemory issues when building source with Maven2. This normally happens when Maven runs out of available system memory.
In such case you have to set MAVEN_OPTS environment variable to increase the available memory.

In Windows
Open a command prompt in which you build the source and enter the following options. (Change the memory size according to the amount of memory in your system)
SET MAVEN_OPTS=-Xmx1024m

In Linux
Open a shell and set the options as follows.
export MAVEN_OPTS="-Xmx1024m -XX:MaxPermSize=128m"

Thursday, July 3, 2008

WSDL2Code UI tool - Easy and efficient code generation utility

If you are an Apache axis2 user, you may already familiar with WSDL2Java code generation tool. WSO2 WSAS is powered by Apache Axis2 and includes a rich set of features to interact with web services. WSDL2Code is a UI based tool integrated with WSO2 WSAS which allows users to generate code from a WSDL hosted in a remote server or a local file system.
Lets see how WSDL2Code tool makes web service developement effort much easier and productive.

Step 1
Download and Install WSO2 WSAS

Step 2
Start wso2wsas. Go to WSAS_HOME/bin and run wso2wsas.bat{sh}

Step 3
Go to http://localhost:9762
Welcome page of the WSAS console will be displayed. Click on WSDL2Code at the left navigation menu. You will be directed to the following screen.



You can see multiple code generation options in the above screen. These code gen options are similar to the command line options available in WSDL2java tool.

Step 4
Lets generate client stub against default version service using the above tool.
Enter 'http://localhost:9762/services/version?wsdl' in the -uri option available on the top of the page.
Select -uw option and click on 'Generate'
It will prompt to save the generated zip file (e.g:- 1.2151049949841125E12.zip). Save it in a directory in your local file system.

Step 5
Unzip the generated file (You will see src folder, build.xml and pom.xml in the extracted directory) and go to the directory where you extracted the zip file.
Assuming your IDE is eclipse, enter the following command to export the generated code in to eclipse and setting up WSAS libraries at once.

mvn eclipse:eclipse


(If you have not installed maven2, download it from here and add maven_home/bin to your PATH)

You will see 'BUILD SUCCESSFUL' message at the end of executing above command.

Step 6
Open eclipse and go to Window-->Preferences-->Java-->Build path-->Classpath variables
Create a new variable, M2_REPO and enter your maven2 repository path as the value. (maven2 repository is located at user home directory. e.g:- C:/Documents and Settings/Charitha/.m2/repository)
(Note that this step has to be done only once. If the M2_REPO variable is already defined, you can ignore this step)

Step 7
In eclipse, select File-->import-->Existing projects into workspace-->next
Browse root directory of the extracted zip file above. Click 'Finish'.
New project will be added to the workspace with an id like N10001-version. You should see that all the necessary WSAS libraries are added to the project.

Now the only remaining step is to write client to invoke version service as follows

public class VersionClient {

public static void main(String[] args)throws Exception {
VersionStub stub = new VersionStub();
System.out.println(stub.getVersion());
}

}

You may realize how WSO2 WSAS reduce the complexity of web service invocation with these kind of tools. Even if you are beginner to the world of SOA, WSO2 WSAS can be considered as the best paltform to get started with minimum effort.