Search in sources :

Example 1 with SingleValueConverter

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

the class ParameterInfo method convert.

public Object convert(String value, LocalizedXStreams.LocalizedXStream xStream) {
    try {
        xStream.setParameterInfo(this);
        SingleValueConverter converter;
        xStream.processAnnotations(getRawType());
        // Needed to unlock annotation processing
        xStream.autodetectAnnotations(true);
        if (transformer != null) {
            transformer.setParameterInfoAndLocale(this, xStream.getLocale());
            converter = transformer;
        } else {
            if (List.class.isAssignableFrom(getRawType())) {
                converter = getListConverter(type, xStream);
            } else {
                converter = xStream.getSingleValueConverter(getRawType());
            }
            if (converter == null) {
                throw new CucumberException(String.format("Don't know how to convert \"%s\" into %s.\n" + "Try writing your own converter:\n" + "\n" + "@%s(%sConverter.class)\n" + "public class %s {}\n", value, getRawType().getName(), XStreamConverter.class.getName(), getRawType().getSimpleName(), getRawType().getSimpleName()));
            }
        }
        return converter.fromString(value);
    } finally {
        xStream.unsetParameterInfo();
    }
}
Also used : SingleValueConverter(cucumber.deps.com.thoughtworks.xstream.converters.SingleValueConverter)

Example 2 with SingleValueConverter

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

the class ParameterInfo method getListConverter.

private SingleValueConverter getListConverter(Type type, LocalizedXStreams.LocalizedXStream xStream) {
    Class elementType = type instanceof ParameterizedType ? getRawType(((ParameterizedType) type).getActualTypeArguments()[0]) : Object.class;
    SingleValueConverter elementConverter = xStream.getSingleValueConverter(elementType);
    if (elementConverter == null) {
        return null;
    } else {
        return xStream.createListConverter(delimiter, elementConverter);
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) SingleValueConverter(cucumber.deps.com.thoughtworks.xstream.converters.SingleValueConverter)

Example 3 with SingleValueConverter

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

the class TableConverter method convert.

/**
     * This method converts a {@link cucumber.api.DataTable} to abother type.
     * When a Step Definition is passed a Gherkin Data Table, the runtime will use this method to convert the
     * {@link cucumber.api.DataTable} to the declared type before invoking the Step Definition.
     * <p/>
     * This method uses reflection to inspect the type and delegates to the appropriate {@code toXxx} method.
     *
     * @param dataTable  the table to convert
     * @param type       the type to convert to
     * @param transposed whether the table should be transposed first.
     * @return the transformed object.
     */
public <T> T convert(DataTable dataTable, Type type, boolean transposed) {
    if (transposed) {
        dataTable = dataTable.transpose();
    }
    if (type == null || (type instanceof Class && ((Class) type).isAssignableFrom(DataTable.class))) {
        return (T) dataTable;
    }
    Type mapKeyType = mapKeyType(type);
    if (mapKeyType != null) {
        Type mapValueType = mapValueType(type);
        return (T) toMap(dataTable, mapKeyType, mapValueType);
    }
    Type itemType = listItemType(type);
    if (itemType == null) {
        throw new CucumberException("Not a Map or List type: " + type);
    }
    Type listItemType = listItemType(itemType);
    if (listItemType != null) {
        return (T) toLists(dataTable, listItemType);
    } else {
        SingleValueConverter singleValueConverter = xStream.getSingleValueConverter(itemType);
        if (singleValueConverter != null) {
            return (T) toList(dataTable, singleValueConverter);
        } else {
            if (itemType instanceof Class) {
                if (Map.class.equals(itemType)) {
                    // Non-generic map
                    return (T) toMaps(dataTable, String.class, String.class);
                } else {
                    return (T) toListOfComplexType(dataTable, (Class) itemType);
                }
            } else {
                return (T) toMaps(dataTable, mapKeyType(itemType), mapValueType(itemType));
            }
        }
    }
}
Also used : DataTable(cucumber.api.DataTable) Utils.mapKeyType(cucumber.runtime.Utils.mapKeyType) Utils.listItemType(cucumber.runtime.Utils.listItemType) Utils.mapValueType(cucumber.runtime.Utils.mapValueType) Type(java.lang.reflect.Type) SingleValueConverter(cucumber.deps.com.thoughtworks.xstream.converters.SingleValueConverter) CucumberException(cucumber.runtime.CucumberException)

Example 4 with SingleValueConverter

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

the class TableConverter method toLists.

public <T> List<List<T>> toLists(DataTable dataTable, Type itemType) {
    try {
        xStream.setParameterInfo(parameterInfo);
        SingleValueConverter itemConverter = xStream.getSingleValueConverter(itemType);
        if (itemConverter == null) {
            throw new CucumberException(String.format("Can't convert DataTable to List<List<%s>>", itemType));
        }
        List<List<T>> result = new ArrayList<List<T>>();
        for (List<String> row : dataTable.raw()) {
            List<T> convertedRow = new ArrayList<T>();
            for (String cell : row) {
                convertedRow.add((T) itemConverter.fromString(cell));
            }
            result.add(Collections.unmodifiableList(convertedRow));
        }
        return Collections.unmodifiableList(result);
    } finally {
        xStream.unsetParameterInfo();
    }
}
Also used : ArrayList(java.util.ArrayList) 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)

Example 5 with SingleValueConverter

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

the class TableConverter method toMap.

public <K, V> Map<K, V> toMap(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 Map<%s,%s>", keyType, valueType));
        }
        Map<K, V> result = new LinkedHashMap<K, V>();
        for (List<String> row : dataTable.raw()) {
            if (row.size() != 2) {
                throw new CucumberException("A DataTable can only be converted to a Map when there are 2 columns");
            }
            K key = (K) keyConverter.fromString(row.get(0));
            V value = (V) valueConverter.fromString(row.get(1));
            result.put(key, value);
        }
        return Collections.unmodifiableMap(result);
    } finally {
        xStream.unsetParameterInfo();
    }
}
Also used : SingleValueConverter(cucumber.deps.com.thoughtworks.xstream.converters.SingleValueConverter) CucumberException(cucumber.runtime.CucumberException) LinkedHashMap(java.util.LinkedHashMap)

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