About   Forum   Wiki   Home  

       
    Managed Chaos      
   
Naresh Jain’s Weblog on Object thinking, Patterns, Open Source, Agile and Adventure Sports

 
`
 
Tags
Recent Comments
Quick Search
Recent Entries
Categories
Archives
November 2006
M T W T F S S
« Sep   Dec »
 12345
6789101112
13141516171819
20212223242526
27282930  
Add to Technorati Favorites

Syndicate This Blog
Entries (RSS)
Comments (RSS)

Date aware FitLibrary Fixtures

Having problems parsing Dates in your fit/fitnesse tests?

Here is a simple way to fix date parsing problem using FitLibrary.

Fit Page

|set date format as|MM/dd/yyyy|

|date|
|11/23/2006|

|set date format as|dd-MMM-yyyy|

|date|
|23-Nov-2006|

Fixture Class

public void setDateFormatAs(String format) {
ValueAdapter.registerParseDelegate(java.sql.Date.class, new SimpleDateFormat(format));
ValueAdapter.registerParseDelegate(java.util.Date.class, new SimpleDateFormat(format));
}

How this works?
The ValueAdapter class holds a static HashMap called PARSE_DELEGATES which contains all the ParseDelegators. The ParseDelegator‘s job is to implement a parse method which take a String and returns the Object of your Data Type. You can register the Data Type and its respective ParseDelegator using the registerParseDelegate() method.

In this case, my ParseDelegator is SimpleDateFormat which has the following method

public Date parse(String source) throws ParseException{
ParsePosition pos = new ParsePosition(0);
Date result = parse(source, pos);
if (pos.index == 0)
throw new ParseException(”Unparseable date: ” + source ,
pos.errorIndex);
return result;
}

All that matters is the following method signature:
public Date parse(String source){

You can use the same mechanism to parse any object of choice.

Leave a Reply

This is a captcha-picture. It is used to prevent mass-access by robots. (see: www.captcha.net)

You must read and type the 5 chars within 0..9 and A..F, and submit the form.

  

Oh no, I cannot read this. Please, generate a

    Licensed under
Creative Commons License
Design by vikivix