CXF @WebService and XMLGregorianCalendar with JAXB

Lately we’ve done a lot of work with some different web-services and at one point we discovered that some people have trouble formatting their timestamps properly. Good solution would be to ask people to use a proper standard and not re-invent their own wheels, but that’s not always a possibility. Fortunately there’s a rather simple solution for this. All you need is to define a @XmlJavaTypeAdapter and teach it how to convert different values into suitable formats.

JAXB and XML by default uses a timestamp format of “yyyy-MM-dd’T’HH:mm:ss’Z'” which translates to 2015-04-11T14:15:12.000 roughly and some people think it’s ok to send us SQL ISO format timestamps that look very similar – specifically missing the T letter in the timestamp, like this: “yyyy-MM-dd HH:mm:ss”.

For example, in my web service entity I have a RelaxedXMLGregorianCalendarAdapter in use which is our own implementation:

@XmlRootElement(name = "Metainfo")
public class Metainfo {
    protected XMLGregorianCalendar generationTime;

And here’s the implementation for the adapter (note that we turn null, empty string and specifically a word “infinity” into null’s):

import javax.xml.bind.annotation.adapters.XmlAdapter;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

public class RelaxedXMLGregorianCalendarAdapter extends XmlAdapter<String, XMLGregorianCalendar> {

	public String marshal(XMLGregorianCalendar v) {
		return v.toXMLFormat();

	public XMLGregorianCalendar unmarshal(String v) throws Exception {

		if (v == null) {
			return null;
		if (v.trim().isEmpty()) {
			return null;
		// Who thinks that this is OK for a date field?
		if ("infinity".equals(v.trim())) {
			return null;
                // Replace the space with T to match our default pattern.
		return DatatypeFactory.newInstance().newXMLGregorianCalendar(v.replace(" ", "T"));

So there you have it. This is how you can customize how JAXB is handling some type conversions and pretty much you can customize anything JAXB does with this approach. Like teach it to treat -1 for specific fields as null or teach it understand different date formats.

Parameterized JUnit test

Today I needed to do a quick test for a custom NameUtils class that capitalized human and business names based on some very specific rules. To do that I decided that the best way is to use a Parameterized JUnit test and the best and fastest way of doing it is by using Junit with JUnitParams. This project adds a new runner to JUnit and provides much easier and readable parameterized tests for JUnit >=4.6. According to their own JUnitParams homepage the main differences to standard JUnit Parameterized runner are:

  • more explicit – params are in test method params, not class fields
  • less code – you don’t need a constructor to set up parameters
  • you can mix parameterised with non-parameterised methods in one class
  • params can be passed as a CSV string or from a parameters provider class
  • parameters provider class can have as many parameters providing methods as you want, so that you can group different cases
  • you can have a test method that provides parameters (no external classes or statics anymore)
  • you can see actual parameter values in your IDE (in JUnit’s Prameterized it’s only consecutive numbers of parameters)

So first, since we are using maven, then we need our dependencies:


While JUnit is very common, JunitParams is probably not so popular yet. It’s a package of small helper annotations that allow you to better write your tests.

So now – what are these parametrized tests I’m talking about? Here’s a quick example:

import static org.junit.Assert.assertEquals;
import junitparams.FileParameters;
import junitparams.JUnitParamsRunner;

import org.junit.Test;
import org.junit.runner.RunWith;

public class NameUtilsTest {

	public void testNameCapitalization(String input, String expected) {
		assertEquals(expected, NameUtils.capitalize(input));


This test asserts that our NameUtils.capitalize method returns expected results.
As you can see, we use @RunWith that tells JUnit to this test using JUnitParamsRunner and then we use @FileParameters that tells junit to run this test once for every single line in that CSV file.

On each of the CSV file lines we have our input and expected output, like this:

john doe, 					John Doe
DR. JOHN DOE, 				Dr. John Doe
ACME Corporation Ltd.,		Acme Corporation Ltd.
Miriam Torrence, 			Miriam Torrence
Shawanna Melody, 			Shawanna Melody
    SPACE     Tuma, 		Space Tuma
Jeraldine Ownbey, 			Jeraldine Ownbey
Nilda Cavaliere, 			Nilda Cavaliere
bUffy Yearta, 				Buffy Yearta,
AMANDA skoog, 				Amanda Skoog
a,							A
a b			, 				A B
Elias johnny cash Rash,		Elias Johnny Cash Rash

And now when we run our code, we get:
parameterized junit test results

Note that the test was ran for every single line in that CSV file and this allows you to quickly add new cases or testing material to constantly improve your code verifications.

If you have a question or having trouble getting it to work on your machine then feel free to write in the comment section and I’ll do my best to help you out. If you would like to read more about Unit Testing related subjects then also make sure to comment on that and if you liked what you read then make sure to subscribe – It’s FREE!

Java watermark image

Today I needed to add a watermark on my client images. I had a look around for available libraries. I didn’t find a good solutions that I could use straight out of the box in Java so I just created my own ImageUtil class and made my own Java watermark image method.

First things first – If we want to watermark an image then we have to decide on where should we anchor our watermark. For that I decided to use a simple ENUM called PlacementPosition:

public enum PlacementPosition {

When you want to add a watermark then you probably need to resize it so you might want a good image resizer. That one I found and we can use an already existing one. I personally prefer to use Scalr named image library from because it’s very good, pretty damn fast and has controllable quality. Also it’s available on Maven repository.

So now the actual code of watermarking an image:

	 * Generate a watermarked image.
	 * @param originalImage
	 * @param watermarkImage
	 * @param position
	 * @param watermarkSizeMaxPercentage
	 * @return image with watermark
	 * @throws IOException
	public static BufferedImage watermark(BufferedImage originalImage,
			BufferedImage watermarkImage, PlacementPosition position,
			double watermarkSizeMaxPercentage) throws IOException {

		int imageWidth = originalImage.getWidth();
		int imageHeight = originalImage.getHeight();

		int watermarkWidth = getWatermarkWidth(originalImage, watermarkImage,
		int watermarkHeight = getWatermarkHeight(originalImage, watermarkImage,

		// We create a new image because we want to keep the originalImage
		// object intact and not modify it.
		BufferedImage bufferedImage = new BufferedImage(imageWidth,
				imageHeight, BufferedImage.TYPE_INT_RGB);
		Graphics2D g2d = (Graphics2D) bufferedImage.getGraphics();
		g2d.drawImage(originalImage, 0, 0, null);

		int x = 0;
		int y = 0;
		if (position != null) {
			switch (position) {
			case TOPLEFT:
				x = 0;
				y = 0;
			case TOPCENTER:
				x = (imageWidth / 2) - (watermarkWidth / 2);
				y = 0;
			case TOPRIGHT:
				x = imageWidth - watermarkWidth;
				y = 0;

				x = 0;
				y = (imageHeight / 2) - (watermarkHeight / 2);
				x = (imageWidth / 2) - (watermarkWidth / 2);
				y = (imageHeight / 2) - (watermarkHeight / 2);
				x = imageWidth - watermarkWidth;
				y = (imageHeight / 2) - (watermarkHeight / 2);

				x = 0;
				y = imageHeight - watermarkHeight;
				x = (imageWidth / 2) - (watermarkWidth / 2);
				y = imageHeight - watermarkHeight;
				x = imageWidth - watermarkWidth;
				y = imageHeight - watermarkHeight;


		g2d.drawImage(Scalr.resize(watermarkImage, Method.ULTRA_QUALITY,
				watermarkWidth, watermarkHeight), x, y, null);

		return bufferedImage;



That method uses some specific methods to calculate the watermark size. I wanted the user to be able to upload their own watermarks but that caused a problem where I didn’t want to hard-code the size of the watermark. So we ended up using a solution where we have defined a maximum percentage of size that the watermark can be relative to the original image.

For that we use these methods:

	 * @param originalImage
	 * @param watermarkImage
	 * @param maxPercentage
	 * @return
	private static Pair<Double, Double> calculateWatermarkDimensions(
			BufferedImage originalImage, BufferedImage watermarkImage,
			double maxPercentage) {

		double imageWidth = originalImage.getWidth();
		double imageHeight = originalImage.getHeight();

		double maxWatermarkWidth = imageWidth / 100.0 * maxPercentage;
		double maxWatermarkHeight = imageHeight / 100.0 * maxPercentage;

		double watermarkWidth = watermarkImage.getWidth();
		double watermarkHeight = watermarkImage.getHeight();

		if (watermarkWidth > maxWatermarkWidth) {
			double aspectRatio = watermarkWidth / watermarkHeight;
			watermarkWidth = maxWatermarkWidth;
			watermarkHeight = watermarkWidth / aspectRatio;

		if (watermarkHeight > maxWatermarkHeight) {
			double aspectRatio = watermarkWidth / watermarkHeight;
			watermarkHeight = maxWatermarkHeight;
			watermarkWidth = watermarkHeight / aspectRatio;

		return Pair.of(watermarkWidth, watermarkHeight);

	 * @param originalImage
	 * @param watermarkImage
	 * @param maxPercentage
	 * @return
	public static int getWatermarkWidth(BufferedImage originalImage,
			BufferedImage watermarkImage, double maxPercentage) {

		return calculateWatermarkDimensions(originalImage, watermarkImage,


	 * @param originalImage
	 * @param watermarkImage
	 * @param maxPercentage
	 * @return
	public static int getWatermarkHeight(BufferedImage originalImage,
			BufferedImage watermarkImage, double maxPercentage) {

		return calculateWatermarkDimensions(originalImage, watermarkImage,


And now we have our scaled logo, this time it’s aligned to middle-left:
Java watermark image

If you have a question or having trouble getting it to work then feel free to write me a comment and I’ll do my best to help you out. If you would like to read more about general Java related subjects then also make sure to comment on that and if you liked what you read then make sure to subscribe – It’s FREE!

Jenkins and Maven Release plugin

OK, So here’s a quick tutorial on how to make a Jenkins and Maven Release plugin build that automatically creates a release build using Jenkins and we are assuming your code is in Git.

Step 1 is easy, get your code from Git. You just configure the Jenkins build as always.

Now, We need one additional plugin for Jenkins which is “Workspace Cleanup Plugin”. This allows us to enable
“Delete workspace before build starts” in Build Environment section.

Now the tricky part: Under Pre Steps you must add a new shell command:

git checkout master

This is because otherwise your git is checked out without a specific branch and maven release plugin will fail.

Build command

-B -Dusername=jenkins -Dpassword=JenkinsPasswordForGit release:prepare release:perform

Important notice – you can configure jenkins user to have automatic access to git in other ways also, but I decided to keep this info in the build.

Once you start releasing your project with Jenkins, you quickly discover that manually managing the version numbers inside your own multi-module project will become a hassle. Simple solution would be to configure Jenkins to upgrade your own dependencies to the latest releases (requires you to limit versions plugin to your own packages):

-B versions:use-latest-releases scm:checkin -Dmessage=$BUILD_NUMBER -Dusername=jenkins -Dpassword=JenkinsPasswordForGit release:prepare release:perform

Now we might want to limit versions plugin to our own com.codeyouneed package since having it upgrade ALL dependencies all the time is really bad.


Now that’s pretty much it. That git checkout master command took me a while to figure out.

Of course, for maven release plugin to work you actually need to add some stuff to your pom.xml also:



Important notice if you are dealing with multi-module maven projects – Submodules have to leave out their name from scm.url since release plugin will add the name by itself into the end of the URL.


So you should leave out the “/submodulename”.

Special thanks to a blog post that lead me to the right path:

Jenkins Logo

Liferay Custom Notifications

Java tutorial on how to handle Liferay custom notifications. There is very little good information on this topic but I’ve seen a good need for a proper documentation so here’s a nice example of how to handle Liferay User Notifications based on Liferay 6.2 Notifications API. The official documentation on Liferay notifications is rather weak but then again the feature is also rather new and it’s not even 100% part of the core Liferay experience. Liferay 6.2 notifications have improved quite well and I feel it’s time to make them a first-class feature.

If you download the Liferay 6.2 bundle with Tomcat then you have the Notifications portlet installed already. If you installed Liferay on your own Tomcat or other Application Server using the WAR installation method then you need to install Notifications Portlet from the Marketplace.
Here’s a link to the Notifications CE Portlet on Liferay Marketplace.

Liferay custom notifications require a User Notification Handler class that turns the notification into a nice HTML fragment that is displayed to the user. Also Liferay wants us to define the notification types that our portlet or application is creating in a definition file upfront.

So the first thing we need to do is to define the location of our class that extends BaseUserNotificationHandler class and definitions inside liferay-portlet.xml. This can be done in liferay-portlet.xml like this:


The important part is user-notification-definitions and user-notification-handler-class tags. Now we need to actually define our notification inside that example-user-notification-definitions.xml. This file should go into your resources folder. Since I’m using a maven based project then this file goes into src/main/resources/:

location of example-user-notification-definitions.xml

If you are running an Ant based project then you have to place this file inside docroot/WEB-INF/src/ folder.

Here’s the example-user-notification-definitions.xml I created:

<?xml version="1.0"?>
<!DOCTYPE user-notification-definitions PUBLIC "-//Liferay//DTD User Notification Definitions 6.2.0//EN" "">

Notice how we are using some public final Strings from class files like this: ${com.example.notifications.ExampleUserNotificationHandler.PORTLET_ID}. This allows us to define our Portlet ID in one location and keep things a little bit simpler.
Ok, so now we have our example defined and we can actually Add a new notification for the user. We can do so by using the built-in UserNotificationEventLocalServiceUtil in either our Service Builder class or one of our portlet classes.

Here’s how to add a new notification event:

    JSONObject payloadJSON = JSONFactoryUtil.createJSONObject();
    payloadJSON.put("userId", user.getUserId());
    payloadJSON.put("yourCustomEntityId", exampleEntity.getEntityId());
    payloadJSON.put("additionalData", "Your notification was added!");

		(new Date()).getTime(),
		false, serviceContext);		

If you are doing this inside a portlet then you need the serviceContext, you can get one like this:

ServiceContext serviceContext = ServiceContextFactory.getInstance(portletRequest);

So now that we have our notification inside the database we want to actually show it to the user. When we look at the dockbar our notification number gets increased so we can be pretty sure that the notification was added. When you attempt to actually look at the notification you will probably see a blank white box. We need the notification handler class to actually handle the display side of our notification. This current example also handles the behavior like providing Approve and Reject buttons right in our example notification so that our example user could perform an action straight from the dockbar notification and not waste extra time when it’s a trivial task. I don’t have the correct screenshot but the look we are going after is similar to that you can see on this picture:
liferay custom notifications

Basic idea being we have an Approve and Reject buttons on our notification, so that you can click on the notification or it’s background to go and View more details, but you can also directly perform some custom actions like Approve or Reject in this Liferay custom notification tutorial. The buttons could also be “Report as Spam” or “Notify the Security”. It’s all up to you where that link takes the user.
So here’s our handler class:

import javax.portlet.ActionRequest;
import javax.portlet.PortletURL;
import javax.portlet.WindowState;

import com.liferay.portal.kernel.json.JSONFactoryUtil;
import com.liferay.portal.kernel.json.JSONObject;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.notifications.BaseUserNotificationHandler;
import com.liferay.portal.kernel.portlet.LiferayPortletResponse;
import com.liferay.portal.kernel.util.StringBundler;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.model.UserNotificationEvent;
import com.liferay.portal.service.ServiceContext;
import com.liferay.portal.service.UserNotificationEventLocalServiceUtil;

public class ExampleUserNotificationHandler extends
		BaseUserNotificationHandler {
	public static final String PORTLET_ID = "example_WAR_exampleportlet";
	public ExampleUserNotificationHandler() {



	protected String getBody(UserNotificationEvent userNotificationEvent,
			ServiceContext serviceContext) throws Exception {

		JSONObject jsonObject = JSONFactoryUtil

		long yourCustomEntityId = jsonObject

		String title = "<strong>Example notification for entity ID "
				+ yourCustomEntityId
				+ "</strong>";

		String bodyText = "Some other text.";

		LiferayPortletResponse liferayPortletResponse = serviceContext

		PortletURL confirmURL = liferayPortletResponse.createActionURL(com.example.notifications.ExampleUserNotificationHandler.PORTLET_ID);

		confirmURL.setParameter(ActionRequest.ACTION_NAME, "doSomethingGood");
		confirmURL.setParameter("redirect", serviceContext.getLayoutFullURL());
		confirmURL.setParameter("yourCustomEntityId", String.valueOf(yourCustomEntityId));
		confirmURL.setParameter("userNotificationEventId", String.valueOf(userNotificationEvent.getUserNotificationEventId()));

		PortletURL ignoreURL = liferayPortletResponse.createActionURL(com.example.notifications.ExampleUserNotificationHandler.PORTLET_ID);
		ignoreURL.setParameter(ActionRequest.ACTION_NAME, "cancelForExample");
		ignoreURL.setParameter("redirect", serviceContext.getLayoutFullURL());
		ignoreURL.setParameter("yourCustomEntityId", String.valueOf(yourCustomEntityId));
		ignoreURL.setParameter("userNotificationEventId", String.valueOf(userNotificationEvent.getUserNotificationEventId()));

		String body = StringUtil.replace(getBodyTemplate(), new String[] {
				"[$CONFIRM$]", "[$CONFIRM_URL$]", "[$IGNORE$]",
				"[$IGNORE_URL$]", "[$TITLE$]", "[$BODY_TEXT$]" }, new String[] {
				serviceContext.translate("approve"), confirmURL.toString(),
				serviceContext.translate("reject"), ignoreURL.toString(),
				title, bodyText });
		return body;

	protected String getLink(UserNotificationEvent userNotificationEvent,
			ServiceContext serviceContext) throws Exception {

		JSONObject jsonObject = JSONFactoryUtil

		long yourCustomEntityId = jsonObject
		LiferayPortletResponse liferayPortletResponse = serviceContext

		PortletURL viewURL = liferayPortletResponse.createActionURL(com.example.notifications.ExampleUserNotificationHandler.PORTLET_ID);
		viewURL.setParameter(ActionRequest.ACTION_NAME, "showDetails");
		viewURL.setParameter("redirect", serviceContext.getLayoutFullURL());
		viewURL.setParameter("yourCustomEntityId", String.valueOf(yourCustomEntityId));
		viewURL.setParameter("userNotificationEventId", String.valueOf(userNotificationEvent.getUserNotificationEventId()));
		return viewURL.toString();

	protected String getBodyTemplate() throws Exception {
		StringBundler sb = new StringBundler(5);
		sb.append("<div class=\"title\">[$TITLE$]</div><div ");
		sb.append("class=\"body\">[$BODY_TEXT$]<a class=\"btn btn-action ");
		sb.append("btn-success\" href=\"[$CONFIRM_URL$]\">[$CONFIRM$]</a>");
		sb.append("<a class=\"btn btn-action btn-warning\" href=\"");
		return sb.toString();


NB Important Information: The com.example.notifications.ExampleUserNotificationHandler.PORTLET_ID string that you use as your notification type has to match an actual portlet ID. It doesn’t actually need to be YOUR portlet ID but that would be the right thing to have there. The reason being that Notifications display portlet uses it to display a small portlet icon next to your notification to help the user identify the source of the notification. Providing a bad Portlet ID or something like null leads to a hard-to-trace NullPointerException in the JSP. Took me an hour to track it down.

The problematic part is this code from notifications-portlet view_entries.jsp source

<span class="portlet-icon">
<liferay-portlet:icon-portlet portlet="<%= PortletLocalServiceUtil.getPortletById(company.getCompanyId(), userNotificationEvent.getType()) %>" />

If you have a question or having trouble getting it working then feel free to write me a comment and I’ll do my best to help you out. If you would like to read more about Liferay related subjects then also make sure to comment on that and if you liked what you read then make sure to subscribe – It’s FREE!

Java tutorial on how to make a Portlet

Java tutorial on how to create a simple Hello world Portlet. Let’s start with a simple overview of files. Depending on your project structure and IDE your source code can be structured differently but in all normal cases you will have a WEB-INF folder and inside of that you should have a portlet.xml file. This is the file that defines what Portlets are included in our  application. You can have more than one portlet in one application.


<?xml version="1.0"?>

<portlet-app xmlns=""
		<display-name>Hello World Tutorial</display-name>
			<title>Hello World Tutorial</title>
			<short-title>Hello World Tutorial</short-title>
			<keywords>hello world, tutorial</keywords>

Now, we also need that class that we reference in portlet.xml. Every portlet needs a class that acts as the Controller in MVC pattern.

import javax.portlet.*;

public class HelloWorldPortlet extends GenericPortlet {

	protected void doView (RenderRequest request, RenderResponse response) throws PortletException, IOException {
		String url = "/view.jsp";

Finally, we also need the view.jsp file, this should go to the same level with WEB-INF folder.


<%@ taglib uri="" prefix="portlet" %>
<portlet:defineObjects />

<h2>Hello World</h2>
This is a tutorial portlet. <br />
Your browser seems to be :<b><%=request.getHeader("user-agent")%></b><br />
And we think that your current language is: <b><%=request.getLocale().getDisplayLanguage()%></b> <br />

Have a lovely day.

Please note that we used portlet_2_0 taglib which is JSR 286. If your environment requires an older version, just leave out the _2_0.

Now while this is a perfectly nice portlet, it’s a little bit too simple. For more complex applications I would suggest you use some framework to get a head start. Spring has a lovely Portlet support but you can also use Liferay provided MVCPortlet as your basis for new Portlets, but you can only do that when you develop your applications for Liferay Portal. Frameworks provide both functionality and structure to a project. Struts 2 enforces the MVC pattern and provides lots of functionality, such as custom tags and validating functionality. With Liferay you can use almost any frameworks in the JavaEE space, including Struts, Spring Framework, and JSF.

The benefit of doing a completely vendor-free(Not using any portal specific stuff) portlet with open-source frameworks is that you can deploy the same portlet in either Liferay, JBoss, Websphere Portal or other JSR 286 compliant portals.

Java tutorial

Why use Liferay?

The Good side of Liferay

Liferay is NOT just a Java based CMS. It’s a complete portal environment that’s suitable for large enterprise usage. It should be used as a platform to develop in-house applications or intranet. If you need just a small website to promote something then Liferay is not for you.

Some people compare Liferay with PHP based CMS systems like Joomla or Wordpress but that’s completely wrong. Liferay does contain CMS and it’s not a bad one. It’s rather useful for providing small applications and even better for big applications.

Supporting JSR 168 and JSR 286 means you can deploy Portlet applications that others have developed. Liferay also has a very good support for groups and communities allowing you to build team sites and custom environments for different user-groups with ease. Some cool features that you get with Liferay out of the box are:

  • User and Group management
  • Multi-site support for having different pages and themes for different user groups
  • Proper Access rights and Role management
  • Internationalization
  • Document library management
  • Wiki, Blogging and Forums
  • Service Oriented Architecture
  • Theme support
  • Integrates with other existing systems using LDAP, RSS, iFrames and other technologies



The Bad side of Liferay

Developing applications and portals for the first time is a taunting task and comes with a rather massive learning curve. If you plan to use Liferay Service Builder and do everything “the Liferay way” then you have to plan probably at least couple of weeks for learning it first.

If you need to develop a small application quickly AND you don’t have an existing Liferay installation already in-house then you can get your application done way faster by NOT using Liferay. But once you have learned Liferay and have enough need for proper portal then Liferay becomes rather irresistible.

When it comes to documentation and tutorials then it’s not the best situation either. It does have a massive developer’s guide. But the Wiki is filled with tons of content that’s rather old and not updated for the latest releases.

The official developer and administration guides are good but often lack information about some crucial parts or fail to provide decent examples. While they claim to be developer friendly I have to admit that some parts of the development platform is severely lacking. Also note that releases are rather infrequent. Also one should be very cautious because early versions are always rather buggy.

AlloyUI that’s provided with Liferay for Javascript and UI is rather difficult to learn and seems also a very large investment time-wise – save yourself the trouble and use jQuery or some other favorite JS library instead.

Struts2 Convention & Spring Junit4

The problem

You normally would use StrutsSpringJunit4TestCase as the base class to unit-test your actions. But this class does not support Struts2 Convention plugin Annotations. So if you are like me and you like to keep your actions and results defined in the Action classes by annotations you are s***-out-of-luck. Most likely on your first attempt you will see exceptions like this:

WARN org.springframework.mock.web.MockServletContext.getResourcePaths:212 – Couldn’t get resource paths for class path resource [WEB-INF/content/] class path resource [WEB-INF/content/] cannot be resolved to URL because it does not exist. This effectively means that the conventions plugin is attempting to locate the resources etc.. Now the simple solution to resolve all those warnings is to modify your struts.xml file and add this setting in there:


Ok, so the warnings are gone but non of your annotated struts actions have actually been loaded into the configuration and you can’t unit-test them. So what gives?

The Solution – Almost

Now, here’s a nice solution almost:

I do want to credit Zarar Siddiqi for that class mostly. It’s a good starting base, but needs a bit of customization for the latest Struts / Spring and Junit 4 environment.

The Solution – Finally

Here’s the source code for the class i created based on the above posting:

import java.util.HashMap;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.Dispatcher;
import org.apache.struts2.dispatcher.mapper.ActionMapper;
import org.apache.struts2.dispatcher.mapper.ActionMapping;
import org.apache.struts2.views.JspSupportServlet;
import org.junit.Before;
import org.junit.BeforeClass;
import org.springframework.context.ApplicationContext;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.mock.web.MockServletConfig;
import org.springframework.mock.web.MockServletContext;
import org.springframework.web.context.ContextLoader;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionProxy;
import com.opensymphony.xwork2.ActionProxyFactory;

 * Improved the sample from originally
 * -struts-2-actions-spring-junit/
 * - Added support for Struts Convention plugin annotations, also made it run
 * with JUnit 4.
 * @author Zarar Siddiqi
 * @author
public abstract class StrutsConventionSpringJUnit4TestCase {

	 * Where are the Spring configurations?
	private static final String CONFIG_LOCATIONS = "applicationContext-test.xml,"
			+ "applicationContext-test-security.xml";

	private static ApplicationContext applicationContext;
	protected Dispatcher dispatcher;
	protected ActionProxy proxy;
	protected static MockServletContext servletContext;
	protected static MockServletConfig servletConfig;
	protected MockHttpServletRequest request;
	protected MockHttpServletResponse response;

	 * Mimic the getActionProxy method the usual StrutsSpringJuni4TestCase uses.
	 * @param clazz
	 *          Class for which to create Action
	 * @param namespace
	 *          Namespace of action
	 * @param name
	 *          Action name
	 * @return Action class
	 * @throws Exception
	 *           Catch-all exception
	protected ActionProxy getActionProxy(String uri) {
		ActionMapping mapping = Dispatcher
				.getMapping(request, Dispatcher.getInstance().getConfigurationManager());
		String namespace = mapping.getNamespace();
		String name = mapping.getName();
		String method = mapping.getMethod();

		// create a proxy class which is just a wrapper around the action call.
		// The proxy is created by checking the namespace and name against the
		// struts.xml configuration
		proxy = dispatcher
				.createActionProxy(namespace, name, method,
						new HashMap&lt;String, Object&gt;(), true, false);

		// Add all request parameters to our action
		ActionContext invocationContext = proxy.getInvocation()
		invocationContext.setParameters(new HashMap&lt;String, Object&gt;(request

		// set the action context to the one used by the proxy

		// By default, have a clean session.
				.setSession(new HashMap&lt;String, Object&gt;());

		// Important - unit testing JSP's is just not very helpful.
		// do not execute the result after executing the action.

		// set the actions context to the one which the proxy is using
		return proxy;

	 * Create a new dispatcher before every test.
	public void initDispatcher() {
		// Dispatcher is the guy that actually handles all requests. Pass in
		// an empty. Map as the parameters but if you want to change stuff like
		// what config files to read, you need to specify them here. Here's how to
		// scan packages for actions. Thanks to Hardy Ferentschik, for action
		// packages ideas. (see Dispatcher's source code)
		dispatcher = new Dispatcher(servletContext, new HashMap&lt;String, String&gt;());
		request = new MockHttpServletRequest();
		response = new MockHttpServletResponse();

	 * Initialize Spring context before the unit tests.
	 * @throws Exception
	 *           When something bad happens.
	public static void setUpContext() throws Exception {
		// Only initialize Spring context once - speeds up the tests a lot.
		if (applicationContext == null) {
			// this is the first time so initialize Spring context
			servletContext = new MockServletContext();
			applicationContext = (new ContextLoader())

			// Struts JSP support servlet (for Freemarker)
			new JspSupportServlet().init(new MockServletConfig(servletContext));


So what’s going on – what do I need to know?

Rather simply but, I needed the unit-testing class to support the newer versions of the used libraries, so the above class is made to work with the following environment:

  • JUnit 4.10
  • Spring 3.1.0
  • Struts

Warning about Tiles and other results

There’s no tiles support in the above class, and executing the results have been disabled. If you want to enable executing the results you have to modify this line:


You can hack it in there if you like but I couldn’t get it to work because of lack of time and because I really didn’t wanted to unit-test my JSP’s and tiles. Here’s the code you need to add to the setUpContext() method to make it initialize the tiles.

// Goes way in the top of the class as a static configuration.
private static final String TILES_CONFIG = "WEB-INF/tiles.xml";

// Goes into setUpContext() method in the end of the class file...
servletContext.addInitParameter(BasicTilesContainer.DEFINITIONS_CONFIG, TILES_CONFIG);
final StrutsTilesListener tilesListener = new StrutsTilesListener();
final ServletContextEvent event = new ServletContextEvent(servletContext);

OrientDB database creation class

Problem: OrientDB does not support BigInteger’s out of the box. The simple solution is to tune OrientDB serializer to output BigIntegers as Integers and this allows us to store them in database nicely.

Although it’s a pretty straight-forward and simple class I decided to create a post about it because it took me a while to fight with the BigInteger issue.


import java.math.BigInteger;

import com.orientechnologies.orient.core.serialization.serializer.object.OObjectSerializer;
import com.orientechnologies.orient.object.db.OObjectDatabaseTx;
import com.orientechnologies.orient.object.serialization.OObjectSerializerContext;
import com.orientechnologies.orient.object.serialization.OObjectSerializerHelper;

public class CreateNewDatabase {

	public static void main(String[] args) {

		// Don't run without database location argument.
		if (args == null || args.length != 1) {
					.println("Expected single argument of folder path where to store the database.");

		String path = args[0];
		if (path.length() < 2) {
			System.out.println("Path is too short: " + path);

		if (!path.endsWith(File.separator)) {
			path = path + File.separator;

		OObjectDatabaseTx db = null;

		try {

			File folder = new File(path);
			if (folder.exists()) {
				throw new Exception("Database already exists on that path!");

			// Create the folder as required.
			if (!folder.mkdirs()) {
				throw new Exception("Failed to create required folders:"
						+ folder.getAbsolutePath());
			System.out.println("Creating database to: " + path);
			db = new OObjectDatabaseTx("plocal:" + path);
			// Avoid overwriting existing databases.
			if (db.exists()) {
				throw new Exception("Database already exists on that path!");

			// Create database

			// Important code to serialize BigIntegers to Integers.
			// It's vital that this code is run before you register your classes.
			OObjectSerializerContext serializerContext = new OObjectSerializerContext();
					.bind(new OObjectSerializer<BigInteger, Integer>() {

						public Integer serializeFieldValue(Class<?> itype,
								BigInteger iFieldValue) {
							return iFieldValue.intValue();

						public BigInteger unserializeFieldValue(Class<?> itype,
								Integer iFieldValue) {
							return new BigInteger(iFieldValue.toString());


			// Tell OrientDB to automatically generate schema during class
			// registration, this saves us some code.
			// Now register our classes...

			// Save the schema of our new database...


		} catch (Exception e) {
		} finally {
			if (db != null) {



As always, freel free to comment and ask questions in the comments.