Skip to content

Commit

Permalink
Fix a record instance generates canonical constructor properties
Browse files Browse the repository at this point in the history
  • Loading branch information
seongahjo committed Jan 18, 2025
1 parent 7a67237 commit 6306df6
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

Expand All @@ -53,22 +54,32 @@
*/
@API(since = "0.5.3", status = Status.MAINTAINED)
public final class ConstructorParameterPropertyGenerator implements PropertyGenerator {
private final Function<Property, List<Constructor<?>>> constructorsSupplier;
private final Predicate<Constructor<?>> constructorPredicate;
private final Matcher matcher;

public ConstructorParameterPropertyGenerator(
Predicate<Constructor<?>> constructorPredicate,
Matcher matcher
) {
this.constructorsSupplier = p -> TypeCache.getDeclaredConstructors(Types.getActualType(p.getType()));
this.constructorPredicate = constructorPredicate;
this.matcher = matcher;
}

public ConstructorParameterPropertyGenerator(
Function<Property, List<Constructor<?>>> constructorsSupplier,
Predicate<Constructor<?>> constructorPredicate,
Matcher matcher
) {
this.constructorsSupplier = constructorsSupplier;
this.constructorPredicate = constructorPredicate;
this.matcher = matcher;
}

@Override
public List<Property> generateChildProperties(Property property) {
Class<?> clazz = Types.getActualType(property.getType());

Constructor<?> declaredConstructor = TypeCache.getDeclaredConstructors(clazz).stream()
Constructor<?> declaredConstructor = constructorsSupplier.apply(property).stream()
.filter(constructorPredicate)
.findFirst()
.orElse(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package com.navercorp.fixturemonkey.api.option;

import java.lang.reflect.Constructor;
import java.time.ZoneId;
import java.util.List;

Expand All @@ -26,13 +27,26 @@

import com.navercorp.fixturemonkey.api.matcher.MatcherOperator;
import com.navercorp.fixturemonkey.api.property.CandidateConcretePropertyResolver;
import com.navercorp.fixturemonkey.api.property.ConstructorParameterPropertyGenerator;
import com.navercorp.fixturemonkey.api.property.PropertyGenerator;
import com.navercorp.fixturemonkey.api.property.SealedTypeCandidateConcretePropertyResolver;
import com.navercorp.fixturemonkey.api.type.Constructors;
import com.navercorp.fixturemonkey.api.type.TypeCache;
import com.navercorp.fixturemonkey.api.type.Types;

@API(since = "1.0.14", status = Status.INTERNAL)
public final class JdkVariantOptions {
private static final CandidateConcretePropertyResolver SEALED_TYPE_CANDIDATE_CONCRETE_PROPERTY_RESOLVER =
new SealedTypeCandidateConcretePropertyResolver();
private static final PropertyGenerator CANONICAL_CONSTRUCTOR_PARAMETER_PROPERTY_GENERATOR =
new ConstructorParameterPropertyGenerator(
p -> Constructors.findPrimaryConstructor(
Types.getActualType(p.getType()),
TypeCache.getDeclaredConstructors(Types.getActualType(p.getType())).toArray(Constructor[]::new)
).stream().toList(),
it -> true,
it -> true
);

public void apply(FixtureMonkeyOptionsBuilder optionsBuilder) {
optionsBuilder.insertFirstCandidateConcretePropertyResolvers(
Expand All @@ -41,6 +55,10 @@ public void apply(FixtureMonkeyOptionsBuilder optionsBuilder) {
SEALED_TYPE_CANDIDATE_CONCRETE_PROPERTY_RESOLVER
)
)
.insertFirstPropertyGenerator(ZoneId.class, property -> List.of());
.insertFirstPropertyGenerator(ZoneId.class, property -> List.of())
.insertFirstPropertyGenerator(
p -> Types.getActualType(p.getType()).isRecord(),
CANONICAL_CONSTRUCTOR_PARAMETER_PROPERTY_GENERATOR
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.navercorp.fixturemonkey.FixtureMonkey;
import com.navercorp.fixturemonkey.api.introspector.ConstructorPropertiesArbitraryIntrospector;
import com.navercorp.fixturemonkey.tests.java17.RecordTestSpecs.BooleanRecord;
import com.navercorp.fixturemonkey.tests.java17.RecordTestSpecs.CanonicalConstructorRecord;
import com.navercorp.fixturemonkey.tests.java17.RecordTestSpecs.CompactConstructorRecord;
import com.navercorp.fixturemonkey.tests.java17.RecordTestSpecs.ComplexContainerRecord;
import com.navercorp.fixturemonkey.tests.java17.RecordTestSpecs.ContainerRecord;
Expand All @@ -39,6 +40,7 @@
class ConstructorPropertiesRecordTest {
private static final FixtureMonkey SUT = FixtureMonkey.builder()
.objectIntrospector(ConstructorPropertiesArbitraryIntrospector.INSTANCE)
.defaultNotNull(true)
.build();

@RepeatedTest(TEST_COUNT)
Expand Down Expand Up @@ -205,4 +207,11 @@ void setWrapperBoolean() {

then(actual).isFalse();
}

@RepeatedTest(TEST_COUNT)
void sampleCanonicalConstructorRecord() {
String actual = SUT.giveMeOne(CanonicalConstructorRecord.class).string();

then(actual).isNotNull();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,10 @@ public record IsPrefixBooleanRecord(boolean isPrimitive, Boolean isWrapper) {

public record BooleanRecord(boolean primitive, Boolean wrapper) {
}

public record CanonicalConstructorRecord(String string) {
public CanonicalConstructorRecord() {
this(null);
}
}
}

0 comments on commit 6306df6

Please sign in to comment.