Skip to content

Commit

Permalink
Remove TraverseContext dependency on generation (#1116)
Browse files Browse the repository at this point in the history
  • Loading branch information
seongahjo authored Dec 9, 2024
1 parent 913ed4f commit c87f4f2
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@

import com.navercorp.fixturemonkey.api.container.ConcurrentLruCache;
import com.navercorp.fixturemonkey.api.generator.ArbitraryContainerInfoGenerator;
import com.navercorp.fixturemonkey.api.generator.ArbitraryGenerator;
import com.navercorp.fixturemonkey.api.generator.ContainerPropertyGenerator;
import com.navercorp.fixturemonkey.api.generator.NullInjectGenerator;
import com.navercorp.fixturemonkey.api.generator.ObjectPropertyGenerator;
Expand All @@ -43,10 +42,8 @@
import com.navercorp.fixturemonkey.api.property.LazyPropertyGenerator;
import com.navercorp.fixturemonkey.api.property.MapEntryElementProperty;
import com.navercorp.fixturemonkey.api.property.Property;
import com.navercorp.fixturemonkey.api.property.PropertyGenerator;
import com.navercorp.fixturemonkey.api.property.PropertyNameResolver;
import com.navercorp.fixturemonkey.api.tree.TraverseNodePredicate.PropertyTraverseNodePredicate;
import com.navercorp.fixturemonkey.api.type.Types;

@API(since = "1.0.4", status = Status.EXPERIMENTAL)
public final class TraverseContext {
Expand All @@ -71,49 +68,6 @@ public final class TraverseContext {
private final NullInjectGenerator defaultNullInjectGenerator;

public TraverseContext(
List<TreeProperty> treeProperties,
List<TreeNodeManipulator> treeManipulators,
List<MatcherOperator<List<TreeNodeManipulator>>> registeredTreeManipulators,
Map<Class<?>, List<Property>> propertyConfigurers,
boolean validOnly,
List<MatcherOperator<PropertyGenerator>> optionalPropertyGenerators,
ArbitraryGenerator defaultArbitraryGenerator,
PropertyGenerator defaultPropertyGenerator,
List<MatcherOperator<ObjectPropertyGenerator>> objectPropertyGenerators,
ObjectPropertyGenerator defaultObjectPropertyGenerator,
List<MatcherOperator<ContainerPropertyGenerator>> containerPropertyGenerators,
List<MatcherOperator<PropertyNameResolver>> propertyNameResolvers,
PropertyNameResolver defaultPropertyNameResolver,
List<MatcherOperator<CandidateConcretePropertyResolver>> candidateConcretePropertyResolvers,
List<MatcherOperator<ArbitraryContainerInfoGenerator>> arbitraryContainerInfoGenerators,
ArbitraryContainerInfoGenerator defaultArbitraryContainerInfoGenerator,
List<MatcherOperator<NullInjectGenerator>> nullInjectGenerators,
NullInjectGenerator defaultNullInjectGenerator
) {
this.treeProperties = treeProperties;
this.treeManipulators = treeManipulators;
this.registeredTreeManipulators = registeredTreeManipulators;
this.propertyConfigurers = propertyConfigurers;
this.validOnly = validOnly;
this.objectPropertyGenerators = objectPropertyGenerators;
this.resolvedPropertyGenerator = initializeResolvedPropertyGenerator(
propertyConfigurers,
optionalPropertyGenerators,
defaultArbitraryGenerator,
defaultPropertyGenerator
);
this.defaultObjectPropertyGenerator = defaultObjectPropertyGenerator;
this.containerPropertyGenerators = containerPropertyGenerators;
this.propertyNameResolvers = propertyNameResolvers;
this.defaultPropertyNameResolver = defaultPropertyNameResolver;
this.candidateConcretePropertyResolvers = candidateConcretePropertyResolvers;
this.arbitraryContainerInfoGenerators = arbitraryContainerInfoGenerators;
this.defaultArbitraryContainerInfoGenerator = defaultArbitraryContainerInfoGenerator;
this.nullInjectGenerators = nullInjectGenerators;
this.defaultNullInjectGenerator = defaultNullInjectGenerator;
}

private TraverseContext(
List<TreeProperty> treeProperties,
List<TreeNodeManipulator> treeManipulators,
List<MatcherOperator<List<TreeNodeManipulator>>> registeredTreeManipulators,
Expand Down Expand Up @@ -340,42 +294,6 @@ public List<Property> resolveCandidateProperties(Property property) {
);
}

private static LazyPropertyGenerator initializeResolvedPropertyGenerator(
Map<Class<?>, List<Property>> propertyConfigurers,
List<MatcherOperator<PropertyGenerator>> optionalPropertyGenerators,
ArbitraryGenerator defaultArbitraryGenerator,
PropertyGenerator defaultPropertyGenerator
) {
PropertyGenerator resolvedPropertyGenerator = property -> {
Class<?> type = Types.getActualType(property.getType());
List<Property> propertyConfigurer = propertyConfigurers.get(type);
if (propertyConfigurer != null) {
return propertyConfigurer;
}

PropertyGenerator propertyGenerator = optionalPropertyGenerators.stream()
.filter(it -> it.match(property))
.map(MatcherOperator::getOperator)
.findFirst()
.orElse(null);

if (propertyGenerator != null) {
return propertyGenerator.generateChildProperties(property);
}

PropertyGenerator defaultArbitraryGeneratorPropertyGenerator =
defaultArbitraryGenerator.getRequiredPropertyGenerator(property);

if (defaultArbitraryGeneratorPropertyGenerator != null) {
return defaultArbitraryGeneratorPropertyGenerator.generateChildProperties(property);
}

return defaultPropertyGenerator.generateChildProperties(property);
};

return new LazyPropertyGenerator(resolvedPropertyGenerator);
}

@Nullable
private static CandidateConcretePropertyResolver getCandidateConcretePropertyResolver(
Property property,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,16 @@
import com.navercorp.fixturemonkey.FixtureMonkey;
import com.navercorp.fixturemonkey.api.arbitrary.CombinableArbitrary;
import com.navercorp.fixturemonkey.api.context.MonkeyContext;
import com.navercorp.fixturemonkey.api.generator.ArbitraryGenerator;
import com.navercorp.fixturemonkey.api.introspector.ArbitraryIntrospector;
import com.navercorp.fixturemonkey.api.matcher.MatcherOperator;
import com.navercorp.fixturemonkey.api.option.FixtureMonkeyOptions;
import com.navercorp.fixturemonkey.api.property.LazyPropertyGenerator;
import com.navercorp.fixturemonkey.api.property.Property;
import com.navercorp.fixturemonkey.api.property.PropertyGenerator;
import com.navercorp.fixturemonkey.api.tree.TraverseContext;
import com.navercorp.fixturemonkey.api.tree.TreeNodeManipulator;
import com.navercorp.fixturemonkey.api.type.Types;
import com.navercorp.fixturemonkey.customizer.ArbitraryManipulator;
import com.navercorp.fixturemonkey.customizer.ContainerInfoManipulator;
import com.navercorp.fixturemonkey.tree.GenerateFixtureContext;
Expand Down Expand Up @@ -231,9 +235,12 @@ public TraverseContext newTraverseContext() {
registeredContainerInfoManipulators,
this.getPropertyConfigurers(),
this.isValidOnly(),
fixtureMonkeyOptions.getPropertyGenerators(),
fixtureMonkeyOptions.getDefaultArbitraryGenerator(),
fixtureMonkeyOptions.getDefaultPropertyGenerator(),
initializeResolvedPropertyGenerator(
this.getPropertyConfigurers(),
fixtureMonkeyOptions.getPropertyGenerators(),
fixtureMonkeyOptions.getDefaultArbitraryGenerator(),
fixtureMonkeyOptions.getDefaultPropertyGenerator()
),
fixtureMonkeyOptions.getObjectPropertyGenerators(),
fixtureMonkeyOptions.getDefaultObjectPropertyGenerator(),
fixtureMonkeyOptions.getContainerPropertyGenerators(),
Expand Down Expand Up @@ -272,4 +279,40 @@ public int getFixedContainerManipulatorSize() {
return fixedContainerManipulatorSize;
}
}

private static LazyPropertyGenerator initializeResolvedPropertyGenerator(
Map<Class<?>, List<Property>> propertyConfigurers,
List<MatcherOperator<PropertyGenerator>> optionalPropertyGenerators,
ArbitraryGenerator defaultArbitraryGenerator,
PropertyGenerator defaultPropertyGenerator
) {
PropertyGenerator resolvedPropertyGenerator = property -> {
Class<?> type = Types.getActualType(property.getType());
List<Property> propertyConfigurer = propertyConfigurers.get(type);
if (propertyConfigurer != null) {
return propertyConfigurer;
}

PropertyGenerator propertyGenerator = optionalPropertyGenerators.stream()
.filter(it -> it.match(property))
.map(MatcherOperator::getOperator)
.findFirst()
.orElse(null);

if (propertyGenerator != null) {
return propertyGenerator.generateChildProperties(property);
}

PropertyGenerator defaultArbitraryGeneratorPropertyGenerator =
defaultArbitraryGenerator.getRequiredPropertyGenerator(property);

if (defaultArbitraryGeneratorPropertyGenerator != null) {
return defaultArbitraryGeneratorPropertyGenerator.generateChildProperties(property);
}

return defaultPropertyGenerator.generateChildProperties(property);
};

return new LazyPropertyGenerator(resolvedPropertyGenerator);
}
}

0 comments on commit c87f4f2

Please sign in to comment.