Skip to content

Commit

Permalink
Fix sampleList always return same element with AnonymousArbitraryIntr…
Browse files Browse the repository at this point in the history
…ospector (#779)
  • Loading branch information
seongahjo authored Oct 10, 2023
1 parent 2979858 commit ed8f41e
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,36 +53,42 @@ public ArbitraryIntrospectorResult introspect(ArbitraryGeneratorContext context)
Property property = context.getResolvedProperty();
Class<?> type = Types.getActualType(property.getType());

Map<String, CombinableArbitrary<?>> arbitrariesByPropertyName =
context.getCombinableArbitrariesByPropertyName();

List<ArbitraryProperty> childrenProperties = context.getChildren();

InvocationHandlerBuilder invocationHandlerBuilder = new InvocationHandlerBuilder(new HashMap<>());

for (ArbitraryProperty arbitraryProperty : childrenProperties) {
Property childProperty = arbitraryProperty.getObjectProperty().getProperty();

if (!(childProperty instanceof MethodProperty)) {
continue;
}

MethodProperty methodProperty = (MethodProperty)childProperty;

Object combined = arbitrariesByPropertyName.get(childProperty.getName()).combined();
invocationHandlerBuilder.put(methodProperty.getMethodName(), combined);
}

if (invocationHandlerBuilder.generatedValuesByMethodName.isEmpty()) {
return new ArbitraryIntrospectorResult(CombinableArbitrary.NOT_GENERATED);
}

return new ArbitraryIntrospectorResult(
CombinableArbitrary.from(
type.cast(
Proxy.newProxyInstance(type.getClassLoader(), new Class[] {type}, invocationHandlerBuilder.build())
CombinableArbitrary.objectBuilder()
.properties(context.getCombinableArbitrariesByArbitraryProperty())
.build(
arbitrariesByPropertyName -> {
List<ArbitraryProperty> childrenProperties = context.getChildren();

InvocationHandlerBuilder invocationHandlerBuilder = new InvocationHandlerBuilder(
new HashMap<>());

for (ArbitraryProperty arbitraryProperty : childrenProperties) {
Property childProperty = arbitraryProperty.getObjectProperty().getProperty();

if (!(childProperty instanceof MethodProperty)) {
continue;
}

MethodProperty methodProperty = (MethodProperty)childProperty;

Object combined = arbitrariesByPropertyName.get(arbitraryProperty);
invocationHandlerBuilder.put(methodProperty.getMethodName(), combined);
}

if (invocationHandlerBuilder.generatedValuesByMethodName.isEmpty()) {
return null;
}

return type.cast(
Proxy.newProxyInstance(
type.getClassLoader(),
new Class[] {type},
invocationHandlerBuilder.build()
)
);
}
)
)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class PrimaryConstructorArbitraryIntrospectorTest {
@Property
fun sampleInterface() {
// when
val actual = sut.giveMeOne<InterfaceClass>()
val actual: InterfaceClass = sut.giveMeOne()

then(actual).isNull()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@
import static org.assertj.core.api.BDDAssertions.then;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.Test;

import com.navercorp.fixturemonkey.FixtureMonkey;
import com.navercorp.fixturemonkey.javax.validation.plugin.JavaxValidationPlugin;
Expand Down Expand Up @@ -209,4 +212,15 @@ void setNestedInheritedInterface() {

then(actual).isEqualTo(expected);
}

@Test
void sampleListWouldReturnDiff() {
Set<Integer> actual = SUT.giveMeBuilder(Interface.class)
.sampleList(2)
.stream()
.map(Interface::integer)
.collect(Collectors.toSet());

then(actual).hasSize(2);
}
}

0 comments on commit ed8f41e

Please sign in to comment.