Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Random Selection of Register Operations When Not Using selectName #1108

Draft
wants to merge 4 commits into
base: 31-register
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Expand Up @@ -40,6 +40,7 @@
import com.navercorp.fixturemonkey.api.type.TypeReference;
import com.navercorp.fixturemonkey.customizer.ArbitraryManipulator;
import com.navercorp.fixturemonkey.customizer.MonkeyManipulatorFactory;
import com.navercorp.fixturemonkey.customizer.PriorityMatcherOperator;
import com.navercorp.fixturemonkey.experimental.ExperimentalArbitraryBuilder;
import com.navercorp.fixturemonkey.resolver.ArbitraryBuilderContext;
import com.navercorp.fixturemonkey.resolver.ArbitraryResolver;
Expand All @@ -53,25 +54,28 @@ public final class FixtureMonkey {
private final ArbitraryTraverser traverser;
private final ManipulatorOptimizer manipulatorOptimizer;
private final MonkeyContext monkeyContext;
private final List<MatcherOperator<? extends ArbitraryBuilder<?>>> registeredArbitraryBuilders = new ArrayList<>();
private final List<PriorityMatcherOperator<? extends ArbitraryBuilder<?>>> registeredArbitraryBuilders
= new ArrayList<>();
private final MonkeyManipulatorFactory monkeyManipulatorFactory;

public FixtureMonkey(
FixtureMonkeyOptions fixtureMonkeyOptions,
ArbitraryTraverser traverser,
ManipulatorOptimizer manipulatorOptimizer,
MonkeyContext monkeyContext,
List<MatcherOperator<Function<FixtureMonkey, ? extends ArbitraryBuilder<?>>>> registeredArbitraryBuilders,
List<PriorityMatcherOperator<Function<FixtureMonkey,
? extends ArbitraryBuilder<?>>>> registeredArbitraryBuildersWithPriority,
MonkeyManipulatorFactory monkeyManipulatorFactory,
Map<String, MatcherOperator<Function<FixtureMonkey, ? extends ArbitraryBuilder<?>>>> mapsByRegisteredName
Map<String, PriorityMatcherOperator<Function<FixtureMonkey,
? extends ArbitraryBuilder<?>>>> registeredPriorityMatchersByName
) {
this.fixtureMonkeyOptions = fixtureMonkeyOptions;
this.traverser = traverser;
this.manipulatorOptimizer = manipulatorOptimizer;
this.monkeyContext = monkeyContext;
this.monkeyManipulatorFactory = monkeyManipulatorFactory;
initializeRegisteredArbitraryBuilders(registeredArbitraryBuilders);
initializeNamedArbitraryBuilderMap(mapsByRegisteredName);
initializeRegisteredArbitraryBuilders(registeredArbitraryBuildersWithPriority);
initializeNamedArbitraryBuilderMap(registeredPriorityMatchersByName);
}

public static FixtureMonkeyBuilder builder() {
Expand Down Expand Up @@ -189,11 +193,14 @@ public <T> Arbitrary<T> giveMeArbitrary(TypeReference<T> typeReference) {
}

private void initializeRegisteredArbitraryBuilders(
List<MatcherOperator<Function<FixtureMonkey, ? extends ArbitraryBuilder<?>>>> registeredArbitraryBuilders
List<PriorityMatcherOperator<Function<FixtureMonkey,
? extends ArbitraryBuilder<?>>>> registeredArbitraryBuilders
) {
List<? extends MatcherOperator<? extends ArbitraryBuilder<?>>> generatedRegisteredArbitraryBuilder =
List<? extends PriorityMatcherOperator<? extends ArbitraryBuilder<?>>> generatedRegisteredArbitraryBuilder =
registeredArbitraryBuilders.stream()
.map(it -> new MatcherOperator<>(it.getMatcher(), it.getOperator().apply(this)))
.map(it -> new PriorityMatcherOperator<>(
it.getMatcher(), it.getOperator().apply(this), it.getPriority())
)
.collect(toList());

for (int i = generatedRegisteredArbitraryBuilder.size() - 1; i >= 0; i--) {
Expand All @@ -202,13 +209,15 @@ private void initializeRegisteredArbitraryBuilders(
}

private void initializeNamedArbitraryBuilderMap(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이러면 registerName이 아닌 register에는 적용이 안되지 않나요??

Copy link
Author

@YongGoose YongGoose Dec 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

register 연산에 모두 적용해야 하는데 registeredName을 통해 등록하는 register 연산만 적용하는 것으로 착각했네요.😅
수정하겠습니다.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

해당 commit에서 작업했습니다. b2abc78

검토 부탁드립니다 :)

Map<String, MatcherOperator<Function<FixtureMonkey, ? extends ArbitraryBuilder<?>>>> mapsByRegisteredName
Map<String, PriorityMatcherOperator<Function<FixtureMonkey,
? extends ArbitraryBuilder<?>>>> mapsByRegisteredName
) {
mapsByRegisteredName.forEach((registeredName, matcherOperator) -> {
registeredArbitraryBuilders.add(
new MatcherOperator<>(
new PriorityMatcherOperator<>(
new NamedMatcher(matcherOperator.getMatcher(), registeredName),
matcherOperator.getOperator().apply(this)
matcherOperator.getOperator().apply(this),
matcherOperator.getPriority()
)
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.apiguardian.api.API;
import org.apiguardian.api.API.Status;

import com.navercorp.fixturemonkey.annotation.Order;
import com.navercorp.fixturemonkey.api.constraint.JavaConstraintGenerator;
import com.navercorp.fixturemonkey.api.container.DecomposedContainerValueFactory;
import com.navercorp.fixturemonkey.api.context.MonkeyContext;
Expand Down Expand Up @@ -62,6 +63,7 @@
import com.navercorp.fixturemonkey.buildergroup.ArbitraryBuilderCandidate;
import com.navercorp.fixturemonkey.buildergroup.ArbitraryBuilderGroup;
import com.navercorp.fixturemonkey.customizer.MonkeyManipulatorFactory;
import com.navercorp.fixturemonkey.customizer.PriorityMatcherOperator;
import com.navercorp.fixturemonkey.expression.ArbitraryExpressionFactory;
import com.navercorp.fixturemonkey.expression.MonkeyExpressionFactory;
import com.navercorp.fixturemonkey.resolver.ManipulatorOptimizer;
Expand All @@ -72,14 +74,16 @@
@SuppressWarnings("unused")
@API(since = "0.4.0", status = Status.MAINTAINED)
public final class FixtureMonkeyBuilder {
private static final int DEFAULT_PRIORITY = Integer.MAX_VALUE;

private final FixtureMonkeyOptionsBuilder fixtureMonkeyOptionsBuilder = FixtureMonkeyOptions.builder();
private final List<MatcherOperator<Function<FixtureMonkey, ? extends ArbitraryBuilder<?>>>>
registeredArbitraryBuilders = new ArrayList<>();
private final List<PriorityMatcherOperator<Function<FixtureMonkey, ? extends ArbitraryBuilder<?>>>>
registeredArbitraryBuildersWithPriority = new ArrayList<>();
private ManipulatorOptimizer manipulatorOptimizer = new NoneManipulatorOptimizer();
private MonkeyExpressionFactory monkeyExpressionFactory = new ArbitraryExpressionFactory();
private final MonkeyContextBuilder monkeyContextBuilder = MonkeyContext.builder();
private final Map<String, MatcherOperator<Function<FixtureMonkey, ? extends ArbitraryBuilder<?>>>>
registeredArbitraryListByRegisteredName = new HashMap<>();
private final Map<String, PriorityMatcherOperator<Function<FixtureMonkey, ? extends ArbitraryBuilder<?>>>>
registeredPriorityMatchersByName = new HashMap<>();
private long seed = System.nanoTime();

// The default plugins are listed below.
Expand Down Expand Up @@ -314,7 +318,15 @@ public FixtureMonkeyBuilder register(
Class<?> type,
Function<FixtureMonkey, ? extends ArbitraryBuilder<?>> registeredArbitraryBuilder
) {
return this.register(MatcherOperator.assignableTypeMatchOperator(type, registeredArbitraryBuilder));
return this.register(type, registeredArbitraryBuilder, DEFAULT_PRIORITY);
}

public FixtureMonkeyBuilder register(
Class<?> type,
Function<FixtureMonkey, ? extends ArbitraryBuilder<?>> registeredArbitraryBuilder,
int priority
) {
return this.register(MatcherOperator.assignableTypeMatchOperator(type, registeredArbitraryBuilder), priority);
}

public FixtureMonkeyBuilder registerExactType(
Expand All @@ -334,7 +346,18 @@ public FixtureMonkeyBuilder registerAssignableType(
public FixtureMonkeyBuilder register(
MatcherOperator<Function<FixtureMonkey, ? extends ArbitraryBuilder<?>>> registeredArbitraryBuilder
) {
this.registeredArbitraryBuilders.add(registeredArbitraryBuilder);
return this.register(registeredArbitraryBuilder, DEFAULT_PRIORITY);
}

public FixtureMonkeyBuilder register(
MatcherOperator<Function<FixtureMonkey, ? extends ArbitraryBuilder<?>>> registeredArbitraryBuilder,
int priority
) {
this.registeredArbitraryBuildersWithPriority.add(
new PriorityMatcherOperator<>(
registeredArbitraryBuilder.getMatcher(), registeredArbitraryBuilder.getOperator(), priority
)
);
return this;
}

Expand All @@ -361,6 +384,11 @@ public FixtureMonkeyBuilder registerGroup(Class<?>... arbitraryBuilderGroups) {
throw new RuntimeException(ex);
}
};

if (arbitraryBuilderGroup.isAnnotationPresent(Order.class)) {
Order order = arbitraryBuilderGroup.getAnnotation(Order.class);
this.register(actualType, registerArbitraryBuilder, order.value());
}
this.register(actualType, registerArbitraryBuilder);
} catch (Exception ex) {
// ignored
Expand All @@ -372,15 +400,24 @@ public FixtureMonkeyBuilder registerGroup(Class<?>... arbitraryBuilderGroups) {

public FixtureMonkeyBuilder registerGroup(ArbitraryBuilderGroup... arbitraryBuilderGroups) {
for (ArbitraryBuilderGroup arbitraryBuilderGroup : arbitraryBuilderGroups) {
List<ArbitraryBuilderCandidate<?>> candidates = arbitraryBuilderGroup.generateCandidateList()
.getCandidates();

for (ArbitraryBuilderCandidate<?> candidate : candidates) {
this.register(
candidate.getClassType(),
candidate.getArbitraryBuilderRegisterer()
);
}
this.registerGroup(arbitraryBuilderGroup, DEFAULT_PRIORITY);
}
return this;
}

public FixtureMonkeyBuilder registerGroup(
ArbitraryBuilderGroup arbitraryBuilderGroup,
int priority
) {
List<ArbitraryBuilderCandidate<?>> candidates = arbitraryBuilderGroup.generateCandidateList()
.getCandidates();

for (ArbitraryBuilderCandidate<?> candidate : candidates) {
this.register(
candidate.getClassType(),
candidate.getArbitraryBuilderRegisterer(),
priority
);
}
return this;
}
Expand All @@ -390,13 +427,26 @@ public FixtureMonkeyBuilder registeredName(
Class<?> type,
Function<FixtureMonkey, ? extends ArbitraryBuilder<?>> arbitraryBuilder
) {
if (registeredArbitraryListByRegisteredName.containsKey(registeredName)) {
return this.registeredName(registeredName, type, arbitraryBuilder, DEFAULT_PRIORITY);
}

public FixtureMonkeyBuilder registeredName(
String registeredName,
Class<?> type,
Function<FixtureMonkey, ? extends ArbitraryBuilder<?>> arbitraryBuilder,
int priority
) {
if (registeredPriorityMatchersByName.containsKey(registeredName)) {
throw new IllegalArgumentException("Duplicated ArbitraryBuilder name: " + registeredName);
}
MatcherOperator<Function<FixtureMonkey, ? extends ArbitraryBuilder<?>>> matcherOperator =
MatcherOperator.assignableTypeMatchOperator(type, arbitraryBuilder);

this.registeredArbitraryListByRegisteredName.put(registeredName, matcherOperator);
this.registeredPriorityMatchersByName.put(
registeredName, new PriorityMatcherOperator<>(
matcherOperator.getMatcher(), matcherOperator.getOperator(), priority
)
);
return this;
}

Expand Down Expand Up @@ -570,9 +620,9 @@ public FixtureMonkey build() {
traverser,
manipulatorOptimizer,
monkeyContext,
registeredArbitraryBuilders,
registeredArbitraryBuildersWithPriority,
monkeyManipulatorFactory,
registeredArbitraryListByRegisteredName
registeredPriorityMatchersByName
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
public @interface Order {
int value() default 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public ContainerInfoManipulator newContainerInfoManipulator(
}

public List<ArbitraryManipulator> newRegisteredArbitraryManipulators(
List<MatcherOperator<? extends ArbitraryBuilder<?>>> registeredArbitraryBuilders,
List<PriorityMatcherOperator<? extends ArbitraryBuilder<?>>> registeredArbitraryBuilders,
Map<Property, List<ObjectNode>> nodesByType,
ArbitraryBuilderContext builderContext
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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.customizer;

import com.navercorp.fixturemonkey.api.matcher.Matcher;
import com.navercorp.fixturemonkey.api.matcher.MatcherOperator;

public final class PriorityMatcherOperator<T> extends MatcherOperator<T> {
private final int priority;

public PriorityMatcherOperator(
Matcher matcher,
T operator,
int priority
) {
super(matcher, operator);
checkPriority(priority);
this.priority = priority;
}

public int getPriority() {
return priority;
}

private void checkPriority(int priority) {
if (priority < 0) {
throw new IllegalArgumentException("Priority must be greater than or equal to 0");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import com.navercorp.fixturemonkey.customizer.ArbitraryManipulator;
import com.navercorp.fixturemonkey.customizer.ContainerInfoManipulator;
import com.navercorp.fixturemonkey.customizer.MonkeyManipulatorFactory;
import com.navercorp.fixturemonkey.customizer.PriorityMatcherOperator;
import com.navercorp.fixturemonkey.tree.ArbitraryTraverser;
import com.navercorp.fixturemonkey.tree.ObjectTree;

Expand All @@ -47,15 +48,15 @@ public final class ArbitraryResolver {
private final MonkeyManipulatorFactory monkeyManipulatorFactory;
private final FixtureMonkeyOptions fixtureMonkeyOptions;
private final MonkeyContext monkeyContext;
private final List<MatcherOperator<? extends ArbitraryBuilder<?>>> registeredArbitraryBuilders;
private final List<PriorityMatcherOperator<? extends ArbitraryBuilder<?>>> registeredArbitraryBuilders;

public ArbitraryResolver(
ArbitraryTraverser traverser,
ManipulatorOptimizer manipulatorOptimizer,
MonkeyManipulatorFactory monkeyManipulatorFactory,
FixtureMonkeyOptions fixtureMonkeyOptions,
MonkeyContext monkeyContext,
List<MatcherOperator<? extends ArbitraryBuilder<?>>> registeredArbitraryBuilders
List<PriorityMatcherOperator<? extends ArbitraryBuilder<?>>> registeredArbitraryBuilders
) {
this.traverser = traverser;
this.manipulatorOptimizer = manipulatorOptimizer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
import com.navercorp.fixturemonkey.customizer.InnerSpec;
import com.navercorp.fixturemonkey.customizer.ManipulatorSet;
import com.navercorp.fixturemonkey.customizer.MonkeyManipulatorFactory;
import com.navercorp.fixturemonkey.customizer.PriorityMatcherOperator;
import com.navercorp.fixturemonkey.experimental.ExperimentalArbitraryBuilder;
import com.navercorp.fixturemonkey.tree.ArbitraryTraverser;

Expand All @@ -84,7 +85,7 @@ public final class DefaultArbitraryBuilder<T> implements ArbitraryBuilder<T>, Ex
private final ArbitraryTraverser traverser;
private final MonkeyManipulatorFactory monkeyManipulatorFactory;
private final ArbitraryBuilderContext context;
private final List<MatcherOperator<? extends ArbitraryBuilder<?>>> registeredArbitraryBuilders;
private final List<PriorityMatcherOperator<? extends ArbitraryBuilder<?>>> registeredArbitraryBuilders;
private final ManipulatorOptimizer manipulatorOptimizer;
private final MonkeyContext monkeyContext;
private final InstantiatorProcessor instantiatorProcessor;
Expand All @@ -96,7 +97,7 @@ public DefaultArbitraryBuilder(
ArbitraryTraverser traverser,
MonkeyManipulatorFactory monkeyManipulatorFactory,
ArbitraryBuilderContext context,
List<MatcherOperator<? extends ArbitraryBuilder<?>>> registeredArbitraryBuilders,
List<PriorityMatcherOperator<? extends ArbitraryBuilder<?>>> registeredArbitraryBuilders,
MonkeyContext monkeyContext,
ManipulatorOptimizer manipulatorOptimizer,
InstantiatorProcessor instantiatorProcessor
Expand Down Expand Up @@ -183,6 +184,7 @@ public ArbitraryBuilder<T> setLazy(PropertySelector propertySelector, Supplier<?
);
}

@Override
public ArbitraryBuilder<T> selectName(String... names) {
ArbitraryBuilderContext builderContext = registeredArbitraryBuilders.stream()
.filter(operator -> Arrays.stream(names)
Expand Down
Loading