diff --git a/docs/content/v1.0.x/release-notes/_index.md b/docs/content/v1.0.x/release-notes/_index.md index da09f791c..712779213 100644 --- a/docs/content/v1.0.x/release-notes/_index.md +++ b/docs/content/v1.0.x/release-notes/_index.md @@ -9,7 +9,9 @@ sectionStart ### v.1.0.13 Add InterfacePlugin supports abstract classes through `abstractClassExtends` option. -Fix setLazy with value wrapped by Just would not be manipulated +Fix setLazy with value wrapped by Just would not be manipulated. + +Fix missing required PropertyGenerator within introspectors. sectionEnd diff --git a/fixture-monkey-api/src/main/java/com/navercorp/fixturemonkey/api/introspector/TypedArbitraryIntrospector.java b/fixture-monkey-api/src/main/java/com/navercorp/fixturemonkey/api/introspector/TypedArbitraryIntrospector.java index 27c1308da..8ee255632 100644 --- a/fixture-monkey-api/src/main/java/com/navercorp/fixturemonkey/api/introspector/TypedArbitraryIntrospector.java +++ b/fixture-monkey-api/src/main/java/com/navercorp/fixturemonkey/api/introspector/TypedArbitraryIntrospector.java @@ -18,6 +18,8 @@ package com.navercorp.fixturemonkey.api.introspector; +import javax.annotation.Nullable; + import org.apiguardian.api.API; import org.apiguardian.api.API.Status; @@ -25,6 +27,7 @@ import com.navercorp.fixturemonkey.api.matcher.Matcher; import com.navercorp.fixturemonkey.api.matcher.MatcherOperator; import com.navercorp.fixturemonkey.api.property.Property; +import com.navercorp.fixturemonkey.api.property.PropertyGenerator; /** * Introspects specific properties matched by {@link Matcher}. @@ -46,4 +49,14 @@ public ArbitraryIntrospectorResult introspect(ArbitraryGeneratorContext context) public boolean match(Property property) { return arbitraryIntrospector.match(property); } + + @Nullable + @Override + public PropertyGenerator getRequiredPropertyGenerator(Property property) { + if (arbitraryIntrospector.match(property)) { + return arbitraryIntrospector.getOperator().getRequiredPropertyGenerator(property); + } + + return null; + } } diff --git a/fixture-monkey-kotlin/src/main/kotlin/com/navercorp/fixturemonkey/kotlin/introspector/PrimaryConstructorArbitraryIntrospector.kt b/fixture-monkey-kotlin/src/main/kotlin/com/navercorp/fixturemonkey/kotlin/introspector/PrimaryConstructorArbitraryIntrospector.kt index 56af3ad70..eb6354192 100644 --- a/fixture-monkey-kotlin/src/main/kotlin/com/navercorp/fixturemonkey/kotlin/introspector/PrimaryConstructorArbitraryIntrospector.kt +++ b/fixture-monkey-kotlin/src/main/kotlin/com/navercorp/fixturemonkey/kotlin/introspector/PrimaryConstructorArbitraryIntrospector.kt @@ -23,7 +23,10 @@ import com.navercorp.fixturemonkey.api.container.ConcurrentLruCache import com.navercorp.fixturemonkey.api.generator.ArbitraryGeneratorContext import com.navercorp.fixturemonkey.api.introspector.ArbitraryIntrospector import com.navercorp.fixturemonkey.api.introspector.ArbitraryIntrospectorResult +import com.navercorp.fixturemonkey.api.property.Property +import com.navercorp.fixturemonkey.api.property.PropertyGenerator import com.navercorp.fixturemonkey.api.type.Types +import com.navercorp.fixturemonkey.kotlin.property.KotlinPropertyGenerator import org.apiguardian.api.API import org.apiguardian.api.API.Status.MAINTAINED import org.slf4j.LoggerFactory @@ -71,6 +74,8 @@ class PrimaryConstructorArbitraryIntrospector : ArbitraryIntrospector { ) } + override fun getRequiredPropertyGenerator(property: Property): PropertyGenerator = KOTLIN_PROPERTY_GENERATOR + private fun resolveArbitrary( parameter: KParameter, arbitrariesByPropertyName: Map, @@ -91,6 +96,7 @@ class PrimaryConstructorArbitraryIntrospector : ArbitraryIntrospector { companion object { val INSTANCE = PrimaryConstructorArbitraryIntrospector() private val LOGGER = LoggerFactory.getLogger(PrimaryConstructorArbitraryIntrospector::class.java) + private val KOTLIN_PROPERTY_GENERATOR = KotlinPropertyGenerator() private val CONSTRUCTOR_CACHE = ConcurrentLruCache, KFunction<*>>(2048) } } diff --git a/fixture-monkey-tests/java-tests/src/test/java/com/navercorp/fixturemonkey/tests/java/ConstructorTestSpecs.java b/fixture-monkey-tests/java-tests/src/test/java/com/navercorp/fixturemonkey/tests/java/ConstructorTestSpecs.java index c938e5574..3fd53a018 100644 --- a/fixture-monkey-tests/java-tests/src/test/java/com/navercorp/fixturemonkey/tests/java/ConstructorTestSpecs.java +++ b/fixture-monkey-tests/java-tests/src/test/java/com/navercorp/fixturemonkey/tests/java/ConstructorTestSpecs.java @@ -1,5 +1,6 @@ package com.navercorp.fixturemonkey.tests.java; +import java.beans.ConstructorProperties; import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -225,4 +226,17 @@ public SimpleContainerObject(List list) { this.list = list; } } + + public static class FieldAndConstructorParameterMismatchObject { + private final String value; + + @ConstructorProperties("integer") + public FieldAndConstructorParameterMismatchObject(int integer) { + this.value = String.valueOf(integer); + } + + public String getValue() { + return value; + } + } } diff --git a/fixture-monkey-tests/java-tests/src/test/java/com/navercorp/fixturemonkey/tests/java/JavaTest.java b/fixture-monkey-tests/java-tests/src/test/java/com/navercorp/fixturemonkey/tests/java/JavaTest.java index 70b5b6806..d88d9d153 100644 --- a/fixture-monkey-tests/java-tests/src/test/java/com/navercorp/fixturemonkey/tests/java/JavaTest.java +++ b/fixture-monkey-tests/java-tests/src/test/java/com/navercorp/fixturemonkey/tests/java/JavaTest.java @@ -62,6 +62,7 @@ import com.navercorp.fixturemonkey.resolver.ArbitraryBuilderCandidateFactory; import com.navercorp.fixturemonkey.resolver.ArbitraryBuilderCandidateList; import com.navercorp.fixturemonkey.tests.java.ConstructorAndPropertyTestSpecs.ConsturctorAndProperty; +import com.navercorp.fixturemonkey.tests.java.ConstructorTestSpecs.FieldAndConstructorParameterMismatchObject; import com.navercorp.fixturemonkey.tests.java.ConstructorTestSpecs.SimpleContainerObject; import com.navercorp.fixturemonkey.tests.java.ImmutableDepthTestSpecs.DepthStringValueList; import com.navercorp.fixturemonkey.tests.java.ImmutableDepthTestSpecs.OneDepthStringValue; @@ -1209,4 +1210,18 @@ void fieldReflectionArbitraryIntrospectorSampleTwiceResultNotMutated() { String expected = stringObject.getObject().getValue(); then(actual).isEqualTo(expected); } + + @Test + void fieldAndConstructorParameterMismatch() { + FixtureMonkey sut = FixtureMonkey.builder() + .pushExactTypeArbitraryIntrospector( + FieldAndConstructorParameterMismatchObject.class, + ConstructorPropertiesArbitraryIntrospector.INSTANCE + ) + .build(); + + String actual = sut.giveMeOne(FieldAndConstructorParameterMismatchObject.class).getValue(); + + then(actual).isNotNull(); + } } diff --git a/fixture-monkey-tests/kotlin-tests/src/test/kotlin/com/navercorp/fixturemonkey/tests/kotlin/KotlinTest.kt b/fixture-monkey-tests/kotlin-tests/src/test/kotlin/com/navercorp/fixturemonkey/tests/kotlin/KotlinTest.kt index 674993bdc..916fe0bee 100644 --- a/fixture-monkey-tests/kotlin-tests/src/test/kotlin/com/navercorp/fixturemonkey/tests/kotlin/KotlinTest.kt +++ b/fixture-monkey-tests/kotlin-tests/src/test/kotlin/com/navercorp/fixturemonkey/tests/kotlin/KotlinTest.kt @@ -40,6 +40,7 @@ import com.navercorp.fixturemonkey.kotlin.giveMeOne import com.navercorp.fixturemonkey.kotlin.instantiator.instantiateBy import com.navercorp.fixturemonkey.kotlin.into import com.navercorp.fixturemonkey.kotlin.intoGetter +import com.navercorp.fixturemonkey.kotlin.introspector.PrimaryConstructorArbitraryIntrospector import com.navercorp.fixturemonkey.kotlin.pushExactTypeArbitraryIntrospector import com.navercorp.fixturemonkey.kotlin.setExp import com.navercorp.fixturemonkey.kotlin.setExpGetter @@ -407,6 +408,22 @@ class KotlinTest { then(actual).isEqualTo(expected) } + @RepeatedTest(TEST_COUNT) + fun pushPrimaryConstructorIntrospector() { + // given + class StringObject(val string: String) + + val sut = FixtureMonkey.builder() + .pushExactTypeArbitraryIntrospector(PrimaryConstructorArbitraryIntrospector.INSTANCE) + .build() + + // when + val actual = sut.giveMeOne().string + + // then + then(actual).isNotNull + } + companion object { private val SUT: FixtureMonkey = FixtureMonkey.builder() .plugin(KotlinPlugin())