Skip to content

Commit

Permalink
Implement a Matcher interface to manage ArbitraryBuilders with a sing…
Browse files Browse the repository at this point in the history
…le variable (#1062)
  • Loading branch information
YongGoose authored Nov 8, 2024
1 parent b8641fc commit e508ba8
Show file tree
Hide file tree
Showing 11 changed files with 150 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
public interface Matcher {
boolean match(Property property);

default boolean match(Property property, MatcherMetadata matcherMetadata) {
return match(property);
}

/**
* Creates and returns a new {@code Matcher} that represents the intersection
* of this matcher with another specified matcher. The resulting matcher will
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Fixture Monkey
*
* Copyright (c) 2021-present NAVER Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.navercorp.fixturemonkey.api.matcher;

public interface MatcherMetadata {
String getName();
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ public static <T, C> MatcherOperator<T> assignableTypeMatchOperator(Class<C> typ
return new MatcherOperator<>(new AssignableTypeMatcher(type), operator);
}

@Override
public boolean match(Property property, MatcherMetadata matcherMetadata) {
return this.matcher.match(property, matcherMetadata);
}

@Override
public boolean match(Property property) {
return this.matcher.match(property);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Fixture Monkey
*
* Copyright (c) 2021-present NAVER Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.navercorp.fixturemonkey.api.matcher;

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

public final class NamedMatcher implements Matcher {
private final Matcher matcher;
private final String registeredName;

public NamedMatcher(Matcher matcher, String registeredName) {
this.matcher = matcher;
this.registeredName = registeredName;
}

@Override
public boolean match(Property property) {
return this.matcher.match(property);
}

@Override
public boolean match(Property property, MatcherMetadata matcherMetadata) {
return this.matcher.match(property) && registeredName.equals(matcherMetadata.getName());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Fixture Monkey
*
* Copyright (c) 2021-present NAVER Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.navercorp.fixturemonkey.api.matcher;

public final class NamedMatcherMetadata implements MatcherMetadata {
private final String name;

public NamedMatcherMetadata(String name) {
this.name = name;
}

@Override
public String getName() {
return name;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import static java.util.stream.Collectors.toList;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
Expand All @@ -34,6 +33,7 @@

import com.navercorp.fixturemonkey.api.context.MonkeyContext;
import com.navercorp.fixturemonkey.api.matcher.MatcherOperator;
import com.navercorp.fixturemonkey.api.matcher.NamedMatcher;
import com.navercorp.fixturemonkey.api.option.FixtureMonkeyOptions;
import com.navercorp.fixturemonkey.api.property.RootProperty;
import com.navercorp.fixturemonkey.api.type.LazyAnnotatedType;
Expand All @@ -55,8 +55,6 @@ public final class FixtureMonkey {
private final MonkeyContext monkeyContext;
private final List<MatcherOperator<? extends ArbitraryBuilder<?>>> registeredArbitraryBuilders = new ArrayList<>();
private final MonkeyManipulatorFactory monkeyManipulatorFactory;
private final Map<String, MatcherOperator<? extends ArbitraryBuilder<?>>>
namedArbitraryBuilderMap = new HashMap<>();

public FixtureMonkey(
FixtureMonkeyOptions fixtureMonkeyOptions,
Expand Down Expand Up @@ -116,7 +114,6 @@ public <T> ArbitraryBuilder<T> giveMeBuilder(TypeReference<T> type) {
monkeyManipulatorFactory,
builderContext.copy(),
registeredArbitraryBuilders,
namedArbitraryBuilderMap,
monkeyContext,
manipulatorOptimizer,
fixtureMonkeyOptions.getInstantiatorProcessor()
Expand Down Expand Up @@ -145,7 +142,6 @@ public <T> ArbitraryBuilder<T> giveMeBuilder(T value) {
monkeyManipulatorFactory,
context,
registeredArbitraryBuilders,
namedArbitraryBuilderMap,
monkeyContext,
manipulatorOptimizer,
fixtureMonkeyOptions.getInstantiatorProcessor()
Expand Down Expand Up @@ -208,9 +204,12 @@ private void initializeRegisteredArbitraryBuilders(
private void initializeNamedArbitraryBuilderMap(
Map<String, MatcherOperator<Function<FixtureMonkey, ? extends ArbitraryBuilder<?>>>> mapsByRegisteredName
) {
mapsByRegisteredName.forEach((name, matcherOperator) -> {
namedArbitraryBuilderMap.put(
name, new MatcherOperator<>(matcherOperator.getMatcher(), matcherOperator.getOperator().apply(this))
mapsByRegisteredName.forEach((registeredName, matcherOperator) -> {
registeredArbitraryBuilders.add(
new MatcherOperator<>(
new NamedMatcher(matcherOperator.getMatcher(), registeredName),
matcherOperator.getOperator().apply(this)
)
);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import com.navercorp.fixturemonkey.api.generator.ArbitraryContainerInfo;
import com.navercorp.fixturemonkey.api.lazy.LazyArbitrary;
import com.navercorp.fixturemonkey.api.matcher.MatcherOperator;
import com.navercorp.fixturemonkey.api.matcher.NamedMatcherMetadata;
import com.navercorp.fixturemonkey.api.property.Property;
import com.navercorp.fixturemonkey.customizer.InnerSpecState.ManipulatorHolderSet;
import com.navercorp.fixturemonkey.customizer.Values.Just;
Expand Down Expand Up @@ -142,7 +143,8 @@ public ContainerInfoManipulator newContainerInfoManipulator(

public List<ArbitraryManipulator> newRegisteredArbitraryManipulators(
List<MatcherOperator<? extends ArbitraryBuilder<?>>> registeredArbitraryBuilders,
Map<Property, List<ObjectNode>> nodesByType
Map<Property, List<ObjectNode>> nodesByType,
ArbitraryBuilderContext builderContext
) {
List<ArbitraryManipulator> manipulators = new ArrayList<>();

Expand All @@ -152,7 +154,14 @@ public List<ArbitraryManipulator> newRegisteredArbitraryManipulators(

DefaultArbitraryBuilder<?> registeredArbitraryBuilder =
(DefaultArbitraryBuilder<?>)registeredArbitraryBuilders.stream()
.filter(it -> it.match(property))
.filter(it -> {
if (builderContext.getSelectedNames().isEmpty()) {
return it.match(property);
}
return builderContext.getSelectedNames().stream().anyMatch(
name -> it.match(property, new NamedMatcherMetadata(name))
);
})
.findFirst()
.map(MatcherOperator::getOperator)
.filter(it -> it instanceof DefaultArbitraryBuilder<?>)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
public final class ArbitraryBuilderContext {
private final List<ArbitraryManipulator> manipulators;
private final List<ContainerInfoManipulator> containerInfoManipulators;
private final List<String> selectNames;
private final Map<Class<?>, List<Property>> propertyConfigurers;
private final Map<Class<?>, ArbitraryIntrospector> arbitraryIntrospectorsByType;
private boolean validOnly;
Expand All @@ -54,6 +55,7 @@ public final class ArbitraryBuilderContext {
public ArbitraryBuilderContext(
List<ArbitraryManipulator> manipulators,
List<ContainerInfoManipulator> containerInfoManipulators,
List<String> selectNames,
Map<Class<?>, List<Property>> propertyConfigurers,
Map<Class<?>, ArbitraryIntrospector> arbitraryIntrospectorsByType,
boolean validOnly,
Expand All @@ -62,6 +64,7 @@ public ArbitraryBuilderContext(
) {
this.manipulators = manipulators;
this.containerInfoManipulators = containerInfoManipulators;
this.selectNames = selectNames;
this.propertyConfigurers = propertyConfigurers;
this.arbitraryIntrospectorsByType = arbitraryIntrospectorsByType;
this.validOnly = validOnly;
Expand All @@ -71,7 +74,8 @@ public ArbitraryBuilderContext(

public ArbitraryBuilderContext() {
this(
new ArrayList<>(), new ArrayList<>(), new HashMap<>(), new HashMap<>(), true, null, null);
new ArrayList<>(), new ArrayList<>(), new ArrayList<>(), new HashMap<>(), new HashMap<>(), true, null, null
);
}

public ArbitraryBuilderContext copy() {
Expand All @@ -82,6 +86,7 @@ public ArbitraryBuilderContext copy() {
return new ArbitraryBuilderContext(
new ArrayList<>(this.manipulators),
copiedContainerInfoManipulators,
new ArrayList<>(this.selectNames),
new HashMap<>(propertyConfigurers),
new HashMap<>(arbitraryIntrospectorsByType),
this.validOnly,
Expand Down Expand Up @@ -114,6 +119,15 @@ public List<ContainerInfoManipulator> getContainerInfoManipulators() {
return Collections.unmodifiableList(containerInfoManipulators);
}

public void addSelectedNames(List<String> selectNames) {
this.selectNames.addAll(selectNames);
}

public List<String> getSelectedNames() {
return this.selectNames;
}


public void putPropertyConfigurer(Class<?> type, List<Property> propertyConfigurer) {
this.propertyConfigurers.put(type, propertyConfigurer);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ public CombinableArbitrary<?> resolve(
List<ArbitraryManipulator> registeredManipulators =
monkeyManipulatorFactory.newRegisteredArbitraryManipulators(
registeredArbitraryBuilders,
objectTree.getMetadata().getNodesByProperty()
objectTree.getMetadata().getNodesByProperty(),
builderContext
);

List<ArbitraryManipulator> joinedManipulators =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
import java.lang.reflect.AnnotatedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Consumer;
Expand Down Expand Up @@ -59,6 +59,7 @@
import com.navercorp.fixturemonkey.api.instantiator.InstantiatorProcessor;
import com.navercorp.fixturemonkey.api.lazy.LazyArbitrary;
import com.navercorp.fixturemonkey.api.matcher.MatcherOperator;
import com.navercorp.fixturemonkey.api.matcher.NamedMatcherMetadata;
import com.navercorp.fixturemonkey.api.option.FixtureMonkeyOptions;
import com.navercorp.fixturemonkey.api.property.PropertyNameResolver;
import com.navercorp.fixturemonkey.api.property.PropertySelector;
Expand Down Expand Up @@ -87,7 +88,6 @@ public final class DefaultArbitraryBuilder<T> implements ArbitraryBuilder<T>, Ex
private final ManipulatorOptimizer manipulatorOptimizer;
private final MonkeyContext monkeyContext;
private final InstantiatorProcessor instantiatorProcessor;
private final Map<String, MatcherOperator<? extends ArbitraryBuilder<?>>> namedArbitraryBuilderMap;

public DefaultArbitraryBuilder(
FixtureMonkeyOptions fixtureMonkeyOptions,
Expand All @@ -97,7 +97,6 @@ public DefaultArbitraryBuilder(
MonkeyManipulatorFactory monkeyManipulatorFactory,
ArbitraryBuilderContext context,
List<MatcherOperator<? extends ArbitraryBuilder<?>>> registeredArbitraryBuilders,
Map<String, MatcherOperator<? extends ArbitraryBuilder<?>>> registeredArbitraryBuildersByRegsiteredName,
MonkeyContext monkeyContext,
ManipulatorOptimizer manipulatorOptimizer,
InstantiatorProcessor instantiatorProcessor
Expand All @@ -109,7 +108,6 @@ public DefaultArbitraryBuilder(
this.context = context;
this.monkeyManipulatorFactory = monkeyManipulatorFactory;
this.registeredArbitraryBuilders = registeredArbitraryBuilders;
this.namedArbitraryBuilderMap = registeredArbitraryBuildersByRegsiteredName;
this.manipulatorOptimizer = manipulatorOptimizer;
this.monkeyContext = monkeyContext;
this.instantiatorProcessor = instantiatorProcessor;
Expand Down Expand Up @@ -185,28 +183,19 @@ public ArbitraryBuilder<T> setLazy(PropertySelector propertySelector, Supplier<?
);
}

@Override
public ArbitraryBuilder<T> selectName(String... names) {
List<MatcherOperator<? extends ArbitraryBuilder<?>>> registeredArbitraryBuildersCopy =
new ArrayList<>(this.registeredArbitraryBuilders);

for (String name : names) {
MatcherOperator<? extends ArbitraryBuilder<?>> namedArbitraryBuilder = namedArbitraryBuilderMap.get(name);

if (namedArbitraryBuilder == null) {
throw new IllegalArgumentException("Given name is not registered. name: " + name);
}
registeredArbitraryBuildersCopy.add(namedArbitraryBuilder);
}

ArbitraryBuilderContext builderContext = registeredArbitraryBuildersCopy.stream()
.filter(it -> it.match(rootProperty))
ArbitraryBuilderContext builderContext = registeredArbitraryBuilders.stream()
.filter(operator -> Arrays.stream(names)
.anyMatch(name -> operator.match(rootProperty, new NamedMatcherMetadata(name)))
)
.map(MatcherOperator::getOperator)
.findAny()
.map(DefaultArbitraryBuilder.class::cast)
.map(DefaultArbitraryBuilder::getContext)
.orElse(new ArbitraryBuilderContext());

builderContext.addSelectedNames(Arrays.asList(names));

return new DefaultArbitraryBuilder<>(
this.fixtureMonkeyOptions,
this.rootProperty,
Expand All @@ -216,13 +205,12 @@ public ArbitraryBuilder<T> selectName(String... names) {
this.monkeyManipulatorFactory,
this.fixtureMonkeyOptions,
this.monkeyContext,
registeredArbitraryBuildersCopy
registeredArbitraryBuilders
),
this.traverser,
this.monkeyManipulatorFactory,
builderContext.copy(),
registeredArbitraryBuildersCopy,
this.namedArbitraryBuilderMap,
registeredArbitraryBuilders,
this.monkeyContext,
this.manipulatorOptimizer,
this.fixtureMonkeyOptions.getInstantiatorProcessor()
Expand Down Expand Up @@ -560,7 +548,6 @@ public ArbitraryBuilder<T> copy() {
monkeyManipulatorFactory,
context.copy(),
registeredArbitraryBuilders,
namedArbitraryBuilderMap,
monkeyContext,
manipulatorOptimizer,
instantiatorProcessor
Expand Down Expand Up @@ -612,7 +599,6 @@ private <R> DefaultArbitraryBuilder<R> generateArbitraryBuilderLazily(LazyArbitr
monkeyManipulatorFactory,
context,
registeredArbitraryBuilders,
namedArbitraryBuilderMap,
monkeyContext,
manipulatorOptimizer,
instantiatorProcessor
Expand Down
Loading

0 comments on commit e508ba8

Please sign in to comment.