Skip to content

Commit

Permalink
Refactor moving ObjectNode mutable properties to ArbitraryProperty (#932
Browse files Browse the repository at this point in the history
)
  • Loading branch information
seongahjo authored Feb 26, 2024
1 parent 9219065 commit aad1490
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 15 deletions.
2 changes: 2 additions & 0 deletions docs/content/v1.0.x/release-notes/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ sectionStart
### v.1.0.14
Add supporting for sealed class and sealed interface.

Deprecate `nullInject` and `childPropertyListsByCandidateProperty` properties in `ObjectNode`. They would be moved to `ArbitraryProperty`.

sectionEnd

sectionStart
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,40 @@

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;

import org.apiguardian.api.API;
import org.apiguardian.api.API.Status;

import com.navercorp.fixturemonkey.api.matcher.Matcher;
import com.navercorp.fixturemonkey.api.property.Property;

@API(since = "0.4.0", status = Status.MAINTAINED)
public final class ArbitraryProperty {
private final ObjectProperty objectProperty;
private final boolean container;
private final double nullInject;
private final Map<Property, List<Property>> childPropertyListsByCandidateProperty;

@Deprecated
public ArbitraryProperty(ObjectProperty objectProperty, boolean container) {
this.objectProperty = objectProperty;
this.container = container;
this.nullInject = objectProperty.getNullInject();
this.childPropertyListsByCandidateProperty = objectProperty.getChildPropertyListsByCandidateProperty();
}

public ArbitraryProperty(
ObjectProperty objectProperty,
boolean container,
double nullInject,
Map<Property, List<Property>> childPropertyListsByCandidateProperty
) {
this.objectProperty = objectProperty;
this.container = container;
this.nullInject = nullInject;
this.childPropertyListsByCandidateProperty = childPropertyListsByCandidateProperty;
}

public ObjectProperty getObjectProperty() {
Expand All @@ -49,13 +68,25 @@ public ArbitraryProperty withNullInject(double nullInject) {
return new ArbitraryProperty(this.objectProperty.withNullInject(nullInject), this.container);
}

public ArbitraryProperty withChildPropertyListsByCandidateProperty(
Map<Property, List<Property>> childPropertyListsByCandidateProperty
) {
return new ArbitraryProperty(
this.objectProperty.withChildPropertyListsByCandidateProperty(childPropertyListsByCandidateProperty),
this.container
);
public double getNullInject() {
return nullInject;
}

public Map<Property, List<Property>> getChildPropertyListsByCandidateProperty() {
return childPropertyListsByCandidateProperty;
}

public Map.Entry<Property, List<Property>> getChildPropertiesByResolvedProperty(Matcher matcher) {
for (
Entry<Property, List<Property>> childPropertyListByPossibleProperty :
childPropertyListsByCandidateProperty.entrySet()
) {
Property property = childPropertyListByPossibleProperty.getKey();
if (matcher.match(property)) {
return childPropertyListByPossibleProperty;
}
}
throw new IllegalArgumentException("No resolved property is found.");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public CombinableArbitrary<?> generate(ArbitraryGeneratorContext context) {

ArbitraryIntrospectorResult result = this.arbitraryIntrospector.introspect(context);
if (result != ArbitraryIntrospectorResult.NOT_INTROSPECTED && result.getValue() != null) {
double nullInject = context.getArbitraryProperty().getObjectProperty().getNullInject();
double nullInject = context.getArbitraryProperty().getNullInject();
return new TraceableCombinableArbitrary<>(
result.getValue()
.injectNull(nullInject),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@ public final class ObjectProperty {

private final PropertyNameResolver propertyNameResolver;

@Deprecated
private final double nullInject;

@Nullable
private final Integer elementIndex;

@Deprecated
private final Map<Property, List<Property>> childPropertyListsByCandidateProperty;

public ObjectProperty(
Expand Down Expand Up @@ -72,6 +74,7 @@ public String getResolvedPropertyName() {
return this.getPropertyNameResolver().resolve(this.property);
}

@Deprecated
public double getNullInject() {
return this.nullInject;
}
Expand All @@ -81,6 +84,7 @@ public Integer getElementIndex() {
return this.elementIndex;
}

@Deprecated
public Map.Entry<Property, List<Property>> getChildPropertiesByResolvedProperty(Matcher matcher) {
for (
Entry<Property, List<Property>> childPropertyListByPossibleProperty :
Expand All @@ -94,6 +98,7 @@ public Map.Entry<Property, List<Property>> getChildPropertiesByResolvedProperty(
throw new IllegalArgumentException("No resolved property is found.");
}

@Deprecated
public Map<Property, List<Property>> getChildPropertyListsByCandidateProperty() {
return childPropertyListsByCandidateProperty;
}
Expand All @@ -102,6 +107,7 @@ public boolean isRoot() {
return this.property instanceof RootProperty;
}

@Deprecated
public ObjectProperty withNullInject(double nullInject) {
return new ObjectProperty(
this.property,
Expand All @@ -112,6 +118,7 @@ public ObjectProperty withNullInject(double nullInject) {
);
}

@Deprecated
public ObjectProperty withChildPropertyListsByCandidateProperty(
Map<Property, List<Property>> childPropertyListsByCandidateProperty
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,13 @@ public final class ObjectPropertyGeneratorContext {
@Nullable
private final ArbitraryProperty ownerProperty;
private final boolean container;
@Deprecated
private final PropertyGenerator propertyGenerator;
private final PropertyNameResolver propertyNameResolver;
@Deprecated
private final NullInjectGenerator nullInjectGenerator;

@Deprecated
public ObjectPropertyGeneratorContext(
Property property,
@Nullable Integer elementIndex,
Expand Down Expand Up @@ -76,6 +79,7 @@ public boolean isContainer() {
return this.container;
}

@Deprecated
public PropertyGenerator getPropertyGenerator() {
return propertyGenerator;
}
Expand All @@ -84,6 +88,7 @@ public PropertyNameResolver getPropertyNameResolver() {
return propertyNameResolver;
}

@Deprecated
public NullInjectGenerator getNullInjectGenerator() {
return nullInjectGenerator;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public ArbitraryIntrospectorResult introspect(ArbitraryGeneratorContext context)
}
Class<?> type = Types.getActualType(property.getObjectProperty().getProperty().getType());
ArbitraryProperty valueProperty = children.get(0);
double presenceProbability = 1 - valueProperty.getObjectProperty().getNullInject();
double presenceProbability = 1 - valueProperty.getNullInject();
if (children.size() == 1) {
presenceProbability = 1.0d;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@ private void setValue(ObjectNode objectNode, @Nullable Object value) {
}

Entry<Property, List<Property>> childPropertiesByResolvedProperty = objectNode.getArbitraryProperty()
.getObjectProperty()
.getChildPropertiesByResolvedProperty(
property -> isAssignable(value.getClass(), Types.getActualType(property.getType()))
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,12 @@ public ObjectNode traverse(
);
}

ArbitraryProperty arbitraryProperty = new ArbitraryProperty(objectProperty, container);
ArbitraryProperty arbitraryProperty = new ArbitraryProperty(
objectProperty,
container,
objectProperty.getNullInject(),
objectProperty.getChildPropertyListsByCandidateProperty()
);

List<ArbitraryProperty> parentArbitraryProperties = new ArrayList<>();
parentArbitraryProperties.add(arbitraryProperty);
Expand Down Expand Up @@ -182,11 +187,10 @@ private ObjectNode generateObjectNode(
@Nullable Property resolvedParentProperty,
TraverseContext context
) {
ObjectProperty objectProperty = arbitraryProperty.getObjectProperty();
List<ObjectNode> children = new ArrayList<>();

Map<Property, List<Property>> childPropertyListsByCandidateProperty =
objectProperty.getChildPropertyListsByCandidateProperty();
arbitraryProperty.getChildPropertyListsByCandidateProperty();

for (
Entry<Property, List<Property>> childPropertiesByCandidateProperty :
Expand Down Expand Up @@ -315,7 +319,9 @@ private List<ObjectNode> generateChildrenNodes(

ArbitraryProperty childArbitraryProperty = new ArbitraryProperty(
childObjectProperty,
childContainerProperty != null
childContainer,
childObjectProperty.getNullInject(),
childObjectProperty.getChildPropertyListsByCandidateProperty()
);

ObjectNode childNode = this.traverse(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ private CombinableArbitrary<?> generateIntrospected(
CombinableArbitrary<?> generated;
if (node.getArbitrary() != null) {
generated = node.getArbitrary()
.injectNull(node.getArbitraryProperty().getObjectProperty().getNullInject());
.injectNull(node.getArbitraryProperty().getNullInject());
} else {
CombinableArbitrary<?> cached = monkeyContext.getCachedArbitrary(node.getProperty());

Expand Down

0 comments on commit aad1490

Please sign in to comment.