diff --git a/README.md b/README.md index def4404..c981a07 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ Library to build your POJO entities using a very intuitive DSL. -**NOTE**: from version 0.2.x the DSL has changed a little bit to provide more support and new features. you will have to update your test in order to use the new version but hopefully the DSL should change. +**NOTE**: from version 0.2.0 the DSL has changed a little bit to provide more support and new features. you will have to update your test in order to use the new version but hopefully the DSL should change. ## Motivation Most of the time when I am writing my tests I have the need to write clean and readable tests. One way to achieve is by having Test Builder, but normally it takes time and are difficult to mantain in time. So after looking around I came up with this library to help you to create your pojo in a different and easy way. @@ -26,10 +26,10 @@ public class PojoBuilder { public static Field name2 = new Field<>("defaultName"); public static Field value2 = new Field<>("defaultValue"); - public static Creator creator = lookUp -> new Pojo(lookUp.get(name, "defaultName"), + public static Function creator = lookUp -> new Pojo(lookUp.get(name, "defaultName"), lookUp.get(value, "defaultValue")); - public static Creator creatorWithPredefinedDefaults = lookUp -> new Pojo(lookUp.get(name2), + public static Function creatorWithPredefinedDefaults = lookUp -> new Pojo(lookUp.get(name2), lookUp.get(value2)); } @@ -38,7 +38,8 @@ There many things going on there but I will try to explain it in the best way th There are s few concepts to keep in mind; Field, Creator and LookUp. ### Creator -The Creator implements a method build that should contains how the object is going to be build. +The Creator is just a java Function that receives as Argument a LookUp and return the object. +**NOTE:** Since version 0.2.1 the Creator interface was removed in order to use a Function. ### Field The Field represent the value that you want to change by using the DSL in the construction of your objects. @@ -94,10 +95,10 @@ If you already have creators and you want to reuse them on other creator, you ca ```java public static Field name = new Field<>(); - public static Creator nameCreator = lookUp -> lookUp.get(name, "test1"); + public static Function nameCreator = lookUp -> lookUp.get(name, "test1"); public static Field secondName = new Field<>(); - public static Creator creator = lookUp -> lookUp.get(secondName, secondCreator); + public static Function creator = lookUp -> lookUp.get(secondName, secondCreator); Pojo pojo = Builder.build() .entity(creator) @@ -110,7 +111,7 @@ If you already have creators and you want to reuse them on other creator, you ca ```java public static Field secondName = new Field<>(); -public static Creator secondCreator = lookUp -> lookUp.get(secondName, "test1"); +public static Function secondCreator = lookUp -> lookUp.get(secondName, "test1"); Pojo pojo = Builder.build() .entity(PojoBuilder.creator) @@ -171,7 +172,5 @@ The library is highly inspired by [Make it Easy](https://github.com/npryce/make-it-easy) And [AssertJ](https://github.com/joel-costigliola/assertj-core) -For the random generation we decided to use [Fyodor](https://github.com/fyodor-org-uk/fyodor) - Make It Ease lib provides a Hamcrest style DSL but I am more fun of using a builder kind of DSL like AssertJ that offers straight away the option that I can use. I want to say thank you to all the collaborator of MakeItEasy project. diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index e0b3fb8..86b1130 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,6 @@ +#Wed Mar 20 19:41:36 GMT 2019 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-all.zip diff --git a/src/main/java/uk/co/caeldev/builder4test/ApplyField.java b/src/main/java/uk/co/caeldev/builder4test/ApplyField.java index 4889298..74e0b54 100644 --- a/src/main/java/uk/co/caeldev/builder4test/ApplyField.java +++ b/src/main/java/uk/co/caeldev/builder4test/ApplyField.java @@ -1,10 +1,11 @@ package uk.co.caeldev.builder4test; +import java.util.function.Function; import java.util.function.Supplier; public interface ApplyField { - L applyCreator(Field field, Creator creator); + L applyCreator(Field field, Function creator); L applySupplier(Field field, Supplier supplier); L applyValue(Field field, U value); diff --git a/src/main/java/uk/co/caeldev/builder4test/Builder.java b/src/main/java/uk/co/caeldev/builder4test/Builder.java index f3f95cb..5769137 100644 --- a/src/main/java/uk/co/caeldev/builder4test/Builder.java +++ b/src/main/java/uk/co/caeldev/builder4test/Builder.java @@ -1,5 +1,7 @@ package uk.co.caeldev.builder4test; +import java.util.function.Function; + public class Builder { private Builder() { @@ -9,11 +11,11 @@ public static Builder build() { return new Builder(); } - public EntityBuilder entity(Creator creator) { + public EntityBuilder entity(Function creator) { return EntityBuilder.entityBuilder(creator); } - public ListBuilder list(Creator creator) { + public ListBuilder list(Function creator) { return ListBuilder.listBuilder(creator); } diff --git a/src/main/java/uk/co/caeldev/builder4test/Creator.java b/src/main/java/uk/co/caeldev/builder4test/Creator.java deleted file mode 100644 index 3c9d2e0..0000000 --- a/src/main/java/uk/co/caeldev/builder4test/Creator.java +++ /dev/null @@ -1,7 +0,0 @@ -package uk.co.caeldev.builder4test; - -public interface Creator { - - T build(LookUp lookUp); - -} diff --git a/src/main/java/uk/co/caeldev/builder4test/ElementBuilder.java b/src/main/java/uk/co/caeldev/builder4test/ElementBuilder.java index 2f4b488..2018799 100644 --- a/src/main/java/uk/co/caeldev/builder4test/ElementBuilder.java +++ b/src/main/java/uk/co/caeldev/builder4test/ElementBuilder.java @@ -6,6 +6,7 @@ import java.util.HashMap; import java.util.Map; +import java.util.function.Function; import java.util.function.Supplier; public class ElementBuilder implements ApplyField> { @@ -39,8 +40,8 @@ public ElementBuilder applyValue(Field field, U value) { } @Override - public ElementBuilder applyCreator(Field field, Creator creator) { - return applySupplier(field, () -> creator.build(new DefaultLookUp(fields))); + public ElementBuilder applyCreator(Field field, Function creator) { + return applySupplier(field, () -> creator.apply(new DefaultLookUp(fields))); } public ElementBuilder nullify(Field field) { diff --git a/src/main/java/uk/co/caeldev/builder4test/ElementListBuilder.java b/src/main/java/uk/co/caeldev/builder4test/ElementListBuilder.java index 43ac0d1..e3e3535 100644 --- a/src/main/java/uk/co/caeldev/builder4test/ElementListBuilder.java +++ b/src/main/java/uk/co/caeldev/builder4test/ElementListBuilder.java @@ -6,19 +6,20 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; +import java.util.function.Function; import java.util.stream.Collectors; public class ElementListBuilder { - private final Creator creator; + private final Function creator; private final List> elements; - private ElementListBuilder(Creator creator) { + private ElementListBuilder(Function creator) { this.creator = creator; this.elements = new ArrayList<>(); } - public static ElementListBuilder elementListBuilder(Creator creator) { + public static ElementListBuilder elementListBuilder(Function creator) { return new ElementListBuilder<>(creator); } diff --git a/src/main/java/uk/co/caeldev/builder4test/EntityBuilder.java b/src/main/java/uk/co/caeldev/builder4test/EntityBuilder.java index 1ae0d7c..75224a9 100644 --- a/src/main/java/uk/co/caeldev/builder4test/EntityBuilder.java +++ b/src/main/java/uk/co/caeldev/builder4test/EntityBuilder.java @@ -5,37 +5,38 @@ import uk.co.caeldev.builder4test.resolvers.ValueResolver; import java.util.Map; +import java.util.function.Function; import java.util.function.Supplier; public class EntityBuilder implements ApplyField> { - private final Creator creator; + private final Function creator; private final LookUp lookUp; - private EntityBuilder(Creator creator) { + private EntityBuilder(Function creator) { this.creator = creator; this.lookUp = new DefaultLookUp(); } - private EntityBuilder(Creator creator, Map fields) { + private EntityBuilder(Function creator, Map fields) { this.creator = creator; this.lookUp = new DefaultLookUp(fields); } - private EntityBuilder(Creator creator, LookUp lookUp) { + private EntityBuilder(Function creator, LookUp lookUp) { this.creator = creator; this.lookUp = lookUp; } - protected static EntityBuilder entityBuilder(Creator creator, Map fields) { + protected static EntityBuilder entityBuilder(Function creator, Map fields) { return new EntityBuilder<>(creator, fields); } - protected static EntityBuilder entityBuilder(Creator creator) { + protected static EntityBuilder entityBuilder(Function creator) { return new EntityBuilder<>(creator); } - protected static EntityBuilder entityBuilder(Creator creator, LookUp lookUp) { + protected static EntityBuilder entityBuilder(Function creator, LookUp lookUp) { return new EntityBuilder<>(creator, lookUp); } @@ -52,8 +53,8 @@ public EntityBuilder applyValue(Field field, U value) { } @Override - public EntityBuilder applyCreator(Field field, Creator creator) { - return applySupplier(field, () -> creator.build(lookUp)); + public EntityBuilder applyCreator(Field field, Function creator) { + return applySupplier(field, () -> creator.apply(lookUp)); } public EntityBuilder nullify(Field field) { @@ -62,6 +63,6 @@ public EntityBuilder nullify(Field field) { } public K get() { - return creator.build(lookUp); + return creator.apply(lookUp); } } diff --git a/src/main/java/uk/co/caeldev/builder4test/FixedSizeListBuilder.java b/src/main/java/uk/co/caeldev/builder4test/FixedSizeListBuilder.java index 23cabee..0e52385 100644 --- a/src/main/java/uk/co/caeldev/builder4test/FixedSizeListBuilder.java +++ b/src/main/java/uk/co/caeldev/builder4test/FixedSizeListBuilder.java @@ -16,17 +16,17 @@ public class FixedSizeListBuilder implements ApplyField> { private final int size; - private final Creator creator; + private final Function creator; private final Map values; - private FixedSizeListBuilder(int size, Creator creator) { + private FixedSizeListBuilder(int size, Function creator) { this.size = size; this.creator = creator; values = new HashMap<>(); } - protected static FixedSizeListBuilder fixedSizeListBuilder(int size, Creator creator) { + protected static FixedSizeListBuilder fixedSizeListBuilder(int size, Function creator) { return new FixedSizeListBuilder<>(size, creator); } @@ -43,8 +43,8 @@ public FixedSizeListBuilder applyValue(Field field, U value) { } @Override - public FixedSizeListBuilder applyCreator(Field field, Creator creator) { - applySupplier(field, () -> creator.build(new DefaultLookUp(values))); + public FixedSizeListBuilder applyCreator(Field field, Function creator) { + applySupplier(field, () -> creator.apply(new DefaultLookUp(values))); return this; } diff --git a/src/main/java/uk/co/caeldev/builder4test/ListBuilder.java b/src/main/java/uk/co/caeldev/builder4test/ListBuilder.java index 67fddaa..9b589d5 100644 --- a/src/main/java/uk/co/caeldev/builder4test/ListBuilder.java +++ b/src/main/java/uk/co/caeldev/builder4test/ListBuilder.java @@ -1,14 +1,16 @@ package uk.co.caeldev.builder4test; +import java.util.function.Function; + public class ListBuilder { - private final Creator creator; + private final Function creator; - private ListBuilder(Creator creator) { + private ListBuilder(Function creator) { this.creator = creator; } - protected static ListBuilder listBuilder(Creator creator) { + protected static ListBuilder listBuilder(Function creator) { return new ListBuilder<>(creator); } diff --git a/src/main/java/uk/co/caeldev/builder4test/LookUp.java b/src/main/java/uk/co/caeldev/builder4test/LookUp.java index 55cd593..1b2a03c 100644 --- a/src/main/java/uk/co/caeldev/builder4test/LookUp.java +++ b/src/main/java/uk/co/caeldev/builder4test/LookUp.java @@ -2,6 +2,8 @@ import uk.co.caeldev.builder4test.resolvers.Resolver; +import java.util.function.Function; + public abstract class LookUp { protected abstract void put(Field field, Resolver value); @@ -10,8 +12,8 @@ public abstract class LookUp { public abstract V get(Field field, V defaultValue); - public V get(Field field, Creator defaultValue) { - return get(field, defaultValue.build(this)); + public V get(Field field, Function defaultValue) { + return get(field, defaultValue.apply(this)); } } diff --git a/src/test/java/integration/BuilderIntegrationTest.java b/src/test/java/integration/BuilderIntegrationTest.java index 0fee812..43b2760 100644 --- a/src/test/java/integration/BuilderIntegrationTest.java +++ b/src/test/java/integration/BuilderIntegrationTest.java @@ -9,16 +9,7 @@ import java.util.List; import static org.assertj.core.api.Assertions.assertThat; -import static uk.co.caeldev.builder4test.impl.PojoBuilder.creator; -import static uk.co.caeldev.builder4test.impl.PojoBuilder.creatorWithPredefinedCreatorDefaults; -import static uk.co.caeldev.builder4test.impl.PojoBuilder.creatorWithPredefinedDefaults; -import static uk.co.caeldev.builder4test.impl.PojoBuilder.name; -import static uk.co.caeldev.builder4test.impl.PojoBuilder.name2; -import static uk.co.caeldev.builder4test.impl.PojoBuilder.testValue; -import static uk.co.caeldev.builder4test.impl.PojoBuilder.value; -import static uk.co.caeldev.builder4test.impl.PojoBuilder.value2; -import static uk.co.caeldev.builder4test.impl.PojoBuilder.valueCreator; -import static uk.co.caeldev.builder4test.impl.PojoBuilder.valueTestCreator; +import static uk.co.caeldev.builder4test.impl.PojoBuilder.*; import static uk.org.fyodor.generators.RDG.string; public class BuilderIntegrationTest { diff --git a/src/test/java/uk/co/caeldev/builder4test/LookUpTest.java b/src/test/java/uk/co/caeldev/builder4test/LookUpTest.java index 0e39ddb..a7b8fd9 100644 --- a/src/test/java/uk/co/caeldev/builder4test/LookUpTest.java +++ b/src/test/java/uk/co/caeldev/builder4test/LookUpTest.java @@ -7,6 +7,8 @@ import org.mockito.junit.jupiter.MockitoExtension; import uk.co.caeldev.builder4test.resolvers.Resolver; +import java.util.function.Function; + import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.verify; @@ -14,7 +16,7 @@ class LookUpTest { @Mock - private Creator creator; + private Function creator; @Test @DisplayName("Should Create two different instance of ElementListBuilder") @@ -26,7 +28,7 @@ public void shouldUseCreatorAsValue() { testLookUp.get(new Field(), creator); //Then - verify(creator).build(any(LookUp.class)); + verify(creator).apply(any(LookUp.class)); } class TestLookUp extends LookUp { diff --git a/src/test/java/uk/co/caeldev/builder4test/impl/PojoBuilder.java b/src/test/java/uk/co/caeldev/builder4test/impl/PojoBuilder.java index ff37a3a..d033804 100644 --- a/src/test/java/uk/co/caeldev/builder4test/impl/PojoBuilder.java +++ b/src/test/java/uk/co/caeldev/builder4test/impl/PojoBuilder.java @@ -1,27 +1,29 @@ package uk.co.caeldev.builder4test.impl; -import uk.co.caeldev.builder4test.Creator; import uk.co.caeldev.builder4test.Field; +import uk.co.caeldev.builder4test.LookUp; + +import java.util.function.Function; public class PojoBuilder { public static Field name = new Field<>(); public static Field value = new Field<>(); - public static Creator creator = lookUp -> new Pojo(lookUp.get(name, "defaultName"), + public static Function creator = lookUp -> new Pojo(lookUp.get(name, "defaultName"), lookUp.get(value, "defaultValue")); public static Field name2 = new Field<>("defaultName"); public static Field value2 = new Field<>("defaultValue"); - public static Creator creatorWithPredefinedDefaults = lookUp -> new Pojo(lookUp.get(name2), + public static Function creatorWithPredefinedDefaults = lookUp -> new Pojo(lookUp.get(name2), lookUp.get(value2)); - public static Creator valueCreator = lookUp -> "test1"; + public static Function valueCreator = lookUp -> "test1"; public static Field testValue = new Field<>(); - public static Creator valueTestCreator = lookUp -> lookUp.get(testValue, "test1"); + public static Function valueTestCreator = lookUp -> lookUp.get(testValue, "test1"); - public static Creator creatorWithPredefinedCreatorDefaults = lookUp -> new Pojo(lookUp.get(name2, valueTestCreator), + public static Function creatorWithPredefinedCreatorDefaults = lookUp -> new Pojo(lookUp.get(name2, valueTestCreator), lookUp.get(value2));