From 575c13e2c57c1e2c5d3d02de150afb6c108b5ca4 Mon Sep 17 00:00:00 2001 From: jbock Date: Sun, 10 Dec 2023 22:16:44 +0100 Subject: [PATCH] ISSUES-36 add withMocks method to factory --- .../processor/writing/ComponentImpl.java | 33 +-------------- .../simple/processor/writing/FactoryImpl.java | 40 +++++++++++++++---- .../jbock/simple/processor/FactoryTest.java | 20 +++++----- .../processor/JakartaQualifierTest.java | 10 ++--- .../simple/processor/JavaxQualifierTest.java | 4 +- .../jbock/simple/processor/PrimitiveTest.java | 4 +- .../jbock/simple/processor/QualifierTest.java | 4 +- 7 files changed, 56 insertions(+), 59 deletions(-) diff --git a/compiler/src/main/java/io/jbock/simple/processor/writing/ComponentImpl.java b/compiler/src/main/java/io/jbock/simple/processor/writing/ComponentImpl.java index 47515b5..21d0013 100644 --- a/compiler/src/main/java/io/jbock/simple/processor/writing/ComponentImpl.java +++ b/compiler/src/main/java/io/jbock/simple/processor/writing/ComponentImpl.java @@ -15,7 +15,6 @@ import io.jbock.simple.processor.binding.DependencyRequest; import io.jbock.simple.processor.binding.FactoryElement; import io.jbock.simple.processor.binding.Key; -import io.jbock.simple.processor.binding.ParameterBinding; import javax.annotation.processing.Generated; import javax.lang.model.element.Modifier; @@ -81,10 +80,7 @@ TypeSpec generate() { } component.factoryElement().ifPresent(factory -> { spec.addMethod(generateFactoryMethod(factory)); - spec.addType(factoryImpl.generate(factory, mockBuilder)); - if (component.mockBuilder()) { - spec.addMethod(generateMockBuilderMethodFactory()); - } + spec.addType(factoryImpl.generate(factory, mockBuilder, mockBuilder2)); }); component.builderElement().ifPresent(builder -> { spec.addMethod(generateBuilderMethod(builder)); @@ -118,7 +114,7 @@ private MethodSpec generateFactoryMethod(FactoryElement factory) { MethodSpec.Builder spec = MethodSpec.methodBuilder(FACTORY_METHOD) .addModifiers(STATIC) .addModifiers(modifiers) - .returns(TypeName.get(factory.element().asType())); + .returns(factory.generatedClass()); if (component.mockBuilder()) { spec.addStatement("return new $T(null)", factory.generatedClass()); } else { @@ -175,31 +171,6 @@ MethodSpec generateMockBuilderMethod() { return method.build(); } - MethodSpec generateMockBuilderMethodFactory() { - MethodSpec.Builder method = MethodSpec.methodBuilder(MOCK_BUILDER_METHOD); - method.addModifiers(STATIC); - method.addJavadoc("Visible for testing. Do not call this method from production code."); - method.returns(mockBuilder2.getClassName()); - List constructorParameters = new ArrayList<>(); - for (NamedBinding namedBinding : sorted.values()) { - Binding b = namedBinding.binding(); - if (!(b instanceof ParameterBinding)) { - continue; - } - Key key = b.key(); - ParameterSpec param = names.apply(key); - method.addParameter(param); - constructorParameters.add(CodeBlock.of("$N", param)); - } - if (component.publicMockBuilder()) { - method.addModifiers(modifiers); - } - method.addStatement("return new $T($L)", - mockBuilder2.getClassName(), - constructorParameters.stream().collect(CodeBlock.joining(", "))); - return method.build(); - } - private List getFields() { List fields = new ArrayList<>(); for (NamedBinding namedBinding : sorted.values()) { diff --git a/compiler/src/main/java/io/jbock/simple/processor/writing/FactoryImpl.java b/compiler/src/main/java/io/jbock/simple/processor/writing/FactoryImpl.java index e12bf3c..d13eadd 100644 --- a/compiler/src/main/java/io/jbock/simple/processor/writing/FactoryImpl.java +++ b/compiler/src/main/java/io/jbock/simple/processor/writing/FactoryImpl.java @@ -41,15 +41,15 @@ private FactoryImpl( this.names = names; } - TypeSpec generate(FactoryElement factory, MockBuilder mockBuilder) { + TypeSpec generate(FactoryElement factory, MockBuilder mockBuilder, MockBuilder2 mockBuilder2) { if (component.mockBuilder()) { - return generateMock(factory, mockBuilder); + return generateMock(factory, mockBuilder, mockBuilder2); } else { return generateNoMock(factory); } } - private TypeSpec generateMock(FactoryElement factory, MockBuilder mockBuilder) { + private TypeSpec generateMock(FactoryElement factory, MockBuilder mockBuilder, MockBuilder2 mockBuilder2) { TypeSpec.Builder spec = TypeSpec.classBuilder(factory.generatedClass()); FieldSpec mockBuilderField = FieldSpec.builder(mockBuilder.getClassName(), "mockBuilder", FINAL).build(); spec.addField(mockBuilderField); @@ -59,6 +59,14 @@ private TypeSpec generateMock(FactoryElement factory, MockBuilder mockBuilder) { .addStatement("this.$N = $N", mockBuilderField, mockBuilderParam) .build()); ExecutableElement abstractMethod = factory.singleAbstractMethod(); + spec.addMethod(generateBuildMethod(abstractMethod, mockBuilderField)); + spec.addMethod(generateWithMocksMethod(mockBuilder2)); + spec.addModifiers(PUBLIC, STATIC, FINAL); + spec.addSuperinterface(factory.element().asType()); + return spec.build(); + } + + private MethodSpec generateBuildMethod(ExecutableElement abstractMethod, FieldSpec mockBuilderField) { MethodSpec.Builder method = MethodSpec.methodBuilder(abstractMethod.getSimpleName().toString()); method.addAnnotation(Override.class); method.addModifiers(abstractMethod.getModifiers().stream() @@ -82,12 +90,30 @@ private TypeSpec generateMock(FactoryElement factory, MockBuilder mockBuilder) { } } method.addParameters(parameters()); - spec.addModifiers(PRIVATE, STATIC, FINAL); - spec.addSuperinterface(factory.element().asType()); method.addStatement("return new $T($L)", component.generatedClass(), constructorParameters().stream() .collect(CodeBlock.joining(", "))); - spec.addMethod(method.build()); - return spec.build(); + return method.build(); + } + + private MethodSpec generateWithMocksMethod(MockBuilder2 mockBuilder2) { + MethodSpec.Builder method = MethodSpec.methodBuilder("withMocks"); + List constructorParameters = new ArrayList<>(); + for (NamedBinding namedBinding : sorted.values()) { + Binding b = namedBinding.binding(); + if (!(b instanceof ParameterBinding)) { + continue; + } + ParameterSpec param = names.apply(b.key()); + constructorParameters.add(CodeBlock.of("$N", param)); + } + if (component.publicMockBuilder()) { + method.addModifiers(PUBLIC); + } + method.addParameters(parameters()); + method.returns(mockBuilder2.getClassName()); + method.addStatement("return new $T($L)", mockBuilder2.getClassName(), + constructorParameters.stream().collect(CodeBlock.joining(", "))); + return method.build(); } private TypeSpec generateNoMock(FactoryElement factory) { diff --git a/compiler/src/test/java/io/jbock/simple/processor/FactoryTest.java b/compiler/src/test/java/io/jbock/simple/processor/FactoryTest.java index 4a5006c..34b2386 100644 --- a/compiler/src/test/java/io/jbock/simple/processor/FactoryTest.java +++ b/compiler/src/test/java/io/jbock/simple/processor/FactoryTest.java @@ -53,11 +53,11 @@ void noParameters() { " return a;", " }", "", - " static TestClass.AComponent.Factory factory() {", + " static Factory_Impl factory() {", " return new Factory_Impl(null);", " }", "", - " private static final class Factory_Impl implements TestClass.AComponent.Factory {", + " public static final class Factory_Impl implements TestClass.AComponent.Factory {", " @Override", " public TestClass.AComponent create() {", " TestClass.A a = this.mockBuilder != null && this.mockBuilder.a != null ? this.mockBuilder.a : new TestClass.A();", @@ -105,15 +105,19 @@ void factoryParameterIdentity() { " return s;", " }", "", - " public static TestClass.AComponent.Factory factory() {", + " public static Factory_Impl factory() {", " return new Factory_Impl(null);", " }", "", - " private static final class Factory_Impl implements TestClass.AComponent.Factory {", + " public static final class Factory_Impl implements TestClass.AComponent.Factory {", " @Override", " public TestClass.AComponent create(String s) {", " return new TestClass_AComponent_Impl(s);", " }", + "", + " MockBuilder2 withMocks(String s) {", + " return new MockBuilder2(s);", + " }", " }", "}"); } @@ -160,15 +164,11 @@ void factoryParameter() { " return a;", " }", "", - " static TestClass.AComponent.Factory factory() {", + " static Factory_Impl factory() {", " return new Factory_Impl(null);", " }", "", - " static MockBuilder2 mockBuilder(String s) {", - " return new MockBuilder2(s);", - " }", - "", - " private static final class Factory_Impl implements TestClass.AComponent.Factory {", + " public static final class Factory_Impl implements TestClass.AComponent.Factory {", " final MockBuilder mockBuilder;", "", " Factory_Impl(MockBuilder mockBuilder) {", diff --git a/compiler/src/test/java/io/jbock/simple/processor/JakartaQualifierTest.java b/compiler/src/test/java/io/jbock/simple/processor/JakartaQualifierTest.java index 28cac2c..c9a32f4 100644 --- a/compiler/src/test/java/io/jbock/simple/processor/JakartaQualifierTest.java +++ b/compiler/src/test/java/io/jbock/simple/processor/JakartaQualifierTest.java @@ -50,20 +50,20 @@ void qualifiedIdentity() { " return a;", " }", "", - " static TestClass.AComponent.Factory factory() {", + " static Factory_Impl factory() {", " return new Factory_Impl(null);", " }", "", - " private static final class Factory_Impl implements TestClass.AComponent.Factory {", + " public static final class Factory_Impl implements TestClass.AComponent.Factory {", " @Override", " public TestClass.AComponent create(String a, String b) {", " return new TestClass_AComponent_Impl(a);", " }", " }", "", - " static final class MockBuilder {", - " TestClass.AComponent.Factory build() {", - " return new Factory_Impl(this);", + " static final class MockBuilder2 {", + " TestClass.AComponent build() {", + " return new TestClass_AComponent_Impl(a);", " }", " }", "}"); diff --git a/compiler/src/test/java/io/jbock/simple/processor/JavaxQualifierTest.java b/compiler/src/test/java/io/jbock/simple/processor/JavaxQualifierTest.java index 094166c..bf13705 100644 --- a/compiler/src/test/java/io/jbock/simple/processor/JavaxQualifierTest.java +++ b/compiler/src/test/java/io/jbock/simple/processor/JavaxQualifierTest.java @@ -50,11 +50,11 @@ void qualifiedIdentity() { " return a;", " }", "", - " static TestClass.AComponent.Factory factory() {", + " static Factory_Impl factory() {", " return new Factory_Impl(null);", " }", "", - " private static final class Factory_Impl implements TestClass.AComponent.Factory {", + " public static final class Factory_Impl implements TestClass.AComponent.Factory {", " @Override", " public TestClass.AComponent create(String a, String b) {", " return new TestClass_AComponent_Impl(a);", diff --git a/compiler/src/test/java/io/jbock/simple/processor/PrimitiveTest.java b/compiler/src/test/java/io/jbock/simple/processor/PrimitiveTest.java index e5f21b8..f2a4e47 100644 --- a/compiler/src/test/java/io/jbock/simple/processor/PrimitiveTest.java +++ b/compiler/src/test/java/io/jbock/simple/processor/PrimitiveTest.java @@ -86,11 +86,11 @@ void providesPrimitive() { " return a;", " }", "", - " public static TestClass.AComponent.Factory factory() {", + " public static Factory_Impl factory() {", " return new Factory_Impl(null);", " }", "", - " private static final class Factory_Impl implements TestClass.AComponent.Factory {", + " public static final class Factory_Impl implements TestClass.AComponent.Factory {", " @Override", " public TestClass.AComponent create(int i) {", " int b = this.mockBuilder != null && this.mockBuilder.b_isSet ? this.mockBuilder.b : TestClass.AComponent.getB(i);", diff --git a/compiler/src/test/java/io/jbock/simple/processor/QualifierTest.java b/compiler/src/test/java/io/jbock/simple/processor/QualifierTest.java index c223259..719cf06 100644 --- a/compiler/src/test/java/io/jbock/simple/processor/QualifierTest.java +++ b/compiler/src/test/java/io/jbock/simple/processor/QualifierTest.java @@ -79,11 +79,11 @@ void qualifiedIdentity() { " return a;", " }", "", - " static TestClass.AComponent.Factory factory() {", + " static Factory_Impl factory() {", " return new Factory_Impl(null);", " }", "", - " private static final class Factory_Impl implements TestClass.AComponent.Factory {", + " public static final class Factory_Impl implements TestClass.AComponent.Factory {", " @Override", " public TestClass.AComponent create(String a, String b) {", " return new TestClass_AComponent_Impl(a);",