Skip to content

Commit

Permalink
refactor: rename instantiators to reflect interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Nylle committed Sep 8, 2024
1 parent b9421db commit 87937b3
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 32 deletions.
6 changes: 3 additions & 3 deletions src/main/java/com/github/nylle/javafixture/SpecimenType.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.github.nylle.javafixture;

import com.github.nylle.javafixture.instantiation.Builder;
import com.github.nylle.javafixture.instantiation.BuilderInstantiator;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
Expand Down Expand Up @@ -226,12 +226,12 @@ public List<Method> getFactoryMethods() {
.collect(Collectors.toList());
}

public List<Builder<T>> findBuilders() {
public List<BuilderInstantiator<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))
.map(m -> BuilderInstantiator.create(m, this))
.filter(b -> b != null)
.collect(toList());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@

import static java.util.stream.Collectors.toList;

public class Builder<T> implements Instantiator<T> {
public class BuilderInstantiator<T> implements Instantiator<T> {

private final Method buildMethod;
private final Method builderMethod;

private Builder(Method builderMethod, Method buildMethod) {
private BuilderInstantiator(Method builderMethod, Method buildMethod) {
this.builderMethod = builderMethod;
this.buildMethod = buildMethod;
}

public static <T> Builder<T> create(Method builderMethodCandidate, SpecimenType<T> targetType) {
public static <T> BuilderInstantiator<T> create(Method builderMethodCandidate, SpecimenType<T> targetType) {
return findBuildMethod(builderMethodCandidate.getReturnType(), targetType.asClass())
.map(x -> new Builder<T>(builderMethodCandidate, x))
.map(x -> new BuilderInstantiator<T>(builderMethodCandidate, x))
.orElse(null);
}

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

import static java.util.Arrays.stream;

public class Constructor<T> implements Instantiator<T> {
public class ConstructorInstantiator<T> implements Instantiator<T> {

private final java.lang.reflect.Constructor<T> constructor;

private Constructor(java.lang.reflect.Constructor<T> constructor) {
private ConstructorInstantiator(java.lang.reflect.Constructor<T> constructor) {
this.constructor = constructor;
}

public static <T> Constructor<T> create(java.lang.reflect.Constructor<T> constructor) {
return new Constructor<>(constructor);
public static <T> ConstructorInstantiator<T> create(java.lang.reflect.Constructor<T> constructor) {
return new ConstructorInstantiator<>(constructor);
}

public T invoke(SpecimenFactory specimenFactory, CustomizationContext customizationContext) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@
import java.util.ArrayList;
import java.util.List;

public class FactoryMethod<T> implements Instantiator<T> {
public class FactoryMethodInstantiator<T> implements Instantiator<T> {

private final Method factoryMethod;
private final SpecimenType<T> targetType;

public FactoryMethod(Method factoryMethod, SpecimenType<T> targetType) {
public FactoryMethodInstantiator(Method factoryMethod, SpecimenType<T> targetType) {
this.factoryMethod = factoryMethod;
this.targetType = targetType;
}

public static <T> FactoryMethod<T> create(Method factoryMethod, SpecimenType<T> targetType) {
return new FactoryMethod<>(factoryMethod, targetType);
public static <T> FactoryMethodInstantiator<T> create(Method factoryMethod, SpecimenType<T> targetType) {
return new FactoryMethodInstantiator<>(factoryMethod, targetType);
}

public T invoke(SpecimenFactory specimenFactory, CustomizationContext customizationContext) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@

import static org.assertj.core.api.Assertions.assertThat;

class BuilderTest {
class BuilderInstantiatorTest {

@Test
void invokeReturnsBuiltObjectWithAllMethodsCalled() throws NoSuchMethodException {
var sut = Builder.create(ClassWithBuilder.class.getMethod("builder"), SpecimenType.fromClass(ClassWithBuilder.class));
var sut = BuilderInstantiator.create(ClassWithBuilder.class.getMethod("builder"), SpecimenType.fromClass(ClassWithBuilder.class));

var result = sut.invoke(new SpecimenFactory(new Context(new Configuration())), CustomizationContext.noContext());

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

import static org.assertj.core.api.Assertions.assertThat;

class ConstructorTest {
class ConstructorInstantiatorTest {

@Test
@DisplayName("returns instance")
void canCreateInstanceFromConstructor() throws NoSuchMethodException {
var sut = Constructor.create(TestObjectWithGenericConstructor.class.getConstructor(String.class, Optional.class));
var sut = ConstructorInstantiator.create(TestObjectWithGenericConstructor.class.getConstructor(String.class, Optional.class));

var actual = sut.invoke(new SpecimenFactory(new Context(Configuration.configure())), new CustomizationContext(List.of(), Map.of(), false));

Expand All @@ -33,7 +33,7 @@ void canCreateInstanceFromConstructor() throws NoSuchMethodException {
@Test
@DisplayName("fields not set by constructor are null")
void fieldsNotSetByConstructorAreNull() throws NoSuchMethodException {
var sut = Constructor.create(TestObjectWithGenericConstructor.class.getConstructor(String.class, Optional.class));
var sut = ConstructorInstantiator.create(TestObjectWithGenericConstructor.class.getConstructor(String.class, Optional.class));

var actual = sut.invoke(new SpecimenFactory(new Context(Configuration.configure())), new CustomizationContext(List.of(), Map.of(), false));

Expand All @@ -44,7 +44,7 @@ void fieldsNotSetByConstructorAreNull() throws NoSuchMethodException {
@Test
@DisplayName("arguments can be customized")
void argumentsCanBeCustomized() throws NoSuchMethodException {
var sut = Constructor.create(TestObject.class.getConstructor(String.class, List.class, Map.class));
var sut = ConstructorInstantiator.create(TestObject.class.getConstructor(String.class, List.class, Map.class));

// use arg0, because .class files do not store formal parameter names by default
var actual = sut.invoke(new SpecimenFactory(new Context(Configuration.configure())), new CustomizationContext(List.of(), Map.of("arg0", "customized"), true));
Expand All @@ -55,7 +55,7 @@ void argumentsCanBeCustomized() throws NoSuchMethodException {
@Test
@DisplayName("using constructor is used for all instances")
void usingConstructorIsRecursive() throws NoSuchMethodException {
var sut = Constructor.create(TestObjectWithConstructedField.class.getConstructor(int.class, TestObjectWithGenericConstructor.class));
var sut = ConstructorInstantiator.create(TestObjectWithConstructedField.class.getConstructor(int.class, TestObjectWithGenericConstructor.class));

var actual = sut.invoke(new SpecimenFactory(new Context(Configuration.configure())), new CustomizationContext(List.of(), Map.of(), true));

Expand All @@ -67,7 +67,7 @@ void usingConstructorIsRecursive() throws NoSuchMethodException {
@Test
@DisplayName("customized arguments are only used for the top level object (no nested objects)")
void constructorArgumentsAreUsedOnce() throws NoSuchMethodException {
var sut = Constructor.create(TestObjectWithConstructedField.class.getConstructor(int.class, TestObjectWithGenericConstructor.class));
var sut = ConstructorInstantiator.create(TestObjectWithConstructedField.class.getConstructor(int.class, TestObjectWithGenericConstructor.class));

// use arg0, because .class files do not store formal parameter names by default
var actual = sut.invoke(new SpecimenFactory(new Context(Configuration.configure())), new CustomizationContext(List.of(), Map.of("arg0", 2), true));
Expand All @@ -78,7 +78,7 @@ void constructorArgumentsAreUsedOnce() throws NoSuchMethodException {
@Test
@DisplayName("customized arguments are used for exclusion, too")
void ignoredConstructorArgsAreRespected() throws NoSuchMethodException {
var sut = Constructor.create(TestObjectWithConstructedField.class.getConstructor(int.class, TestObjectWithGenericConstructor.class));
var sut = ConstructorInstantiator.create(TestObjectWithConstructedField.class.getConstructor(int.class, TestObjectWithGenericConstructor.class));

// use arg0, because .class files do not store formal parameter names by default
var actual = sut.invoke(new SpecimenFactory(new Context(Configuration.configure())), new CustomizationContext(List.of("arg0"), Map.of(), true));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
import static com.github.nylle.javafixture.SpecimenType.fromClass;
import static org.assertj.core.api.Assertions.assertThat;

class FactoryMethodTest {
class FactoryMethodInstantiatorTest {

@Test
@DisplayName("returns instance of class using factory method without arguments")
void factoryMethodWithoutArgument() throws NoSuchMethodException {
var sut = FactoryMethod.<FactoryMethodWithoutArgument>create(FactoryMethodWithoutArgument.class.getDeclaredMethod("factoryMethod"), fromClass(FactoryMethodWithoutArgument.class));
var sut = FactoryMethodInstantiator.<FactoryMethodWithoutArgument>create(FactoryMethodWithoutArgument.class.getDeclaredMethod("factoryMethod"), fromClass(FactoryMethodWithoutArgument.class));

var actual = sut.invoke(new SpecimenFactory(new Context(Configuration.configure())), noContext());

Expand All @@ -33,7 +33,7 @@ void factoryMethodWithoutArgument() throws NoSuchMethodException {
@Test
@DisplayName("returns instance of class using factory method with arguments")
void returnsInstanceOfClassUsingFactoryMethodWithArguments() throws NoSuchMethodException {
var sut = FactoryMethod.<FactoryMethodWithArgument>create(FactoryMethodWithArgument.class.getDeclaredMethod("factoryMethod", int.class), fromClass(FactoryMethodWithArgument.class));
var sut = FactoryMethodInstantiator.<FactoryMethodWithArgument>create(FactoryMethodWithArgument.class.getDeclaredMethod("factoryMethod", int.class), fromClass(FactoryMethodWithArgument.class));

var actual = sut.invoke(new SpecimenFactory(new Context(Configuration.configure())), noContext());

Expand All @@ -43,7 +43,7 @@ void returnsInstanceOfClassUsingFactoryMethodWithArguments() throws NoSuchMethod
@Test
@DisplayName("returns instance of class using factory method with generic argument")
void factoryMethodWithGenericArgument() throws NoSuchMethodException {
var sut = FactoryMethod.create(FactoryMethodWithGenericArgument.class.getDeclaredMethod("factoryMethod", Object.class), new SpecimenType<FactoryMethodWithGenericArgument<Integer>>() {});
var sut = FactoryMethodInstantiator.create(FactoryMethodWithGenericArgument.class.getDeclaredMethod("factoryMethod", Object.class), new SpecimenType<FactoryMethodWithGenericArgument<Integer>>() {});

var actual = sut.invoke(new SpecimenFactory(new Context(Configuration.configure())), noContext());

Expand All @@ -53,7 +53,7 @@ void factoryMethodWithGenericArgument() throws NoSuchMethodException {
@Test
@DisplayName("returns instance of abstract class using factory method without arguments")
void returnsInstanceOfAbstractClassUsingFactoryMethod() throws NoSuchMethodException {
var sut = FactoryMethod.create(Charset.class.getDeclaredMethod("defaultCharset"), new SpecimenType<Charset>() {});
var sut = FactoryMethodInstantiator.create(Charset.class.getDeclaredMethod("defaultCharset"), new SpecimenType<Charset>() {});

var actual = sut.invoke(new SpecimenFactory(new Context(Configuration.configure())), noContext());

Expand All @@ -63,7 +63,7 @@ void returnsInstanceOfAbstractClassUsingFactoryMethod() throws NoSuchMethodExcep
@Test
@DisplayName("returns instance of Optional using factory method with arguments")
void createOptionalWithArgument() throws NoSuchMethodException {
var sut = FactoryMethod.create(Optional.class.getDeclaredMethod("of", Object.class), new SpecimenType<Optional<String>>() {});
var sut = FactoryMethodInstantiator.create(Optional.class.getDeclaredMethod("of", Object.class), new SpecimenType<Optional<String>>() {});

var actual = sut.invoke(new SpecimenFactory(new Context(Configuration.configure())), noContext());

Expand All @@ -75,7 +75,7 @@ void createOptionalWithArgument() throws NoSuchMethodException {
@Test
@DisplayName("returns instance of Optional using factory method without arguments")
void createOptionalWithoutArgument() throws NoSuchMethodException {
var sut = FactoryMethod.create(Optional.class.getDeclaredMethod("empty"), new SpecimenType<Optional<String>>() {});
var sut = FactoryMethodInstantiator.create(Optional.class.getDeclaredMethod("empty"), new SpecimenType<Optional<String>>() {});

var actual = sut.invoke(new SpecimenFactory(new Context(Configuration.configure())), noContext());

Expand All @@ -86,7 +86,7 @@ void createOptionalWithoutArgument() throws NoSuchMethodException {
@Test
@DisplayName("returns instance of generic class using generic method without arguments")
void genericNoArgumentFactoryMethod() throws NoSuchMethodException {
var sut = FactoryMethod.create(GenericClassWithFactoryMethodWithoutArgument.class.getDeclaredMethod("factoryMethod"), new SpecimenType<GenericClassWithFactoryMethodWithoutArgument<Integer>>() {});
var sut = FactoryMethodInstantiator.create(GenericClassWithFactoryMethodWithoutArgument.class.getDeclaredMethod("factoryMethod"), new SpecimenType<GenericClassWithFactoryMethodWithoutArgument<Integer>>() {});

var actual = sut.invoke(new SpecimenFactory(new Context(Configuration.configure())), noContext());

Expand Down

0 comments on commit 87937b3

Please sign in to comment.