Search in sources :

Example 6 with SingleValueConverter

use of cucumber.deps.com.thoughtworks.xstream.converters.SingleValueConverter in project cucumber-jvm by cucumber.

the class ConvertersTest method shouldTransformToTypeWithStringCtor.

@Test
public void shouldTransformToTypeWithStringCtor() {
    SingleValueConverter c = ((DynamicClassBasedSingleValueConverter) en.lookupConverterForType(MyClass.class)).converterForClass(MyClass.class);
    assertEquals("X", ((MyClass) c.fromString("X")).s);
}
Also used : SingleValueConverter(cucumber.deps.com.thoughtworks.xstream.converters.SingleValueConverter) Test(org.junit.Test)

Example 7 with SingleValueConverter

use of cucumber.deps.com.thoughtworks.xstream.converters.SingleValueConverter in project cucumber-jvm by cucumber.

the class ConvertersTest method shouldTransformToTypeWithObjectCtor.

@Test
public void shouldTransformToTypeWithObjectCtor() {
    SingleValueConverter c = ((DynamicClassBasedSingleValueConverter) en.lookupConverterForType(MyOtherClass.class)).converterForClass(MyOtherClass.class);
    assertEquals("X", ((MyOtherClass) c.fromString("X")).o);
}
Also used : SingleValueConverter(cucumber.deps.com.thoughtworks.xstream.converters.SingleValueConverter) Test(org.junit.Test)

Example 8 with SingleValueConverter

use of cucumber.deps.com.thoughtworks.xstream.converters.SingleValueConverter in project cucumber-jvm by cucumber.

the class TableConverter method toMaps.

public <K, V> List<Map<K, V>> toMaps(DataTable dataTable, Type keyType, Type valueType) {
    try {
        xStream.setParameterInfo(parameterInfo);
        SingleValueConverter keyConverter = xStream.getSingleValueConverter(keyType);
        SingleValueConverter valueConverter = xStream.getSingleValueConverter(valueType);
        if (keyConverter == null || valueConverter == null) {
            throw new CucumberException(String.format("Can't convert DataTable to List<Map<%s,%s>>", keyType, valueType));
        }
        List<Map<K, V>> result = new ArrayList<Map<K, V>>();
        List<String> keyStrings = dataTable.topCells();
        List<K> keys = new ArrayList<K>();
        for (String keyString : keyStrings) {
            keys.add((K) keyConverter.fromString(keyString));
        }
        List<List<String>> valueRows = dataTable.cells(1);
        for (List<String> valueRow : valueRows) {
            Map<K, V> map = new LinkedHashMap<K, V>();
            int i = 0;
            for (String cell : valueRow) {
                map.put(keys.get(i), (V) valueConverter.fromString(cell));
                i++;
            }
            result.add(Collections.unmodifiableMap(map));
        }
        return Collections.unmodifiableList(result);
    } finally {
        xStream.unsetParameterInfo();
    }
}
Also used : ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) SingleValueConverter(cucumber.deps.com.thoughtworks.xstream.converters.SingleValueConverter) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) CucumberException(cucumber.runtime.CucumberException) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 9 with SingleValueConverter

use of cucumber.deps.com.thoughtworks.xstream.converters.SingleValueConverter in project cucumber-jvm by cucumber.

the class ConvertersTest method shouldTransformToTheRightType.

@Test
public void shouldTransformToTheRightType() {
    assertTrue((Boolean) ((SingleValueConverter) en.lookupConverterForType(Boolean.class)).fromString("true"));
    assertTrue((Boolean) ((SingleValueConverter) en.lookupConverterForType(Boolean.TYPE)).fromString("true"));
    assertEquals(3000.15f, (Float) ((SingleValueConverter) en.lookupConverterForType(Float.class)).fromString("3000.15"), 0.000001);
    assertEquals(3000.15f, (Float) ((SingleValueConverter) en.lookupConverterForType(Float.TYPE)).fromString("3000.15"), 0.000001);
    assertEquals(new BigDecimal("3000.15"), ((SingleValueConverter) en.lookupConverterForType(BigDecimal.class)).fromString("3000.15"));
    assertEquals(3000.15f, (Float) ((SingleValueConverter) no.lookupConverterForType(Float.TYPE)).fromString("3000,15"), 0.000001);
}
Also used : SingleValueConverter(cucumber.deps.com.thoughtworks.xstream.converters.SingleValueConverter) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Aggregations

SingleValueConverter (cucumber.deps.com.thoughtworks.xstream.converters.SingleValueConverter)9 CucumberException (cucumber.runtime.CucumberException)4 Test (org.junit.Test)3 ArrayList (java.util.ArrayList)2 Arrays.asList (java.util.Arrays.asList)2 LinkedHashMap (java.util.LinkedHashMap)2 List (java.util.List)2 DataTable (cucumber.api.DataTable)1 Utils.listItemType (cucumber.runtime.Utils.listItemType)1 Utils.mapKeyType (cucumber.runtime.Utils.mapKeyType)1 Utils.mapValueType (cucumber.runtime.Utils.mapValueType)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 BigDecimal (java.math.BigDecimal)1 Map (java.util.Map)1