org.apache.commons.lang3.reflect.TypeLiteral Java Examples

The following examples show how to use org.apache.commons.lang3.reflect.TypeLiteral. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JsonComparisonOptionsConverter.java    From vividus with Apache License 2.0 6 votes vote down vote up
@Override
@SuppressWarnings({"rawtypes", "unchecked"})
public Options convertValue(String value, Type type)
{
    Type listOfOption = new TypeLiteral<List<Option>>() { }.getType();
    Set options = new HashSet<>(fluentEnumListConverter.convertValue(value, listOfOption));
    if (options.isEmpty())
    {
        return Options.empty();
    }
    Iterator<Option> iterator = options.iterator();
    Options jsonComparisonOptions = new Options(iterator.next());
    while (iterator.hasNext())
    {
        jsonComparisonOptions = jsonComparisonOptions.with(iterator.next());
    }
    return jsonComparisonOptions;
}
 
Example #2
Source File: FluentEnumListConverterTests.java    From vividus with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings("rawtypes")
void testConvertValue()
{
    FluentEnumListConverter fluentEnumListConverter = new FluentEnumListConverter(new FluentTrimmedEnumConverter());
    Type type = new TypeLiteral<List<Option>>() { }.getType();

    assertTrue(fluentEnumListConverter.accept(type));
    List list = fluentEnumListConverter.convertValue("ignoring extra fields, ignoring values", type);
    assertIterableEquals(Arrays.asList(Option.IGNORING_EXTRA_FIELDS, Option.IGNORING_VALUES), list);
}
 
Example #3
Source File: ParameterConvertersDecoratorTests.java    From vividus with Apache License 2.0 5 votes vote down vote up
@Test
void shouldConvertToEmptyList()
{
    String value = "  ";
    List<Integer> convertedValue = List.of();
    Type type = new TypeLiteral<List<Integer>>() { }.value;
    when(expressionAdaptor.process(value)).thenReturn(value);
    when(parameterAdaptor.convert(value)).thenReturn(value);
    List<Integer> actual = (List<Integer>) parameterConverters.convert(value, type);
    assertEquals(convertedValue, actual);
    verifyNoInteractions(stepMonitor);
}
 
Example #4
Source File: ParameterConvertersDecoratorTests.java    From vividus with Apache License 2.0 5 votes vote down vote up
@Test
void shouldConvertToEmptyOptional()
{
    String value = " ";
    Optional<Integer> convertedValue = Optional.empty();
    Type type = new TypeLiteral<Optional<Integer>>() { }.value;
    when(expressionAdaptor.process(value)).thenReturn(value);
    when(parameterAdaptor.convert(value)).thenReturn(value);
    Optional<Integer> actual = (Optional<Integer>) parameterConverters.convert(value, type);
    assertEquals(convertedValue, actual);
    verifyNoInteractions(stepMonitor);
}
 
Example #5
Source File: ParameterConvertersDecoratorTests.java    From vividus with Apache License 2.0 5 votes vote down vote up
@Test
void shouldConvertToNonEmptyOptional()
{
    Integer baseConvertedValue = Integer.valueOf(VALUE);
    Optional<Integer> convertedValue = Optional.of(baseConvertedValue);
    Type type = new TypeLiteral<Optional<Integer>>() { }.value;
    when(expressionAdaptor.process(VALUE)).thenReturn(VALUE);
    when(parameterAdaptor.convert(VALUE)).thenReturn(VALUE);
    Optional<Integer> actual = (Optional<Integer>) parameterConverters.convert(VALUE, type);
    assertEquals(convertedValue, actual);
    verify(stepMonitor).convertedValueOfType(VALUE, Integer.class, baseConvertedValue,
            new LinkedList<>(List.of(NumberConverter.class)));
}
 
Example #6
Source File: ParameterConvertersDecoratorTests.java    From vividus with Apache License 2.0 5 votes vote down vote up
@Test
void shouldReturnValueAsIsIfAdaptedValueTypeAssignableFromRequestedType()
{
    Type type = new TypeLiteral<List<Map<String, Object>>>() { }.value;
    List<Map<Object, Object>> adaptedValue = List.of(Map.of());
    when(parameterAdaptor.convert(VALUE)).thenReturn(adaptedValue);
    assertEquals(adaptedValue, parameterConverters.convert(VALUE, type));
    verifyNoInteractions(expressionAdaptor);
}
 
Example #7
Source File: SerializerRegistrarTest.java    From pmd-designer with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Test
public void testListSerializer() {
    roundTrip(new TypeLiteral<List<List<String>>>() { }, asList(asList("a", "b"), asList("c", "d")));
    roundTrip(new TypeLiteral<List<List<String>>>() { }, emptyList());
}
 
Example #8
Source File: SerializerRegistrarTest.java    From pmd-designer with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Test
public void testWildcardSerializer() {
    roundTrip(new TypeLiteral<List<? extends List<String>>>() { }, asList(emptyList(), asList("", "foo & <bar></bar>")));
    roundTrip(new TypeLiteral<List<? extends List<String>>>() { }, emptyList());
}
 
Example #9
Source File: SerializerRegistrarTest.java    From pmd-designer with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Test
public void testNullValue() {
    roundTrip(String.class, null);
    roundTrip(new TypeLiteral<List<? extends List<String>>>() { }, null);
    roundTrip(new TypeLiteral<List<? extends List<String>>>() { }, asList(null, asList(null, "")));
}
 
Example #10
Source File: SerializerRegistrarTest.java    From pmd-designer with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Test
public void testListSerializerRegistrarOverride() {

    testRegistrar.register(nullSerializer(), new TypeLiteral<List<List<String>>>() { });

    Serializer<List<List<String>>> serializer =
        testRegistrar.getSerializer(new TypeLiteral<List<List<String>>>() { });

    Element element = serializer.toXml(
        asList(asList("a", "b"), asList("c", "d")),
        dummyElementFactory()
    );

    assertNull(serializer.fromXml(element));

}