Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5fae01b

Browse files
committed
Add methods for passing a single traitname or an array of traitnames
1 parent 4a3a458 commit 5fae01b

File tree

1 file changed

+55
-7
lines changed

1 file changed

+55
-7
lines changed

src/main/groovy/nl/topicus/overheid/javafactorybot/BaseFactory.groovy

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import java.lang.reflect.ParameterizedType
1111
* A factory is a special class which is able to generate new valid objects, for testing purposes.
1212
* These objects can be randomized by using a faker.
1313
*
14-
* @param < M > The type of the generated object
15-
* @param < F > The type of the faker of this factory. This allows to override the faker with a custom implementation.
14+
* @param < M > The type of the generated object
15+
* @param < F > The type of the faker of this factory. This allows to override the faker with a custom implementation.
1616
*/
1717
abstract class BaseFactory<M, F extends Faker> extends Definition<M> {
1818
/**
@@ -94,6 +94,9 @@ abstract class BaseFactory<M, F extends Faker> extends Definition<M> {
9494

9595
/**
9696
* Returns a new instance that is not saved.
97+
* <p>
98+
* In normal usage, this method should not be overriden. If you want to change how the object is built, use
99+
* {@link #onAfterBuild} or {@link #internalBuild}.
97100
*
98101
* @param traits A list of traits to apply to new object. A trait is basically a collection of attribute/relation
99102
* updates, meant to create an object representing a certain state. The possible traits are specified in the factory.
@@ -103,10 +106,27 @@ abstract class BaseFactory<M, F extends Faker> extends Definition<M> {
103106
build([:], traits)
104107
}
105108

109+
/**
110+
* Returns a new instance that is not saved.
111+
* <p>
112+
* In normal usage, this method should not be overriden. If you want to change how the object is built, use
113+
* {@link #onAfterBuild} or {@link #internalBuild}.
114+
*
115+
* @param traits An array of traits to apply to new object. A trait is basically a collection of attribute/relation
116+
* updates, meant to create an object representing a certain state. The possible traits are specified in the factory.
117+
* @return The new instance.
118+
*/
119+
M build(String... traits) {
120+
build([:], traits.toList())
121+
}
122+
106123
/**
107124
* Returns the passed object, after it is saved.
108125
* <p>
109126
* This method exists so it is possible to completely override a relation by passing your own instance, or null.
127+
* <p>
128+
* To persist the object, the current context (@link FactoryManager#currentContext} is temporarily set to {@link FactoryManager#createContext}.
129+
* This context specifies the strategy to use to persist the object.
110130
*
111131
* @param object The custom instance of the object.
112132
* @return The passed object, after is saved.
@@ -118,8 +138,8 @@ abstract class BaseFactory<M, F extends Faker> extends Definition<M> {
118138
/**
119139
* Returns a new instance that is saved.
120140
* <p>
121-
* In normal usage, this method should not be overriden. If you want to change how the object is built, use
122-
* {@link #onAfterBuild} or {@link #internalBuild}.
141+
* To persist the object, the current context (@link FactoryManager#currentContext} is temporarily set to {@link FactoryManager#createContext}.
142+
* This context specifies the strategy to use to persist the object.
123143
*
124144
* @return The new saved instance.
125145
*/
@@ -130,8 +150,8 @@ abstract class BaseFactory<M, F extends Faker> extends Definition<M> {
130150
/**
131151
* Returns a new instance that is saved.
132152
* <p>
133-
* In normal usage, this method should not be overriden. If you want to change how the object is built, use
134-
* {@link #onAfterBuild} or {@link #internalBuild}.
153+
* To persist the object, the current context (@link FactoryManager#currentContext} is temporarily set to {@link FactoryManager#createContext}.
154+
* This context specifies the strategy to use to persist the object..
135155
*
136156
* @param overrides Additional overrides to use when building a new object.
137157
* Build parameters allow to define custom values for attributes and relations.
@@ -143,6 +163,34 @@ abstract class BaseFactory<M, F extends Faker> extends Definition<M> {
143163
doInCreateContext { build(overrides, traits) }
144164
}
145165

166+
/**
167+
* Returns a new instance that is saved.
168+
* <p>
169+
* To persist the object, the current context (@link FactoryManager#currentContext} is temporarily set to {@link FactoryManager#createContext}.
170+
* This context specifies the strategy to use to persist the object..
171+
*
172+
* @param traits A list of traits to apply to new object. A trait is basically a collection of attribute/relation
173+
* updates, meant to create an object representing a certain state. The possible traits are specified in the factory.
174+
* @return The new saved instance.
175+
*/
176+
M create(List<String> traits) {
177+
doInCreateContext { build(traits) }
178+
}
179+
180+
/**
181+
* Returns a new instance that is saved.
182+
* <p>
183+
* To persist the object, the current context (@link FactoryManager#currentContext} is temporarily set to {@link FactoryManager#createContext}.
184+
* This context specifies the strategy to use to persist the object..
185+
*
186+
* @param traits An array of traits to apply to new object. A trait is basically a collection of attribute/relation
187+
* updates, meant to create an object representing a certain state. The possible traits are specified in the factory.
188+
* @return The new saved instance.
189+
*/
190+
M create(String... traits) {
191+
doInCreateContext { build(traits) }
192+
}
193+
146194
/**
147195
* Returns a list of new instances that are not saved.
148196
*
@@ -263,7 +311,7 @@ abstract class BaseFactory<M, F extends Faker> extends Definition<M> {
263311
*/
264312
private M applyAfterBuildHooks(M object, List<String> traits = null) {
265313
onAfterBuild(object)
266-
if (traits) traits.each { findTrait(it).onAfterBuild(object) }
314+
if (traits) traits.each { findTrait(it).onAfterBuild(object) }
267315
object
268316
}
269317

0 commit comments

Comments
 (0)