Skip to content

Commit

Permalink
Fix generating ArbitraryNode even if self-recursive type (#541)
Browse files Browse the repository at this point in the history
  • Loading branch information
seongahjo authored Jan 10, 2023
1 parent 17fa09f commit ef97dc1
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package com.navercorp.fixturemonkey.resolver;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -171,10 +172,6 @@ private List<ArbitraryNode> generateChildrenNodes(
for (int sequence = 0; sequence < childProperties.size(); sequence++) {
Property childProperty = childProperties.get(sequence);

if (context.isTraversed(childProperty)) {
continue;
}

ContainerPropertyGenerator containerPropertyGenerator =
this.generateOptions.getContainerPropertyGenerator(childProperty);
boolean childContainer = containerPropertyGenerator != null;
Expand Down Expand Up @@ -226,11 +223,21 @@ private List<ArbitraryNode> generateChildrenNodes(
childObjectProperty,
childContainerProperty
);
ArbitraryNode childNode = this.traverse(
childArbitraryProperty,
resolvedParentProperty,
context.appendArbitraryProperty(childArbitraryProperty)
);

ArbitraryNode childNode;
if (context.isTraversed(childProperty)) {
childNode = new ArbitraryNode(
resolvedParentProperty,
childArbitraryProperty,
Collections.emptyList()
);
} else {
childNode = this.traverse(
childArbitraryProperty,
resolvedParentProperty,
context.appendArbitraryProperty(childArbitraryProperty)
);
}
children.add(childNode);
}
return new ArrayList<>(children);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.apiguardian.api.API.Status;

import com.navercorp.fixturemonkey.api.generator.ArbitraryProperty;
import com.navercorp.fixturemonkey.api.property.MapEntryElementProperty;
import com.navercorp.fixturemonkey.api.property.Property;

@API(since = "0.4.0", status = Status.EXPERIMENTAL)
Expand Down Expand Up @@ -66,6 +67,11 @@ public TraverseContext appendArbitraryProperty(
public boolean isTraversed(Property property) {
return property.equals(rootArbitraryProperty.getObjectProperty().getProperty())
|| arbitraryProperties.stream()
.anyMatch(it -> property.equals(it.getObjectProperty().getProperty()));
.anyMatch(it -> isSameType(property, it.getObjectProperty().getProperty()));
}

private static boolean isSameType(Property p1, Property p2) {
boolean notMapEntry = !(p1 instanceof MapEntryElementProperty) || !(p2 instanceof MapEntryElementProperty);
return notMapEntry && p1.getAnnotatedType().getType().equals(p2.getAnnotatedType().getType());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -279,4 +279,25 @@ public static class ConcreteStringValue extends AbstractValue {
public static class ConcreteIntValue extends AbstractValue {
private int intValue;
}

@Getter
public abstract static class SelfRecursiveAbstractValue {
private final List<SelfRecursiveAbstractValue> recursives;

@ConstructorProperties("recursives")
public SelfRecursiveAbstractValue(List<SelfRecursiveAbstractValue> recursives) {
this.recursives = recursives;
}
}

@Getter
public static class SelfRecursiveImplementationValue extends SelfRecursiveAbstractValue {
private final String value;

@ConstructorProperties({"recursives", "value"})
public SelfRecursiveImplementationValue(List<SelfRecursiveAbstractValue> recursives, String value) {
super(recursives);
this.value = value;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@
import com.navercorp.fixturemonkey.test.FixtureMonkeyV04OptionsAdditionalTestSpecs.PairContainerPropertyGenerator;
import com.navercorp.fixturemonkey.test.FixtureMonkeyV04OptionsAdditionalTestSpecs.PairIntrospector;
import com.navercorp.fixturemonkey.test.FixtureMonkeyV04OptionsAdditionalTestSpecs.RegisterGroup;
import com.navercorp.fixturemonkey.test.FixtureMonkeyV04OptionsAdditionalTestSpecs.SelfRecursiveAbstractValue;
import com.navercorp.fixturemonkey.test.FixtureMonkeyV04OptionsAdditionalTestSpecs.SelfRecursiveImplementationValue;
import com.navercorp.fixturemonkey.test.FixtureMonkeyV04OptionsAdditionalTestSpecs.SimpleObjectChild;
import com.navercorp.fixturemonkey.test.FixtureMonkeyV04TestSpecs.ComplexObject;
import com.navercorp.fixturemonkey.test.FixtureMonkeyV04TestSpecs.CustomContainer;
Expand Down Expand Up @@ -1418,4 +1420,19 @@ void setConcreteList() {

then(actual).isEqualTo(expected);
}

@Property
void sampleSelfRecursiveAbstract() {
LabMonkey sut = LabMonkey.labMonkeyBuilder()
.interfaceImplements(
SelfRecursiveAbstractValue.class,
Collections.singletonList(SelfRecursiveImplementationValue.class)
)
.objectIntrospector(ConstructorPropertiesArbitraryIntrospector.INSTANCE)
.build();

SelfRecursiveAbstractValue actual = sut.giveMeOne(SelfRecursiveAbstractValue.class);

then(actual).isNotNull();
}
}

0 comments on commit ef97dc1

Please sign in to comment.