lördag 29 januari 2011

Why I am tired of Java

Recently I have partly gotten tired of developing in Java, because the language won't allow me to make the constructs that I want to do. Just recently I made some big refactoring of a test framework I designed for testing REST web applications. I am not particularly proud of the design I have made, but my knowledge of Java didn't allow me to come up with a more practical solution.

So I have one super class which all test case classes inherit from, this class implements several convenience methods as well as life-cycle methods for handling setup and tear-down of needed clients and resources. I use TestNG as a unit test framework, so the life-cycle handling is pretty straight forward, I just setup the http client I use and the spring JdbcTemplate for database access, so far so good. So we had this requirement that the tests should be written using as little code as possible so I made a compromise by implementing assert methods that actually perform the http calls as well as assert the response, ugly I know. But we also have assert methods that check the database with the response and so on. Here is where I get to the problem where I do not really like Java. How do you construct a test case super class with all these assert methods still have a class that is pure, readable and not polluted by code that really don't belong there?

If I could have split the http calls from the assertion of the response I know I could have used the regular convention of static imports. I could have done my RestAssertions class which asserts a http resonse, so I guess I have to suffer for my pragmatic approach and bad design. Lesson learned, you should never compromise with design, or should you? From my limited knowledge so far about Scala and Ruby, they at least have a language construct that allows you to do such a design in a natural way, traits/mixins. When looking at Java I can't really see how to solve this. I ended up using composition where I could.

The test case class is like a wrapper for different Client and Utility classes. I have a DBUtil class implementing db related asserts, and I have a RestClient class implementing http call method and response related asserts, and they all have their wrapper method in my test case super class. This way I at least minimized the amount of code in the super class, and by such made it more readable. So if anyone know a better solution for tackling this design problem, please let me know.

If I start over I would have kept assertion methods pure, it would have made the tests contain more lines of code, but it would have made the underlying classes more readable and maintainable. But still, if I had gotten the choice I would have loved to try to implement this in Scala. I think if I had gotten enough time for development and also got some time to delve deeper into Scala I might have been able to create a small DSL for testing REST web applications, which even would made writing tests so much cleaner and more approachable by test developers.

So for the time being I'm gonna live with this design, and let it be. However as I am trying learn Scala at my spare time, I would love to try to do something similar for Scala. We will see in the future if I will take time to pursue it.

onsdag 5 januari 2011

Setting up a SBT Scala project for IntelliJ IDEA

I have previously set up Scala projects using maven and found it to be quite a hassle the first time I set up a project. My main problem was to find a good up-to-date archetype to create my project from, but since then I just copy my stub project any time I want to try out something new.

Since then I have been reading more about Scala and find that SBT seem to be a quite popular tool for handling your project dependencies and builds, so I wanted to try to use SBT instead to see if I find to be a more suitable build/dependency management tool.

My first step was to download (http://code.google.com/p/simple-build-tool/downloads/list), install and setup SBT, where I followed the setup instructions found in http://code.google.com/p/simple-build-tool/wiki/Setup

My main problem with the guide, being a windows user mainly (but running Ubuntu now), was that I was expected to create my own shell script named sbt, and the instructions for it didn't make sense to me. The following was the instructions of what to put in the shell script according to the guide:
java -Xmx512M -jar `dirname $0`/sbt-launch.jar "$@"
But I didn't know what the $0 actually did and didn't get it to work, so I just ended up using the syntax I know for starting jar-files in java
java -Xmx512M -jar /home/myuser/dev/app/sbt/sbt-launch.jar "$@"
So assuming that you also use linux, the sbt shell script should be put somewhere on your path. Myself I have a directory setup under my user folder in /home which is simply called "bin", where I put all scripts I want to be able to run in the shell. This directory I have mapped in my path, so I can write "sbt" independent of where I am in the shell and it will run. Next step is to run the following on your sbt shell script:
chmod u+x ~/bin/sbt

Congratulations, you now have sbt installed and should be able to run it from the shell. But before doing that we are going to create a folder for our project. Open the Shell and change directory to where you want to store your project, then do the following:
mkdir ScalaExperiment
cd ScalaExperiment
sbt
After entering sbt you are asked if you want to create a project and if you answer yes you are asked for information about the project name etc. Just enter the same data as I did in the log below:
Project does not exist, create new project? (y/N/s) y
Name: ScalaExperiment
Organization: org.thoughtmeld
Version [1.0]: 
Scala version [2.8.1]: 
sbt version [0.7.5.RC0]:

Your project is created and ready to be used? Not entirely, unless you want manually set up your IntelliJ IDEA project with this project stub. Which isn't necessary anymore thanks to the sbt-idea plugin found at https://github.com/mpeltonen/sbt-idea. All you have to do is write the following lines in sbt
*sbtIdeaRepo at http://mpeltonen.github.com/maven/
*idea is com.github.mpeltonen sbt-idea-processor 0.2.0
This will define a new processor repository and download the sbt-idea processor. So now all you need to do is run the update action and then the idea action to create the idea project files.
update
idea
Now you have a project stub setup using SBT together with some generated IntelliJ IDEA project files, now you can just boot up your IntelliJ IDEA with the scala and sbt plugin installed and open the project. Happy coding!