Java EE 6 Testing Part II – Introduction to Arquillian and ShrinkWrap

May 3rd, 2012 by Samuel Santos 3 comments »

In Java EE 6 Testing Part I I briefly introduced the EJB 3.1 Embeddable API using Glassfish embedded container to demonstrate how to start the container, lookup a bean in the project classpath and run a very simple integration test.

This post focus on Arquillian and ShrinkWrap and why they are awesome tools for integration testing of enterprise Java applications.

The source code used for this post is available on GitHub under the folder arquillian-shrinkwrap.

The tools

Arquillian

Arquillian brings test execution to the target runtime, alleviating the burden on the developer of managing the runtime from within the test or project build. To invert this control, Arquillian wraps a lifecycle around test execution that does the following:

  • Manages the lifecycle of one or more containers
  • Bundles the test case, dependent classes and resources as ShrinkWrap archives
  • Deploys the archives to the containers
  • Enriches the test case with dependency injection and other declarative services
  • Executes the tests inside (or against) the containers
  • Returns the results to the test runner for reporting
ShrinkWrap

ShrinkWrap, a central component of Arquillian, provides a simple mechanism to assemble archives like JARs, WARs, and EARs with a friendly, fluent API.

One of the major benefits of using Arquillian is that you run the tests in a remote container (i.e. application server). That means you’ll be testing the real deal. No mocks. Not even embedded runtimes!

Agenda

The following topics will be covered on this post:

  • Configure the Arquillian infrastructure in a Maven-based Java project
  • Inject EJBs and Managed Beans (CDI) directly in test instances
  • Test Java Persistence API (JPA) layer
  • Run Arquillian in client mode
  • Run and debug Arquillian tests inside your IDE

» Read more: Java EE 6 Testing Part II – Introduction to Arquillian and ShrinkWrap

Java EE 6 Testing Part I – EJB 3.1 Embeddable API

December 6th, 2011 by Samuel Santos 2 comments »

One of the most common requests we hear from Enterprise JavaBeans developers is for improved unit/integration testing support.
EJB 3.1 Specification introduced the EJB 3.1 Embeddable API for executing EJB components within a Java SE environment.

Unlike traditional Java EE server-based execution, embeddable usage allows client code and its corresponding enterprise beans to run within the same JVM and class loader. This provides better support for testing, offline processing (e.g. batch), and the use of the EJB programming model in desktop applications.
[...]
The embeddable EJB container provides a managed environment with support for the same basic services that exist within a Java EE runtime: injection, access to a component environment, container-managed transactions, etc. In general, enterprise bean components are unaware of the kind of managed environment in which they are running. This allows maximum reusability of enterprise components across a wide range of testing and deployment scenarios without significant rework.

» Read more: Java EE 6 Testing Part I – EJB 3.1 Embeddable API

Changing URL parameters with jQuery

September 20th, 2011 by Samuel Santos No comments »

You can find plenty of resources about this topic just by googling the web, most of which will point to jQuery plugins.
But the fact is that it’s so easy to achieve this by simply using jQuery that you do not need a plugin.

The code is pretty much self explanatory:

/*
 * queryParameters -> handles the query string parameters
 * queryString -> the query string without the fist '?' character
 * re -> the regular expression
 * m -> holds the string matching the regular expression
 */
var queryParameters = {}, queryString = location.search.substring(1),
    re = /([^&=]+)=([^&]*)/g, m;

// Creates a map with the query string parameters
while (m = re.exec(queryString)) {
    queryParameters[decodeURIComponent(m[1])] = decodeURIComponent(m[2]);
}

// Add new parameters or update existing ones
queryParameters['newParameter'] = 'new parameter';
queryParameters['existingParameter'] = 'new value';

/*
 * Replace the query portion of the URL.
 * Query.param() -> create a serialized representation of an array or
 *     object, suitable for use in a URL query string or Ajax request.
 */
location.search = $.param(queryParameters);

You can clearly improve the regular expression, but the one above meet my needs.

Improving web performance with Apache and htaccess

May 23rd, 2011 by Samuel Santos 20 comments »

Web performance is getting more and more attention from web developers and is one of the hottest topic in web development.

Fred Wilson considered it at 10 Golden Principles of Successful Web Apps as the #1 principle for successful web apps.

First and foremost, we believe that speed is more than a feature. Speed is the most important feature. If your application is slow, people won’t use it.

Faster website means more revenue and traffic
  • Amazon: 100 ms of extra load time caused a 1% drop in sales (source: Greg Linden, Amazon).
  • Google: 500 ms of extra load time caused 20% fewer searches (source: Marrissa Mayer, Google).
  • Yahoo!: 400 ms of extra load time caused a 5–9% increase in the number of people who clicked “back” before the page even loaded (source: Nicole Sullivan, Yahoo!).

Google experiments reached similar results:

Our experiments demonstrate that slowing down the search results page by 100 to 400 milliseconds has a measurable impact on the number of searches per user of -0.2% to -0.6% (averaged over four or six weeks depending on the experiment). That’s 0.2% to 0.6% fewer searches for changes under half a second!

And speed is now a factor contributing to Google Page Rank:

Google, in their ongoing effort to make the Web faster, blogged last month that “we’ve decided to take site speed into account in our search rankings.” This is yet another way in which improving web performance will have a positive impact on the bottom line.

The good news is that some of the most important speed optimizations can be easily done with simple .htaccess rules.
These rules can make any website faster by compressing content and enabling browser cache, and follow the Best Practices for Speeding Up Your Web Site from Yahoo!’s Exceptional Performance team.

» Read more: Improving web performance with Apache and htaccess

Stripes framework XSS Interceptor

August 3rd, 2010 by Samuel Santos No comments »

I have created a new project at Google Code named Stripes XSS Interceptor.

This project escapes all the parameters that Stripes binds during its Validation & Binding phase using a wrapped request object (a convenient implementation of the HttpServletRequest interface).

The code follows the XSS (Cross Site Scripting) security guidance posted at OWASP (Open Web Application Security Project).

Please feel free to report any bug you find in the project Issue Tracking.

Maven 2 Cobertura Plugin – Updated

April 14th, 2010 by Samuel Santos 3 comments »

My previous Maven 2 Cobertura Plugin article gives a workaround for the very buggy version 2.1 of the Cobertura Maven Plugin.

This bug is fixed on versions 2.2 or higher, and consequently, that workaround does not work anymore.
For those reading my previous article and having difficulties configuring the plugin, this is my actual configuration.

» Read more: Maven 2 Cobertura Plugin – Updated

samaxesJS JavaScript library updates

April 5th, 2010 by Samuel Santos No comments »

Both the TOC (Table of Contents) components from samaxesJS JavaScript library have been updated.

Changes include:

  • Reduced file size by removing duplicated code using a for loop when defining and processing indexes (1.8KB for the minified jQuery TOC plugin).
  • Added a new option: context, allowing the TOC to list headings from only a portion of the page.

Please report any bug you may find in the project Issue Tracking.

Run PHP on Google App Engine

December 14th, 2009 by Samuel Santos 6 comments »

By adding Java to their App Engine, Google has opened the door for a whole slew of languages that have been implemented on the JVM, now including PHP via Quercus.

This weekend I decided to give it a try and deploy an old tutorial of mine – PHP Tutorials – on GAE.

I must admit that I was pleasantly surprised by how effortless it was. OK, it’s a very rudimentary PHP application, the only PHP code used was to run the examples described on the code blocks and do some includes; nevertheless I didn’t feel the need to change a single line of code.

Also, deploying a Java application to GAE is simpler than a Python one. Not only because you have a very handy Eclipse plugin, but you will also find configuring the file appengine-web.xml a lot easier when compared to app.yaml.

All you need to do in order to deploy a PHP application, at least as simple as the one I’ve tried, is to follow these steps:
» Read more: Run PHP on Google App Engine

W3C Widgets Compatibility Matrix for Packaging and Configuration

November 14th, 2009 by Samuel Santos No comments »

Daniel Silva, Marcos Caceres and myself have completed Phase 1 of Widget Packaging and Configuration compatibility testing. We have also detailed the results as part of the conformance matrix.
We would like to publish the results as a working group note. Phase 2 will begin in about 3 weeks, in which we are hoping to start working with vendors to improve overall conformance.

We need help with Phase 2: if you know a team contact for any of the targeted products that are claiming conformance to W3C Widgets, then would appreciate your help in making them aware of the results of the testing – Implementation Report: Widgets Packaging and Configuration.

Java Web Development with Stripes framework

September 17th, 2009 by Samuel Santos 5 comments »

Stripes Framework presentation for the Portuguese Java User Group session on the JavaPT09 event.

The used source code is also available.