HtmlUnit – How to do a POST

June 24, 2009

Background

I’ve just started using HtmlUnit (I also use Selenium but find it too slow for 90% of my testing).

HtmlUnit is generally easy to work with. Generally, it doesn’t need documentation as the Javadoc is useful and the methods are obvious. However, this is not the case for POST… so, here is how you do a POST đŸ™‚

How to do a POST in HtmlUnit

There really isn’t much to it – you just need to know what code to use:

final WebClient webClient = new WebClient();

// Instead of requesting the page directly we create a WebRequestSettings object
WebRequestSettings requestSettings = new WebRequestSettings(
  new URL("URL GOES HERE"), HttpMethod.POST);

// Then we set the request parameters
requestSettings.setRequestParameters(new ArrayList());
requestSettings.getRequestParameters().add(new NameValuePair("name of value to post", "value"));

// Finally, we can get the page
HtmlPage page = webClient.getPage(requestSettings);

Quite a lot of work for a simple… I imagine it won’t be hard to wrap this up into a neat POST method.


C3P0: Tried to check-in a foreign resource!

March 25, 2009

Earlier today I encountered the following stack trace whilst using C3P0 and Hibernate:

com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool - An Exception occurred while trying to check a PooledConection into a ResourcePool.
com.mchange.v2.resourcepool.ResourcePoolException: ResourcePool [BROKEN!]: Tried to check-in a foreign resource!
	at com.mchange.v2.resourcepool.BasicResourcePool.checkinResource(BasicResourcePool.java:657)
	at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$ConnectionEventListenerImpl.doCheckinResource(C3P0PooledConnectionPool.java:636)
	at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$ConnectionEventListenerImpl.connectionClosed(C3P0PooledConnectionPool.java:630)
	at com.mchange.v2.c3p0.util.ConnectionEventSupport.fireConnectionClosed(ConnectionEventSupport.java:55)
	at com.mchange.v2.c3p0.impl.NewPooledConnection.fireConnectionClosed(NewPooledConnection.java:510)
	at com.mchange.v2.c3p0.impl.NewPooledConnection.markClosedProxyConnection(NewPooledConnection.java:381)
	at com.mchange.v2.c3p0.impl.NewProxyConnection.close(NewProxyConnection.java:1246)
	at org.hibernate.connection.C3P0ConnectionProvider.closeConnection(C3P0ConnectionProvider.java:92)
	at org.hibernate.jdbc.ConnectionManager.closeConnection(ConnectionManager.java:474)
	at org.hibernate.jdbc.ConnectionManager.aggressiveRelease(ConnectionManager.java:429)
	at org.hibernate.jdbc.ConnectionManager.afterTransaction(ConnectionManager.java:316)
	at org.hibernate.jdbc.JDBCContext.afterNontransactionalQuery(JDBCContext.java:268)
	at org.hibernate.impl.SessionImpl.afterOperation(SessionImpl.java:444)
	at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1153)
	at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)

Apologies for the scroll bars… but hopefully this is indexed neatly by Google to help others!

After a bit of investigation it turned out this was due to a multi-threading issue and the solution can be summarised quite neatly:

never shut down Hibernate before all threads have finished what they’re doing (if they are using Hibernate)

Otherwise, you may get the above exception (or an equally unhelpful exception).


AJDT and Ant

February 27, 2009

I’ve just had one of those mornings where you spend hours trying to crack a problem and just getting nowhere.

The problem was getting AspectJ to work under Ant but only by using the AJDT 1.6.3 Eclipse plugin.

Unfortunately, this is very poorly documented. Fortunately, this helps you make the right decision: don’t use AspectJ in Ant by relying on AJDT.

There is a very good reason for this (beyond it being hard to get working): you cannot rely on AJDT being available when compiling in anything other than Eclipse (e.g. from a CI server).

What’s the alternative? Download AspectJ and use these libraries directly in Ant – they’re well documented and just work.


Bug in Annotation Processing (Java 6 Update 12 / Eclipse)

February 26, 2009

Just uncovered a fun bug in one of the Java Compiler / Eclipse (haven’t checked to see which one is behaving exactly correctly).

If you are working Eclipse then the Eclipse compiler believes the following snippet of code is OK:

@org.hibernate.annotations.Table(
  appliesTo = "PendingVotes",
  indexes = {
    @Index(name = "VoteId", columnNames = { "VoteId" }),
  })

However, the Java compiler (Version 6, update 12) will report the following error:

annotation.java:6: illegal start of expression
})
^

Now, I’ll display that snippet again and highlight the problem:

@org.hibernate.annotations.Table(
  appliesTo = "PendingVotes",
  indexes = {
    @Index(name = "VoteId", columnNames = { "VoteId" }),
  })

See that sneaky little comma at the end? I believe this is valid syntax (it allows someone to add another item later and only alter a single line – useful for generating diffs).

Remove that comma and everything becomes pain free again.