2003-10-06

Learning the hard way

I just found a naughty bug in one of my Java projects. I was using a StringTokenizer to parse a string separated by semicolons into a list:

StringTokenizer tokenizer = new StringTokenizer(tokenString, ";");
List tokens = new ArrayList();
for(int i = 0; i < tokenizer.countTokens(); i++) {
    tokens.add(tokenizer.nextToken().trim());
}

Can you spot my mistake? The countTokens() method doesn't actually give you the overall count of tokens in the string. It calculates how many tokens are left, i.e. how many more times you can call nextToken() without getting an exception! So of course I was getting too few entries in the list.

Now being the good boy that I am, I had of course written a JUnit test for the related class. Among other things, it tests whether the list parsed from a well-known string was the same as the one parsed from a string from an external source by the class to be tested. Of course, I used a similar tokenizer loop …

And the morale is of course: Write unit tests, but don't trust them blindly. After all, somebody has to control the controller.

By the way, the correct loop is this:

while(tokenizer.hasMoreTokens()) {
    tokens.add(tokenizer.nextToken().trim());
}

Trackbacks are closed for this story.

Comments are closed for this story.