From 001f0269c8f3428ad5e64b994a1cf83c1b52afaa Mon Sep 17 00:00:00 2001 From: yongjunhong Date: Wed, 16 Oct 2024 09:47:34 +0900 Subject: [PATCH] Enhancing MatcherOperator for improved ArbitraryBuilder management --- .../api/matcher/MatcherOperator.java | 2 +- .../api/matcher/NamedMatcherOperator.java | 49 +++++++++++++++++++ .../matcher/NamedMatcherOperatorAdapter.java | 26 ++++++++++ .../fixturemonkey/FixtureMonkey.java | 21 +++++--- .../resolver/DefaultArbitraryBuilder.java | 30 +++++------- 5 files changed, 103 insertions(+), 25 deletions(-) create mode 100644 fixture-monkey-api/src/main/java/com/navercorp/fixturemonkey/api/matcher/NamedMatcherOperator.java create mode 100644 fixture-monkey-api/src/main/java/com/navercorp/fixturemonkey/api/matcher/NamedMatcherOperatorAdapter.java diff --git a/fixture-monkey-api/src/main/java/com/navercorp/fixturemonkey/api/matcher/MatcherOperator.java b/fixture-monkey-api/src/main/java/com/navercorp/fixturemonkey/api/matcher/MatcherOperator.java index 60cc38e6b..a29f63a7c 100644 --- a/fixture-monkey-api/src/main/java/com/navercorp/fixturemonkey/api/matcher/MatcherOperator.java +++ b/fixture-monkey-api/src/main/java/com/navercorp/fixturemonkey/api/matcher/MatcherOperator.java @@ -24,7 +24,7 @@ import com.navercorp.fixturemonkey.api.property.Property; @API(since = "0.4.0", status = Status.MAINTAINED) -public final class MatcherOperator implements Matcher { +public class MatcherOperator implements Matcher { private final Matcher matcher; private final T operator; diff --git a/fixture-monkey-api/src/main/java/com/navercorp/fixturemonkey/api/matcher/NamedMatcherOperator.java b/fixture-monkey-api/src/main/java/com/navercorp/fixturemonkey/api/matcher/NamedMatcherOperator.java new file mode 100644 index 000000000..ab2bfd507 --- /dev/null +++ b/fixture-monkey-api/src/main/java/com/navercorp/fixturemonkey/api/matcher/NamedMatcherOperator.java @@ -0,0 +1,49 @@ +/* + * 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 NamedMatcherOperator extends MatcherOperator { + private final String registeredName; + private boolean active; + + public NamedMatcherOperator(Matcher matcher, T operator, String name) { + super(matcher, operator); + this.registeredName = name; + this.active = false; + } + + public String getRegisteredName() { + return registeredName; + } + + public void activate() { + this.active = true; + } + + public void deactivate() { + this.active = false; + } + + @Override + public boolean match(Property property) { + return active && super.match(property); + } +} diff --git a/fixture-monkey-api/src/main/java/com/navercorp/fixturemonkey/api/matcher/NamedMatcherOperatorAdapter.java b/fixture-monkey-api/src/main/java/com/navercorp/fixturemonkey/api/matcher/NamedMatcherOperatorAdapter.java new file mode 100644 index 000000000..67f58d017 --- /dev/null +++ b/fixture-monkey-api/src/main/java/com/navercorp/fixturemonkey/api/matcher/NamedMatcherOperatorAdapter.java @@ -0,0 +1,26 @@ +/* + * 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 NamedMatcherOperatorAdapter { + public static NamedMatcherOperator adapt(Matcher matcher, T operator, String name) { + return new NamedMatcherOperator<>(matcher, operator, name); + } +} diff --git a/fixture-monkey/src/main/java/com/navercorp/fixturemonkey/FixtureMonkey.java b/fixture-monkey/src/main/java/com/navercorp/fixturemonkey/FixtureMonkey.java index 0cb031b6d..58a7ea994 100644 --- a/fixture-monkey/src/main/java/com/navercorp/fixturemonkey/FixtureMonkey.java +++ b/fixture-monkey/src/main/java/com/navercorp/fixturemonkey/FixtureMonkey.java @@ -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; @@ -34,6 +33,8 @@ import com.navercorp.fixturemonkey.api.context.MonkeyContext; import com.navercorp.fixturemonkey.api.matcher.MatcherOperator; +import com.navercorp.fixturemonkey.api.matcher.NamedMatcherOperatorAdapter; +import com.navercorp.fixturemonkey.api.matcher.NamedMatcherOperator; import com.navercorp.fixturemonkey.api.option.FixtureMonkeyOptions; import com.navercorp.fixturemonkey.api.property.RootProperty; import com.navercorp.fixturemonkey.api.type.LazyAnnotatedType; @@ -55,8 +56,6 @@ public final class FixtureMonkey { private final MonkeyContext monkeyContext; private final List>> registeredArbitraryBuilders = new ArrayList<>(); private final MonkeyManipulatorFactory monkeyManipulatorFactory; - private final Map>> - namedArbitraryBuilderMap = new HashMap<>(); public FixtureMonkey( FixtureMonkeyOptions fixtureMonkeyOptions, @@ -93,6 +92,12 @@ public ArbitraryBuilder giveMeBuilder(Class type) { public ArbitraryBuilder giveMeBuilder(TypeReference type) { RootProperty rootProperty = new RootProperty(type.getAnnotatedType()); + for (MatcherOperator> operator : registeredArbitraryBuilders) { + if (operator instanceof NamedMatcherOperator) { + ((NamedMatcherOperator)operator).deactivate(); + } + } + ArbitraryBuilderContext builderContext = registeredArbitraryBuilders.stream() .filter(it -> it.match(rootProperty)) .map(MatcherOperator::getOperator) @@ -116,7 +121,6 @@ public ArbitraryBuilder giveMeBuilder(TypeReference type) { monkeyManipulatorFactory, builderContext.copy(), registeredArbitraryBuilders, - namedArbitraryBuilderMap, monkeyContext, manipulatorOptimizer, fixtureMonkeyOptions.getInstantiatorProcessor() @@ -145,7 +149,6 @@ public ArbitraryBuilder giveMeBuilder(T value) { monkeyManipulatorFactory, context, registeredArbitraryBuilders, - namedArbitraryBuilderMap, monkeyContext, manipulatorOptimizer, fixtureMonkeyOptions.getInstantiatorProcessor() @@ -209,8 +212,12 @@ private void initializeNamedArbitraryBuilderMap( Map>>> mapsByRegisteredName ) { mapsByRegisteredName.forEach((name, matcherOperator) -> { - namedArbitraryBuilderMap.put( - name, new MatcherOperator<>(matcherOperator.getMatcher(), matcherOperator.getOperator().apply(this)) + registeredArbitraryBuilders.add( + NamedMatcherOperatorAdapter.adapt( + matcherOperator.getMatcher(), + matcherOperator.getOperator().apply(this), + name + ) ); }); } diff --git a/fixture-monkey/src/main/java/com/navercorp/fixturemonkey/resolver/DefaultArbitraryBuilder.java b/fixture-monkey/src/main/java/com/navercorp/fixturemonkey/resolver/DefaultArbitraryBuilder.java index 2d1553059..bf7816591 100644 --- a/fixture-monkey/src/main/java/com/navercorp/fixturemonkey/resolver/DefaultArbitraryBuilder.java +++ b/fixture-monkey/src/main/java/com/navercorp/fixturemonkey/resolver/DefaultArbitraryBuilder.java @@ -28,7 +28,6 @@ import java.lang.reflect.Type; import java.util.ArrayList; import java.util.List; -import java.util.Map; import java.util.function.BiConsumer; import java.util.function.BiFunction; import java.util.function.Consumer; @@ -59,6 +58,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.NamedMatcherOperator; import com.navercorp.fixturemonkey.api.option.FixtureMonkeyOptions; import com.navercorp.fixturemonkey.api.property.PropertyNameResolver; import com.navercorp.fixturemonkey.api.property.PropertySelector; @@ -87,7 +87,6 @@ public final class DefaultArbitraryBuilder implements ArbitraryBuilder, Ex private final ManipulatorOptimizer manipulatorOptimizer; private final MonkeyContext monkeyContext; private final InstantiatorProcessor instantiatorProcessor; - private final Map>> namedArbitraryBuilderMap; public DefaultArbitraryBuilder( FixtureMonkeyOptions fixtureMonkeyOptions, @@ -97,7 +96,6 @@ public DefaultArbitraryBuilder( MonkeyManipulatorFactory monkeyManipulatorFactory, ArbitraryBuilderContext context, List>> registeredArbitraryBuilders, - Map>> registeredArbitraryBuildersByRegsiteredName, MonkeyContext monkeyContext, ManipulatorOptimizer manipulatorOptimizer, InstantiatorProcessor instantiatorProcessor @@ -109,7 +107,6 @@ public DefaultArbitraryBuilder( this.context = context; this.monkeyManipulatorFactory = monkeyManipulatorFactory; this.registeredArbitraryBuilders = registeredArbitraryBuilders; - this.namedArbitraryBuilderMap = registeredArbitraryBuildersByRegsiteredName; this.manipulatorOptimizer = manipulatorOptimizer; this.monkeyContext = monkeyContext; this.instantiatorProcessor = instantiatorProcessor; @@ -187,19 +184,21 @@ public ArbitraryBuilder setLazy(PropertySelector propertySelector, Supplier selectName(String... names) { - List>> registeredArbitraryBuildersCopy = - new ArrayList<>(this.registeredArbitraryBuilders); - for (String name : names) { - MatcherOperator> namedArbitraryBuilder = namedArbitraryBuilderMap.get(name); - - if (namedArbitraryBuilder == null) { + boolean found = registeredArbitraryBuilders.stream() + .filter(operator -> operator instanceof NamedMatcherOperator) + .map(operator -> (NamedMatcherOperator) operator) + .filter(operator -> operator.getRegisteredName().equals(name)) + .peek(NamedMatcherOperator::activate) + .findFirst() + .isPresent(); + + if (!found) { throw new IllegalArgumentException("Given name is not registered. name: " + name); } - registeredArbitraryBuildersCopy.add(namedArbitraryBuilder); } - ArbitraryBuilderContext builderContext = registeredArbitraryBuildersCopy.stream() + ArbitraryBuilderContext builderContext = registeredArbitraryBuilders.stream() .filter(it -> it.match(rootProperty)) .map(MatcherOperator::getOperator) .findAny() @@ -216,13 +215,12 @@ public ArbitraryBuilder 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() @@ -560,7 +558,6 @@ public ArbitraryBuilder copy() { monkeyManipulatorFactory, context.copy(), registeredArbitraryBuilders, - namedArbitraryBuilderMap, monkeyContext, manipulatorOptimizer, instantiatorProcessor @@ -612,7 +609,6 @@ private DefaultArbitraryBuilder generateArbitraryBuilderLazily(LazyArbitr monkeyManipulatorFactory, context, registeredArbitraryBuilders, - namedArbitraryBuilderMap, monkeyContext, manipulatorOptimizer, instantiatorProcessor