Skip to content

Commit

Permalink
ISSUES-36 add withMocks method to factory
Browse files Browse the repository at this point in the history
  • Loading branch information
h908714124 committed Dec 10, 2023
1 parent 383f520 commit 575c13e
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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<CodeBlock> 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<FieldSpec> getFields() {
List<FieldSpec> fields = new ArrayList<>();
for (NamedBinding namedBinding : sorted.values()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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()
Expand All @@ -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<CodeBlock> 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) {
Expand Down
20 changes: 10 additions & 10 deletions compiler/src/test/java/io/jbock/simple/processor/FactoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();",
Expand Down Expand Up @@ -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);",
" }",
" }",
"}");
}
Expand Down Expand Up @@ -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) {",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);",
" }",
" }",
"}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);",
Expand Down

0 comments on commit 575c13e

Please sign in to comment.