Skip to content

Commit

Permalink
Enhancing MatcherOperator for improved ArbitraryBuilder management
Browse files Browse the repository at this point in the history
  • Loading branch information
YongGoose committed Oct 16, 2024
1 parent b8641fc commit 001f026
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import com.navercorp.fixturemonkey.api.property.Property;

@API(since = "0.4.0", status = Status.MAINTAINED)
public final class MatcherOperator<T> implements Matcher {
public class MatcherOperator<T> implements Matcher {
private final Matcher matcher;
private final T operator;

Expand Down
Original file line number Diff line number Diff line change
@@ -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<T> extends MatcherOperator<T> {
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);
}
}
Original file line number Diff line number Diff line change
@@ -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 <T> NamedMatcherOperator<T> adapt(Matcher matcher, T operator, String name) {
return new NamedMatcherOperator<>(matcher, operator, 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,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;
Expand All @@ -55,8 +56,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 @@ -93,6 +92,12 @@ public <T> ArbitraryBuilder<T> giveMeBuilder(Class<T> type) {
public <T> ArbitraryBuilder<T> giveMeBuilder(TypeReference<T> type) {
RootProperty rootProperty = new RootProperty(type.getAnnotatedType());

for (MatcherOperator<? extends ArbitraryBuilder<?>> operator : registeredArbitraryBuilders) {
if (operator instanceof NamedMatcherOperator) {
((NamedMatcherOperator<?>)operator).deactivate();
}
}

ArbitraryBuilderContext builderContext = registeredArbitraryBuilders.stream()
.filter(it -> it.match(rootProperty))
.map(MatcherOperator::getOperator)
Expand All @@ -116,7 +121,6 @@ public <T> ArbitraryBuilder<T> giveMeBuilder(TypeReference<T> type) {
monkeyManipulatorFactory,
builderContext.copy(),
registeredArbitraryBuilders,
namedArbitraryBuilderMap,
monkeyContext,
manipulatorOptimizer,
fixtureMonkeyOptions.getInstantiatorProcessor()
Expand Down Expand Up @@ -145,7 +149,6 @@ public <T> ArbitraryBuilder<T> giveMeBuilder(T value) {
monkeyManipulatorFactory,
context,
registeredArbitraryBuilders,
namedArbitraryBuilderMap,
monkeyContext,
manipulatorOptimizer,
fixtureMonkeyOptions.getInstantiatorProcessor()
Expand Down Expand Up @@ -209,8 +212,12 @@ 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))
registeredArbitraryBuilders.add(
NamedMatcherOperatorAdapter.adapt(
matcherOperator.getMatcher(),
matcherOperator.getOperator().apply(this),
name
)
);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -87,7 +87,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 +96,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 +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;
Expand Down Expand Up @@ -187,19 +184,21 @@ 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) {
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()
Expand All @@ -216,13 +215,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 +558,6 @@ public ArbitraryBuilder<T> copy() {
monkeyManipulatorFactory,
context.copy(),
registeredArbitraryBuilders,
namedArbitraryBuilderMap,
monkeyContext,
manipulatorOptimizer,
instantiatorProcessor
Expand Down Expand Up @@ -612,7 +609,6 @@ private <R> DefaultArbitraryBuilder<R> generateArbitraryBuilderLazily(LazyArbitr
monkeyManipulatorFactory,
context,
registeredArbitraryBuilders,
namedArbitraryBuilderMap,
monkeyContext,
manipulatorOptimizer,
instantiatorProcessor
Expand Down

0 comments on commit 001f026

Please sign in to comment.