Skip to content

Commit

Permalink
Fix missing required PropertyGenerator within introspectors (#908)
Browse files Browse the repository at this point in the history
  • Loading branch information
seongahjo authored Jan 30, 2024
1 parent 019c6e0 commit e02c115
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 1 deletion.
4 changes: 3 additions & 1 deletion docs/content/v1.0.x/release-notes/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@

package com.navercorp.fixturemonkey.api.introspector;

import javax.annotation.Nullable;

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

import com.navercorp.fixturemonkey.api.generator.ArbitraryGeneratorContext;
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}.
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -71,6 +74,8 @@ class PrimaryConstructorArbitraryIntrospector : ArbitraryIntrospector {
)
}

override fun getRequiredPropertyGenerator(property: Property): PropertyGenerator = KOTLIN_PROPERTY_GENERATOR

private fun resolveArbitrary(
parameter: KParameter,
arbitrariesByPropertyName: Map<String?, Any?>,
Expand All @@ -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<Class<*>, KFunction<*>>(2048)
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -225,4 +226,17 @@ public SimpleContainerObject(List<JavaTypeObject> 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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<StringObject>(PrimaryConstructorArbitraryIntrospector.INSTANCE)
.build()

// when
val actual = sut.giveMeOne<StringObject>().string

// then
then(actual).isNotNull
}

companion object {
private val SUT: FixtureMonkey = FixtureMonkey.builder()
.plugin(KotlinPlugin())
Expand Down

0 comments on commit e02c115

Please sign in to comment.