Dienstag, November 14, 2006

Testing with maven2

While I like maven2 because of its productivity benefits now a simple task was taking me some time of research. I have to deal with some legacy code where some tests exists, but these are plain java code with main methods. I look forward to refactor them to junit tests, but step by step. So I have to tell maven what class to use and what not. For two reasons I'd like to gather them all in a testsuite. First I could start the suite easily from my IDE, and second I have another testsuite, which will require an external Corbaserver running. So I would like to seperate these tests.

There are two points to consider:

1) Because maven doesnt seem to work with suites you have to write a TestCase like this:

public class SuiteTest extends TestCase {

TestResult tr = null;

public void testSuite() {
TestSuite suite = (TestSuite) YourTestSuite.suite();

public void run(TestResult res) {
tr = res;

2) Whereas in maven1 it was simple to configure the unit tests with the <unittest> tag, this doesnt work in maven2 anymore. In the pom.xml you have to configure the surefire plugin which runs the test. Thats not obvious for a maven1 user, so its not easy to find the right documentation. But the maven2 concept is to be more plugable. So its easier to include Testng for instance.

Sonntag, Oktober 29, 2006

Amateur projects

It happend to me again. Today I found myself in a new project where everything seems to go wrong. The climate is non productive, they have sitting 12 developers, where I guess 2 or 3 good ones can do the same job. They have absolutly no unit tests, no build server, no documatation, neither external nor within code, no code reviews, a lot of changing developers, an over complicated build system which doesnt work, too much code which is solved by open source libraries, and so on. If you work like this you get used to it, but if you know there is a much different way there is only one way: you have to tell. The decision is as always between leave this as soon as possible or try to change, which of course is much harder. But this depends on the situation, as long as there is enough money nobody cares. Its easier to convice them if they realize its a case of surving.

Samstag, Juli 15, 2006

Checkout Maven

I worked for a company where they used Maven as a standard build tool. They used maven1, but I had some spare time the last couple of weeks, so I used it to play with maven2. I read the book Maven from the Developers Notebook series and Better Build with Maven.

First of all, why use Maven instead of Ant, which are most familiar with?

1) Its a collection of best practices of build & configuration managment: Where do I put my configuration files, how do I integrate common tools like xdoclet, clover or checkstyle, make project reports, where do I put my external jars, how to announce a new version, do the deployment and so on. When I checkout old projects of mine, which are based on Ant, I always have to look in the build.xml how and where files are copied and so on. With maven thats not the case, because everything is just at the right place. And because with Ant there is no standard build structure, every project is different.

2) Reuse: Maven has a plugin concept. Every task is a plugin which gets loaded when you use it the first time, or gets updated if there is a new version. Even common tasks like clean are plugins. So you dont need to write scripts, you just call mvn clean without having any definition of clean in your project file.

3) Productivity: you start creating a new project with mvn archetype:create -DgroupId=your.main.package -DartifactId=projectname and you get a basic project structure. You define your final archive like jar, war or ear, add your external jar dependencies to pom.xml, call mvn eclipse:eclipse or mvn idea:idea, you get your project files for your IDE and can start working within seconds. And finally mvn install compiles your code, runs your unit tests and packages your archive. All that is done without the need to write any build code. Last but not least maven2 is faster than Ant.

There is much more you get with maven for free, like cruisecontrol integration, repository integration plus reports and stats, you can run your httpunit or cactus tests out-of-the-box.

The dependency managment is great: Maven supports transitive dependencies, so if you use Struts you just add Struts to your dependencies and get all jars needed by Struts. Maven provides automatic conflict managment. And every dependency has a scope, some jars are just needed at compile time, for tests and so on. All that is nothing you have to worry about, you just add the scope tag to your dependency.

So I really believe every java developer should consider using Maven, at least give it a try and play around with it a little bit.

Freitag, Juni 23, 2006

Fun with testing

I will not talk about the need for unit tests. This I would say is common sense for a modern thinking developer. I recently had to do some small changes in a project. The main developer had already left the company and I got a brief introduction. They all felt very secure because there was a testcase for every single usecase, and also for every possible error. But these were integrationtests, they wrote something into a database, sent requests per URL and parsed the result with httpunit.
So far so good, everyone was happy, but me. Why that? Of course because there were no unit tests at all. It took me about three days to get the tests running. They used real services instead of mocks, so the tests ran just on a dedicated machine, because of firewall restrictions. The logs where analysed to ensure proper behaviour, but because they where running on an external machine, there where some shell scripts to pipe the logs to a socket. I played around for a while, even tried to use logj4 SocketAppender but failed to get the whole thing running. God knows why. So I dropped that validation. The script to run the tests swallowed every exception. The tests were running about 30 minutes, so after reading the latest news you just saw that some of the tests failed but had no glue why.

That all pissed me of, so I started writing unit tests and mock objects. One of my first tests failed and I was wondering why. I discovered a bug where two flags from an external database had been read in the wrong order. You have to know these where not just two flags nobody cared. These had been two of five values these app was evaluating, one of the reasons why these service was written. To evaluate these and to redirect the user depending on the combination of these.

WTF?! But what about the integration tests? At least they ran without an error. So after looking at the testcode, I couldnt believe my eyes: the good old boy was reversing his error and exchanged the flags at initialization time, so everything was looking fine again. I can imagine the situation, maybe he was wondering why his tests failed and thought, oh i might have exchanged the flags. So after reversing them he got the green bar.

This app was in production state for about one year and everyone was happy, but me .....

Dienstag, Juni 20, 2006

SpringOne in Antwerp

It was my first conference since a long time and it was a real pleasure to be there. I do Spring development for over half a year right now and I was excited to see whats currently going on. There were about 410 attendees, and we could listen to 40 presentations. But we had to choose beween four parallel talks, which was a hard decision sometimes. The slides will be published on the SpringOne site at the end of June and the DVD will be available in September. 

The biggest news was Spring 2.0 is ready to use, the final is scheduled for July. Its fully compatible with Spring 1, so no changes for existing apps. Which is great I think, and shows the big advantage of working with POJOs and AOP with loose coupling in mind. You dont destroy old code if you add more. Most of the talks had a relation to the new features included in this new version. For a complete overview click here.

At the keynote Rod Johnson told how many professionals are using Spring, even in mission critical projects like in the banking sector. There were some case studies given, and the message was clear: Spring is not just production ready, every JEE project should switch. Spring is more, its an philosophy, a commitment to agile development, to simplicity, and more or less to test driven development, because its one of Springs key points, to hold your code testable. 

It was obvious that this conference was sponsored by at least two majors namely Oracle and BEA, they even had two talks together. Both are very into JPA, which is supported by Spring since 2.0. I for myself found it funny that Hibernate was mentioned very seldom at this conference ;-) JPA should be the new one and only persistence API in the future, say goodby to JDO. The Hibernate -API will be maintained in the future, but will not developed any further. But Hibernate is on their way to support the new JSR-220, so this should be no principal problem for Hibernate users, like myself. It was news for me that Kodo is bought by BEA and Opensource as well as the core engine of TopLink, TopLink Essentials. The latter is the reference implementation for the new Java persistence API. 

Both companys are very commited to Spring, they will support Springs way to a defacto standard for middleware apps. So Pitchfork is a co-production from BEA and Interface21, an add-on for the Spring Framework for Java EE 5. Spring should provide a foundation for serveral Java Enterprise Edition 5 key components, like EJB 3 interception. But it has more features (e.g. typesafety and pointcuts for interceptors) and is more flexible.

Which leads me to another main feature of Spring 2.0, better AspectJ integration and support. The merge of AspectJ and AspectWerks was a big step forward standardizing AOP, so its great that Spring is not further going their own way. 

A not so spectacular but I think very usefull new feature is custom namespaces in Spring configurations. My last several projects were a typical SOA landscape with many small services. We had some naming conflicts in our configurations, and custom namespace is an solution. On the other hand your configuration gets more readable and less error prone if you define your own types. There are small restrictions though like just singletons are supported (which will change asap). 

The DWR Framework, an Ajax protocol layer for Java, which was presented at the very beginning, is one of the early implementers of this feature. It really does make sense to have more context aware tag- and attributenames. And with schemas you can define possible enumeration-values for instance. So your IDE shows immediatly the read line for wrong values. 

Juergen Hoeller presented the JMS support by Spring, of course including the new features like asynchronous JMS and the task executers, which can work like message driven beans in non managed environments. I dont want to go too much into detail here, but he brought some gotchas and best practices with sending JMS. The new thing is the ability to receive messages in a typical spring way. He used a SessionAwareMessageListener to send a reply message, which is a Spring style MDB. It can participate in transactions and so one. The only thing missing here is a connection pool, this you get from your JMS provider. 

The last talk I listened to was about the new Spring-WS framework, an Spring add-on. First off all, why another Webservice framework? Its key points are of course  ease-of-use and excellent Spring integration, but it focuses on document driven Webservices and contract first WSDL development. Thats a difference to XFire for instance. So you work more with XML, but its the payload XML without the SOAP stuff. To the contrary, with XFire you expose your objects. You get Marshallers and Unmarshallers of course, to parse the XML. But XML is the first thing you have got in your hand with Spring-WS. 

As I said many talks were in parallel so I missed some of them, like the clustering solution from terracota, some in depth talks about JPA, domain driven development and so on. 

Despite the landing of the new version, there are many ideas where to go in the future. Simplier configuration and better production support, like profiling and tuning and more use of JMX are some ideas. But it also depends on the community. Everyone who is interested in or even working with Spring, should regularly check and better post to the forums