From d844f16b335c6fe4b2541c87ec8c363eede6cc96 Mon Sep 17 00:00:00 2001 From: jbock Date: Wed, 29 Nov 2023 17:38:30 +0100 Subject: [PATCH] ISSUES-4 add method mockBuilder --- .../simple/processor/util/UniqueNameSet.java | 8 ++ .../processor/writing/ComponentImpl.java | 95 +++++++++---------- .../simple/processor/writing/Generator.java | 1 + .../simple/processor/writing/MockBuilder.java | 47 ++++++++- .../jbock/simple/processor/BuilderTest.java | 18 ++-- .../jbock/simple/processor/FactoryTest.java | 14 +-- .../simple/processor/JakartaInjectTest.java | 6 +- .../processor/JakartaQualifierTest.java | 10 +- .../simple/processor/JavaxInjectTest.java | 6 +- .../simple/processor/JavaxQualifierTest.java | 4 +- .../jbock/simple/processor/PrimitiveTest.java | 9 +- .../processor/ProcessorComponentTest.java | 71 +++++++++++--- .../jbock/simple/processor/ProviderTest.java | 12 +-- .../jbock/simple/processor/ProvidesTest.java | 12 +-- .../jbock/simple/processor/QualifierTest.java | 4 +- .../simple/processor/StaticInjectionTest.java | 6 +- 16 files changed, 204 insertions(+), 119 deletions(-) diff --git a/compiler/src/main/java/io/jbock/simple/processor/util/UniqueNameSet.java b/compiler/src/main/java/io/jbock/simple/processor/util/UniqueNameSet.java index 481f727..f971063 100644 --- a/compiler/src/main/java/io/jbock/simple/processor/util/UniqueNameSet.java +++ b/compiler/src/main/java/io/jbock/simple/processor/util/UniqueNameSet.java @@ -34,4 +34,12 @@ public String getUniqueName(CharSequence base) { } return name; } + + /** + * Adds {@code name} without any modification to the name set. Has no effect if {@code name} is + * already present in the set. + */ + public void claim(CharSequence name) { + uniqueNames.add(name.toString()); + } } 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 c30459c..dc0aa36 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 @@ -22,7 +22,6 @@ import javax.lang.model.element.Modifier; import javax.lang.model.type.TypeMirror; import java.util.ArrayList; -import java.util.Collection; import java.util.List; import java.util.Map; import java.util.function.Function; @@ -89,17 +88,14 @@ TypeSpec generate() { if (component.factoryElement().isEmpty() && component.builderElement().isEmpty()) { spec.addMethod(generateCreateMethod()); spec.addMethod(generateMockCreateMethod()); - spec.addType(mockBuilder.generate()); } + spec.addType(mockBuilder.generate()); spec.addAnnotation(AnnotationSpec.builder(Generated.class) .addMember("value", CodeBlock.of("$S", SimpleComponentProcessor.class.getCanonicalName())) .addMember("comments", CodeBlock.of("$S", "https://github.com/jbock-java/simple-component")) .build()); spec.addModifiers(FINAL); - spec.addMethod(generateConstructor()); - if (sorted.values().stream().anyMatch(binding -> !(binding.binding() instanceof ParameterBinding))) { - spec.addMethod(generateAllParametersConstructor()); - } + spec.addMethod(generateAllParametersConstructor()); spec.addOriginatingElement(component.element()); return spec.build(); } @@ -128,33 +124,14 @@ private MethodSpec generateCreateMethod() { } MethodSpec generateMockCreateMethod() { - MethodSpec.Builder method = MethodSpec.methodBuilder("mockCreate"); - method.addModifiers(modifiers); + MethodSpec.Builder method = MethodSpec.methodBuilder("mockBuilder"); + method.addJavadoc("Visible for testing. Do not call this method from production code."); method.addStatement("return new $T()", mockBuilder.getClassName()); method.returns(mockBuilder.getClassName()); + method.addModifiers(STATIC); return method.build(); } - private MethodSpec generateConstructor() { - MethodSpec.Builder constructor = MethodSpec.constructorBuilder().addModifiers(PRIVATE); - for (NamedBinding namedBinding : sorted.values()) { - Binding b = namedBinding.binding(); - Key key = b.key(); - String name = namedBinding.name(); - if (namedBinding.isComponentRequest()) { - FieldSpec field = FieldSpec.builder(key.typeName(), name, PRIVATE, FINAL).build(); - constructor.addStatement("this.$N = $L", field, b.invocation(names)); - } else if (!(b instanceof ParameterBinding)) { - ParameterSpec param = names.apply(key); - constructor.addStatement("$T $N = $L", key.typeName(), param, b.invocation(names)); - } - if (b instanceof ParameterBinding) { - constructor.addParameter(names.apply(key)); - } - } - return constructor.build(); - } - private List getFields() { List fields = new ArrayList<>(); for (NamedBinding namedBinding : sorted.values()) { @@ -182,7 +159,6 @@ private MethodSpec generateAllParametersConstructor() { } private TypeSpec createFactoryImpl(FactoryElement factory) { - Collection parameterBindings = component.parameterBindings(); TypeSpec.Builder spec = TypeSpec.classBuilder(factory.generatedClass()); spec.addModifiers(PRIVATE, STATIC, FINAL); spec.addSuperinterface(factory.element().asType()); @@ -192,41 +168,62 @@ private TypeSpec createFactoryImpl(FactoryElement factory) { method.addModifiers(abstractMethod.getModifiers().stream() .filter(m -> m == PUBLIC || m == PROTECTED).collect(Collectors.toList())); method.returns(TypeName.get(component.element().asType())); - method.addStatement("return new $T($L)", component.generatedClass(), parameterBindings.stream() - .map(b -> CodeBlock.of("$N", names.apply(b.key()))) - .collect(CodeBlock.joining(", "))); - for (ParameterBinding b : parameterBindings) { - method.addParameter(names.apply(b.key())); + List constructorParameters = new ArrayList<>(); + for (NamedBinding namedBinding : sorted.values()) { + Binding b = namedBinding.binding(); + Key key = b.key(); + CodeBlock invocation = b.invocation(names); + ParameterSpec param = names.apply(key); + if (namedBinding.isComponentRequest()) { + constructorParameters.add(CodeBlock.of("$N", names.apply(key))); + } + if (b instanceof ParameterBinding) { + method.addParameter(names.apply(b.key())); + } else { + method.addStatement("$T $N = $L", key.typeName(), param, invocation); + } } + method.addStatement("return new $T($L)", component.generatedClass(), constructorParameters.stream() + .collect(CodeBlock.joining(", "))); spec.addMethod(method.build()); return spec.build(); } private TypeSpec createBuilderImpl(BuilderElement builder) { - Collection parameterBindings = component.parameterBindings(); TypeMirror builderType = builder.element().asType(); TypeSpec.Builder spec = TypeSpec.classBuilder(builder.generatedClass()); - for (ParameterBinding b : parameterBindings) { - spec.addField(FieldSpec.builder(b.key().typeName(), names.apply(b.key()).name).build()); - MethodSpec.Builder setterMethod = MethodSpec.methodBuilder(b.element().getSimpleName().toString()); - setterMethod.addAnnotation(Override.class); - setterMethod.addParameter(names.apply(b.key())); - setterMethod.addStatement("this.$N = $N", names.apply(b.key()), names.apply(b.key())); - setterMethod.addStatement("return this"); - setterMethod.returns(TypeName.get(builderType)); - setterMethod.addModifiers(b.element().getModifiers().stream() - .filter(m -> m == PUBLIC || m == PROTECTED).collect(Collectors.toList())); - spec.addMethod(setterMethod.build()); + MethodSpec.Builder buildMethod = MethodSpec.methodBuilder(builder.buildMethod().getSimpleName().toString()); + List constructorParameters = new ArrayList<>(); + for (NamedBinding namedBinding : sorted.values()) { + Binding b = namedBinding.binding(); + Key key = b.key(); + CodeBlock invocation = b.invocation(names); + ParameterSpec param = names.apply(key); + if (namedBinding.isComponentRequest()) { + constructorParameters.add(CodeBlock.of("$N", names.apply(key))); + } + if (b instanceof ParameterBinding) { + spec.addField(FieldSpec.builder(b.key().typeName(), names.apply(b.key()).name).build()); + MethodSpec.Builder setterMethod = MethodSpec.methodBuilder(b.element().getSimpleName().toString()); + setterMethod.addAnnotation(Override.class); + setterMethod.addParameter(names.apply(b.key())); + setterMethod.addStatement("this.$N = $N", names.apply(b.key()), names.apply(b.key())); + setterMethod.addStatement("return this"); + setterMethod.returns(TypeName.get(builderType)); + setterMethod.addModifiers(b.element().getModifiers().stream() + .filter(m -> m == PUBLIC || m == PROTECTED).collect(Collectors.toList())); + spec.addMethod(setterMethod.build()); + } else { + buildMethod.addStatement("$T $N = $L", key.typeName(), param, invocation); + } } spec.addModifiers(PRIVATE, STATIC, FINAL); spec.addSuperinterface(builderType); - MethodSpec.Builder buildMethod = MethodSpec.methodBuilder(builder.buildMethod().getSimpleName().toString()); buildMethod.addAnnotation(Override.class); buildMethod.addModifiers(builder.buildMethod().getModifiers().stream() .filter(m -> m == PUBLIC || m == PROTECTED).collect(Collectors.toList())); buildMethod.returns(TypeName.get(component.element().asType())); - buildMethod.addStatement("return new $T($L)", component.generatedClass(), parameterBindings.stream() - .map(b -> CodeBlock.of("$N", names.apply(b.key()))) + buildMethod.addStatement("return new $T($L)", component.generatedClass(), constructorParameters.stream() .collect(CodeBlock.joining(", "))); spec.addMethod(buildMethod.build()); return spec.build(); diff --git a/compiler/src/main/java/io/jbock/simple/processor/writing/Generator.java b/compiler/src/main/java/io/jbock/simple/processor/writing/Generator.java index 0ec6732..9ec216d 100644 --- a/compiler/src/main/java/io/jbock/simple/processor/writing/Generator.java +++ b/compiler/src/main/java/io/jbock/simple/processor/writing/Generator.java @@ -35,6 +35,7 @@ public TypeSpec generate(List bindings) { private Map addNames(List bindings) { UniqueNameSet uniqueNameSet = new UniqueNameSet(); + uniqueNameSet.claim("build"); Map result = new LinkedHashMap<>(); for (Binding b : bindings) { String name = uniqueNameSet.getUniqueName(validJavaName(b.suggestedVariableName())); diff --git a/compiler/src/main/java/io/jbock/simple/processor/writing/MockBuilder.java b/compiler/src/main/java/io/jbock/simple/processor/writing/MockBuilder.java index 8ceb928..3ae4265 100644 --- a/compiler/src/main/java/io/jbock/simple/processor/writing/MockBuilder.java +++ b/compiler/src/main/java/io/jbock/simple/processor/writing/MockBuilder.java @@ -2,6 +2,7 @@ import io.jbock.javapoet.ClassName; import io.jbock.javapoet.CodeBlock; +import io.jbock.javapoet.FieldSpec; import io.jbock.javapoet.MethodSpec; import io.jbock.javapoet.ParameterSpec; import io.jbock.javapoet.TypeName; @@ -10,6 +11,7 @@ import io.jbock.simple.processor.binding.Binding; import io.jbock.simple.processor.binding.ComponentElement; import io.jbock.simple.processor.binding.Key; +import io.jbock.simple.processor.binding.ParameterBinding; import javax.lang.model.element.Modifier; import java.util.ArrayList; @@ -18,6 +20,7 @@ import java.util.function.Function; import static javax.lang.model.element.Modifier.FINAL; +import static javax.lang.model.element.Modifier.PRIVATE; import static javax.lang.model.element.Modifier.PUBLIC; import static javax.lang.model.element.Modifier.STATIC; @@ -28,7 +31,7 @@ public class MockBuilder { private final Function names; private final Modifier[] modifiers; - public MockBuilder( + MockBuilder( ComponentElement component, Map sorted, Function names) { @@ -44,6 +47,8 @@ TypeSpec generate() { .addModifiers(modifiers) .addModifiers(STATIC, FINAL); spec.addMethod(buildMethod()); + spec.addFields(getFields()); + spec.addMethods(getMethods()); return spec.build(); } @@ -62,7 +67,14 @@ private MethodSpec buildMethod() { if (namedBinding.isComponentRequest()) { constructorParameters.add(CodeBlock.of("$N", names.apply(key))); } - method.addStatement("$T $N = $L", key.typeName(), param, invocation); + if (namedBinding.binding() instanceof ParameterBinding) { + method.addParameter(names.apply(b.key())); + } else if (!key.typeName().isPrimitive()) { + method.addStatement("$1T $2N = this.$2N != null ? this.$2N : $3L", key.typeName(), param, invocation); + } else { + // TODO allow mocking primitives + method.addStatement("$T $N = $L", key.typeName(), param, invocation); + } } return method .addModifiers(modifiers) @@ -73,6 +85,37 @@ private MethodSpec buildMethod() { .build(); } + private List getFields() { + List fields = new ArrayList<>(); + for (NamedBinding namedBinding : sorted.values()) { + if (namedBinding.binding() instanceof ParameterBinding) { + continue; + } + TypeName type = namedBinding.binding().key().typeName(); + FieldSpec field = FieldSpec.builder(type, namedBinding.name(), PRIVATE).build(); + fields.add(field); + } + return fields; + } + + private List getMethods() { + List methods = new ArrayList<>(); + for (NamedBinding namedBinding : sorted.values()) { + if (namedBinding.binding() instanceof ParameterBinding) { + continue; + } + Binding b = namedBinding.binding(); + Key key = b.key(); + ParameterSpec param = names.apply(key); + MethodSpec method = MethodSpec.methodBuilder(param.name) + .addModifiers(modifiers) + .addParameter(param) + .addStatement("this.$1N = $1N", param) + .build(); + methods.add(method); + } + return methods; + } public static final class Factory { private final ComponentElement component; diff --git a/compiler/src/test/java/io/jbock/simple/processor/BuilderTest.java b/compiler/src/test/java/io/jbock/simple/processor/BuilderTest.java index 24985fe..5b483af 100644 --- a/compiler/src/test/java/io/jbock/simple/processor/BuilderTest.java +++ b/compiler/src/test/java/io/jbock/simple/processor/BuilderTest.java @@ -44,8 +44,8 @@ void noParameters() { "final class TestClass_AComponent_Impl implements TestClass.AComponent {", " private final TestClass.A a;", "", - " private TestClass_AComponent_Impl() {", - " this.a = new TestClass.A();", + " private TestClass_AComponent_Impl(TestClass.A a) {", + " this.a = a;", " }", "", " @Override", @@ -60,14 +60,15 @@ void noParameters() { " private static final class Builder_Impl implements TestClass.AComponent.Builder {", " @Override", " public TestClass.AComponent build() {", - " return new TestClass_AComponent_Impl();", + " TestClass.A a = new TestClass.A();", + " return new TestClass_AComponent_Impl(a);", " }", " }", "}"); } @Test - void factoryParameterIdentity() { + void builderParameterIdentity() { JavaFileObject component = forSourceLines("test.TestClass", "package test;", "", @@ -128,7 +129,7 @@ void factoryParameterIdentity() { @Test - void factoryParameter() { + void builderParameter() { JavaFileObject component = forSourceLines("test.TestClass", "package test;", "", @@ -161,8 +162,8 @@ void factoryParameter() { "public final class TestClass_AComponent_Impl implements TestClass.AComponent {", " private final TestClass.A a;", "", - " private TestClass_AComponent_Impl(String s) {", - " this.a = new TestClass.A(s);", + " private TestClass_AComponent_Impl(TestClass.A a) {", + " this.a = a;", " }", "", " @Override", @@ -185,7 +186,8 @@ void factoryParameter() { "", " @Override", " public TestClass.AComponent build() {", - " return new TestClass_AComponent_Impl(s);", + " TestClass.A a = new TestClass.A(s);", + " return new TestClass_AComponent_Impl(a);", " }", " }", "}"); 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 dac5fca..dc742e5 100644 --- a/compiler/src/test/java/io/jbock/simple/processor/FactoryTest.java +++ b/compiler/src/test/java/io/jbock/simple/processor/FactoryTest.java @@ -44,8 +44,8 @@ void noParameters() { "final class TestClass_AComponent_Impl implements TestClass.AComponent {", " private final TestClass.A a;", "", - " private TestClass_AComponent_Impl() {", - " this.a = new TestClass.A();", + " private TestClass_AComponent_Impl(TestClass.A a) {", + " this.a = a;", " }", "", " @Override", @@ -60,7 +60,8 @@ void noParameters() { " private static final class Factory_Impl implements TestClass.AComponent.Factory {", " @Override", " public TestClass.AComponent create() {", - " return new TestClass_AComponent_Impl();", + " TestClass.A a = new TestClass.A();", + " return new TestClass_AComponent_Impl(a);", " }", " }", "}"); @@ -150,8 +151,8 @@ void factoryParameter() { "final class TestClass_AComponent_Impl implements TestClass.AComponent {", " private final TestClass.A a;", "", - " private TestClass_AComponent_Impl(String s) {", - " this.a = new TestClass.A(s);", + " private TestClass_AComponent_Impl(TestClass.A a) {", + " this.a = a;", " }", "", " @Override", @@ -166,7 +167,8 @@ void factoryParameter() { " private static final class Factory_Impl implements TestClass.AComponent.Factory {", " @Override", " public TestClass.AComponent create(String s) {", - " return new TestClass_AComponent_Impl(s);", + " TestClass.A a = new TestClass.A(s);", + " return new TestClass_AComponent_Impl(a);", " }", " }", "}"); diff --git a/compiler/src/test/java/io/jbock/simple/processor/JakartaInjectTest.java b/compiler/src/test/java/io/jbock/simple/processor/JakartaInjectTest.java index b68b7db..41dfdce 100644 --- a/compiler/src/test/java/io/jbock/simple/processor/JakartaInjectTest.java +++ b/compiler/src/test/java/io/jbock/simple/processor/JakartaInjectTest.java @@ -51,10 +51,8 @@ void staticMethodBindings() { "final class TestClass_AComponent_Impl implements TestClass.AComponent {", " private final TestClass.A a;", "", - " private TestClass_AComponent_Impl() {", - " TestClass.C c = TestClass.C.createC();", - " TestClass.B b = TestClass.B.createB(c);", - " this.a = new TestClass.A(b);", + " private TestClass_AComponent_Impl(TestClass.A a) {", + " this.a = a;", " }", "", " @Override", 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 e8d211d..0dd3496 100644 --- a/compiler/src/test/java/io/jbock/simple/processor/JakartaQualifierTest.java +++ b/compiler/src/test/java/io/jbock/simple/processor/JakartaQualifierTest.java @@ -41,7 +41,7 @@ void qualifiedIdentity() { "final class TestClass_AComponent_Impl implements TestClass.AComponent {", " private final String a;", "", - " private TestClass_AComponent_Impl(String a, String b) {", + " private TestClass_AComponent_Impl(String a) {", " this.a = a;", " }", "", @@ -57,7 +57,13 @@ void qualifiedIdentity() { " private static final class Factory_Impl implements TestClass.AComponent.Factory {", " @Override", " public TestClass.AComponent create(String a, String b) {", - " return new TestClass_AComponent_Impl(a, b);", + " return new TestClass_AComponent_Impl(a);", + " }", + " }", + "", + " static final class MockBuilder {", + " TestClass.AComponent build(String a, String b) {", + " return new TestClass_AComponent_Impl(a);", " }", " }", "}"); diff --git a/compiler/src/test/java/io/jbock/simple/processor/JavaxInjectTest.java b/compiler/src/test/java/io/jbock/simple/processor/JavaxInjectTest.java index 01ee16a..97ef01d 100644 --- a/compiler/src/test/java/io/jbock/simple/processor/JavaxInjectTest.java +++ b/compiler/src/test/java/io/jbock/simple/processor/JavaxInjectTest.java @@ -51,10 +51,8 @@ void staticMethodBindings() { "final class TestClass_AComponent_Impl implements TestClass.AComponent {", " private final TestClass.A a;", "", - " private TestClass_AComponent_Impl() {", - " TestClass.C c = TestClass.C.createC();", - " TestClass.B b = TestClass.B.createB(c);", - " this.a = new TestClass.A(b);", + " private TestClass_AComponent_Impl(TestClass.A a) {", + " this.a = a;", " }", "", " @Override", 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 1bba715..aa830d1 100644 --- a/compiler/src/test/java/io/jbock/simple/processor/JavaxQualifierTest.java +++ b/compiler/src/test/java/io/jbock/simple/processor/JavaxQualifierTest.java @@ -41,7 +41,7 @@ void qualifiedIdentity() { "final class TestClass_AComponent_Impl implements TestClass.AComponent {", " private final String a;", "", - " private TestClass_AComponent_Impl(String a, String b) {", + " private TestClass_AComponent_Impl(String a) {", " this.a = a;", " }", "", @@ -57,7 +57,7 @@ void qualifiedIdentity() { " private static final class Factory_Impl implements TestClass.AComponent.Factory {", " @Override", " public TestClass.AComponent create(String a, String b) {", - " return new TestClass_AComponent_Impl(a, 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 3668548..c53d569 100644 --- a/compiler/src/test/java/io/jbock/simple/processor/PrimitiveTest.java +++ b/compiler/src/test/java/io/jbock/simple/processor/PrimitiveTest.java @@ -39,8 +39,8 @@ void primitiveParameter() { assertThat(compilation).succeeded(); assertThat(compilation).generatedSourceFile("test.TestClass_AComponent_Impl") .containsLines( - " private TestClass_AComponent_Impl(int i) {", - " this.a = new TestClass.A(i);", + " private TestClass_AComponent_Impl(TestClass.A a) {", + " this.a = a;", " }"); } @@ -77,9 +77,8 @@ void providesPrimitive() { assertThat(compilation).succeeded(); assertThat(compilation).generatedSourceFile("test.TestClass_AComponent_Impl") .containsLines( - " private TestClass_AComponent_Impl(int i) {", - " int b = TestClass.AComponent.getB(i);", - " this.a = new TestClass.A(b);", + " private TestClass_AComponent_Impl(TestClass.A a) {", + " this.a = a;", " }"); } } diff --git a/compiler/src/test/java/io/jbock/simple/processor/ProcessorComponentTest.java b/compiler/src/test/java/io/jbock/simple/processor/ProcessorComponentTest.java index e894409..58dc382 100644 --- a/compiler/src/test/java/io/jbock/simple/processor/ProcessorComponentTest.java +++ b/compiler/src/test/java/io/jbock/simple/processor/ProcessorComponentTest.java @@ -51,10 +51,8 @@ void staticMethodBindings() { "public final class TestClass_AComponent_Impl implements TestClass.AComponent {", " private final TestClass.A a;", "", - " private TestClass_AComponent_Impl() {", - " TestClass.C c = TestClass.C.createC();", - " TestClass.B b = TestClass.B.createB(c);", - " this.a = new TestClass.A(b);", + " private TestClass_AComponent_Impl(TestClass.A a) {", + " this.a = a;", " }", "", " @Override", @@ -69,17 +67,29 @@ void staticMethodBindings() { " return new TestClass_AComponent_Impl(a);", " }", "", - " public MockBuilder mockCreate() {", + " static MockBuilder mockBuilder() {", " return new MockBuilder();", " }", "", " public static final class MockBuilder {", + " private TestClass.C c;", + " private TestClass.B b;", + " private TestClass.A a;", " public TestClass.AComponent build() {", - " TestClass.C c = TestClass.C.createC();", - " TestClass.B b = TestClass.B.createB(c);", - " TestClass.A a = new TestClass.A(b);", + " TestClass.C c = this.c != null ? this.c : TestClass.C.createC();", + " TestClass.B b = this.b != null ? this.b : TestClass.B.createB(c);", + " TestClass.A a = this.a != null ? this.a : new TestClass.A(b);", " return new TestClass_AComponent_Impl(a);", " }", + " public void c(TestClass.C c) {", + " this.c = c;", + " }", + " public void b(TestClass.B b) {", + " this.b = b;", + " }", + " public void a(TestClass.A a) {", + " this.a = a;", + " }", " }", "}"); } @@ -124,13 +134,6 @@ void dependencyDiamond() { "final class TestClass_AComponent_Impl implements TestClass.AComponent {", " private final TestClass.A a;", "", - " private TestClass_AComponent_Impl() {", - " TestClass.E e = new TestClass.E();", - " TestClass.C c = new TestClass.C(e);", - " TestClass.B b = new TestClass.B(e);", - " this.a = new TestClass.A(b, c);", - " }", - "", " private TestClass_AComponent_Impl(TestClass.A a) {", " this.a = a;", " }", @@ -147,6 +150,44 @@ void dependencyDiamond() { " TestClass.A a = new TestClass.A(b, c);", " return new TestClass_AComponent_Impl(a);", " }", + "", + " static MockBuilder mockBuilder() {", + " return new MockBuilder();", + " }", + "", + " static final class MockBuilder {", + " private TestClass.E e;", + "", + " private TestClass.C c;", + "", + " private TestClass.B b;", + "", + " private TestClass.A a;", + "", + " TestClass.AComponent build() {", + " TestClass.E e = this.e != null ? this.e : new TestClass.E();", + " TestClass.C c = this.c != null ? this.c : new TestClass.C(e);", + " TestClass.B b = this.b != null ? this.b : new TestClass.B(e);", + " TestClass.A a = this.a != null ? this.a : new TestClass.A(b, c);", + " return new TestClass_AComponent_Impl(a);", + " }", + "", + " void e(TestClass.E e) {", + " this.e = e;", + " }", + "", + " void c(TestClass.C c) {", + " this.c = c;", + " }", + "", + " void b(TestClass.B b) {", + " this.b = b;", + " }", + "", + " void a(TestClass.A a) {", + " this.a = a;", + " }", + " }", "}"); } diff --git a/compiler/src/test/java/io/jbock/simple/processor/ProviderTest.java b/compiler/src/test/java/io/jbock/simple/processor/ProviderTest.java index 9f33aed..af9201c 100644 --- a/compiler/src/test/java/io/jbock/simple/processor/ProviderTest.java +++ b/compiler/src/test/java/io/jbock/simple/processor/ProviderTest.java @@ -77,10 +77,8 @@ void qualifierSuccess() { "final class TestClass_AComponent_Impl implements TestClass.AComponent {", " private final TestClass.A a;", "", - " private TestClass_AComponent_Impl() {", - " Provider bProvider = () -> TestClass.B.createB();", - " TestClass.B b = TestClass.B.createB();", - " this.a = new TestClass.A(bProvider, b);", + " private TestClass_AComponent_Impl(TestClass.A a) {", + " this.a = a;", " }", "", " @Override", @@ -129,10 +127,8 @@ void providedParameter() { "final class TestClass_AComponent_Impl implements TestClass.AComponent {", " private final TestClass.A a;", "", - " private TestClass_AComponent_Impl(TestClass.B b) {", - " Provider bProvider = () -> b;", - " Provider bProvider2 = () -> TestClass.B.createB();", - " this.a = new TestClass.A(bProvider, bProvider2);", + " private TestClass_AComponent_Impl(TestClass.A a) {", + " this.a = a;", " }", "", " @Override", diff --git a/compiler/src/test/java/io/jbock/simple/processor/ProvidesTest.java b/compiler/src/test/java/io/jbock/simple/processor/ProvidesTest.java index d6f7f56..ce3a0de 100644 --- a/compiler/src/test/java/io/jbock/simple/processor/ProvidesTest.java +++ b/compiler/src/test/java/io/jbock/simple/processor/ProvidesTest.java @@ -46,10 +46,8 @@ void providesString() { "final class TestClass_AComponent_Impl implements TestClass.AComponent {", " private final TestClass.A a;", "", - " private TestClass_AComponent_Impl() {", - " TestClass.B b = new TestClass.B();", - " String string = TestClass.AComponent.getString(b);", - " this.a = new TestClass.A(string);", + " private TestClass_AComponent_Impl(TestClass.A a) {", + " this.a = a;", " }", "", " @Override", @@ -96,10 +94,8 @@ void providesFunction() { "final class TestClass_AComponent_Impl implements TestClass.AComponent {", " private final TestClass.A a;", "", - " private TestClass_AComponent_Impl() {", - " TestClass.B b = new TestClass.B();", - " Map> myMap = TestClass.AComponent.provideMyMap(b);", - " this.a = new TestClass.A(myMap);", + " private TestClass_AComponent_Impl(TestClass.A a) {", + " this.a = a;", " }", "", " @Override", 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 8f1b4e7..b490bc8 100644 --- a/compiler/src/test/java/io/jbock/simple/processor/QualifierTest.java +++ b/compiler/src/test/java/io/jbock/simple/processor/QualifierTest.java @@ -70,7 +70,7 @@ void qualifiedIdentity() { "final class TestClass_AComponent_Impl implements TestClass.AComponent {", " private final String a;", "", - " private TestClass_AComponent_Impl(String a, String b) {", + " private TestClass_AComponent_Impl(String a) {", " this.a = a;", " }", "", @@ -86,7 +86,7 @@ void qualifiedIdentity() { " private static final class Factory_Impl implements TestClass.AComponent.Factory {", " @Override", " public TestClass.AComponent create(String a, String b) {", - " return new TestClass_AComponent_Impl(a, b);", + " return new TestClass_AComponent_Impl(a);", " }", " }", "}"); diff --git a/compiler/src/test/java/io/jbock/simple/processor/StaticInjectionTest.java b/compiler/src/test/java/io/jbock/simple/processor/StaticInjectionTest.java index 81c3563..2606933 100644 --- a/compiler/src/test/java/io/jbock/simple/processor/StaticInjectionTest.java +++ b/compiler/src/test/java/io/jbock/simple/processor/StaticInjectionTest.java @@ -45,10 +45,8 @@ void clashResolvedByQualifiers() { "final class TestClass_AComponent_Impl implements TestClass.AComponent {", " private final TestClass.A a;", "", - " private TestClass_AComponent_Impl() {", - " TestClass.B b = TestClass.B.create1();", - " TestClass.B b2 = TestClass.B.create2();", - " this.a = new TestClass.A(b, b2);", + " private TestClass_AComponent_Impl(TestClass.A a) {", + " this.a = a;", " }", "", " @Override",