cucumber.api
Class Transformer<T>

java.lang.Object
  extended by cucumber.api.Transformer<T>
Type Parameters:
T - the type to be instantiated
All Implemented Interfaces:
cucumber.deps.com.thoughtworks.xstream.converters.ConverterMatcher, cucumber.deps.com.thoughtworks.xstream.converters.SingleValueConverter

public abstract class Transformer<T>
extends Object
implements cucumber.deps.com.thoughtworks.xstream.converters.SingleValueConverter

Allows transformation of a step definition argument to a custom type, giving you full control over how that type is instantiated.

Consider the following Gherkin step:

Given today's date is "10/03/1985"

As an example, let's assume we want Cucumber to transform the substring "10/03/1985" into an instance of org.joda.time.LocalDate class:

     @Given("today's date is \"(.*)\"")
     public void todays_date_is(LocalDate d) {
     }
 

If the parameter's class has a constructor with a single String or Object argument, then Cucumber will instantiate it without any further ado. However, in this case that might not give you what you want. Depending on your Locale, the date may be Oct 3 or March 10!

This is when you can use a custom transformer. You'll also have to do that if your parameter class doesn't have a constructor with a single String or Object argument. For the JODA Time example:

     @Given("today's date is \"(.*)\"")
     public void todays_date_is(@Transform(JodaTimeConverter.class) LocalDate d) {
     }
 

And then a JodaTimeConverter class:

public static class JodaTimeConverter extends Transformer<LocalDate> {
         private static DateTimeFormatter FORMATTER = DateTimeFormat.forStyle("S-");

         &#064;Override
         public LocalDate transform(String value) {
             return FORMATTER.withLocale(getLocale()).parseLocalDate(value);
         }
     }
 

An alternative to annotating parameters with Transform is to annotate your class with cucumber.deps.com.thoughtworks.xstream.annotations.XStreamConverter:

     @XStreamConverter(MyConverter.class)
     public class MyClass {
     }
 

This will also enable a DataTable to be transformed to a List<MyClass;>

See Also:
Transform

Constructor Summary
Transformer()
           
 
Method Summary
 boolean canConvert(Class type)
           
 Object fromString(String s)
           
protected  Locale getLocale()
           
 void setParameterInfoAndLocale(cucumber.runtime.ParameterInfo parameterInfo, Locale locale)
           
 String toString(Object o)
           
abstract  T transform(String value)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Transformer

public Transformer()
Method Detail

toString

public String toString(Object o)
Specified by:
toString in interface cucumber.deps.com.thoughtworks.xstream.converters.SingleValueConverter

fromString

public final Object fromString(String s)
Specified by:
fromString in interface cucumber.deps.com.thoughtworks.xstream.converters.SingleValueConverter

canConvert

public boolean canConvert(Class type)
Specified by:
canConvert in interface cucumber.deps.com.thoughtworks.xstream.converters.ConverterMatcher

transform

public abstract T transform(String value)

setParameterInfoAndLocale

public void setParameterInfoAndLocale(cucumber.runtime.ParameterInfo parameterInfo,
                                      Locale locale)

getLocale

protected Locale getLocale()
Returns:
the current locale


Copyright © 2013. All Rights Reserved.