Skip to content
Closed
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 @@ -18,9 +18,11 @@

package com.navercorp.fixturemonkey.assembly;

import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Deque;
import java.util.HashMap;
Expand All @@ -30,6 +32,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedSet;
import java.util.function.BiFunction;
import java.util.function.Predicate;

Expand Down Expand Up @@ -59,6 +62,8 @@
import com.navercorp.fixturemonkey.api.property.PropertyNameResolver;
import com.navercorp.fixturemonkey.api.property.PropertyPath;
import com.navercorp.fixturemonkey.api.property.RootProperty;
import com.navercorp.fixturemonkey.api.type.Reflections;
import com.navercorp.fixturemonkey.api.type.TypeCache;
import com.navercorp.fixturemonkey.api.type.Types;
import com.navercorp.fixturemonkey.customizer.DirectivePrecedence;
import com.navercorp.fixturemonkey.customizer.ScopeChain;
Expand Down Expand Up @@ -259,7 +264,10 @@ private CombinableArbitrary<?> assembleNode(
return assembleNodeDefault(node, state, parentContext, parentPath, currentPath, visitedTypes);
}
return traceAndReturnValue(
setValue,
copyCollectionForSample(
setValue,
collectionCopyConstructor(setValue, currentRawType, node.getDeclaredType().getRawType())
),
bestCandidate.sourceLabel(),
currentPath,
currentRawType,
Expand Down Expand Up @@ -313,8 +321,13 @@ private CombinableArbitrary<?> assembleNode(
);
applyDecomposeResult(decomposeResult, state);
if (decomposeResult.hasEarlyReturn()) {
Object earlyValue = decomposeResult.getEarlyReturnValue();
Constructor<?> copyConstructor = collectionCopyConstructor(
earlyValue, currentRawType, node.getDeclaredType().getRawType()
);

return wrapValueWithFiltersAndCustomizers(
decomposeResult.getEarlyReturnValue(),
copyCollectionForSample(earlyValue, copyConstructor),
currentPath,
currentRawType,
state
Expand Down Expand Up @@ -1794,6 +1807,57 @@ private CombinableArbitrary<?> applyCustomizers(
return result;
}

private static @Nullable Object copyCollectionForSample(
@Nullable Object value,
@Nullable Constructor<?> constructor
) {
if (!(value instanceof Collection) || constructor == null) {
return value;
}
Collection<?> collection = (Collection<?>)value;
return CombinableArbitrary.from(() -> Reflections.newInstance(constructor, collection));
}

private static @Nullable Constructor<?> collectionCopyConstructor(
@Nullable Object value,
Class<?> resolvedType,
Class<?> declaredType
) {
if (!(value instanceof Collection)
|| !declaredType.isAssignableFrom(resolvedType)
|| !Collection.class.isAssignableFrom(resolvedType)) {
return null;
}
// Prefer the supplied implementation when it satisfies the resolved type.
Class<?> copyType = resolvedType.isInstance(value) ? value.getClass() : resolvedType;
Constructor<?> constructor = findCollectionCopyConstructor(copyType, value instanceof SortedSet);
return constructor != null || copyType == resolvedType
? constructor
: findCollectionCopyConstructor(resolvedType, value instanceof SortedSet);
}

private static @Nullable Constructor<?> findCollectionCopyConstructor(Class<?> type, boolean sorted) {
if (!Modifier.isPublic(type.getModifiers())) {
return null;
}
Constructor<?> collectionConstructor = null;
for (Constructor<?> constructor : TypeCache.getDeclaredConstructors(type)) {
Class<?>[] parameters = constructor.getParameterTypes();
if (!Modifier.isPublic(constructor.getModifiers()) || parameters.length != 1) {
continue;
}
// TreeSet(Collection) would discard a supplied comparator.
if (sorted && parameters[0] == SortedSet.class) {
return constructor;
}
if (parameters[0] == Collection.class) {
collectionConstructor = constructor;
}
}
// Types without a public copy constructor keep their existing generation path.
return collectionConstructor;
}

private static void applyDecomposeResult(DecomposeResult result, AssemblyState state) {
state.candidates.removeCandidatesUnder(result.getSubtreesToRemove());
state.candidates.putDecomposedCandidates(result.getValuesToPut());
Expand Down
Loading
Loading