Created
April 25, 2012 09:07
-
-
Save tknerr/2488376 to your computer and use it in GitHub Desktop.
cucumber-jvm: alternative proposal for specifying default xstream converters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package features.foo.alternative; | |
import cucumber.runtime.xstream.converters.ConverterMatcher; | |
public interface ConverterRegistry { | |
Class<? extends ConverterMatcher> getConverter(Class<?> type); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package features.foo.alternative; | |
import java.util.List; | |
import java.util.Map; | |
import junit.framework.Assert; | |
import cucumber.annotation.en.Given; | |
import cucumber.runtime.xstream.annotations.XStreamConverter; | |
import cucumber.runtime.xstream.converters.ConverterMatcher; | |
import cucumber.runtime.xstream.converters.basic.AbstractSingleValueConverter; | |
import cucumber.runtime.xstream.converters.extended.ToStringConverter; | |
import features.foo.alternative.DefaultConvertersAlternative.MyConverters; | |
@StepDefOptions(defaultConverters = MyConverters.class) | |
public class FooStepDefs { | |
// default converters | |
static class MyConverters extends MapBasedConverterRegistry { | |
public MyConverters(Map<Class<?>, Class<? extends ConverterMatcher>> converterRegistryMap) { | |
super(new ConverterMapBuilder() | |
.convert(Foo.class).with(FooConverter.class) | |
.convert(Bar.class).with(BarConverter.class) | |
.convert(Baz.class).with(BazConverter.class) | |
.build()); | |
} | |
} | |
@Given("^I have some foo named \"([^\"]*)\"$") | |
public void I_have_some_foo_named(Foo foo) { | |
Assert.assertNotNull(foo.getName()); | |
} | |
@Given("^I have some stuff in a data table:$") | |
public void I_have_some_stuff_in_a_data_table(List<StuffInTable> stuffInTable) { | |
for (StuffInTable stuff : stuffInTable) { | |
Assert.assertNotNull(stuff.foo.getName()); | |
Assert.assertNotNull(stuff.bar.getName()); | |
Assert.assertNotNull(stuff.baz.getName()); | |
} | |
} | |
public static class StuffInTable { | |
public Foo foo; | |
// override default converter if necessary | |
@XStreamConverter(AlternativeBarConverter.class) | |
public Bar bar; | |
public Baz baz; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package features.foo.alternative; | |
import java.util.HashMap; | |
import java.util.Map; | |
import cucumber.runtime.xstream.converters.ConverterMatcher; | |
public class MapBasedConverterRegistry implements ConverterRegistry { | |
protected static class ConverterMapBuilder { | |
private final Map<Class<?>, Class<? extends ConverterMatcher>> map = new HashMap<Class<?>, Class<? extends ConverterMatcher>>(); | |
private Class<?> type; | |
public ConverterMapBuilder convert(Class<?> type) { | |
assert type != null; | |
assert this.type == null; | |
this.type = type; | |
return this; | |
} | |
public ConverterMapBuilder with( | |
Class<? extends ConverterMatcher> converter) { | |
assert converter != null; | |
map.put(type, converter); | |
this.type = null; | |
return this; | |
} | |
public HashMap<Class<?>, Class<? extends ConverterMatcher>> build() { | |
assert this.type == null; | |
return new HashMap<Class<?>, Class<? extends ConverterMatcher>>(map); | |
} | |
} | |
private final Map<Class<?>, Class<? extends ConverterMatcher>> converterRegistryMap; | |
public MapBasedConverterRegistry( | |
Map<Class<?>, Class<? extends ConverterMatcher>> converterRegistryMap) { | |
this.converterRegistryMap = converterRegistryMap; | |
} | |
public Class<? extends ConverterMatcher> getConverter(Class<?> type) { | |
return converterRegistryMap.get(type); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package features.foo.alternative; | |
import java.lang.annotation.ElementType; | |
import java.lang.annotation.Retention; | |
import java.lang.annotation.RetentionPolicy; | |
import java.lang.annotation.Target; | |
@Retention(RetentionPolicy.RUNTIME) | |
@Target({ ElementType.TYPE }) | |
public @interface StepDefOptions { | |
Class<? extends ConverterRegistry> defaultConverters(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment