Details
-
Type: Improvement
-
Status: Open
-
Priority: Major
-
Resolution: Unresolved
-
Affects Version/s: None
-
Fix Version/s: None
-
Component/s: None
-
Labels:None
-
Number of attachments :
Description
Simplifying adding ParameterConverter to Steps. Here's an example :
public class DateTimeConverter implements ParameterConverter { @Override public boolean accept(Type type) { return type.equals( DateTime.class ); } @Override public Object convertValue(String value, Type type) { return new DateTime( value ); } }
@ParameterConverters(DateTimeConverter.class) public class PersonSteps { private Person person; private DateTime currentDate; // ... steps }
The ParameterConverters annotation takes an array of TypeConverter :
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) public @interface ParameterConverters { Class<? extends ParameterConverter>[] value(); }
Here is a steps factory :
public class ParameterConverterStepsFactory { public CandidateSteps[] createCandidateSteps(Object... stepsInstances) throws InstantiationException, IllegalAccessException { CandidateSteps[] candidateSteps = new CandidateSteps[stepsInstances.length]; for (int i = 0; i < stepsInstances.length; i++) { Object stepsInstance = stepsInstances[i]; StepsConfiguration configuration; ParameterConverters parameterConverters = stepsInstance.getClass().getAnnotation( ParameterConverters.class ); if( parameterConverters != null ) { configuration = createConfiguration( parameterConverters ); } else { configuration = new StepsConfiguration(); } candidateSteps[i] = new Steps(configuration, stepsInstance); } return candidateSteps; } private StepsConfiguration createConfiguration( ParameterConverters annotation) throws InstantiationException, IllegalAccessException { ParameterConverter[] converters = new ParameterConverter[ annotation.value().length ]; for( int i = 0; i < converters.length; i ++ ) { converters[ i ] = annotation.value()[ i ].newInstance(); } org.jbehave.scenario.steps.ParameterConverters converterAggregate = new org.jbehave.scenario.steps.ParameterConverters( converters ); return new StepsConfiguration( converterAggregate ); } }
Interesting stuff, Bruno. My only comment is that the annotation route might work nicely with converters that have default constructors, but how do we handle those that take parameters upon instantiation. It might get pretty messy.