Skip to content

Commit

Permalink
Fix Map.Entry handling of parameterized tests
Browse files Browse the repository at this point in the history
  • Loading branch information
geoand committed Apr 20, 2022
1 parent 844caa3 commit 36f139d
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Stream;

Expand Down Expand Up @@ -119,6 +120,15 @@ static List<Arguments> testDataListArguments() {
return Arrays.asList(Arguments.of(new TestData(), "foo"), Arguments.of(new TestData(), "foo"));
}

@ParameterizedTest
@MethodSource("testStreamOfMapEntryArguments")
public void methodList(Map.Entry<String, String> ignore) {
}

static Stream<Map.Entry<String, String>> testStreamOfMapEntryArguments() {
return Map.of("a", "b").entrySet().stream();
}

@SuppressWarnings("unused")
static class TestData {
final List<String> foo = Arrays.asList("one", "two", "three");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package io.quarkus.test.junit.internal;

import java.util.AbstractMap;
import java.util.Map;
import java.util.Set;

import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.converters.collections.MapConverter;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.mapper.Mapper;

/**
* A custom Map.Entry converter that always uses AbstractMap.SimpleEntry for unmarshalling.
* This is probably not semantically correct 100% of the time, but it's likely fine
* for all the cases where we are using marshalling / unmarshalling.
*
* The reason for doing this is to avoid XStream causing illegal access issues
* for internal JDK types
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public class CustomMapEntryConverter extends MapConverter {

private final Set<String> SUPPORTED_CLASS_NAMES = Set
.of(Map.entry(Integer.MAX_VALUE, Integer.MAX_VALUE).getClass().getName());

public CustomMapEntryConverter(Mapper mapper) {
super(mapper);
}

@Override
public boolean canConvert(Class type) {
return (type != null) && SUPPORTED_CLASS_NAMES.contains(type.getName());
}

@Override
public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
var entryName = mapper().serializedClass(Map.Entry.class);
var entry = (Map.Entry) source;
writer.startNode(entryName);
writeCompleteItem(entry.getKey(), context, writer);
writeCompleteItem(entry.getValue(), context, writer);
writer.endNode();
}

@Override
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
reader.moveDown();
var key = readCompleteItem(reader, context, null);
var value = readCompleteItem(reader, context, null);
reader.moveUp();
return new AbstractMap.SimpleEntry(key, value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class XStreamDeepClone implements DeepClone {
result.registerConverter(new CustomListConverter(result.getMapper()));
result.registerConverter(new CustomSetConverter(result.getMapper()));
result.registerConverter(new CustomMapConverter(result.getMapper()));
result.registerConverter(new CustomMapEntryConverter(result.getMapper()));
return result;
};
}
Expand Down

0 comments on commit 36f139d

Please sign in to comment.