Skip to main content

Posts

Showing posts from August, 2008

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: Engineering , Flash , Game , Fantastic Contraption , Anthony Lieuallen , Arantius , Andrew Beacock

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 c

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

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 e

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 -

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: At DEBUG level you see the SQL but all bind variables are displayed as a ? 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: Spring , Hibernate , Java , Prepared Statement , Bind Variable , Andy Kayley , Andrew Beacock