forked from testng-team/testng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parameters.java
943 lines (859 loc) · 34.6 KB
/
Parameters.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
package org.testng.internal;
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Parameter;
import java.util.*;
import org.testng.DataProviderHolder;
import org.testng.IDataProviderInterceptor;
import org.testng.IDataProviderListener;
import org.testng.IDataProviderMethod;
import org.testng.IRetryDataProvider;
import org.testng.ITestClass;
import org.testng.ITestContext;
import org.testng.ITestNGMethod;
import org.testng.ITestObjectFactory;
import org.testng.ITestResult;
import org.testng.TestNGException;
import org.testng.annotations.*;
import org.testng.annotations.IDataProviderAnnotation;
import org.testng.annotations.IParametersAnnotation;
import org.testng.collections.Lists;
import org.testng.collections.Maps;
import org.testng.internal.annotations.AnnotationHelper;
import org.testng.internal.annotations.IAnnotationFinder;
import org.testng.internal.annotations.IDataProvidable;
import org.testng.internal.collections.ArrayIterator;
import org.testng.internal.invokers.MethodInvocationHelper;
import org.testng.internal.invokers.ParameterHolder;
import org.testng.internal.invokers.ParameterHolder.ParameterOrigin;
import org.testng.internal.objects.Dispenser;
import org.testng.internal.objects.IObjectDispenser;
import org.testng.internal.objects.pojo.BasicAttributes;
import org.testng.internal.objects.pojo.CreationAttributes;
import org.testng.internal.reflect.DataProviderMethodMatcher;
import org.testng.internal.reflect.InjectableParameter;
import org.testng.internal.reflect.MethodMatcher;
import org.testng.internal.reflect.MethodMatcherContext;
import org.testng.internal.reflect.ReflectionRecipes;
import org.testng.util.Strings;
import org.testng.xml.XmlSuite;
import org.testng.xml.XmlTest;
/** Methods that bind parameters declared in testng.xml to actual values used to invoke methods. */
public class Parameters {
@Deprecated public static final String NULL_VALUE = org.testng.annotations.Parameters.NULL_VALUE;
private static final List<Class<? extends Annotation>> annotationList =
Arrays.asList(
BeforeSuite.class,
AfterSuite.class,
BeforeTest.class,
AfterTest.class,
BeforeClass.class,
AfterClass.class,
BeforeGroups.class,
AfterGroups.class,
BeforeMethod.class,
AfterMethod.class,
Factory.class);
private static final Map<String, List<Class<?>>> mapping = Maps.newHashMap();
/*
+--------------+--------------+---------+--------+----------+-------------+
| Annotation | ITestContext | XmlTest | Method | Object[] | ITestResult |
+--------------+--------------+---------+--------+----------+-------------+
| BeforeSuite | Yes | Yes | No | No | No |
+--------------+--------------+---------+--------+----------+-------------+
| BeforeTest | Yes | Yes | No | No | No |
+--------------+--------------+---------+--------+----------+-------------+
| BeforeGroups | Yes | Yes | No | No | No |
+--------------+--------------+---------+--------+----------+-------------+
| BeforeClass | Yes | Yes | No | No | No |
+--------------+--------------+---------+--------+----------+-------------+
| BeforeMethod | Yes | Yes | Yes | Yes | Yes |
+--------------+--------------+---------+--------+----------+-------------+
| AfterSuite | Yes | Yes | No | No | No |
+--------------+--------------+---------+--------+----------+-------------+
| AfterTest | Yes | Yes | No | No | No |
+--------------+--------------+---------+--------+----------+-------------+
| AfterGroups | Yes | Yes | No | No | No |
+--------------+--------------+---------+--------+----------+-------------+
| AfterClass | Yes | Yes | No | No | No |
+--------------+--------------+---------+--------+----------+-------------+
| AfterMethod | Yes | Yes | Yes | Yes | Yes |
+--------------+--------------+---------+--------+----------+-------------+
| Test | Yes | Yes | Yes | No | No |
+--------------+--------------+---------+--------+----------+-------------+
| Factory | Yes | Yes | No | No | No |
+--------------+--------------+---------+--------+----------+-------------+
*/
static {
List<Class<?>> ctxTest = Arrays.asList(ITestContext.class, XmlTest.class);
List<Class<?>> beforeAfterMethod =
Arrays.asList(
ITestContext.class, XmlTest.class, Method.class, Object[].class, ITestResult.class);
mapping.put(BeforeSuite.class.getSimpleName(), ctxTest);
mapping.put(AfterSuite.class.getSimpleName(), ctxTest);
mapping.put(BeforeTest.class.getSimpleName(), ctxTest);
mapping.put(AfterTest.class.getSimpleName(), ctxTest);
mapping.put(BeforeGroups.class.getSimpleName(), ctxTest);
mapping.put(AfterGroups.class.getSimpleName(), ctxTest);
mapping.put(BeforeClass.class.getSimpleName(), ctxTest);
mapping.put(AfterClass.class.getSimpleName(), ctxTest);
mapping.put(BeforeMethod.class.getSimpleName(), beforeAfterMethod);
mapping.put(AfterMethod.class.getSimpleName(), beforeAfterMethod);
mapping.put(
Test.class.getSimpleName(), Arrays.asList(ITestContext.class, XmlTest.class, Method.class));
mapping.put(Factory.class.getSimpleName(), ctxTest);
}
/** Creates the parameters needed for constructing a test class instance. */
public static Object[] createInstantiationParameters(
Constructor<?> ctor,
String methodAnnotation,
IAnnotationFinder finder,
String[] parameterNames,
Map<String, String> params,
XmlSuite xmlSuite) {
return createParametersForConstructor(
ctor,
ctor.getParameterTypes(),
finder.findOptionalValues(ctor),
methodAnnotation,
parameterNames,
new MethodParameters(params, Collections.emptyMap()),
xmlSuite);
}
/**
* Creates the parameters needed for the specified <code>@Configuration</code> <code>Method</code>
* .
*
* @param m the configuraton method
* @param currentTestMethod the current @Test method or <code>null</code> if no @Test is available
* (this is not only in case the configuration method is a @Before/@AfterMethod
* @param finder the annotation finder
*/
public static Object[] createConfigurationParameters(
Method m,
Map<String, String> params,
Object[] parameterValues,
ITestNGMethod currentTestMethod,
IAnnotationFinder finder,
XmlSuite xmlSuite,
ITestContext ctx,
ITestResult testResult) {
Method currentTestMeth =
currentTestMethod != null ? currentTestMethod.getConstructorOrMethod().getMethod() : null;
Map<String, String> methodParams =
currentTestMethod != null
? currentTestMethod.findMethodParameters(ctx.getCurrentXmlTest())
: Collections.emptyMap();
Class<? extends Annotation> annotation = retrieveConfigAnnotation(m);
String name = annotation == null ? "" : annotation.getSimpleName();
return createParameters(
new ConstructorOrMethod(m),
new MethodParameters(
params, methodParams, parameterValues, currentTestMeth, ctx, testResult),
finder,
xmlSuite,
name);
}
private static Class<? extends Annotation> retrieveConfigAnnotation(Method m) {
return annotationList.stream()
.filter(annotation -> m.getAnnotation(annotation) != null)
.findAny()
.orElse(null);
}
private static Object[] createParametersForConstructor(
Constructor<?> constructor,
Class<?>[] parameterTypes,
String[] optionalValues,
String methodAnnotation,
String[] parameterNames,
MethodParameters params,
XmlSuite xmlSuite) {
if (parameterTypes.length == 0) {
return new Object[0];
}
checkParameterTypes(constructor.getName(), parameterTypes, methodAnnotation, parameterNames);
List<Object> vResult = Lists.newArrayList();
if (canInject(methodAnnotation)) {
Parameter[] paramsArray = ReflectionRecipes.getConstructorParameters(constructor);
Object[] inject =
ReflectionRecipes.inject(
paramsArray,
InjectableParameter.Assistant.ALL_INJECTS,
new Object[0],
constructor,
params.context,
params.testResult);
if (inject != null) {
vResult.addAll(Arrays.asList(inject));
}
}
List<Object> consParams =
createParams(
constructor.getName(),
"constructor",
methodAnnotation,
parameterTypes,
optionalValues,
parameterNames,
params,
xmlSuite);
vResult.addAll(consParams);
return vResult.toArray(new Object[0]);
}
private static List<Object> createParams(
String name,
String prefix,
String methodAnnotation,
Class<?>[] parameterTypes,
String[] optionalValues,
String[] parameterNames,
MethodParameters params,
XmlSuite xmlSuite) {
List<Object> vResult = Lists.newArrayList();
if (optionalValues.length != parameterNames.length) {
FilterOutInjectedTypesResult filterOutResult =
filterOutInjectedTypesFromOptionalValues(parameterTypes, optionalValues);
optionalValues = filterOutResult.getOptionalValues();
parameterTypes = filterOutResult.getParameterTypes();
}
if (parameterNames.length == 0 && optionalValues.length > 0) {
for (int i = 0; i < parameterTypes.length; i++) {
vResult.add(PropertyUtils.convertType(parameterTypes[i], optionalValues[i], ""));
}
return vResult;
}
for (int i = 0; i < parameterNames.length; i++) {
String p = parameterNames[i];
String value = System.getProperty(p, params.xmlParameters.get(p));
if (null == value) {
if (optionalValues != null) {
value = optionalValues[i];
}
if (null == value) {
throw new TestNGException(
"Parameter '"
+ p
+ "' is required by "
+ methodAnnotation
+ " on "
+ prefix
+ " "
+ name
+ " but has not been marked @Optional or defined\n"
+ (xmlSuite.getFileName() != null ? "in " + xmlSuite.getFileName() : ""));
}
}
vResult.add(PropertyUtils.convertType(parameterTypes[i], value, p));
}
return vResult;
}
/**
* Remove injected types from parameterTypes and optionalValues
*
* @param parameterTypes - The parameter types to be used
* @param optionalValues - The optional values to be considered.
* @return FilterOutInjectedTypesResult
*/
static FilterOutInjectedTypesResult filterOutInjectedTypesFromOptionalValues(
Class<?>[] parameterTypes, String[] optionalValues) {
List<Class<?>> typeList = Lists.newArrayList(parameterTypes);
List<String> optionalValueList = Lists.newArrayList(optionalValues);
Iterator<Class<?>> typeIterator = typeList.iterator();
Iterator<String> optionalIterator = optionalValueList.iterator();
while (typeIterator.hasNext()) {
Class<?> parameterType = typeIterator.next();
optionalIterator.next();
if (INJECTED_TYPES.contains(parameterType)) {
optionalIterator.remove();
typeIterator.remove();
}
}
return new FilterOutInjectedTypesResult(
typeList.toArray(new Class<?>[0]), optionalValueList.toArray(new String[0]));
}
/** Store the result of parameterTypes and optionalValues after filter out injected types */
static final class FilterOutInjectedTypesResult {
private final Class<?>[] parameterTypes;
private final String[] optionalValues;
private FilterOutInjectedTypesResult(Class<?>[] parameterTypes, String[] optionalValues) {
this.parameterTypes = parameterTypes;
this.optionalValues = optionalValues;
}
Class<?>[] getParameterTypes() {
return parameterTypes;
}
String[] getOptionalValues() {
return optionalValues;
}
}
private static boolean areAllOptionalValuesNull(String[] optionalValues) {
if (optionalValues == null || optionalValues.length == 0) {
return true;
}
boolean isNull = true;
for (String optionalValue : optionalValues) {
if (optionalValue != null) {
isNull = false;
break;
}
}
return isNull;
}
/**
* @return An array of parameters suitable to invoke this method, possibly picked from the
* property file
*/
private static Object[] createParametersForMethod(
ConstructorOrMethod method,
Class<?>[] parameterTypes,
String[] optionalValues,
String methodAnnotation,
String[] parameterNames,
MethodParameters params,
XmlSuite xmlSuite) {
if (parameterTypes.length == 0) {
return new Object[0];
}
if (areAllOptionalValuesNull(optionalValues)) {
checkParameterTypes(method.getName(), parameterTypes, methodAnnotation, parameterNames);
}
List<Object> vResult = Lists.newArrayList();
List<Object> consParams =
createParams(
method.getName(),
"method",
methodAnnotation,
parameterTypes,
optionalValues,
parameterNames,
params,
xmlSuite);
if (canInject(methodAnnotation)) {
Parameter[] paramsArray = extractParameters(method);
Object[] inject =
ReflectionRecipes.inject(
paramsArray,
InjectableParameter.Assistant.ALL_INJECTS,
consParams.toArray(new Object[0]),
params.currentTestMethod,
params.context,
params.testResult);
if (inject != null) {
vResult.addAll(Arrays.asList(inject));
}
} else {
vResult.addAll(consParams);
}
return vResult.toArray(new Object[0]);
}
private static Parameter[] extractParameters(ConstructorOrMethod method) {
if (method.getMethod() != null) {
return ReflectionRecipes.getMethodParameters(method.getMethod());
}
return ReflectionRecipes.getConstructorParameters(method.getConstructor());
}
private static boolean canInject(String annotation) {
return !("@" + Test.class.getSimpleName()).equalsIgnoreCase(annotation);
}
private static final List<Class<?>> INJECTED_TYPES =
Arrays.asList(
ITestContext.class, ITestResult.class, XmlTest.class, Method.class, Object[].class);
private static void checkParameterTypes(
String methodName,
Class<?>[] parameterTypes,
String methodAnnotation,
String[] parameterNames) {
int totalLength = parameterTypes.length;
for (Class<?> parameterType : parameterTypes) {
if (INJECTED_TYPES.contains(parameterType)) {
totalLength--;
}
}
if (parameterNames.length == 0) {
// parameterNames is usually populated via the @Parameters annotation, so we would need to
// apply our logic only when @Parameters annotation is not involved.
boolean invalid = (totalLength != 0) || (!validParameters(methodAnnotation, parameterTypes));
if (invalid) {
String annotation = methodAnnotation;
if (!methodAnnotation.startsWith("@")) {
annotation = "@" + methodAnnotation;
}
String errPrefix;
if (mapping.containsKey(methodAnnotation)) {
errPrefix =
"Can inject only one of "
+ prettyFormat(mapping.get(methodAnnotation))
+ " into a "
+ annotation
+ " annotated "
+ methodName;
} else {
errPrefix =
"Cannot inject "
+ annotation
+ " annotated Method ["
+ methodName
+ "] with "
+ Arrays.toString(parameterTypes);
}
throw new TestNGException(
errPrefix
+ ".\nFor more information on native dependency injection please refer to "
+ "https://testng.org/doc/documentation-main.html#native-dependency-injection");
}
}
if (parameterNames.length != totalLength) {
throw new TestNGException(
"Method "
+ methodName
+ " requires "
+ parameterTypes.length
+ " parameters but "
+ parameterNames.length
+ " were supplied in the "
+ methodAnnotation
+ " annotation.");
}
}
private static boolean validParameters(String methodAnnotation, Class<?>[] parameterTypes) {
List<Class<?>> localMapping = mapping.get(methodAnnotation.replace("@", ""));
if (localMapping == null) {
return false;
}
for (Class<?> parameterType : parameterTypes) {
if (!localMapping.contains(parameterType)) {
return false;
}
}
return true;
}
private static String prettyFormat(List<Class<?>> classes) {
StringBuilder builder = new StringBuilder("<");
if (classes.size() == 1) {
builder.append(classes.get(0));
} else {
int length = classes.size();
for (int i = 0; i < length - 1; i++) {
builder.append(classes.get(i).getSimpleName()).append(", ");
}
builder.append(classes.get(length - 1).getSimpleName());
}
builder.append(">");
return builder.toString();
}
private static IDataProviderMethod findDataProvider(
ITestObjectFactory objectFactory,
Object instance,
ITestClass clazz,
ConstructorOrMethod m,
IAnnotationFinder finder,
ITestContext context) {
IDataProviderMethod result = null;
IDataProvidable dp = findDataProviderInfo(clazz, m, finder);
if (dp != null) {
String dataProviderName = dp.getDataProvider();
Class<?> dataProviderClass = dp.getDataProviderClass();
boolean isDynamicDataProvider =
dataProviderClass == null && !dp.getDataProviderDynamicClass().isEmpty();
if (isDynamicDataProvider) {
try {
dataProviderClass = new DataProviderLoader().loadClazz(dp.getDataProviderDynamicClass());
} catch (ClassNotFoundException e) {
throw new TestNGException("Dynamic data provider class %s not found", e);
}
}
if (!Utils.isStringEmpty(dataProviderName)) {
result =
findDataProvider(
objectFactory,
instance,
clazz,
finder,
dataProviderName,
dataProviderClass,
isDynamicDataProvider,
context);
if (null == result) {
throw new TestNGException(
"Method "
+ m
+ " requires a @DataProvider named : "
+ dataProviderName
+ (dataProviderClass != null ? " in class " + dataProviderClass.getName() : ""));
}
}
}
return result;
}
/**
* Find the data provider info (data provider name and class) on either @Test(dataProvider),
* <code>@Factory(dataProvider)</code> on a method or @Factory(dataProvider) on a constructor.
*/
private static IDataProvidable findDataProviderInfo(
ITestClass clazz, ConstructorOrMethod m, IAnnotationFinder finder) {
if (m.getMethod() == null) {
// @Factory(dataProvider) on a constructor
return AnnotationHelper.findFactory(finder, m.getConstructor());
}
// @Test(dataProvider) on a method
ITestAnnotation result = AnnotationHelper.findTest(finder, m.getMethod());
if (result != null) {
// We may have a class level @Test annotation on which there might have been a data provider
// defined
// @Test(dataProvider) on a class
ITestAnnotation classLevel = AnnotationHelper.findTest(finder, clazz.getRealClass());
if (classLevel == null) {
return result;
}
return merge(result, classLevel);
}
// @Factory(dataProvider) on a method
IFactoryAnnotation factory = AnnotationHelper.findFactory(finder, m.getMethod());
if (factory != null) {
return factory;
}
// @Test(dataProvider) on a class
return AnnotationHelper.findTest(finder, clazz.getRealClass());
}
private static IDataProvidable merge(ITestAnnotation methodLevel, ITestAnnotation classLevel) {
// If no data provider information was provided at class level, then exit
if (isDataProviderClassEmpty(classLevel) && isDataProviderNameEmpty(classLevel)) {
return methodLevel;
}
if (Strings.isNullOrEmpty(methodLevel.getDataProvider())
&& Strings.isNotNullAndNotEmpty(classLevel.getDataProvider())) {
methodLevel.setDataProvider(classLevel.getDataProvider());
}
if (isDataProviderClassEmpty(methodLevel) && !isDataProviderClassEmpty(classLevel)) {
methodLevel.setDataProviderClass(classLevel.getDataProviderClass());
}
if (isDynamicDataProviderClassEmpty(methodLevel)
&& !isDynamicDataProviderClassEmpty(classLevel)) {
methodLevel.setDataProviderDynamicClass(classLevel.getDataProviderDynamicClass());
}
return methodLevel;
}
private static boolean isDataProviderClassEmpty(ITestAnnotation annotation) {
return annotation.getDataProviderClass() == null
|| Object.class.equals(annotation.getDataProviderClass());
}
private static boolean isDynamicDataProviderClassEmpty(ITestAnnotation annotation) {
return annotation.getDataProviderDynamicClass().isEmpty();
}
private static boolean isDataProviderNameEmpty(ITestAnnotation annotation) {
return Strings.isNullOrEmpty(annotation.getDataProvider());
}
/** Find a method that has a @DataProvider(name=name) */
private static IDataProviderMethod findDataProvider(
ITestObjectFactory objectFactory,
Object instance,
ITestClass clazz,
IAnnotationFinder finder,
String name,
Class<?> dataProviderClass,
boolean isDynamicDataProvider,
ITestContext context) {
IDataProviderMethod result = null;
Class<?> cls = clazz.getRealClass();
boolean shouldBeStatic = false;
boolean isDataProviderClassAbstract = Modifier.isAbstract(cls.getModifiers());
if (dataProviderClass != null && !isDataProviderClassAbstract) {
cls = dataProviderClass;
shouldBeStatic = true;
}
for (Method m : ClassHelper.getAvailableMethods(cls)) {
IDataProviderAnnotation dp = finder.findAnnotation(m, IDataProviderAnnotation.class);
boolean proceed = null != dp && name.equals(getDataProviderName(dp, m));
if (!proceed) {
continue;
}
Object instanceToUse = instance;
if (shouldBeStatic && (m.getModifiers() & Modifier.STATIC) == 0) {
IObjectDispenser dispenser = Dispenser.newInstance(objectFactory);
BasicAttributes basic = new BasicAttributes(clazz, dataProviderClass);
CreationAttributes attributes = new CreationAttributes(context, basic, null);
instanceToUse = dispenser.dispense(attributes);
}
// Not a static method but no instance exists, then create new one if possible
if ((m.getModifiers() & Modifier.STATIC) == 0 && instanceToUse == null) {
try {
instanceToUse = objectFactory.newInstance(cls);
} catch (TestNGException ignored) {
}
}
if (result != null) {
throw new TestNGException("Found two providers called '" + name + "' on " + cls);
}
if (isDynamicDataProvider) {
result = new DataProviderMethodRemovable(instanceToUse, m, dp);
} else {
result = new DataProviderMethod(instanceToUse, m, dp);
}
}
return result;
}
private static String getDataProviderName(IDataProviderAnnotation dp, Method m) {
return Strings.isNullOrEmpty(dp.getName()) ? m.getName() : dp.getName();
}
private static String[] extractOptionalValues(
IAnnotationFinder finder, ConstructorOrMethod consMethod) {
if (consMethod.getMethod() != null) {
return finder.findOptionalValues(consMethod.getMethod());
}
return finder.findOptionalValues(consMethod.getConstructor());
}
private static Object[] createParameters(
ConstructorOrMethod m,
MethodParameters params,
IAnnotationFinder finder,
XmlSuite xmlSuite,
String atName) {
List<Object> result = Lists.newArrayList();
String[] extraOptionalValues = extractOptionalValues(finder, m);
Object[] extraParameters;
//
// Try to find an @Parameters annotation
//
IParametersAnnotation annotation = finder.findAnnotation(m, IParametersAnnotation.class);
Class<?>[] types = m.getParameterTypes();
if (null != annotation) {
String[] parameterNames = annotation.getValue();
extraParameters =
createParametersForMethod(
m, types, extraOptionalValues, atName, parameterNames, params, xmlSuite);
}
//
// Else, use the deprecated syntax
//
else {
extraParameters =
createParametersForMethod(
m, types, extraOptionalValues, atName, new String[0], params, xmlSuite);
}
//
// Add the extra parameters we found
//
Collections.addAll(result, extraParameters);
// If the method declared an Object[] parameter and we have parameter values, inject them
for (int i = 0; i < types.length; i++) {
if (Object[].class.equals(types[i])) {
result.add(i, params.parameterValues);
}
}
return result.toArray(new Object[0]);
}
/**
* If the method has parameters, fill them in. Either by using a @DataProvider if any was
* provided, or by looking up <code><parameters></code> in testng.xml
*
* @return An Iterator over the values for each parameter of this method.
*/
public static ParameterHolder handleParameters(
ITestObjectFactory objectFactory,
ITestNGMethod testMethod,
Map<String, String> allParameterNames,
Object instance,
MethodParameters methodParams,
XmlSuite xmlSuite,
IAnnotationFinder annotationFinder,
Object fedInstance,
DataProviderHolder holder) {
return handleParameters(
objectFactory,
testMethod,
allParameterNames,
instance,
methodParams,
xmlSuite,
annotationFinder,
fedInstance,
holder,
"@Test");
}
/**
* If the method has parameters, fill them in. Either by using a @DataProvider if any was
* provided, or by looking up <code><parameters></code> in testng.xml
*
* @return An Iterator over the values for each parameter of this method.
*/
public static ParameterHolder handleParameters(
ITestObjectFactory objectFactory,
ITestNGMethod testMethod,
Map<String, String> allParameterNames,
Object instance,
MethodParameters methodParams,
XmlSuite xmlSuite,
IAnnotationFinder annotationFinder,
Object fedInstance,
DataProviderHolder holder,
String annotationName) {
/*
* Do we have a @DataProvider? If yes, then we have several
* sets of parameters for this method
*/
final IDataProviderMethod dataProviderMethod =
findDataProvider(
objectFactory,
instance,
testMethod.getTestClass(),
testMethod.getConstructorOrMethod(),
annotationFinder,
methodParams.context);
ParameterOrigin origin;
if (null != dataProviderMethod) {
if (testMethod instanceof TestNGMethod) {
((TestNGMethod) testMethod).setDataProviderMethod(dataProviderMethod);
}
int parameterCount = testMethod.getConstructorOrMethod().getParameterTypes().length;
for (int i = 0; i < parameterCount; i++) {
String n = "param" + i;
allParameterNames.put(n, n);
}
Class<?> retryClass = dataProviderMethod.retryUsing();
boolean shouldRetry = !retryClass.equals(IRetryDataProvider.DisableDataProviderRetries.class);
IRetryDataProvider retry = null;
if (shouldRetry) {
IObjectDispenser dispenser = Dispenser.newInstance(objectFactory);
BasicAttributes basic = new BasicAttributes(testMethod.getTestClass(), retryClass);
CreationAttributes attributes = new CreationAttributes(methodParams.context, basic, null);
retry = (IRetryDataProvider) dispenser.dispense(attributes);
}
Iterator<Object[]> initParams = null;
do {
for (IDataProviderListener dataProviderListener : holder.getListeners()) {
dataProviderListener.beforeDataProviderExecution(
dataProviderMethod, testMethod, methodParams.context);
}
try {
initParams =
MethodInvocationHelper.invokeDataProvider(
dataProviderMethod
.getInstance(), /* a test instance or null if the data provider is static*/
dataProviderMethod.getMethod(),
testMethod,
methodParams.context,
fedInstance,
annotationFinder);
shouldRetry = false;
} catch (RuntimeException e) {
for (IDataProviderListener each : holder.getListeners()) {
each.onDataProviderFailure(testMethod, methodParams.context, e);
}
if (shouldRetry) {
shouldRetry = retry.retry(dataProviderMethod);
} else {
throw e;
}
}
} while (shouldRetry);
for (IDataProviderListener dataProviderListener : holder.getListeners()) {
dataProviderListener.afterDataProviderExecution(
dataProviderMethod, testMethod, methodParams.context);
}
// If the data provider is restricting the indices to return, filter them out
final List<Integer> allIndices = new ArrayList<>();
allIndices.addAll(testMethod.getInvocationNumbers());
allIndices.addAll(dataProviderMethod.getIndices());
Iterator<Object[]> filteredParameters =
new FilteredParameters(initParams, testMethod, dataProviderMethod.getName(), allIndices);
testMethod.setMoreInvocationChecker(filteredParameters::hasNext);
for (IDataProviderInterceptor interceptor : holder.getInterceptors()) {
filteredParameters =
interceptor.intercept(
filteredParameters, dataProviderMethod, testMethod, methodParams.context);
}
if (dataProviderMethod instanceof DataProviderMethodRemovable) {
((DataProviderMethodRemovable) dataProviderMethod).setMethod(null);
((DataProviderMethodRemovable) dataProviderMethod).setInstance(null);
if (testMethod instanceof TestNGMethod) {
((TestNGMethod) testMethod).setDataProviderMethod(null);
}
}
return new ParameterHolder(
filteredParameters, ParameterOrigin.ORIGIN_DATA_PROVIDER, dataProviderMethod);
} else if (methodParams.xmlParameters.isEmpty()) {
origin = ParameterOrigin.NATIVE;
} else {
origin = ParameterOrigin.ORIGIN_XML;
}
//
// Normal case: we have only one set of parameters coming from testng.xml
//
allParameterNames.putAll(methodParams.xmlParameters);
// Create an Object[][] containing just one row of parameters
Object[][] allParameterValuesArray = new Object[1][];
allParameterValuesArray[0] =
createParameters(
testMethod.getConstructorOrMethod(),
methodParams,
annotationFinder,
xmlSuite,
annotationName);
// Mark that this method needs to have at least a certain
// number of invocations (needed later to call AfterGroups
// at the right time).
testMethod.setParameterInvocationCount(allParameterValuesArray.length);
// Turn it into an Iterable
Iterator<Object[]> parameters = new ArrayIterator(allParameterValuesArray);
return new ParameterHolder(parameters, origin, null);
}
/**
* Gets an array of parameter values returned by data provider or the ones that are injected based
* on parameter type. The method also checks for {@code NoInjection} annotation
*
* @param parameterValues parameter values from a data provider
* @param method method to be invoked
* @param context test context
*/
public static Object[] injectParameters(
Object[] parameterValues, Method method, ITestContext context) throws TestNGException {
MethodMatcherContext matcherContext =
new MethodMatcherContext(method, parameterValues, context, null);
final MethodMatcher matcher = new DataProviderMethodMatcher(matcherContext);
return matcher.getConformingArguments();
}
/** A parameter passing helper class. */
public static class MethodParameters {
private final Map<String, String> xmlParameters;
private final Method currentTestMethod;
private final ITestContext context;
private final Object[] parameterValues;
private final ITestResult testResult;
public MethodParameters(Map<String, String> params, Map<String, String> methodParams) {
this(params, methodParams, null, null, null, null);
}
public static MethodParameters newInstance(
Map<String, String> params, ITestNGMethod testNGMethod, ITestContext context) {
Map<String, String> methodParams =
testNGMethod.findMethodParameters(context.getCurrentXmlTest());
Method method = testNGMethod.getConstructorOrMethod().getMethod();
return new MethodParameters(params, methodParams, null, method, context, null);
}
/**
* @param params parameters found in the suite and test tags
* @param methodParams parameters found in the include tag
* @param pv parameter values to be used.
* @param m the {@link Method} object.
* @param ctx The {@link ITestContext} object representing the current test
* @param tr - The {@link ITestResult} object.
*/
public MethodParameters(
Map<String, String> params,
Map<String, String> methodParams,
Object[] pv,
Method m,
ITestContext ctx,
ITestResult tr) {
Map<String, String> allParams = Maps.newHashMap();
allParams.putAll(params);
allParams.putAll(methodParams);
xmlParameters = allParams;
currentTestMethod = m;
context = ctx;
parameterValues = pv;
testResult = tr;
}
}
}