Skip to content

Commit

Permalink
Add customizeValidOnly option (#1103)
Browse files Browse the repository at this point in the history
  • Loading branch information
seongahjo authored Nov 22, 2024
1 parent d4feae0 commit d5ceb71
Show file tree
Hide file tree
Showing 16 changed files with 379 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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 java.lang.annotation.Annotation;
import java.util.Set;

import org.apiguardian.api.API;
import org.apiguardian.api.API.Status;

@API(since = "1.0.4", status = Status.EXPERIMENTAL)
public final class DefaultTreeMatcherMetadata implements TreeMatcherMetadata {
private final Set<Annotation> annotations;

public DefaultTreeMatcherMetadata(Set<Annotation> annotations) {
this.annotations = annotations;
}

@Override
public Set<Annotation> getAnnotations() {
return annotations;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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 org.apiguardian.api.API;
import org.apiguardian.api.API.Status;

/**
* It is mainly used to customize the ObjectTree that meets the specific condition with {@link TreeMatcherOperator}.
* <p>
* The main difference to the {@link Matcher} is the scope.
* {@link Matcher} is intended to reference the nodes of the ObjectTree,
* but {@link TreeMatcher} is intended to reference the ObjectTree itself.
*
* @see Matcher
*/
@API(since = "1.0.4", status = Status.EXPERIMENTAL)
@FunctionalInterface
public interface TreeMatcher {
/**
* Determines if the ObjectTree meets the condition.
*
* @param metadata the metadata of the ObjectTree
* @return {@code true} if the condition matches, {@code false} if not matches
*/
boolean match(TreeMatcherMetadata metadata);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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 java.lang.annotation.Annotation;
import java.util.Set;

import org.apiguardian.api.API;
import org.apiguardian.api.API.Status;

/**
* It contains the metadata of the ObjectTree.
*/
@API(since = "1.0.4", status = Status.EXPERIMENTAL)
public interface TreeMatcherMetadata {
/**
* Retrieves the annotations of all nodes in the ObjectTree.
*
* @return the annotations of all nodes
*/
Set<Annotation> getAnnotations();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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 org.apiguardian.api.API;
import org.apiguardian.api.API.Status;

@API(since = "1.0.4", status = Status.EXPERIMENTAL)
public final class TreeMatcherOperator<T> implements TreeMatcher {
private final TreeMatcher matcher;
private final T operator;

public TreeMatcherOperator(TreeMatcher matcher, T operator) {
this.matcher = matcher;
this.operator = operator;
}

@Override
public boolean match(TreeMatcherMetadata metadata) {
return this.matcher.match(metadata);
}

public T getOperator() {
return operator;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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.option;

import org.apiguardian.api.API;
import org.apiguardian.api.API.Status;

/**
* It is a public API to customize the ObjectTree.
* <p>
* It should contain the customizing options for the ObjectTree, not for the node.
*/
@API(since = "1.1.4", status = Status.EXPERIMENTAL)
public final class BuilderContextInitializer {
private final boolean validOnly;

private BuilderContextInitializer(boolean validOnly) {
this.validOnly = validOnly;
}

public static BuilderContextInitializer validOnly(boolean validOnly) {
return new BuilderContextInitializer(validOnly);
}

public boolean isValidOnly() {
return validOnly;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
import com.navercorp.fixturemonkey.api.matcher.MatcherOperator;
import com.navercorp.fixturemonkey.api.matcher.Matchers;
import com.navercorp.fixturemonkey.api.matcher.SingleGenericTypeMatcher;
import com.navercorp.fixturemonkey.api.matcher.TreeMatcherOperator;
import com.navercorp.fixturemonkey.api.property.CandidateConcretePropertyResolver;
import com.navercorp.fixturemonkey.api.property.CompositeCandidateConcretePropertyResolver;
import com.navercorp.fixturemonkey.api.property.ConcreteTypeCandidateConcretePropertyResolver;
Expand Down Expand Up @@ -167,6 +168,7 @@ public final class FixtureMonkeyOptions {
private final InstantiatorProcessor instantiatorProcessor;
private final List<MatcherOperator<CandidateConcretePropertyResolver>> candidateConcretePropertyResolvers;
private final boolean enableLoggingFail;
private final List<TreeMatcherOperator<BuilderContextInitializer>> builderContextInitializers;

public FixtureMonkeyOptions(
List<MatcherOperator<PropertyGenerator>> propertyGenerators,
Expand All @@ -188,7 +190,8 @@ public FixtureMonkeyOptions(
JavaConstraintGenerator javaConstraintGenerator,
InstantiatorProcessor instantiatorProcessor,
List<MatcherOperator<CandidateConcretePropertyResolver>> candidateConcretePropertyResolvers,
boolean enableLoggingFail
boolean enableLoggingFail,
List<TreeMatcherOperator<BuilderContextInitializer>> builderContextCustomizer
) {
this.propertyGenerators = propertyGenerators;
this.defaultPropertyGenerator = defaultPropertyGenerator;
Expand All @@ -210,6 +213,7 @@ public FixtureMonkeyOptions(
this.instantiatorProcessor = instantiatorProcessor;
this.candidateConcretePropertyResolvers = candidateConcretePropertyResolvers;
this.enableLoggingFail = enableLoggingFail;
this.builderContextInitializers = builderContextCustomizer;
}

public static FixtureMonkeyOptionsBuilder builder() {
Expand Down Expand Up @@ -342,6 +346,10 @@ public boolean isEnableLoggingFail() {
return enableLoggingFail;
}

public List<TreeMatcherOperator<BuilderContextInitializer>> getBuilderContextInitializers() {
return builderContextInitializers;
}

@Nullable
public CandidateConcretePropertyResolver getCandidateConcretePropertyResolver(Property property) {
List<CandidateConcretePropertyResolver> candidateConcretePropertyResolverList =
Expand Down Expand Up @@ -373,7 +381,8 @@ public FixtureMonkeyOptionsBuilder toBuilder() {
.decomposedContainerValueFactory(decomposedContainerValueFactory)
.javaConstraintGenerator(javaConstraintGenerator)
.instantiatorProcessor(instantiatorProcessor)
.candidateConcretePropertyResolvers(candidateConcretePropertyResolvers);
.candidateConcretePropertyResolvers(candidateConcretePropertyResolvers)
.builderContextInitializers(builderContextInitializers);
}

private static List<MatcherOperator<ContainerPropertyGenerator>> getDefaultContainerPropertyGenerators() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
import com.navercorp.fixturemonkey.api.jqwik.JqwikJavaTypeArbitraryGeneratorSet;
import com.navercorp.fixturemonkey.api.matcher.Matcher;
import com.navercorp.fixturemonkey.api.matcher.MatcherOperator;
import com.navercorp.fixturemonkey.api.matcher.TreeMatcherOperator;
import com.navercorp.fixturemonkey.api.plugin.Plugin;
import com.navercorp.fixturemonkey.api.property.CandidateConcretePropertyResolver;
import com.navercorp.fixturemonkey.api.property.DefaultPropertyGenerator;
Expand Down Expand Up @@ -139,6 +140,7 @@ public final class FixtureMonkeyOptionsBuilder {
private InstantiatorProcessor instantiatorProcessor = new JavaInstantiatorProcessor();
private List<MatcherOperator<CandidateConcretePropertyResolver>> candidateConcretePropertyResolvers =
new ArrayList<>(FixtureMonkeyOptions.DEFAULT_CANDIDATE_CONCRETE_PROPERTY_RESOLVERS);
private List<TreeMatcherOperator<BuilderContextInitializer>> builderContextInitializers = new ArrayList<>();

FixtureMonkeyOptionsBuilder() {
new JdkVariantOptions().apply(this);
Expand Down Expand Up @@ -539,6 +541,23 @@ public FixtureMonkeyOptionsBuilder insertFirstCandidateConcretePropertyResolvers
return this;
}

public FixtureMonkeyOptionsBuilder builderContextInitializers(
List<TreeMatcherOperator<BuilderContextInitializer>> builderContextInitializers
) {
this.builderContextInitializers = builderContextInitializers;
return this;
}

public FixtureMonkeyOptionsBuilder insertFirstBuilderContextInitializer(
TreeMatcherOperator<BuilderContextInitializer> builderContextInitializer
) {
this.builderContextInitializers = insertFirst(
this.builderContextInitializers,
builderContextInitializer
);
return this;
}

public FixtureMonkeyOptions build() {
ObjectPropertyGenerator defaultObjectPropertyGenerator = defaultIfNull(
this.defaultObjectPropertyGenerator,
Expand Down Expand Up @@ -671,7 +690,8 @@ public FixtureMonkeyOptions build() {
resolvedJavaConstraintGenerator,
this.instantiatorProcessor,
this.candidateConcretePropertyResolvers,
this.enableLoggingFail
this.enableLoggingFail,
this.builderContextInitializers
);
}

Expand Down
Loading

0 comments on commit d5ceb71

Please sign in to comment.