Example usage for org.springframework.core.convert.converter Converter convert

List of usage examples for org.springframework.core.convert.converter Converter convert

Introduction

In this page you can find the example usage for org.springframework.core.convert.converter Converter convert.

Prototype

@Nullable
T convert(S source);

Source Link

Document

Convert the source object of type S to target type T .

Usage

From source file:de.perdian.commons.lang.conversion.support.TestChainedConverter.java

@Test
public void testConvert() {

    Converter<String, String> converterA = Mockito.mock(Converter.class);
    Mockito.when(converterA.convert(Matchers.eq("C"))).thenReturn("B");
    Converter<String, String> converterB = Mockito.mock(Converter.class);
    Mockito.when(converterB.convert(Matchers.eq("B"))).thenReturn("A");

    Converter<String, String> chainedConverter = new ChainedConverter<>(converterA, converterB);
    Assert.assertEquals("A", chainedConverter.convert("C"));

}

From source file:de.perdian.commons.lang.conversion.support.TestDefaultWhenNullConverter.java

@Test
public void testConverterNotNull() {

    Converter<Integer, String> originalConverter = Mockito.mock(Converter.class);
    Mockito.when(originalConverter.convert(Matchers.any(Integer.class))).thenReturn("4711");
    DefaultWhenNullConverter<Integer, String> converter = new DefaultWhenNullConverter<>(originalConverter,
            "42");

    Assert.assertEquals("4711", converter.convert(Integer.valueOf(4711)));

}

From source file:de.perdian.commons.lang.conversion.support.TestDefaultWhenNullConverter.java

@Test
public void testConverterNull() {

    Converter<Integer, String> originalConverter = Mockito.mock(Converter.class);
    Mockito.when(originalConverter.convert(Matchers.any(Integer.class))).thenReturn(null);
    DefaultWhenNullConverter<Integer, String> converter = new DefaultWhenNullConverter<>(originalConverter,
            "42");

    Assert.assertEquals("42", converter.convert(Integer.valueOf(4711)));

}

From source file:de.perdian.commons.lang.conversion.support.TestSwallowExceptionConverter.java

@Test
public void testConvert() {

    Converter<String, String> converter = Mockito.mock(Converter.class);
    Mockito.when(converter.convert(Matchers.any(String.class)))
            .thenThrow(new ConverterException("X", "Message"));

    SwallowExceptionConverter<String, String> swallowExceptionConverter = new SwallowExceptionConverter<>(
            converter);//from w  w  w . jav a 2s.  co m
    Assert.assertNull(swallowExceptionConverter.convert("A"));
    Mockito.verify(converter).convert(Matchers.any(String.class));

}

From source file:de.perdian.commons.lang.conversion.support.TestSwallowExceptionConverter.java

@Test
public void testConvertWithDefaultValue() {

    Converter<String, String> converter = Mockito.mock(Converter.class);
    Mockito.when(converter.convert(Matchers.any(String.class)))
            .thenThrow(new ConverterException("X", "Message"));

    SwallowExceptionConverter<String, String> swallowExceptionConverter = new SwallowExceptionConverter<>(
            converter);//from w  w  w . j ava 2s  .  c  o m
    swallowExceptionConverter.setDefaultResult("defaultResult");
    Assert.assertEquals("defaultResult", swallowExceptionConverter.convert("A"));

    Mockito.verify(converter).convert(Matchers.any(String.class));

}

From source file:org.openmrs.module.kenyaemr.converter.StringToEnumConverterFactoryTest.java

/**
 * @see StringToEnumConverterFactory#getConverter(Class)
 *//*w  w w.j av  a  2  s  . co  m*/
@Test
public void getConverter() {
    Converter<String, ? extends Enum> converter = factory.getConverter(TestEnum.class);

    Assert.assertThat(converter.convert(null), nullValue());
    Assert.assertThat(converter.convert(""), nullValue());
    Assert.assertThat(converter.convert("ONE"), is((Object) TestEnum.ONE));
}

From source file:test.searchbox.domain.search.FieldValueConditionTest.java

@Test
public void testConverter() {

    Converter<String, FieldValueCondition> vv = new FieldValueCondition.FieldValueConditionConverter();
    FieldValueCondition vc = vv.convert(URL_PARAM);
    Assert.assertEquals("FieldName value", FIELD_NAME, vc.getFieldName());
    Assert.assertEquals("Value value", FIELD_VALUE, vc.getValue());
}

From source file:test.searchbox.domain.search.FieldValueConditionTest.java

@Test
public void testLoopConverter() {
    FieldFacet facet = new FieldFacet("test Facet", "athor");

    FieldFacet.Value value = facet.new Value("Stephane", "stephane", 3);

    FieldValueCondition orig = new FieldValueCondition("athor", "stephane", facet.getSticky());

    AbstractSearchCondition condition = value.getSearchCondition();

    Assert.assertEquals("Original and from Facet", orig, condition);

    // TODO Fix fails because of tag issue in Adapter...
    Converter<String, FieldValueCondition> vv = new FieldValueCondition.FieldValueConditionConverter();
    FieldValueCondition vc = vv.convert(value.getParamValue());
    LOGGER.info("ORIG:\t" + orig);
    LOGGER.info("URL: \t" + vc);
    Assert.assertEquals("Transformed and original", vc, orig);
}

From source file:com.stormpath.sample.impl.converters.DefaultMapValueRetriever.java

private <T> T tryStringToInstanceConversion(String propertyName, String propertyValue, Class<T> targetClass) {

    Converter<String, ?> converter = converterMap.get(targetClass);

    Assert.notNull(converter, "There is not converter for class: " + targetClass);

    try {/*from  w  ww  . ja  v a2s  .co  m*/
        String cleanValue = StringUtils.clean(propertyValue);
        return (T) converter.convert(cleanValue);
    } catch (RuntimeException re) {
        throw new ClientValidationException(Error.INVALID_VALUE, propertyName);
    }
}

From source file:com.github.obiteaaron.common.data.FixChunk.java

/**
 * Applies the given {@link Converter} to the content of the {@link FixChunk}.
 *
 * @param converter must not be {@literal null}.
 * @return/*from ww w .  j a  v a2s  .c  o  m*/
 */
protected <S> List<S> getConvertedContent(Converter<? super T, ? extends S> converter) {

    Assert.notNull(converter, "Converter must not be null!");

    List<S> result = new ArrayList<S>(content.size());

    for (T element : this) {
        result.add(converter.convert(element));
    }

    return result;
}