Search in sources :

Example 1 with ParameterConverter

use of org.jbehave.core.steps.ParameterConverters.ParameterConverter in project jbehave-core by jbehave.

the class ParameterConvertersBehaviour method shouldConvertEnumList.

@Test
public void shouldConvertEnumList() {
    ParameterConverter converter = new EnumListConverter();
    Type type = new TypeLiteral<List<SomeEnum>>() {
    }.getType();
    assertThat(converter.accept(type), is(true));
    List<Enum> list = (List<Enum>) converter.convertValue("ONE,TWO,THREE", type);
    assertThatCollectionIs(list, SomeEnum.ONE, SomeEnum.TWO, SomeEnum.THREE);
}
Also used : Type(java.lang.reflect.Type) AbstractParameterConverter(org.jbehave.core.steps.ParameterConverters.AbstractParameterConverter) ParameterConverter(org.jbehave.core.steps.ParameterConverters.ParameterConverter) Arrays.asList(java.util.Arrays.asList) EnumListConverter(org.jbehave.core.steps.ParameterConverters.EnumListConverter) Test(org.junit.Test)

Example 2 with ParameterConverter

use of org.jbehave.core.steps.ParameterConverters.ParameterConverter in project jbehave-core by jbehave.

the class ParameterConvertersBehaviour method shouldConvertCommaSeparatedValuesOfSpecificNumberTypes.

@SuppressWarnings("unchecked")
@Test
public void shouldConvertCommaSeparatedValuesOfSpecificNumberTypes() {
    ParameterConverter converter = new NumberListConverter();
    Type doubleType = new TypeLiteral<List<Double>>() {
    }.getType();
    List<Double> doubles = (List<Double>) converter.convertValue("3, 0.5, 0.0, 8.00, " + NAN + ", " + POSITIVE_INFINITY, doubleType);
    assertThatCollectionIs(doubles, 3.0, 0.5, 0.0, 8.0, Double.NaN, Double.POSITIVE_INFINITY);
    Type floatType = new TypeLiteral<List<Float>>() {
    }.getType();
    List<Float> floats = (List<Float>) converter.convertValue("3, 0.5, 0.0, 8.00, " + NAN + ", " + NEGATIVE_INFINITY, floatType);
    assertThatCollectionIs(floats, 3.0f, 0.5f, 0.0f, 8.0f, Float.NaN, Float.NEGATIVE_INFINITY);
    Type longType = new TypeLiteral<List<Long>>() {
    }.getType();
    List<Long> longs = (List<Long>) converter.convertValue("3, 0, 8", longType);
    assertThatCollectionIs(longs, 3L, 0L, 8L);
    Type intType = new TypeLiteral<List<Integer>>() {
    }.getType();
    List<Integer> ints = (List<Integer>) converter.convertValue("3, 0, 8", intType);
    assertThatCollectionIs(ints, 3, 0, 8);
}
Also used : AbstractParameterConverter(org.jbehave.core.steps.ParameterConverters.AbstractParameterConverter) ParameterConverter(org.jbehave.core.steps.ParameterConverters.ParameterConverter) NumberListConverter(org.jbehave.core.steps.ParameterConverters.NumberListConverter) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BigInteger(java.math.BigInteger) Type(java.lang.reflect.Type) AtomicLong(java.util.concurrent.atomic.AtomicLong) Arrays.asList(java.util.Arrays.asList) Test(org.junit.Test)

Example 3 with ParameterConverter

use of org.jbehave.core.steps.ParameterConverters.ParameterConverter in project jbehave-core by jbehave.

the class ParameterConvertersBehaviour method shouldFailToConvertParameterFromFailingMethodReturningValue.

@Test
public void shouldFailToConvertParameterFromFailingMethodReturningValue() throws IntrospectionException {
    expectedException.expect(ParameterConvertionFailed.class);
    Method method = SomeSteps.methodFor("aFailingMethodReturningExamplesTable");
    ParameterConverter converter = new MethodReturningConverter(method, new SomeSteps());
    String value = "|col1|col2|\n|row11|row12|\n|row21|row22|\n";
    converter.convertValue(value, ExamplesTable.class);
}
Also used : AbstractParameterConverter(org.jbehave.core.steps.ParameterConverters.AbstractParameterConverter) ParameterConverter(org.jbehave.core.steps.ParameterConverters.ParameterConverter) MethodReturningConverter(org.jbehave.core.steps.ParameterConverters.MethodReturningConverter) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 4 with ParameterConverter

use of org.jbehave.core.steps.ParameterConverters.ParameterConverter in project jbehave-core by jbehave.

the class ConcurrencyBehaviour method shouldAllowConcurrentAdditionOfParameterConvertersInThreadSafeMode.

@Test
public void shouldAllowConcurrentAdditionOfParameterConvertersInThreadSafeMode() {
    final ParameterConverters parameterConverters = new ParameterConverters(new LoadFromClasspath(), new ParameterControls(), new TableTransformers(), true);
    final boolean[] active = new boolean[] { true };
    final ParameterConverter[] toAdd = new ParameterConverter[] { mock(ParameterConverter.class, "one"), mock(ParameterConverter.class, "two"), mock(ParameterConverter.class, "three") };
    Thread t = new Thread(new Runnable() {

        @Override
        public void run() {
            while (active[0]) {
                parameterConverters.addConverters(toAdd);
            }
        }
    });
    t.start();
    parameterConverters.convert("test", String.class);
    active[0] = false;
}
Also used : ParameterConverter(org.jbehave.core.steps.ParameterConverters.ParameterConverter) LoadFromClasspath(org.jbehave.core.io.LoadFromClasspath) TableTransformers(org.jbehave.core.model.TableTransformers) Test(org.junit.Test)

Example 5 with ParameterConverter

use of org.jbehave.core.steps.ParameterConverters.ParameterConverter in project jbehave-core by jbehave.

the class SpringAnnotationBuilder method getBeansOfType.

private List<ParameterConverter> getBeansOfType(ApplicationContext context, Class<ParameterConverter> type) {
    Map<String, ParameterConverter> beansOfType = context.getBeansOfType(type);
    List<ParameterConverter> converters = new ArrayList<>();
    for (ParameterConverter converter : beansOfType.values()) {
        converters.add(converter);
    }
    return converters;
}
Also used : ParameterConverter(org.jbehave.core.steps.ParameterConverters.ParameterConverter) ArrayList(java.util.ArrayList)

Aggregations

ParameterConverter (org.jbehave.core.steps.ParameterConverters.ParameterConverter)9 Test (org.junit.Test)8 AbstractParameterConverter (org.jbehave.core.steps.ParameterConverters.AbstractParameterConverter)7 Type (java.lang.reflect.Type)4 LoadFromClasspath (org.jbehave.core.io.LoadFromClasspath)4 TableTransformers (org.jbehave.core.model.TableTransformers)4 Arrays.asList (java.util.Arrays.asList)3 Method (java.lang.reflect.Method)2 ExamplesTableFactory (org.jbehave.core.model.ExamplesTableFactory)2 ExamplesTableParametersConverter (org.jbehave.core.steps.ParameterConverters.ExamplesTableParametersConverter)2 MethodReturningConverter (org.jbehave.core.steps.ParameterConverters.MethodReturningConverter)2 MyParameters (org.jbehave.core.steps.SomeSteps.MyParameters)2 BigInteger (java.math.BigInteger)1 ArrayList (java.util.ArrayList)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 ExamplesTable (org.jbehave.core.model.ExamplesTable)1 EnumListConverter (org.jbehave.core.steps.ParameterConverters.EnumListConverter)1 NumberListConverter (org.jbehave.core.steps.ParameterConverters.NumberListConverter)1