Skip to content

Commit

Permalink
feat: find builders in specimen-type
Browse files Browse the repository at this point in the history
  • Loading branch information
Nylle committed Sep 7, 2024
1 parent 6cb8947 commit 5d908a0
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/main/java/com/github/nylle/javafixture/SpecimenType.java
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,16 @@ public List<Method> getFactoryMethods() {
.collect(Collectors.toList());
}

public List<Builder<T>> findBuilders() {
return Stream.of(asClass().getDeclaredMethods())
.filter(m -> Modifier.isStatic(m.getModifiers()))
.filter(m -> Modifier.isPublic(m.getModifiers()))
.filter(m -> !m.getReturnType().equals(asClass()))
.map(m -> Builder.create(m, this))
.filter(b -> b != null)
.collect(toList());
}

@Override
public boolean equals(final Object o) {
if (this == o) {
Expand Down
31 changes: 31 additions & 0 deletions src/test/java/com/github/nylle/javafixture/SpecimenTypeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.github.nylle.javafixture.annotations.testcases.TestCase;
import com.github.nylle.javafixture.annotations.testcases.TestWithCases;
import com.github.nylle.javafixture.testobjects.ClassWithBuilder;
import com.github.nylle.javafixture.testobjects.ITestGeneric;
import com.github.nylle.javafixture.testobjects.TestAbstractClass;
import com.github.nylle.javafixture.testobjects.TestEnum;
Expand Down Expand Up @@ -58,6 +59,7 @@
import java.util.concurrent.LinkedTransferQueue;
import java.util.concurrent.TransferQueue;

import static com.github.nylle.javafixture.CustomizationContext.noContext;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;

Expand Down Expand Up @@ -368,6 +370,35 @@ void takesAValueMapper() {
}
}

@Nested
class FindBuilders {

@Test
void returnsListOfBuilders() {
var specimenFactory = new SpecimenFactory(new Context(new Configuration()));

var sut = new SpecimenType<ClassWithBuilder>(){};

var actual = sut.findBuilders();

assertThat(actual).hasSize(1);
assertThat(actual.get(0).invoke(specimenFactory, noContext())).isInstanceOf(ClassWithBuilder.class);
}

@TestWithCases
@TestCase(class1 = TestObject.class)
@TestCase(class1 = ITestGeneric.class)
@TestCase(class1 = TestEnum.class)
@TestCase(class1 = String.class)
void returnsEmptyListIfNoBuildersFound(Class<?> typeWithoutBuilder) {
var sut = SpecimenType.fromClass(typeWithoutBuilder);

var actual = sut.findBuilders();

assertThat(actual).isEmpty();
}
}

@Test
void getComponentType() {
assertThat(new SpecimenType<int[]>() {}.getComponentType()).isEqualTo(int.class);
Expand Down

0 comments on commit 5d908a0

Please sign in to comment.