XNSIO
  About   Slides   Home  

 
Managed Chaos
Naresh Jain's Random Thoughts on Software Development and Adventure Sports
     
`
 
RSS Feed
Recent Thoughts
Tags
Recent Comments

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

1
2
3
4
5
6
<br />
public void setDateFormatAs(String format) {<br />
ValueAdapter.registerParseDelegate(java.sql.Date.class, new SimpleDateFormat(format));<br />
ValueAdapter.registerParseDelegate(java.util.Date.class, new SimpleDateFormat(format));<br />
}</p>
	<p>

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

1
2
3
4
5
6
7
8
9
10
<br />
public Date parse(String source) throws ParseException{<br />
ParsePosition pos = new ParsePosition(0);<br />
Date result = parse(source, pos);<br />
if (pos.index == 0)<br />
throw new ParseException("Unparseable date: " + source ,<br />
pos.errorIndex);<br />
return result;<br />
}</p>
	<p>

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.


    Licensed under
Creative Commons License