1
1
package pl .pojo .tester .api .assertion ;
2
2
3
- import org .apache .commons .collections4 .MultiValuedMap ;
4
- import org .apache .commons .collections4 .multimap .ArrayListValuedHashMap ;
5
3
import org .slf4j .Logger ;
4
+ import pl .pojo .tester .api .AbstractObjectInstantiator ;
6
5
import pl .pojo .tester .api .ClassAndFieldPredicatePair ;
7
6
import pl .pojo .tester .api .ConstructorParameters ;
8
7
import pl .pojo .tester .internal .field .AbstractFieldValueChanger ;
8
+ import pl .pojo .tester .internal .instantiator .SupplierInstantiator ;
9
+ import pl .pojo .tester .internal .instantiator .UserDefinedConstructorInstantiator ;
10
+ import pl .pojo .tester .internal .tester .AbstractTester ;
11
+ import pl .pojo .tester .internal .utils .ClassLoader ;
9
12
import pl .pojo .tester .internal .utils .Permutator ;
10
13
import pl .pojo .tester .internal .utils .SublistFieldPermutator ;
11
14
import pl .pojo .tester .internal .utils .ThoroughFieldPermutator ;
12
- import pl .pojo .tester .internal .tester .AbstractTester ;
13
- import pl .pojo .tester .internal .utils .ClassLoader ;
14
15
15
16
import java .util .Arrays ;
16
17
import java .util .HashSet ;
18
+ import java .util .LinkedList ;
19
+ import java .util .List ;
17
20
import java .util .Set ;
21
+ import java .util .function .Supplier ;
18
22
import java .util .stream .Collectors ;
19
23
20
24
import static pl .pojo .tester .internal .preconditions .ParameterPreconditions .checkNotBlank ;
@@ -40,7 +44,7 @@ public abstract class AbstractAssertion {
40
44
.forEach (DEFAULT_TESTERS ::add );
41
45
}
42
46
43
- private final MultiValuedMap < Class <?>, ConstructorParameters > constructorParameters = new ArrayListValuedHashMap <>();
47
+ private final List < AbstractObjectInstantiator > instantiators = new LinkedList <>();
44
48
Set <AbstractTester > testers = new HashSet <>();
45
49
private AbstractFieldValueChanger abstractFieldValueChanger ;
46
50
private Permutator permutator = new ThoroughFieldPermutator ();
@@ -109,26 +113,6 @@ public AbstractAssertion testing(final Method method) {
109
113
return this ;
110
114
}
111
115
112
- /**
113
- * Performs specified tests on classes using declared field value changer.
114
- *
115
- * @see Method
116
- * @see AbstractFieldValueChanger
117
- */
118
- public void areWellImplemented () {
119
- if (testers .isEmpty ()) {
120
- testers = DEFAULT_TESTERS ;
121
- }
122
- if (abstractFieldValueChanger != null ) {
123
- testers .forEach (tester -> tester .setFieldValuesChanger (abstractFieldValueChanger ));
124
- }
125
-
126
- testers .forEach (tester -> tester .setPermutator (permutator ));
127
- testers .forEach (tester -> tester .setUserDefinedConstructors (constructorParameters ));
128
-
129
- runAssertions ();
130
- }
131
-
132
116
/**
133
117
* Indicates, that class should be constructed using given constructor parameters. Constructor will be selected
134
118
* based on constructor parameter's types.
@@ -164,8 +148,8 @@ public AbstractAssertion create(final String qualifiedClassName,
164
148
checkNotNull ("constructorParameters" , constructorParameters );
165
149
166
150
final Class <?> clazz = ClassLoader .loadClass (qualifiedClassName );
167
- this . constructorParameters . put ( clazz , constructorParameters );
168
- return this ;
151
+
152
+ return create ( clazz , constructorParameters ) ;
169
153
}
170
154
171
155
/**
@@ -188,7 +172,6 @@ public AbstractAssertion create(final Class<?> clazz,
188
172
return create (clazz , constructorParameter );
189
173
}
190
174
191
-
192
175
/**
193
176
* Indicates, that class should be constructed using given constructor parameters. Constructor will be selected
194
177
* based on constructor parameter's types.
@@ -202,10 +185,76 @@ public AbstractAssertion create(final Class<?> clazz, final ConstructorParameter
202
185
checkNotNull ("clazz" , clazz );
203
186
checkNotNull ("constructorParameters" , constructorParameters );
204
187
205
- this .constructorParameters .put (clazz , constructorParameters );
188
+ final UserDefinedConstructorInstantiator instantiator = new UserDefinedConstructorInstantiator (clazz ,
189
+ constructorParameters );
190
+ return create (clazz , instantiator );
191
+ }
192
+
193
+ /**
194
+ * Indicates, that class should be constructed using given instantiator.
195
+ *
196
+ * @param instantiator instantiator which will create instance of given class
197
+ * @return itself
198
+ * @see ConstructorParameters
199
+ */
200
+ public AbstractAssertion create (final AbstractObjectInstantiator instantiator ) {
201
+ checkNotNull ("clazz" , instantiator .getClazz ());
202
+ checkNotNull ("instantiator" , instantiator );
203
+
204
+ return create (instantiator .getClazz (), instantiator ::instantiate );
205
+ }
206
+
207
+ /**
208
+ * Indicates, that class should be constructed using given instantiator.
209
+ *
210
+ * @param clazz class to instantiate
211
+ * @param instantiator instantiator which will create instance of given class
212
+ * @return itself
213
+ * @see ConstructorParameters
214
+ */
215
+ public AbstractAssertion create (final Class <?> clazz , final AbstractObjectInstantiator instantiator ) {
216
+ checkNotNull ("clazz" , clazz );
217
+ checkNotNull ("instantiator" , instantiator );
218
+
219
+ return create (clazz , instantiator ::instantiate );
220
+ }
221
+
222
+ /**
223
+ * Indicates, that class should be constructed using given supplier.
224
+ *
225
+ * @param clazz class to instantiate
226
+ * @param supplier supplier that will create given class
227
+ * @return itself
228
+ * @see ConstructorParameters
229
+ */
230
+ public AbstractAssertion create (final Class <?> clazz , final Supplier <?> supplier ) {
231
+ checkNotNull ("clazz" , clazz );
232
+ checkNotNull ("supplier" , supplier );
233
+
234
+ this .instantiators .add (new SupplierInstantiator (clazz , supplier ));
206
235
return this ;
207
236
}
208
237
238
+ /**
239
+ * Performs specified tests on classes using declared field value changer.
240
+ *
241
+ * @see Method
242
+ * @see AbstractFieldValueChanger
243
+ */
244
+ public void areWellImplemented () {
245
+ if (testers .isEmpty ()) {
246
+ testers = DEFAULT_TESTERS ;
247
+ }
248
+ if (abstractFieldValueChanger != null ) {
249
+ testers .forEach (tester -> tester .setFieldValuesChanger (abstractFieldValueChanger ));
250
+ }
251
+
252
+ testers .forEach (tester -> tester .setPermutator (permutator ));
253
+ testers .forEach (tester -> tester .setUserDefinedInstantiators (instantiators ));
254
+
255
+ runAssertions ();
256
+ }
257
+
209
258
protected abstract void runAssertions ();
210
259
211
260
protected void logTestersAndClasses (final Logger logger ,
@@ -218,6 +267,7 @@ protected void logTestersAndClasses(final Logger logger,
218
267
logger .debug ("Running {} testers on {} classes" , testers .size (), classAndFieldPredicatePairs .length );
219
268
logger .debug ("Testers: {}" , testers );
220
269
logger .debug ("Classes: {}" , classes );
270
+ logger .debug ("Predefined instantiators: {}" , instantiators );
221
271
}
222
272
}
223
273
}
0 commit comments