jump to navigation

JSP as integration testing glue July 25, 2008

Posted by javafoo in j2ee, java, java http, javanotes.
1 comment so far

As part of one of our new projects, a team mate and I are working on two different areas of the project. This other team mate is responsible for writing the integration glue code that uses all the components that I am writing. But he is not ready yet and I will be on vacation when he is ready. Of course I have unit tests to test my parts. But I want to test the integration part of it. I can do this the boring way: write a Junit that sets up all the test data and almost end up writing very complex tests to integrate all my components. The more fun way is to write JSPs to do all the integration tests, the JSP can act as a loose test glue script. And I am building the JSPs in such a way that I can stop and view the results at each integration point, before moving on to the next one. It’s visual and fun. And I have managed to catch quite some bugs. I wonder why I haven’t thought of looking at JSPs this way.

Java HttpURLConnection with redirect March 16, 2007

Posted by javafoo in java, java http.
2 comments

So I am trying to automate an application A at work, that needs to talk to another system B(also j2ee based), via http. But system B does not expose its applications as services and also certain applications need a user to login. So here’s what I do, I decide to write a http client that will emulate a login on system B (it’s a jsp on system B), grab the session cookie and then call the protected servlet on System B. Sounds simple enough.

But looks like it was going to be a long evening. I call login.jsp on system B and look at the logs on system B, seems like system B liked the username and password and it was also passing back the session cookie to my http client. But the subsequent call to the servlet in system B was failing, due to invalid authentication. After spending a couple of hours trying to debug my http client, I realize that the login.jsp on System B is doing a http redirect (well the HTTP 302 code should have tipped me off), so even though I was using the correct username and password the authentication was failing. So I add setInstanceFollowRedirects(false) on the HttpURLConnection object and everything starts working.  I don’t really care about the content that login.jsp delivers, so my client does not need to redirect. There is also a setFollowRedirects(boolean) static method that works on the class, so use caution when using this method as opposed to the instance method.

That is my lesson for the day and I will stick my head out of the window for a blast of that chilly mid western breeze.