Hamcrest Matchers in JUnit tests

With JUnit being the dominant Test framework, chances are high that during your career as a software engineer, you’ve encountered the JUnit framework in one or more projects. Therefor, code like this:

Should not be unfamiliar to you. This blogpost is on improving the example above. There are good alternatives in writing your tests, like Spock, but these take a little more effort to setup, since it’s mostly used in Groovy projects. The goal of this blogpost is to improve the above Java test with little effort, and for that, we’re going to use Hamcrest.

Hamcrest is (in our case) a set of Java matchers which improve the readability of the code and error messages shown when assertions do not match. So, let’s get back to our original example: what does it really say?

Means something like: verify that the getValue method of result should return the String hello, and if not, print the message ‘result doesn’t match hello’. Wouldn’t it be more understandable if we could write something like this in a more natural language? Well, with Hamcrest we can:

In this example, we’re using ‘assertThat’, which is a method coming from JUnit. JUnit itself supports a random amount of Matchers, but I would recommend on forgetting those, and just use only the Matchers from Hamcrest itself (which means, adding the Hamcrest library to the project, version 1.3 at this moment), since the Hamcrest library contains a much bigger set of Matchers. Also, note that the ‘is’ method here is optional, but I added it to improve readability.

Hamcrest can be added to your Maven project by including the following dependency:

And using this library, you’ll get many matchers, supporting String, Collections, Maps, Numbers, and even XML elements, but more on that in my next blog!

One last note: be aware to use the latests version of JUnit though (currently 4.11). When using an older version you might (read: will) run into dependency issues. Don’t say I didn’t warn you!

Reloading Log4j Configuration in Tomcat

When you’re in production environment and you want to diagnose some problems, it’s often handy to have some more logging available. So, when you’re using Log4j, and you want to have your log4j.xml reloaded, you can use Log4jWebConfigurer to make this happen.

To use it, you need to add a listener your web.xml. Be careful to place this before the ContextLoaderListener entry. Also important is to add a log4jRefreshInterval (in milliseconds), else Log4j won’t start it’s deamon thread.

web.xml

This should reload your log4j.xml changes whenever they occur.

Getting started with Maven, GXT 3 and GWT 2.5

This guide covers the step by step creation of a Maven GXT 3 project with GWT 2.5. It’s based on this, this and this document, so thanks for that!

Step 1: Create the Maven project

Since we don’t want to depend on any Eclipse or IntelliJ features, we’ll create a simple project using Maven. The Maven Archetype is really handy in this, and is it’s current version, 2.5-rc1 is stable enough to get our HelloWorld application running.

Open the command prompt in the directory you want to create your project in and use the GWT archetype by executing the following command:


mvn archetype:generate
-DarchetypeGroupId=org.codehaus.mojo
-DarchetypeArtifactId=gwt-maven-plugin
-DarchetypeVersion=2.5.0-rc1
-DgroupId=com.foo.gwt
-DartifactId=demo
-Dversion=1.0-SNAPSHOT

I used MyModule as the module name.

Step 2: Add the GXT dependency

Open the pom.xml, and add the Sencha repository, the GXT version (3.0), and the GXT dependency:

At this point you should be able to execute

mvn verify

After that, verify that GWT specific tasks can be run:

mvn gwt:run

Step 3: Add GXT to the project

From the migration guide:

The resources required to use GXT are all managed internally now – it is no longer necessary to keep a resources directory up to date. Instead, the GWT ClientBundle feature is used to manage images and stylesheets, making sure they are present as part of the compiled project. It is still necessary to link to a stylesheet from the main html page however – every compiled project will have a reset.css file, used to normalize differences between browsers.

So, open the $MODULE_NAME.html file, and add the following line as the first stylesheet in the file:

For example, if your module is called MyModule, add the following:

Remove the table for the Web Application Starter project. We don’t need it for this guide.

Add the line below to the module.gwt.xml, in src/main/resources/com/foo/gwt/MyModule.gwt.xml.

And remove the following line from the same file:

Now, on to the final part, the actual application. Replace the MyModule.java in src/main/java/com/foo/gwt/client/MyModule.java with the following code:

Also, in the same directory, add a file called BasicTabExample.java, with the following contents:

Now, type the following:

mvn gwt:run

And browse to http://127.0.0.1:8888/MyModule.html?gwt.codesvr=127.0.0.1:9997. Wait a little to see your application up and running!

Resources

Getting started with Camel, Spring and Maven

Getting up and running with Camel is not so hard, once you know how to do it. It took me a little more time than I wanted despite the extensive amount of examples provides by Camel. This post should help you get an up and running example in no time!

To start, I’ll start with the pom.xml:

This setups a simple application. Next, we’ll create a Camel route:

The nice thing here is that a route is also executable, which makes testing of the route really easy. As you can see here, the application is looking into the target location, and will process everything found there by giving an Exchange to the process method of the myProcessor bean. This bean is wired in the camel-context.xml, which should be placed in the src/main/resources/META-INF/spring directory.

And the MyProcessor bean:

And that’s it. That should get you up to speed with using Camel, Spring and Maven!

Getting started with Spring, RabbitMQ and Maven on MacOS

So, you want to get started with RabbitMQ in a Maven project as soon as possible? Well, bear with me here, and we’ll get you up and running in no time!

First things first: you need to install RabbitMQ. You can do this by using MacPorts:
sudo port install rabbitmq-server

After RabbitMQ has been installed and is running, it’s time to create the project. Using the following Maven POM and standard Maven structure, we create our project:

This pom.xml includes all the dependencies needed to make the client connect with RabbitMQ

Next up, is the code to receive some messages, and to produce some messages. We’ll call them the Producer and Consumer.

and the consumer:

and we’re almost done. All we need now is the Spring applicationContext.xml, which will create the RabbitMQ queue, and wire everything together:

If you start the Producer now, the Producer will send a message to the automatically created queue (you can see this in the http://localhost:55672/#/queues. When running the Consumer, it will get the message from the RabbitMQ queue, and will convert and display it in the console. Now you’ve sent your first message on RabbitMQ!

Retrieving Clob Data From Oracle Stored Procedures using Spring

Whenever you’ve got a Function or Stored Procedure in Oracle which returns a Clob, you may have to convert it to a String. This can be done by registering a new SqlReturnType for the SqlParameter.

For example, when using the following function as seen in a previous blog post:


function get_content_value ( p_type numberic(10,0)
, p_name varchar2
)
return clob;

The Clob can be mapped by using the following:

The ClobReturnType looks like this:

It might help someone in the future.

Calling Oracle Functions using the Spring Framework

Consider you have a function like this:


function get_content_value ( p_type numberic(10,0)
, p_name varchar2
)
return clob;

And you want to call this function using Spring. One of the easiest way to do this is by using the Spring StoredProcedure. There are some small things to consider though, when using this class to call Oracle Functions.

Calling the above function can be done like this:

So, in short, the trick is to:

  • Define output parameters before the input parameters
  • Call the setFunction(true) method

Debugging Tomcat SEVERE: Error filterStart or listenerStart

Today, we spent quite some time debugging our Grails application to find out why in ‘grails run-app’, the application worked fine, however, when running the application in Tomcat (6.0.35), an error popped up. The error not quite helpful indeed: SEVERE: Error filterStart.

To debug this, after some Googling, which pointed us into the wrong direction, I downloaded the Tomcat source and found the issue. All you have to do is change the logging.properties file in your TOMCAT_HOME/conf directory, and add the following:

This showed us the stacktrace, and pointed us in the direction of Spring Security Core (org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named ‘springSecurityFilterChain’ is defined), which will hopefully be a blog for tomorrow 😉