From 58536001271c43f84233848751eee7c361f0c5b9 Mon Sep 17 00:00:00 2001 From: Mathieu Boretti Date: Fri, 22 May 2020 16:46:12 +0200 Subject: [PATCH] Issue #319 - Fix format --- .../fields/FieldDSLMethodBuilder.java | 264 +++++++++--------- .../fields/FieldDSLMethodBuilderTest.java | 166 +++++------ 2 files changed, 215 insertions(+), 215 deletions(-) diff --git a/src/main/java/ch/powerunit/extensions/matchers/provideprocessor/fields/FieldDSLMethodBuilder.java b/src/main/java/ch/powerunit/extensions/matchers/provideprocessor/fields/FieldDSLMethodBuilder.java index 3a559af8a..e48e69c8d 100644 --- a/src/main/java/ch/powerunit/extensions/matchers/provideprocessor/fields/FieldDSLMethodBuilder.java +++ b/src/main/java/ch/powerunit/extensions/matchers/provideprocessor/fields/FieldDSLMethodBuilder.java @@ -1,132 +1,132 @@ -/** - * Powerunit - A JDK1.8 test framework - * Copyright (C) 2014 Mathieu Boretti. - * - * This file is part of Powerunit - * - * Powerunit is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Powerunit is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Powerunit. If not, see . - */ -package ch.powerunit.extensions.matchers.provideprocessor.fields; - -import java.util.Arrays; -import java.util.Optional; - -import ch.powerunit.extensions.matchers.provideprocessor.fields.lang.BuilderDeclaration; -import ch.powerunit.extensions.matchers.provideprocessor.fields.lang.BuilderImplementation; -import ch.powerunit.extensions.matchers.provideprocessor.fields.lang.BuilderJavadoc; - -public class FieldDSLMethodBuilder { - - private static final String MATCHERS = "org.hamcrest.Matchers"; - - public static BuilderDeclaration of(AbstractFieldDescription fieldDescription) { - return new Builder(fieldDescription); - } - - public static class Builder implements BuilderDeclaration, BuilderJavadoc, BuilderImplementation { - private final AbstractFieldDescription fieldDescription; - private String declaration; - private String javadoc; - - private Builder(AbstractFieldDescription fieldDescription) { - this.fieldDescription = fieldDescription; - } - - @Override - public BuilderImplementation withJavaDoc(Optional addToDescription, Optional param, - Optional see) { - javadoc = getJavaDocFor(fieldDescription, addToDescription, param, see); - return this; - } - - @Override - public FieldDSLMethod havingDefault(String innerMatcher) { - return new FieldDSLMethod(buildDefaultDsl(fieldDescription, javadoc, declaration, innerMatcher), ""); - } - - @Override - public FieldDSLMethod havingImplementation(String body) { - return new FieldDSLMethod(buildDsl(javadoc, declaration), buildImplementation(declaration, body)); - } - - @Override - public BuilderJavadoc withExplicitDeclaration(String declaration) { - this.declaration = declaration; - return this; - } - - @Override - public BuilderJavadoc withGenericDeclaration(String generic, String postFix, String arguments) { - this.declaration = generic + " " + String.format("%1$s %2$s%3$s(%4$s)", - fieldDescription.getDefaultReturnMethod(), fieldDescription.getFieldName(), postFix, arguments); - return this; - } - - @Override - public FieldDSLMethod withExplicitDeclarationJavadocAndImplementation(String declaration, - String addToDescription, String body) { - return withExplicitDeclaration(declaration).withJavaDoc(addToDescription).havingImplementation(body); - } - - @Override - public FieldDSLMethod withJavaDocAndDefault(String addToDescription, String innerMatcher) { - return withJavaDoc(addToDescription).havingDefault(innerMatcher); - } - - @Override - public FieldDSLMethod withSuffixDeclarationJavadocAndDefault(String declaration, String addToDescription, - String innerMatcher) { - return withSuffixDeclaration(declaration).withJavaDocAndDefault(addToDescription, innerMatcher); - } - - } - - public static String getJavaDocFor(AbstractFieldDescription fieldDescription, Optional addToDescription, - Optional param, Optional see) { - String linkToAccessor = "{@link " + fieldDescription.getFullyQualifiedNameEnclosingClassOfField() + "#" - + fieldDescription.getFieldAccessor() + " This field is accessed by using this approach}."; - StringBuilder sb = new StringBuilder(); - sb.append("/**\n * Add a validation on the field `").append(fieldDescription.getFieldName()).append("`"); - addToDescription.ifPresent(t -> sb.append(" ").append(t)); - sb.append(".\n *

").append("\n *\n * ").append(linkToAccessor).append("\n *

\n"); - sb.append( - " * In case method specifing a matcher on a fields are used several times, only the last setted matcher will be used. \n"); - sb.append( - " * When several control must be done on a single field, hamcrest itself provides a way to combine several matchers (See for instance {@link " - + MATCHERS + "#both(org.hamcrest.Matcher)}.\n"); - sb.append(" *\n"); - param.ifPresent( - t -> Arrays.stream(t.split("\n")).forEach(l -> sb.append(" * @param ").append(l).append(".\n"))); - sb.append(" * @return the DSL to continue the construction of the matcher.\n"); - see.ifPresent(t -> sb.append(" * @see ").append(t).append("\n")); - sb.append(" */"); - return sb.toString(); - } - - public static String buildImplementation(String declaration, String body) { - return String.format("@Override\npublic %1$s {\n %2$s\n}\n", declaration, body.replaceAll("\\R", "\n" + " ")); - } - - public static String buildDsl(String javadoc, String declaration) { - return String.format("%1$s\n%2$s;\n", javadoc.replaceAll("\\R", "\n"), declaration); - } - - public static String buildDefaultDsl(AbstractFieldDescription fieldDescription, String javadoc, String declaration, - String innerMatcher) { - return String.format("%1$s\ndefault %2$s{\n return %3$s(%4$s);\n}", javadoc.replaceAll("\\R", "\n"), - declaration, fieldDescription.getFieldName(), innerMatcher); - - } - -} +/** + * Powerunit - A JDK1.8 test framework + * Copyright (C) 2014 Mathieu Boretti. + * + * This file is part of Powerunit + * + * Powerunit is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Powerunit is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Powerunit. If not, see . + */ +package ch.powerunit.extensions.matchers.provideprocessor.fields; + +import static java.util.Arrays.stream; +import static java.util.stream.Collectors.joining; + +import java.util.Optional; + +import ch.powerunit.extensions.matchers.provideprocessor.fields.lang.BuilderDeclaration; +import ch.powerunit.extensions.matchers.provideprocessor.fields.lang.BuilderImplementation; +import ch.powerunit.extensions.matchers.provideprocessor.fields.lang.BuilderJavadoc; + +public class FieldDSLMethodBuilder { + + private static final String MATCHERS = "org.hamcrest.Matchers"; + + private static final String DEFAULT_JAVADOCFORMAT = "/**\n" + " * %1$s\n" + " *

\n" + " *\n" + " * %2$s\n" + + " *

\n" + + " * In case method specifing a matcher on a fields are used several times, only the last setted matcher will be used. \n" + + " * When several control must be done on a single field, hamcrest itself provides a way to combine several matchers.\n" + + " * (See for instance {@link " + MATCHERS + "#both(org.hamcrest.Matcher)}.\n" + " *\n" + "%3$s" + + " * @return the DSL to continue the construction of the matcher.\n" + "%4$s */"; + + public static BuilderDeclaration of(AbstractFieldDescription fieldDescription) { + return new Builder(fieldDescription); + } + + public static class Builder implements BuilderDeclaration, BuilderJavadoc, BuilderImplementation { + private final AbstractFieldDescription fieldDescription; + private String declaration; + private String javadoc; + + private Builder(AbstractFieldDescription fieldDescription) { + this.fieldDescription = fieldDescription; + } + + @Override + public BuilderImplementation withJavaDoc(Optional addToDescription, Optional param, + Optional see) { + javadoc = getJavaDocFor(fieldDescription, addToDescription, param, see); + return this; + } + + @Override + public FieldDSLMethod havingDefault(String innerMatcher) { + return new FieldDSLMethod(buildDefaultDsl(fieldDescription, javadoc, declaration, innerMatcher), ""); + } + + @Override + public FieldDSLMethod havingImplementation(String body) { + return new FieldDSLMethod(buildDsl(javadoc, declaration), buildImplementation(declaration, body)); + } + + @Override + public BuilderJavadoc withExplicitDeclaration(String declaration) { + this.declaration = declaration; + return this; + } + + @Override + public BuilderJavadoc withGenericDeclaration(String generic, String postFix, String arguments) { + this.declaration = generic + " " + String.format("%1$s %2$s%3$s(%4$s)", + fieldDescription.getDefaultReturnMethod(), fieldDescription.getFieldName(), postFix, arguments); + return this; + } + + @Override + public FieldDSLMethod withExplicitDeclarationJavadocAndImplementation(String declaration, + String addToDescription, String body) { + return withExplicitDeclaration(declaration).withJavaDoc(addToDescription).havingImplementation(body); + } + + @Override + public FieldDSLMethod withJavaDocAndDefault(String addToDescription, String innerMatcher) { + return withJavaDoc(addToDescription).havingDefault(innerMatcher); + } + + @Override + public FieldDSLMethod withSuffixDeclarationJavadocAndDefault(String declaration, String addToDescription, + String innerMatcher) { + return withSuffixDeclaration(declaration).withJavaDocAndDefault(addToDescription, innerMatcher); + } + + } + + public static String getJavaDocFor(AbstractFieldDescription fieldDescription, Optional addToDescription, + Optional param, Optional see) { + String linkToAccessor = String.format("{@link %1$s#%2$s This field is accessed by using this approach}.", + fieldDescription.getFullyQualifiedNameEnclosingClassOfField(), fieldDescription.getFieldAccessor()); + String title = String.format("Add a validation on the field `%1$s`%2$s.", fieldDescription.getFieldName(), + addToDescription.map(s -> " " + s).orElse("")); + String paramString = param + .map(t -> stream(t.split("\n")).map(l -> " * @param " + l + ".\n").collect(joining())) + .orElse(""); + String seeString = see.map(s -> " * @see " + s + "\n").orElse(""); + return String.format(DEFAULT_JAVADOCFORMAT, title, linkToAccessor, paramString, seeString); + } + + public static String buildImplementation(String declaration, String body) { + return String.format("@Override\npublic %1$s {\n %2$s\n}\n", declaration, body.replaceAll("\\R", "\n" + " ")); + } + + public static String buildDsl(String javadoc, String declaration) { + return String.format("%1$s\n%2$s;\n", javadoc.replaceAll("\\R", "\n"), declaration); + } + + public static String buildDefaultDsl(AbstractFieldDescription fieldDescription, String javadoc, String declaration, + String innerMatcher) { + return String.format("%1$s\ndefault %2$s{\n return %3$s(%4$s);\n}", javadoc.replaceAll("\\R", "\n"), + declaration, fieldDescription.getFieldName(), innerMatcher); + + } + +} diff --git a/src/test/java/ch/powerunit/extensions/matchers/provideprocessor/fields/FieldDSLMethodBuilderTest.java b/src/test/java/ch/powerunit/extensions/matchers/provideprocessor/fields/FieldDSLMethodBuilderTest.java index eb45e0d9a..ead8c28dc 100644 --- a/src/test/java/ch/powerunit/extensions/matchers/provideprocessor/fields/FieldDSLMethodBuilderTest.java +++ b/src/test/java/ch/powerunit/extensions/matchers/provideprocessor/fields/FieldDSLMethodBuilderTest.java @@ -1,83 +1,83 @@ -package ch.powerunit.extensions.matchers.provideprocessor.fields; - -import static org.mockito.Mockito.when; - -import java.util.Optional; - -import org.mockito.Mock; - -import ch.powerunit.Rule; -import ch.powerunit.Test; -import ch.powerunit.TestRule; -import ch.powerunit.TestSuite; - -public class FieldDSLMethodBuilderTest implements TestSuite { - @Rule - public final TestRule rules = mockitoRule().around(before(this::prepareMock)); - - @Mock - private AbstractFieldDescription fieldDescription; - - private void prepareMock() { - when(fieldDescription.getFieldName()).thenReturn("fn"); - when(fieldDescription.getFieldAccessor()).thenReturn("fa"); - when(fieldDescription.getFullyQualifiedNameEnclosingClassOfField()).thenReturn("fqnecof"); - when(fieldDescription.getFieldType()).thenReturn("ft"); - when(fieldDescription.getDefaultReturnMethod()).thenReturn("drm"); - } - - @Test - public void testBuildImplementationOneLine() { - assertThatBiFunction(FieldDSLMethodBuilder::buildImplementation, "name", "ligne1") - .is("@Override\npublic name {\n ligne1\n}\n"); - } - - @Test - public void testBuildImplementationTwoLine() { - assertThatBiFunction(FieldDSLMethodBuilder::buildImplementation, "name", "ligne1\nligne2") - .is("@Override\npublic name {\n ligne1\n ligne2\n}\n"); - } - - @Test - public void testBuildDsl() { - assertThatBiFunction(FieldDSLMethodBuilder::buildDsl, "javadoc", "declaration").is("javadoc\ndeclaration;\n"); - } - - @Test(fastFail = false) - public void testGetJavaDocFor() { - assertThat(FieldDSLMethodBuilder.getJavaDocFor(fieldDescription, Optional.empty(), Optional.empty(), - Optional.empty())).is( - "/**\n * Add a validation on the field `fn`.\n *

\n *\n * {@link fqnecof#fa This field is accessed by using this approach}.\n *

\n * In case method specifing a matcher on a fields are used several times, only the last setted matcher will be used. \n * When several control must be done on a single field, hamcrest itself provides a way to combine several matchers (See for instance {@link org.hamcrest.Matchers#both(org.hamcrest.Matcher)}.\n *\n * @return the DSL to continue the construction of the matcher.\n */"); - - assertThat(FieldDSLMethodBuilder.getJavaDocFor(fieldDescription, Optional.of("add"), Optional.empty(), - Optional.empty())).is( - "/**\n * Add a validation on the field `fn` add.\n *

\n *\n * {@link fqnecof#fa This field is accessed by using this approach}.\n *

\n * In case method specifing a matcher on a fields are used several times, only the last setted matcher will be used. \n * When several control must be done on a single field, hamcrest itself provides a way to combine several matchers (See for instance {@link org.hamcrest.Matchers#both(org.hamcrest.Matcher)}.\n *\n * @return the DSL to continue the construction of the matcher.\n */"); - - assertThat(FieldDSLMethodBuilder.getJavaDocFor(fieldDescription, Optional.empty(), Optional.of("p is p"), - Optional.empty())).is( - "/**\n * Add a validation on the field `fn`.\n *

\n *\n * {@link fqnecof#fa This field is accessed by using this approach}.\n *

\n * In case method specifing a matcher on a fields are used several times, only the last setted matcher will be used. \n * When several control must be done on a single field, hamcrest itself provides a way to combine several matchers (See for instance {@link org.hamcrest.Matchers#both(org.hamcrest.Matcher)}.\n *\n * @param p is p.\n * @return the DSL to continue the construction of the matcher.\n */"); - - assertThat(FieldDSLMethodBuilder.getJavaDocFor(fieldDescription, Optional.empty(), Optional.empty(), - Optional.of("other"))).is( - "/**\n * Add a validation on the field `fn`.\n *

\n *\n * {@link fqnecof#fa This field is accessed by using this approach}.\n *

\n * In case method specifing a matcher on a fields are used several times, only the last setted matcher will be used. \n * When several control must be done on a single field, hamcrest itself provides a way to combine several matchers (See for instance {@link org.hamcrest.Matchers#both(org.hamcrest.Matcher)}.\n *\n * @return the DSL to continue the construction of the matcher.\n * @see other\n */"); - } - - @Test(fastFail = false) - public void testDefaultImplementationV1() { - FieldDSLMethod fieldDSLMethod = FieldDSLMethodBuilder.of(fieldDescription).withDeclaration("a a") - .withDefaultJavaDoc().havingImplementation("b"); - assertThat(fieldDSLMethod.asDSLMethod()).is( - "/**\n * Add a validation on the field `fn`.\n *

\n *\n * {@link fqnecof#fa This field is accessed by using this approach}.\n *

\n * In case method specifing a matcher on a fields are used several times, only the last setted matcher will be used. \n * When several control must be done on a single field, hamcrest itself provides a way to combine several matchers (See for instance {@link org.hamcrest.Matchers#both(org.hamcrest.Matcher)}.\n *\n * @return the DSL to continue the construction of the matcher.\n */\n drm fn(a a);\n"); - assertThat(fieldDSLMethod.asImplementationMethod()).is("@Override\npublic drm fn(a a) {\n b\n}\n"); - } - - @Test(fastFail = false) - public void testDefaultDSLV1() { - FieldDSLMethod fieldDSLMethod = FieldDSLMethodBuilder.of(fieldDescription).withDeclaration("a a") - .withDefaultJavaDoc().havingDefault("m(a)"); - assertThat(fieldDSLMethod.asDSLMethod()).is( - "/**\n * Add a validation on the field `fn`.\n *

\n *\n * {@link fqnecof#fa This field is accessed by using this approach}.\n *

\n * In case method specifing a matcher on a fields are used several times, only the last setted matcher will be used. \n * When several control must be done on a single field, hamcrest itself provides a way to combine several matchers (See for instance {@link org.hamcrest.Matchers#both(org.hamcrest.Matcher)}.\n *\n * @return the DSL to continue the construction of the matcher.\n */\ndefault drm fn(a a){\n return fn(m(a));\n}"); - assertThat(fieldDSLMethod.asImplementationMethod()).is(""); - } - -} +package ch.powerunit.extensions.matchers.provideprocessor.fields; + +import static org.mockito.Mockito.when; + +import java.util.Optional; + +import org.mockito.Mock; + +import ch.powerunit.Rule; +import ch.powerunit.Test; +import ch.powerunit.TestRule; +import ch.powerunit.TestSuite; + +public class FieldDSLMethodBuilderTest implements TestSuite { + @Rule + public final TestRule rules = mockitoRule().around(before(this::prepareMock)); + + @Mock + private AbstractFieldDescription fieldDescription; + + private void prepareMock() { + when(fieldDescription.getFieldName()).thenReturn("fn"); + when(fieldDescription.getFieldAccessor()).thenReturn("fa"); + when(fieldDescription.getFullyQualifiedNameEnclosingClassOfField()).thenReturn("fqnecof"); + when(fieldDescription.getFieldType()).thenReturn("ft"); + when(fieldDescription.getDefaultReturnMethod()).thenReturn("drm"); + } + + @Test + public void testBuildImplementationOneLine() { + assertThatBiFunction(FieldDSLMethodBuilder::buildImplementation, "name", "ligne1") + .is("@Override\npublic name {\n ligne1\n}\n"); + } + + @Test + public void testBuildImplementationTwoLine() { + assertThatBiFunction(FieldDSLMethodBuilder::buildImplementation, "name", "ligne1\nligne2") + .is("@Override\npublic name {\n ligne1\n ligne2\n}\n"); + } + + @Test + public void testBuildDsl() { + assertThatBiFunction(FieldDSLMethodBuilder::buildDsl, "javadoc", "declaration").is("javadoc\ndeclaration;\n"); + } + + @Test(fastFail = false) + public void testGetJavaDocFor() { + assertThat(FieldDSLMethodBuilder.getJavaDocFor(fieldDescription, Optional.empty(), Optional.empty(), + Optional.empty())).is( + "/**\n * Add a validation on the field `fn`.\n *

\n *\n * {@link fqnecof#fa This field is accessed by using this approach}.\n *

\n * In case method specifing a matcher on a fields are used several times, only the last setted matcher will be used. \n * When several control must be done on a single field, hamcrest itself provides a way to combine several matchers.\n * (See for instance {@link org.hamcrest.Matchers#both(org.hamcrest.Matcher)}.\n *\n * @return the DSL to continue the construction of the matcher.\n */"); + + assertThat(FieldDSLMethodBuilder.getJavaDocFor(fieldDescription, Optional.of("add"), Optional.empty(), + Optional.empty())).is( + "/**\n * Add a validation on the field `fn` add.\n *

\n *\n * {@link fqnecof#fa This field is accessed by using this approach}.\n *

\n * In case method specifing a matcher on a fields are used several times, only the last setted matcher will be used. \n * When several control must be done on a single field, hamcrest itself provides a way to combine several matchers.\n * (See for instance {@link org.hamcrest.Matchers#both(org.hamcrest.Matcher)}.\n *\n * @return the DSL to continue the construction of the matcher.\n */"); + + assertThat(FieldDSLMethodBuilder.getJavaDocFor(fieldDescription, Optional.empty(), Optional.of("p is p"), + Optional.empty())).is( + "/**\n * Add a validation on the field `fn`.\n *

\n *\n * {@link fqnecof#fa This field is accessed by using this approach}.\n *

\n * In case method specifing a matcher on a fields are used several times, only the last setted matcher will be used. \n * When several control must be done on a single field, hamcrest itself provides a way to combine several matchers.\n * (See for instance {@link org.hamcrest.Matchers#both(org.hamcrest.Matcher)}.\n *\n * @param p is p.\n * @return the DSL to continue the construction of the matcher.\n */"); + + assertThat(FieldDSLMethodBuilder.getJavaDocFor(fieldDescription, Optional.empty(), Optional.empty(), + Optional.of("other"))).is( + "/**\n * Add a validation on the field `fn`.\n *

\n *\n * {@link fqnecof#fa This field is accessed by using this approach}.\n *

\n * In case method specifing a matcher on a fields are used several times, only the last setted matcher will be used. \n * When several control must be done on a single field, hamcrest itself provides a way to combine several matchers.\n * (See for instance {@link org.hamcrest.Matchers#both(org.hamcrest.Matcher)}.\n *\n * @return the DSL to continue the construction of the matcher.\n * @see other\n */"); + } + + @Test(fastFail = false) + public void testDefaultImplementationV1() { + FieldDSLMethod fieldDSLMethod = FieldDSLMethodBuilder.of(fieldDescription).withDeclaration("a a") + .withDefaultJavaDoc().havingImplementation("b"); + assertThat(fieldDSLMethod.asDSLMethod()).is( + "/**\n * Add a validation on the field `fn`.\n *

\n *\n * {@link fqnecof#fa This field is accessed by using this approach}.\n *

\n * In case method specifing a matcher on a fields are used several times, only the last setted matcher will be used. \n * When several control must be done on a single field, hamcrest itself provides a way to combine several matchers.\n * (See for instance {@link org.hamcrest.Matchers#both(org.hamcrest.Matcher)}.\n *\n * @return the DSL to continue the construction of the matcher.\n */\n drm fn(a a);\n"); + assertThat(fieldDSLMethod.asImplementationMethod()).is("@Override\npublic drm fn(a a) {\n b\n}\n"); + } + + @Test(fastFail = false) + public void testDefaultDSLV1() { + FieldDSLMethod fieldDSLMethod = FieldDSLMethodBuilder.of(fieldDescription).withDeclaration("a a") + .withDefaultJavaDoc().havingDefault("m(a)"); + assertThat(fieldDSLMethod.asDSLMethod()).is( + "/**\n * Add a validation on the field `fn`.\n *

\n *\n * {@link fqnecof#fa This field is accessed by using this approach}.\n *

\n * In case method specifing a matcher on a fields are used several times, only the last setted matcher will be used. \n * When several control must be done on a single field, hamcrest itself provides a way to combine several matchers.\n * (See for instance {@link org.hamcrest.Matchers#both(org.hamcrest.Matcher)}.\n *\n * @return the DSL to continue the construction of the matcher.\n */\ndefault drm fn(a a){\n return fn(m(a));\n}"); + assertThat(fieldDSLMethod.asImplementationMethod()).is(""); + } + +}