- Revision
- 909
- Author
- mauro
- Date
- 2008-08-31 16:49:34 -0500 (Sun, 31 Aug 2008)
Log Message
Extracted SomeSteps to top-level class so they can be used in multiple behaviours. Ensured converter values are trimmed after being split from CSV values.
Modified Paths
- trunk/jbehave-core/src/behaviour/org/jbehave/scenario/steps/CandidateStepBehaviour.java
- trunk/jbehave-core/src/behaviour/org/jbehave/scenario/steps/ParameterConvertersBehaviour.java
- trunk/jbehave-core/src/java/org/jbehave/scenario/steps/ParameterConverters.java
Added Paths
Diff
Modified: trunk/jbehave-core/src/behaviour/org/jbehave/scenario/steps/CandidateStepBehaviour.java (908 => 909)
--- trunk/jbehave-core/src/behaviour/org/jbehave/scenario/steps/CandidateStepBehaviour.java 2008-08-31 09:26:47 UTC (rev 908) +++ trunk/jbehave-core/src/behaviour/org/jbehave/scenario/steps/CandidateStepBehaviour.java 2008-08-31 21:49:34 UTC (rev 909) @@ -7,11 +7,6 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; -import java.beans.BeanInfo; -import java.beans.IntrospectionException; -import java.beans.Introspector; -import java.beans.MethodDescriptor; -import java.lang.reflect.Method; import java.util.List; import org.jbehave.scenario.parser.PrefixCapturingPatternBuilder; @@ -112,21 +107,21 @@ public void shouldConvertArgsToListOfNumbers() throws Exception { SomeSteps someSteps = new SomeSteps(); CandidateStep candidateStep = new CandidateStep("windows on the $nth floors", - methodFor("aMethodWithListOfLongs"), someSteps, PATTERN_BUILDER, MONITOR, new ParameterConverters(), "Given", "When", "Then"); + SomeSteps.methodFor("aMethodWithListOfLongs"), someSteps, PATTERN_BUILDER, MONITOR, new ParameterConverters(), "Given", "When", "Then"); candidateStep.createFrom("When windows on the 1L,2L,3L floors").perform(); ensureThat(((List<?>) someSteps.args).toString(), equalTo(asList(1L, 2L, 3L).toString())); - candidateStep = new CandidateStep("windows on the $nth floors", methodFor("aMethodWithListOfIntegers"), + candidateStep = new CandidateStep("windows on the $nth floors", SomeSteps.methodFor("aMethodWithListOfIntegers"), someSteps, PATTERN_BUILDER, MONITOR, new ParameterConverters(), "Given", "When", "Then"); candidateStep.createFrom("When windows on the 1,2,3 floors").perform(); ensureThat(((List<?>) someSteps.args).toString(), equalTo(asList(1, 2, 3).toString())); - candidateStep = new CandidateStep("windows on the $nth floors", methodFor("aMethodWithListOfDoubles"), + candidateStep = new CandidateStep("windows on the $nth floors", SomeSteps.methodFor("aMethodWithListOfDoubles"), someSteps, PATTERN_BUILDER, MONITOR, new ParameterConverters(), "Given", "When", "Then"); candidateStep.createFrom("When windows on the 1.1,2.2,3.3 floors").perform(); ensureThat(((List<?>) someSteps.args).toString(), equalTo(asList(1.1, 2.2, 3.3).toString())); - candidateStep = new CandidateStep("windows on the $nth floors", methodFor("aMethodWithListOfFloats"), + candidateStep = new CandidateStep("windows on the $nth floors", SomeSteps.methodFor("aMethodWithListOfFloats"), someSteps, PATTERN_BUILDER, MONITOR, new ParameterConverters(), "Given", "When", "Then"); candidateStep.createFrom("When windows on the 1.1f,2.2f,3.3f floors").perform(); ensureThat(((List<?>) someSteps.args).toString(), equalTo(asList(1.1f, 2.2f, 3.3f).toString())); @@ -137,67 +132,8 @@ public void shouldConvertArgsToListOfStrings() throws Exception { SomeSteps someSteps = new SomeSteps(); CandidateStep candidateStep = new CandidateStep("windows on the $nth floors", - methodFor("aMethodWithListOfStrings"), someSteps, PATTERN_BUILDER, MONITOR, new ParameterConverters(), "Given", "When", "Then"); + SomeSteps.methodFor("aMethodWithListOfStrings"), someSteps, PATTERN_BUILDER, MONITOR, new ParameterConverters(), "Given", "When", "Then"); candidateStep.createFrom("When windows on the 1,2,3 floors").perform(); ensureThat(((List<?>) someSteps.args).toString(), equalTo(asList("1", "2", "3").toString())); } - - public static Method methodFor(String methodName) throws IntrospectionException { - BeanInfo beanInfo = Introspector.getBeanInfo(SomeSteps.class); - for (MethodDescriptor md : beanInfo.getMethodDescriptors()) { - if (md.getMethod().getName().equals(methodName)) { - return md.getMethod(); - } - } - return null; - } - - public class SomeSteps extends Steps { - private Object args; - - public void aMethod() { - - } - - public void aMethodWith(String args) { - this.args = args; - } - - public void aMethodWith(double args) { - this.args = args; - } - - public void aMethodWith(long args) { - this.args = args; - } - - public void aMethodWith(int args) { - this.args = args; - } - - public void aMethodWith(float args) { - this.args = args; - } - - public void aMethodWithListOfStrings(List<String> args) { - this.args = args; - } - - public void aMethodWithListOfLongs(List<Long> args) { - this.args = args; - } - - public void aMethodWithListOfIntegers(List<Integer> args) { - this.args = args; - } - - public void aMethodWithListOfDoubles(List<Double> args) { - this.args = args; - } - - public void aMethodWithListOfFloats(List<Float> args) { - this.args = args; - } - - } }
Modified: trunk/jbehave-core/src/behaviour/org/jbehave/scenario/steps/ParameterConvertersBehaviour.java (908 => 909)
--- trunk/jbehave-core/src/behaviour/org/jbehave/scenario/steps/ParameterConvertersBehaviour.java 2008-08-31 09:26:47 UTC (rev 908) +++ trunk/jbehave-core/src/behaviour/org/jbehave/scenario/steps/ParameterConvertersBehaviour.java 2008-08-31 21:49:34 UTC (rev 909) @@ -1,33 +1,35 @@ package org.jbehave.scenario.steps; +import static org.hamcrest.CoreMatchers.equalTo; import static org.jbehave.Ensure.ensureThat; -import static org.hamcrest.CoreMatchers.equalTo; +import java.beans.IntrospectionException; +import java.lang.reflect.Type; import java.text.NumberFormat; import java.text.ParseException; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; import java.util.List; import org.junit.Test; public class ParameterConvertersBehaviour { - @Test + private static final NumberFormat NUMBER_FORMAT = NumberFormat.getInstance(); + + @Test public void shouldConvertStringsToNumbers() { ParameterConverters converters = new ParameterConverters(); ensureThat((Integer)converters.convert("3", int.class), equalTo(3)); } -// @SuppressWarnings("unchecked") -// @Test -// public void shouldConvertCommaSeparatedValuesToListsOfNumbers() throws ParseException { -// ParameterConverters converters = new ParameterConverters(); -// List<Number> list = (List<Number>)converters.convert("3, 5, 6, 8.00", Arrays.asList(new Number[]{}).getClass()); -// ensureThat(list.get(0), equalTo(NumberFormat.getInstance().parse("3"))); -// ensureThat(list.get(1), equalTo(NumberFormat.getInstance().parse("5"))); -// ensureThat(list.get(2), equalTo(NumberFormat.getInstance().parse("6"))); -// ensureThat(list.get(3), equalTo(NumberFormat.getInstance().parse("8.00"))); -// } + @SuppressWarnings("unchecked") + @Test + public void shouldConvertCommaSeparatedValuesToListsOfNumbers() throws ParseException, IntrospectionException { + ParameterConverters converters = new ParameterConverters(); + Type type = SomeSteps.methodFor("aMethodWithListOfNumbers").getGenericParameterTypes()[0]; + List<Number> list = (List<Number>)converters.convert("3, 5, 6, 8.00", type); + ensureThat(list.get(0), equalTo(NUMBER_FORMAT.parse("3"))); + ensureThat(list.get(1), equalTo(NUMBER_FORMAT.parse("5"))); + ensureThat(list.get(2), equalTo(NUMBER_FORMAT.parse("6"))); + ensureThat(list.get(3), equalTo(NUMBER_FORMAT.parse("8.00"))); + } }
Added: trunk/jbehave-core/src/behaviour/org/jbehave/scenario/steps/SomeSteps.java (0 => 909)
--- trunk/jbehave-core/src/behaviour/org/jbehave/scenario/steps/SomeSteps.java (rev 0) +++ trunk/jbehave-core/src/behaviour/org/jbehave/scenario/steps/SomeSteps.java 2008-08-31 21:49:34 UTC (rev 909) @@ -0,0 +1,71 @@ +package org.jbehave.scenario.steps; + +import java.beans.BeanInfo; +import java.beans.IntrospectionException; +import java.beans.Introspector; +import java.beans.MethodDescriptor; +import java.lang.reflect.Method; +import java.util.List; + +public class SomeSteps extends Steps { + Object args; + + public void aMethod() { + + } + + public void aMethodWith(String args) { + this.args = args; + } + + public void aMethodWith(double args) { + this.args = args; + } + + public void aMethodWith(long args) { + this.args = args; + } + + public void aMethodWith(int args) { + this.args = args; + } + + public void aMethodWith(float args) { + this.args = args; + } + + public void aMethodWithListOfStrings(List<String> args) { + this.args = args; + } + + public void aMethodWithListOfLongs(List<Long> args) { + this.args = args; + } + + public void aMethodWithListOfIntegers(List<Integer> args) { + this.args = args; + } + + public void aMethodWithListOfDoubles(List<Double> args) { + this.args = args; + } + + public void aMethodWithListOfFloats(List<Float> args) { + this.args = args; + } + + public void aMethodWithListOfNumbers(List<Number> args) { + this.args = args; + } + + public static Method methodFor(String methodName) throws IntrospectionException { + BeanInfo beanInfo = Introspector.getBeanInfo(SomeSteps.class); + for (MethodDescriptor md : beanInfo.getMethodDescriptors()) { + if (md.getMethod().getName().equals(methodName)) { + return md.getMethod(); + } + } + return null; + } + +}
Modified: trunk/jbehave-core/src/java/org/jbehave/scenario/steps/ParameterConverters.java (908 => 909)
--- trunk/jbehave-core/src/java/org/jbehave/scenario/steps/ParameterConverters.java 2008-08-31 09:26:47 UTC (rev 908) +++ trunk/jbehave-core/src/java/org/jbehave/scenario/steps/ParameterConverters.java 2008-08-31 21:49:34 UTC (rev 909) @@ -69,7 +69,8 @@ } private static class NumberConverter implements ParameterConverter { - private static List<Class> acceptedClasses = Arrays.asList(new Class[] { + @SuppressWarnings("unchecked") + private static List<Class> acceptedClasses = Arrays.asList(new Class[] { Integer.class, int.class, Long.class, long.class, Double.class, double.class, Float.class, float.class }); @@ -110,7 +111,7 @@ } public Object convertValue(String value, Type type) { - List<String> values = asList(value.split(COMMA)); + List<String> values = trim(asList(value.split(COMMA))); NumberFormat numberFormat = NumberFormat.getInstance(); List<Number> numbers = new ArrayList<Number>(); for (String numberValue : values) { @@ -139,9 +140,17 @@ } public Object convertValue(String value, Type type) { - return asList(value.split(COMMA)); + return trim(asList(value.split(COMMA))); } } + public static List<String> trim(List<String> values) { + List<String> trimmed = new ArrayList<String>(); + for ( String value : values ){ + trimmed.add(value.trim()); + } + return trimmed; + } + }
To unsubscribe from this list please visit:
