From 87937b3ae7936a9972ec24cc1ec9dcbbde5b3132 Mon Sep 17 00:00:00 2001 From: Jan Date: Sun, 8 Sep 2024 15:42:50 +0200 Subject: [PATCH] refactor: rename instantiators to reflect interface --- .../github/nylle/javafixture/SpecimenType.java | 6 +++--- .../{Builder.java => BuilderInstantiator.java} | 8 ++++---- ...tructor.java => ConstructorInstantiator.java} | 8 ++++---- ...ethod.java => FactoryMethodInstantiator.java} | 8 ++++---- ...derTest.java => BuilderInstantiatorTest.java} | 4 ++-- ...est.java => ConstructorInstantiatorTest.java} | 14 +++++++------- ...t.java => FactoryMethodInstantiatorTest.java} | 16 ++++++++-------- 7 files changed, 32 insertions(+), 32 deletions(-) rename src/main/java/com/github/nylle/javafixture/instantiation/{Builder.java => BuilderInstantiator.java} (87%) rename src/main/java/com/github/nylle/javafixture/instantiation/{Constructor.java => ConstructorInstantiator.java} (83%) rename src/main/java/com/github/nylle/javafixture/instantiation/{FactoryMethod.java => FactoryMethodInstantiator.java} (79%) rename src/test/java/com/github/nylle/javafixture/instantiation/{BuilderTest.java => BuilderInstantiatorTest.java} (84%) rename src/test/java/com/github/nylle/javafixture/instantiation/{ConstructorTest.java => ConstructorInstantiatorTest.java} (80%) rename src/test/java/com/github/nylle/javafixture/instantiation/{FactoryMethodTest.java => FactoryMethodInstantiatorTest.java} (73%) diff --git a/src/main/java/com/github/nylle/javafixture/SpecimenType.java b/src/main/java/com/github/nylle/javafixture/SpecimenType.java index e942805..a5d841c 100644 --- a/src/main/java/com/github/nylle/javafixture/SpecimenType.java +++ b/src/main/java/com/github/nylle/javafixture/SpecimenType.java @@ -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; @@ -226,12 +226,12 @@ public List getFactoryMethods() { .collect(Collectors.toList()); } - public List> findBuilders() { + public List> 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()); } diff --git a/src/main/java/com/github/nylle/javafixture/instantiation/Builder.java b/src/main/java/com/github/nylle/javafixture/instantiation/BuilderInstantiator.java similarity index 87% rename from src/main/java/com/github/nylle/javafixture/instantiation/Builder.java rename to src/main/java/com/github/nylle/javafixture/instantiation/BuilderInstantiator.java index faf237d..111142b 100644 --- a/src/main/java/com/github/nylle/javafixture/instantiation/Builder.java +++ b/src/main/java/com/github/nylle/javafixture/instantiation/BuilderInstantiator.java @@ -14,19 +14,19 @@ import static java.util.stream.Collectors.toList; -public class Builder implements Instantiator { +public class BuilderInstantiator implements Instantiator { 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 Builder create(Method builderMethodCandidate, SpecimenType targetType) { + public static BuilderInstantiator create(Method builderMethodCandidate, SpecimenType targetType) { return findBuildMethod(builderMethodCandidate.getReturnType(), targetType.asClass()) - .map(x -> new Builder(builderMethodCandidate, x)) + .map(x -> new BuilderInstantiator(builderMethodCandidate, x)) .orElse(null); } diff --git a/src/main/java/com/github/nylle/javafixture/instantiation/Constructor.java b/src/main/java/com/github/nylle/javafixture/instantiation/ConstructorInstantiator.java similarity index 83% rename from src/main/java/com/github/nylle/javafixture/instantiation/Constructor.java rename to src/main/java/com/github/nylle/javafixture/instantiation/ConstructorInstantiator.java index 01d142c..943bd02 100644 --- a/src/main/java/com/github/nylle/javafixture/instantiation/Constructor.java +++ b/src/main/java/com/github/nylle/javafixture/instantiation/ConstructorInstantiator.java @@ -11,16 +11,16 @@ import static java.util.Arrays.stream; -public class Constructor implements Instantiator { +public class ConstructorInstantiator implements Instantiator { private final java.lang.reflect.Constructor constructor; - private Constructor(java.lang.reflect.Constructor constructor) { + private ConstructorInstantiator(java.lang.reflect.Constructor constructor) { this.constructor = constructor; } - public static Constructor create(java.lang.reflect.Constructor constructor) { - return new Constructor<>(constructor); + public static ConstructorInstantiator create(java.lang.reflect.Constructor constructor) { + return new ConstructorInstantiator<>(constructor); } public T invoke(SpecimenFactory specimenFactory, CustomizationContext customizationContext) { diff --git a/src/main/java/com/github/nylle/javafixture/instantiation/FactoryMethod.java b/src/main/java/com/github/nylle/javafixture/instantiation/FactoryMethodInstantiator.java similarity index 79% rename from src/main/java/com/github/nylle/javafixture/instantiation/FactoryMethod.java rename to src/main/java/com/github/nylle/javafixture/instantiation/FactoryMethodInstantiator.java index b142286..69899e3 100644 --- a/src/main/java/com/github/nylle/javafixture/instantiation/FactoryMethod.java +++ b/src/main/java/com/github/nylle/javafixture/instantiation/FactoryMethodInstantiator.java @@ -9,18 +9,18 @@ import java.util.ArrayList; import java.util.List; -public class FactoryMethod implements Instantiator { +public class FactoryMethodInstantiator implements Instantiator { private final Method factoryMethod; private final SpecimenType targetType; - public FactoryMethod(Method factoryMethod, SpecimenType targetType) { + public FactoryMethodInstantiator(Method factoryMethod, SpecimenType targetType) { this.factoryMethod = factoryMethod; this.targetType = targetType; } - public static FactoryMethod create(Method factoryMethod, SpecimenType targetType) { - return new FactoryMethod<>(factoryMethod, targetType); + public static FactoryMethodInstantiator create(Method factoryMethod, SpecimenType targetType) { + return new FactoryMethodInstantiator<>(factoryMethod, targetType); } public T invoke(SpecimenFactory specimenFactory, CustomizationContext customizationContext) { diff --git a/src/test/java/com/github/nylle/javafixture/instantiation/BuilderTest.java b/src/test/java/com/github/nylle/javafixture/instantiation/BuilderInstantiatorTest.java similarity index 84% rename from src/test/java/com/github/nylle/javafixture/instantiation/BuilderTest.java rename to src/test/java/com/github/nylle/javafixture/instantiation/BuilderInstantiatorTest.java index 93afdfc..5472fa3 100644 --- a/src/test/java/com/github/nylle/javafixture/instantiation/BuilderTest.java +++ b/src/test/java/com/github/nylle/javafixture/instantiation/BuilderInstantiatorTest.java @@ -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()); diff --git a/src/test/java/com/github/nylle/javafixture/instantiation/ConstructorTest.java b/src/test/java/com/github/nylle/javafixture/instantiation/ConstructorInstantiatorTest.java similarity index 80% rename from src/test/java/com/github/nylle/javafixture/instantiation/ConstructorTest.java rename to src/test/java/com/github/nylle/javafixture/instantiation/ConstructorInstantiatorTest.java index 053f9fc..159211c 100644 --- a/src/test/java/com/github/nylle/javafixture/instantiation/ConstructorTest.java +++ b/src/test/java/com/github/nylle/javafixture/instantiation/ConstructorInstantiatorTest.java @@ -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)); @@ -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)); @@ -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)); @@ -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)); @@ -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)); @@ -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)); diff --git a/src/test/java/com/github/nylle/javafixture/instantiation/FactoryMethodTest.java b/src/test/java/com/github/nylle/javafixture/instantiation/FactoryMethodInstantiatorTest.java similarity index 73% rename from src/test/java/com/github/nylle/javafixture/instantiation/FactoryMethodTest.java rename to src/test/java/com/github/nylle/javafixture/instantiation/FactoryMethodInstantiatorTest.java index 0e354f8..267a716 100644 --- a/src/test/java/com/github/nylle/javafixture/instantiation/FactoryMethodTest.java +++ b/src/test/java/com/github/nylle/javafixture/instantiation/FactoryMethodInstantiatorTest.java @@ -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.create(FactoryMethodWithoutArgument.class.getDeclaredMethod("factoryMethod"), fromClass(FactoryMethodWithoutArgument.class)); + var sut = FactoryMethodInstantiator.create(FactoryMethodWithoutArgument.class.getDeclaredMethod("factoryMethod"), fromClass(FactoryMethodWithoutArgument.class)); var actual = sut.invoke(new SpecimenFactory(new Context(Configuration.configure())), noContext()); @@ -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.create(FactoryMethodWithArgument.class.getDeclaredMethod("factoryMethod", int.class), fromClass(FactoryMethodWithArgument.class)); + var sut = FactoryMethodInstantiator.create(FactoryMethodWithArgument.class.getDeclaredMethod("factoryMethod", int.class), fromClass(FactoryMethodWithArgument.class)); var actual = sut.invoke(new SpecimenFactory(new Context(Configuration.configure())), noContext()); @@ -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>() {}); + var sut = FactoryMethodInstantiator.create(FactoryMethodWithGenericArgument.class.getDeclaredMethod("factoryMethod", Object.class), new SpecimenType>() {}); var actual = sut.invoke(new SpecimenFactory(new Context(Configuration.configure())), noContext()); @@ -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() {}); + var sut = FactoryMethodInstantiator.create(Charset.class.getDeclaredMethod("defaultCharset"), new SpecimenType() {}); var actual = sut.invoke(new SpecimenFactory(new Context(Configuration.configure())), noContext()); @@ -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>() {}); + var sut = FactoryMethodInstantiator.create(Optional.class.getDeclaredMethod("of", Object.class), new SpecimenType>() {}); var actual = sut.invoke(new SpecimenFactory(new Context(Configuration.configure())), noContext()); @@ -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>() {}); + var sut = FactoryMethodInstantiator.create(Optional.class.getDeclaredMethod("empty"), new SpecimenType>() {}); var actual = sut.invoke(new SpecimenFactory(new Context(Configuration.configure())), noContext()); @@ -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>() {}); + var sut = FactoryMethodInstantiator.create(GenericClassWithFactoryMethodWithoutArgument.class.getDeclaredMethod("factoryMethod"), new SpecimenType>() {}); var actual = sut.invoke(new SpecimenFactory(new Context(Configuration.configure())), noContext());