Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Map.Entry handling of parameterized tests #25038

Merged
merged 1 commit into from
Apr 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
gastaldi marked this conversation as resolved.
Show resolved Hide resolved

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

@Override
public boolean canConvert(Class type) {
return (type != null) && SUPPORTED_CLASS_NAMES.contains(type.getName());
gastaldi marked this conversation as resolved.
Show resolved Hide resolved
}

@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