Grails

January 24th, 2011

As I said in my previous post we have been using Grails a lot and think it’s great.

I promise to blog a bit more about the things we are doing in Grails and how it is aiding our productivity.

Some of the interesting things we have used are:

  • Spring Security Core Plugin – for coarse grained security restricting URLs based on roles
  • Spring Security ACL Plugin – for finer grained security restricting users access to specific instances of objects
  • The Google Chart Plugin – for drawing fancy charts, obviously :)
  • The Eastwood Chart Plugin – to provide the functionality of Google Charts without calling out over the internet

The security stuff was particularly interesting especially when implementing paging on objects restricted using ACL. I will blog about this next as there doesn’t seem to be too much out there about how to achieve it successfully.

Grails & STS/Eclipse Integration

January 24th, 2011

Blimey it’s been a long time since I last blogged about anything. We have been very busy here with many projects having gone live and more in the pipeline.

Since last blogging about JSF 2.0 and the new scopes we have done a couple of projects in Grails. I must admit that I am completely sold on Grails.

So much so that a project we were doing in JSF has been rewritten from scratch in Grails and progress has been at a far more impressive rate.

Development is being done using Springsource Tool Suite (STS) as it has Grails & Groovy integration.

One problem though is we have commited the .classpath and .project files into the repository to allow other developers to set the project up in STS (or eclipse).

Obviously this isn’t ideal and if it were a maven project then we would just use the eclipse plugin to generate the appropriate files from the command line.

Today I found that Grails actually has a very similar tool which is invoked from the command line.

Simply type grails integrate-with --eclipse and voila, you now have the appropriate .classpath and .project files.

From that point on you can import the existing project into the workspace as normal.

See this page for more information on integration with STS

Trying out JSF 2.0 – View scope & Spring

July 19th, 2010

One of the limitations with JSF up to and including version 1.2 was the types of scope available for the backing beans. The three you had available were Request, Session and Application.

In a lot of cases Request scope wasn’t adequate. An exampe is when rendering a table with command buttons on each row. When the user clicked on the button the backing bean had been thrown away after the page had been rendered and so didn’t work correctly.

The workaround for this was you had to create all you beans as Session scope or you had to write some custom components to save away the state. Neither of which is ideal.

JSF 2.0 addresses this issue and adds two new scopes (plus the ability to add custom ones). They are View and Flash scope.

The View scope addresses exactly the problem of the table with command links. The bean is preserved until the user has finished with that particular view.

For a more detailed look at the new scopes and other features of JSF 2.0 I would recommend this blog post that I came across.

So View scope is the thing that I need. The only problem I now have is that I am using Spring to manage my beans for me rather than JSF.

Since View and Flash scope are specific to JSF and not Spring they are not available straight out of the box.

Fortunately it turns out to be easy to write a custom scope in Spring and register it with the context. I found this blog which details how to do it. The instructions are for Spring 3.0 but I was using version 2.5 and it’s practically identical.

It works a treat and I am now able to use View scope quite happily!

Using the maven eclipse plugin to configure your workspace

July 19th, 2010

I find that the maven eclipse plugin m2 is far more hassle than it is worth and so I never use it. Instead I build my projects from the command line and then use the maven eclipse plugin to update the projects and classpaths when required.

One niggle of this approach is when creating a new workspace sometimes eclipse is unaware of the location of the maven repository and will report many build errors.

This can be fixed from the command line by running the following:

mvn eclipse:configure-workspace -Declipse.workspace=<path to workspace>

Note that previously you could use the eclipse:add-maven-repo goal but this has since been deprecated.

Piping in a file to SQL Server from the command line

January 28th, 2010

SQL Server Management Studio doesn’t like big SQL scripts. So much so that it just tends to fall over with an error.

Many thanks to Jamie for pointing me at his blog where he has the command to pipe in the file from the command line, which works a treat!

Disabling AspectJ with Maven Eclipse Plugin

October 8th, 2009

When running the Maven Eclipse plugin to generate the projects and settings the property to disable AspectJ is ajdtVersion and the value to use is none.


mvn eclipse:eclipse -Declipse.ajdtVersion=none

Getting Selenium to work with firefox 3.5 on Windows

October 1st, 2009

What a nightmare trying to get selenium to work properly with firefox 3.5 on windows. It took me a good couple of hours to figure out what was going wrong.

When trying to run my selenium tests it was unable to load the browser correctly, complaining it couldn’t shutdown while preparing a profile.


java.lang.RuntimeException: Firefox refused shutdown while preparing a profile

Googling around it seemed lots of people had had this problem when trying to use firefox 3.0 and selenium server 1.0 Beta 1. There was lots of advice about trying to create a custom profile and getting selenium to use that. Unfortunately I had no luck with this solution.

I was running with selenium server 1.0 Beta 2 and checked to see if there was a more recent version. Fortunately it looks like it has come out of beta and I was able to download version 1.0.1.

Using this more recent version fixed the problem and didn’t require any tweaks to get firefox up and running.

Skipping tests in Maven

October 1st, 2009

For some reason I really struggle to remember the correct parameter to pass to maven when wanting to skip the tests.

So mainly as an aide-mémoire to myself the parameter I need is -Dmaven.test.skip=true.

mvn clean install -Dmaven.test.skip=true

JUnit gotcha with BigDecimal

June 9th, 2009

Anyone working with Java and numbers will be familiar with the idiosyncrasies of determining whether two BigDecimals are equal. As defined by the javadoc for the equals method on the BigDecimal class it defines two numbers as being equal if:

Unlike compareTo, this method considers two BigDecimal objects equal only if they are equal in value and scale (thus 2.0 is not equal to 2.00 when compared by this method).

So when it comes to working out if two BigDecimals are equal and you don’t care about scale then you should use the compareTo method:

  boolean equal = new BigDecimal("1.00").compareTo("1.0") == 0;

Now the fun comes when you write a JUnit test to check for the equality of two BigDecimals. Take the following two test methods:

	@Test
	public void testBigDecimalEquality1()
	{
		junit.framework.Assert.assertEquals(
                   new BigDecimal("1.00"), new BigDecimal("1.01"));
	}

	@Test
	public void testBigDecimalEquality2()
	{
		org.junit.Assert.assertEquals(
                  new BigDecimal("1.00"), new BigDecimal("1.01"));
	}

Given what we know about BigDecimal and it’s implementation of equals (as you would assume that’s what the JUnit assertion would invoke) we would expect both tests to fail.

However rather surprisingly the second test method testBigDecimalEquality2() passes. It shouldn’t as the numbers are quite clearly not the same value.

To find out why we get this strange behaviour I downloaded the source code for the version* of JUnit I was using and had a look at the implementations of the assertions.

For the junit.framework.Assert version we have:

	/**
	 * Asserts that two objects are equal. If they are not
	 * an AssertionFailedError is thrown with the given message.
	 */
	static public void assertEquals(String message, Object expected, Object actual) {
		if (expected == null && actual == null)
			return;
		if (expected != null && expected.equals(actual))
			return;
		failNotEquals(message, expected, actual);
	}

This is what I would have expected the implementation to look like, it simply delegates down to the objects equals method.

For the org.junit.Assert version we have:

	/**
	 * Asserts that two objects are equal. If they are not, an {@link AssertionError}
	 * is thrown with the given message. If expected and actual
	 * are null, they are considered equal.
	 * @param message the identifying message or null for the {@link AssertionError}
	 * @param expected expected value
	 * @param actual actual value
	 */
	static public void assertEquals(String message, Object expected, Object actual) {
		if (expected == null && actual == null)
			return;
		if (expected != null && isEquals(expected, actual))
			return;
		else if (expected instanceof String && actual instanceof String) {
			String cleanMessage= message == null ? "" : message;
			throw new ComparisonFailure(cleanMessage, (String)expected, (String)actual);
		}
		else
			failNotEquals(message, expected, actual);
	}

	private static boolean isEquals(Object expected, Object actual) {
		if (expected instanceof Number && actual instanceof Number)
			return ((Number) expected).longValue() == ((Number) actual).longValue();
		return expected.equals(actual);
	}

This implementation delegates down to the private method isEquals(). Now that checks if the object is an instance of the Number class (which BigDecimal is) and then retrieves the long values from the expected and actual and does a simple comparison. This will lose any precision following the decimal point and is why the test passed incorrectly as 1.00 and 1.01 have the same whole number part.

Obviously this begs the question of why do we have two implementations of the Assert class supplied with JUnit. For an answer to this I found a discussion on stackoverflow.com.

This is more than a little worrying as you may have tests that pass successfully but because of a bug in the test framework your code is actually incorrect. Not a good position to be in, especially when you are dealing with numbers and calculations. Imagine if you were writing a financial application and you were only accurate up to the decimal point, I don’t think the client would be best pleased.

With this in mind I tend to use the assertTrue method and use either the equals or compareTo method as appropriate (depending on if you are concerned about scale or not), for example:

	@Test
	public void testBigDecimalEquality3()
	{
		Assert.assertTrue(new BigDecimal("1.00")
				.equals(new BigDecimal("1.00")));

		Assert.assertTrue(new BigDecimal("1.00")
				.compareTo(new BigDecimal("1.0")) == 0);
	}

*Note: The version of JUnit I ran the examples on is 4.3.1

Keeping it simple

June 4th, 2009

The other day I was discussing my previous post on the complicators gloves with a colleague and it brought to mind a progamming quotation I like.

I think I first read it somewhere on codinghorror.com and believe it is referred to as Kernighan’s Law:

Debugging is twice as hard as writing the program, so if you write the program as cleverly as you can, by definition, you won’t be clever enough to debug it.

It’s worth repeating that if you want to make life easier for yourself in the long run then don’t try and be super clever when writing code.

Are you writing it as cleverly as possible for performance reasons? If so then it’s most likely you are prematurely optimising and may be solving a problem you don’t have. Wait until its written and then profile it to see whether you really do have a performance bottleneck.

If it’s not for performance reasons then is it just to prove to every one else how clever you are? Not a good idea as when you come back in a couple of weeks time then you won’t understand it either.

I am currently re-reading bits of Code Complete 2 at the moment and Steve McConnell is fond of pointing out that a really great developer knows that he is writing his code more for the benefit of other developers than the computer.

With that in mind it really does pay to keep it as simple as you possibly can, that’s where the true skill lies!