Fantastic Contraption - a great flash game for anyone with an engineering mind

I've found a flash game that I just have to share! It's called Fantastic Contraption and it's brilliant fun for anyone with an engineering mind, it's just so addictive.

The goal is to transport a red ball from it's starting point into a red goal area using your 'contraption' - your self-built vehicle made out of wheels, sticks and water power. Below is a screenshot of my 'flatbed' truck in mid-flow:



Give it a go but don't try it at work, you will not be able to do anything else for the rest of the day!

I found this via the Arantius blog, thanks Anthony Lieuallen!

Technorati Tags: , , , , , ,

How to test Exceptions and verify Mock Objects using EasyMock

Let's say you are unit testing, using EasyMock to verify the behaviour of your code under test and now you want to test that your method throws a particular exception:

@Test(expected = SQLException.class)
public void parseThrowsException() throws SQLException {
ResultSet mockResultSet = createMock(ResultSet.class);
expect(mockResultSet.next()).andThrow(new SQLException());
mockResultSet.close();

replay(mockResultSet);

ResultReporter reporter = new ResultReporter();
reporter.parse(mockResultSet);

verify(mockResultSet);
}
As you can see this is another test from my previous EasyMock post on how to use EasyMock's expect method when unit testing. We are now testing that when we call the next method it throws an SQLException (to simulate the database going down).

We expect that the next method call will throw an exception and then we are expecting that close will be called. If you run this test then you will find that it passes with green flying colours - but there's a problem, this is the implementation of the parse method:
public boolean parse(ResultSet results) throws SQLException {
return results.next();
}
As you can see it never calls the close method of the passed in ResultSet - so the test should have failed. It passes because the test has exited before it gets to verify the mock ResultSet for expected behaviour.

The way around this is to use an elegant try/finally block around the code under test like this:
try {
ResultReporter reporter = new ResultReporter();
reporter.parse(mockResultSet);

} finally {
verify(mockResultSet);
}
If you make this change to the above test then it fails as expected, now I just need to fixed the code... So you now know how to test for thrown exceptions and ensure that your mock objects are verified as well!

Technorati Tags: , , , , ,

Setting up HD/Progressive Scan on a Sony PlayStation 2 (PS2) with Component cables

I recently upgraded my television from a Philips 28" widescreen tube to a 1080p Panasonic 42" plasma. My old Sony PlayStation 2 was now looking a little weak when upscaled to 42" so I decided to invest in some component cables to take advantage of the best output that the PS2 had to offer as well as enabling "Progressive Scan Mode" in some supporting games (Guitar Hero 2 for example).

Here are my step by step instructions on how to HD-enable your PlayStation2.

Buy a quality component cable

My PS2 came with some SVIDEO-style connectors and a SCART block to plug them in to. The image quality on my old TV was fine but needless to say on a 42" HD plasma things looked a little different!


I spent a lot of time reading all sorts of reviews of PS2 component cables and it seemed that people were having mixed results with the cables in the £5-£15 bracket. After some searching about I found a PS3 to 5 RCA (component) cable from IXOS which also works with the PS2 (they use the same AV connector):


It's the IXOS XPP104 which is strangely not listed under their PS2 section but under their PS3 section instead (although it does state on the page that it's the "XPP104 PS2 & PS3 Component Video + Analogue Audio" cable). I bought mine from Sound & Vision for £30.

Tell your PlayStation that you are about to use component connections

Power up the PS2 with no disc in the drive so that you get the "Browser/System Configuration" screen:


Press down on the controller until the screen changes to the "Component Video Out" option and select "Y Cb/Pb Cr/Pr" rather than "RGB", then press circle to go back and then power off your PS2:

Plug it into the component sockets of your TV

Find the video and audio component sockets on the back of your TV:


Plug the Green/Blue/Red non-audio cables into the Y/Pb/Pr sockets:


Plug the Red & White audio cables (clearly marked AUDIO on the IXOS cables) into the Audio In sockets:


Then plug the other end into the back of your PS2.

That's the tricky bit done so now pop in your favourite game to see what it looks like. Even without enabling the in-game progressive scan option I immediately noticed a much clearly steadier picture with no combing (scroll down to the tomato) which I was experiencing before the cable upgrade.

Some newer games have an option to enable "Progressive Scan" - Guitar Hero 2 is one such game. Here are a couple of shots of my TV's info display before and after enabling progressive scan:





I also read that if your game does not have an in-game option you can hold Triangle and "X" at the same time while booting up the game but this didn't work for me.

It's made a world of difference to my PS2 image quality so I think the £30 was money well spent! Oh and Wikipedia has a pretty decent list of PS2 games that support HD as well...

Technorati Tags: , , , , , ,

Write simpler equals & hashCode Java methods using EqualsBuilder & HashCodeBuilder from Apache Commons

Last week I posted about how to implement a Hibernate-safe equals() method using instanceof and accessors (getters) in Eclipse. This showed that you don't want to be comparing the member variables directly but should use the accessor methods to protect against Hibernate's use of proxy objects.

My old colleague Guy Francis pointed out that rather than using Eclipse's generated (lengthy and rather ugly) equals and hashCode methods I should take a look at the Apache Commons EqualsBuilder and HashCodeBuilder.

Both of these builders have a very useful reflection-based equals/hashcode method but that does a deep internal comparison of member variables so that is no good for use with Hibernate objects. One use that they don't really explain clearly (and hence the reason for this blog post) is whether you can use accessor (getter) methods instead of direct variable access when using these builders...

...well you can!

You create a new EqualsBuilder object inside your equals method, append access to any properties of your object that make it unique and then call the isEquals method to test the properties. You do a very similar thing for hashCode. This is my new version of last week's methods now using these builders rather than Eclipse's generated code:

@Override
public boolean equals(Object obj) {
if (obj instanceof Person) {
Person other = (Person) obj;
EqualsBuilder builder = new EqualsBuilder();
builder.append(getId(), other.getId());
builder.append(getName(), other.getName());
return builder.isEquals();
}
return false;
}

@Override
public int hashCode() {
HashCodeBuilder builder = new HashCodeBuilder();
builder.append(getId());
builder.append(getName());
return builder.toHashCode();
}
They are not only smaller and simpler, they are also much less error-prone and it's very clear which object properties are used to see if an object is 'equal'. I'm going to be using these builders from now on for all my equals and hashCode methods!

Technorati Tags: , , , , ,

How to implement a Hibernate-safe equals() method using instanceof and accessors (getters) in Eclipse

When coding in Java you often want to check to see if two objects are 'equal'. This might range from a check to see if just one attribute is the same (the ID for example), or a complete comparisons of all attributes all the way up to ensuring that two objects are referencing the same instance of a particular class.

There is a lot of information out on the internet about how to write a correct equals() method (and the corresponding hashCode()) but I think Joshua Bloch's explanation from his excellent Effective Java book is the best.

Here are some tips that I learnt the hard way today when trying to test some JPA/Hibernate-backed objects for equality:

  • Don't use the default settings of Eclipse's built-in generator for equals() and hashCode() - it generates equals methods based on checking whether the two objects are from the same class (using getClass()), Hibernate proxies everything so this will never match

  • Don't use Eclipse's built-in generator at all - it's alternative option to "Use 'instanceof' to compare types" is more correct than it's default but still doesn't work for Hibernate classes

  • Do ensure that instanceof is used when ever checking if two objects have a similar background, the Hibernate proxies seem to respect this check so the proxies are instances of the classes that they proxy.

  • Do ensure that you use the accessors (getters) when getting the member variable values rather than the variables directly as the values maybe lazy loaded so id may actually return null whereas getId() will return the actual value from the database.
Feel free to use Eclipse's generator (with the instanceof option selected) as a starting point (I do) but don't forget to add in the extra bits described above.

So go from this:
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof Person))
return false;
final Person other = (Person) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
to this:
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof Person))
return false;
final Person other = (Person) obj;
if (getId() == null) {
if (other.getId() != null)
return false;
} else if (!getId().equals(other.getId()))
return false;
if (getName() == null) {
if (other.getName() != null)
return false;
} else if (!getName().equals(other.getName()))
return false;
return true;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((getId() == null) ? 0 : getId().hashCode());
result = prime * result + ((getName() == null) ? 0 : getName().hashCode());
return result;
}
A blog post from wenhu on the subject as well as some information from my colleagues were the sources for this post and the remedy to today's headache!

Technorati Tags: , , , , , , ,

Hibernate Tip: How to view the values of Bind Variables

If you use Hibernate then you will have probably wanted at some point to see the underlying SQL that was being run. One solution is to enable SQL logging by increasing the Log4J logging levels of the org.hibernate packages.

There are two issues with this technique though:

  1. At DEBUG level you see the SQL but all bind variables are displayed as a ?
  2. At TRACE level you get everything and the kitchen sink, SQL, bind variables, results, etc. - it's very verbose!
Wouldn't it be nice if you could see the SQL and the bind variables without all the other Hibernate excessive tracing? Well my ex-colleague and good friend Andy Kayley has blogged about a solution that he found recently - check out his blog post:

How to Print Out Bind Variables in Java Prepared Statements

I've not tried it yet, but the next time I need to see the bind variable values I certainly will be!

Technorati Tags: , , , , , ,