Skip to content

Commit

Permalink
Added: @BeforeMethod can now declare Object[] as a parameter, which w…
Browse files Browse the repository at this point in the history
…ill be filled by the parameters of the test method
  • Loading branch information
cbeust committed Jun 12, 2008
1 parent 16017d2 commit facba56
Show file tree
Hide file tree
Showing 13 changed files with 153 additions and 60 deletions.
1 change: 1 addition & 0 deletions CHANGES.txt
@@ -1,6 +1,7 @@
===========================================================================
5.8.1

Added: @BeforeMethod can now declare Object[] as a parameter, which will be filled by the parameters of the test method
Fixed: TESTNG-249: Overridden test methods were shadowing each other if specified with <include>
Fixed: DataProviders from @Factory-created tests were all invoked from the same instance
Fixed: enabled was not working on configuration methods
Expand Down
6 changes: 4 additions & 2 deletions src/main/org/testng/SuiteRunner.java
Expand Up @@ -283,7 +283,8 @@ private void privateRun() {
if(beforeSuiteMethods.values().size() > 0) {
invoker.invokeConfigurations(null,
beforeSuiteMethods.values().toArray(new ITestNGMethod[beforeSuiteMethods.size()]),
m_suite, m_suite.getParameters(),
m_suite, m_suite.getParameters(), null, /* no parameter values */

null /* instance */
);
}
Expand Down Expand Up @@ -314,7 +315,8 @@ private void privateRun() {
if (afterSuiteMethods.values().size() > 0) {
invoker.invokeConfigurations(null,
afterSuiteMethods.values().toArray(new ITestNGMethod[afterSuiteMethods.size()]),
m_suite, m_suite.getAllParameters(),
m_suite, m_suite.getAllParameters(), null, /* no parameter values */

null /* instance */);
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/org/testng/TestRunner.java
Expand Up @@ -509,6 +509,7 @@ private void beforeRun() {
testConfigurationMethods,
m_xmlTest.getSuite(),
m_xmlTest.getParameters(),
null, /* no parameter values */
null /* instance */);
}
}
Expand Down Expand Up @@ -729,6 +730,7 @@ private void afterRun() {
testConfigurationMethods,
m_xmlTest.getSuite(),
m_xmlTest.getParameters(),
null, /* no parameter values */
null /* instance */);
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/org/testng/internal/FactoryMethod.java
Expand Up @@ -61,7 +61,7 @@ public Object[] invoke() {
Parameters.handleParameters(this,
allParameterNames,
m_instance,
new Parameters.MethodParameters(m_xmlTest.getParameters(), null, m_testContext),
new Parameters.MethodParameters(m_xmlTest.getParameters(), null, null, m_testContext),
m_xmlTest.getSuite(),
m_annotationFinder,
null /* fedInstance */);
Expand Down
1 change: 1 addition & 0 deletions src/main/org/testng/internal/IInvoker.java
Expand Up @@ -30,6 +30,7 @@ public void invokeConfigurations(IClass testClass,
ITestNGMethod[] allMethods,
XmlSuite suite,
Map<String, String> parameters,
Object[] parameterValues,
Object instance);

/**
Expand Down
40 changes: 25 additions & 15 deletions src/main/org/testng/internal/Invoker.java
Expand Up @@ -82,16 +82,18 @@ public void invokeConfigurations(IClass testClass,
ITestNGMethod[] allMethods,
XmlSuite suite,
Map<String, String> params,
Object[] parameterValues,
Object instance)
{
invokeConfigurations(testClass, null, allMethods, suite, params, instance);
invokeConfigurations(testClass, null, allMethods, suite, params, parameterValues, instance);
}

private void invokeConfigurations(IClass testClass,
ITestNGMethod currentTestMethod,
ITestNGMethod[] allMethods,
XmlSuite suite,
Map<String, String> params,
Object[] parameterValues,
Object instance)
{
if(null == allMethods) {
Expand Down Expand Up @@ -138,12 +140,13 @@ private void invokeConfigurations(IClass testClass,

log(3, "Invoking " + Utils.detailedMethodName(tm, true));

Object[] parameters= Parameters.createConfigurationParameters(tm.getMethod(),
params,
currentTestMethod,
m_annotationFinder,
suite,
m_testContext);
Object[] parameters = Parameters.createConfigurationParameters(tm.getMethod(),
params,
parameterValues,
currentTestMethod,
m_annotationFinder,
suite,
m_testContext);
testResult.setParameters(parameters);

Object[] newInstances= (null != instance) ? new Object[] { instance } : instances;
Expand Down Expand Up @@ -458,7 +461,7 @@ private ITestResult invokeMethod(Object[] instances,
//
invokeConfigurations(testClass, tm,
filterConfigurationMethods(tm, beforeMethods, true /* beforeMethods */),
suite, params,
suite, params, parameterValues,
instances[instanceIndex]);

//
Expand Down Expand Up @@ -571,7 +574,7 @@ private ITestResult invokeMethod(Object[] instances,
//
invokeConfigurations(testClass, tm,
filterConfigurationMethods(tm, afterMethods, false /* beforeMethods */),
suite, params,
suite, params, parameterValues,
instances[instanceIndex]);

//
Expand Down Expand Up @@ -693,7 +696,9 @@ private void invokeBeforeGroupsConfigurations(ITestClass testClass,
if(beforeMethodsArray.length > 0) {
// don't pass the IClass or the instance as the method may be external
// the invocation must be similar to @BeforeTest/@BeforeSuite
invokeConfigurations(null, beforeMethodsArray, suite, params, null);
invokeConfigurations(null, beforeMethodsArray, suite, params,
null, /* no parameter values */
null);
}

//
Expand Down Expand Up @@ -749,7 +754,9 @@ private void invokeAfterGroupsConfigurations(ITestClass testClass,
ITestNGMethod[] afterMethodsArray = afterMethods.keySet().toArray(new ITestNGMethod[afterMethods.size()]);
// don't pass the IClass or the instance as the method may be external
// the invocation must be similar to @BeforeTest/@BeforeSuite
invokeConfigurations(null, afterMethodsArray, suite, params, null);
invokeConfigurations(null, afterMethodsArray, suite, params,
null, /* no parameter values */
null);

// Remove the groups so they don't get run again
groupMethods.removeAfterGroups(filteredGroups.keySet());
Expand Down Expand Up @@ -792,7 +799,7 @@ private int retryFailed(Object[] instances,
* one specific set. Should optimize it by only recreating the set needed.
*/
ParameterBag bag = createParameters(testClass, tm, parameters,
allParameters, suite, testContext, null /* fedInstance */);
allParameters, null, suite, testContext, null /* fedInstance */);
Object[] parameterValues = getParametersFromIndex(bag.parameterValues, parametersIndex);

result.add(
Expand All @@ -809,6 +816,7 @@ private ParameterBag createParameters(ITestClass testClass,
ITestNGMethod testMethod,
Map<String, String> parameters,
Map<String, String> allParameterNames,
Object[] parameterValues,
XmlSuite suite,
ITestContext testContext,
Object fedInstance)
Expand All @@ -823,7 +831,7 @@ private ParameterBag createParameters(ITestClass testClass,
}

ParameterBag bag= handleParameters(testMethod,
instance, allParameterNames, parameters, suite, testContext, fedInstance);
instance, allParameterNames, parameters, parameterValues, suite, testContext, fedInstance);

return bag;
}
Expand Down Expand Up @@ -905,7 +913,7 @@ public List<ITestResult> invokeTestMethods(ITestNGMethod testMethod,

Map<String, String> allParameterNames = new HashMap<String, String>();
ParameterBag bag = createParameters(testClass, testMethod,
parameters, allParameterNames, suite, testContext, instances[0]);
parameters, allParameterNames, null, suite, testContext, instances[0]);

if(bag.hasErrors()) {
failureCount = handleInvocationResults(testMethod,
Expand Down Expand Up @@ -1033,6 +1041,7 @@ private ParameterBag handleParameters(ITestNGMethod testMethod,
Object instance,
Map<String, String> allParameterNames,
Map<String, String> parameters,
Object[] parameterValues,
XmlSuite suite,
ITestContext testContext,
Object fedInstance)
Expand All @@ -1041,7 +1050,8 @@ private ParameterBag handleParameters(ITestNGMethod testMethod,
return new ParameterBag(Parameters.handleParameters(testMethod,
allParameterNames,
instance,
new Parameters.MethodParameters(parameters, testMethod.getMethod(), testContext),
new Parameters.MethodParameters(parameters, parameterValues,
testMethod.getMethod(), testContext),
suite,
m_annotationFinder,
fedInstance), null);
Expand Down
86 changes: 51 additions & 35 deletions src/main/org/testng/internal/Parameters.java
Expand Up @@ -59,15 +59,16 @@ public static Object[] createInstantiationParameters(Constructor ctor,
* @return
*/
public static Object[] createConfigurationParameters(Method m,
Map<String, String> params,
ITestNGMethod currentTestMethod,
IAnnotationFinder finder,
XmlSuite xmlSuite,
ITestContext ctx)
Map<String, String> params,
Object[] parameterValues,
ITestNGMethod currentTestMethod,
IAnnotationFinder finder,
XmlSuite xmlSuite,
ITestContext ctx)
{
Method currentTestMeth= currentTestMethod != null ?
currentTestMethod.getMethod() : null;
return createParameters(m, new MethodParameters(params, currentTestMeth, ctx),
return createParameters(m, new MethodParameters(params, parameterValues, currentTestMeth, ctx),
finder, xmlSuite, IConfigurationAnnotation.class, "@Configuration");
}

Expand Down Expand Up @@ -95,37 +96,39 @@ private static Object[] createParameters(String methodName,

checkParameterTypes(methodName, parameterTypes, methodAnnotation, parameterNames);

for(int i =0, j = 0; i < parameterTypes.length; i++) {
for(int i = 0, j = 0; i < parameterTypes.length; i++) {
if (Method.class.equals(parameterTypes[i])) {
vResult.add(params.currentTestMethod);
}
else if (ITestContext.class.equals(parameterTypes[i])) {
vResult.add(params.context);
}
else {
String p = parameterNames[j];
String value = params.xmlParameters.get(p);
if(null == value) {
// try SysEnv entries
value= System.getProperty(p);
}
if (null == value) {
if (optionalValues != null) {
value = optionalValues[i];
if (j < parameterNames.length) {
String p = parameterNames[j];
String value = params.xmlParameters.get(p);
if(null == value) {
// try SysEnv entries
value= System.getProperty(p);
}
if (null == value) {
throw new TestNGException("Parameter '" + p + "' is required by "
+ methodAnnotation
+ " on method "
+ methodName
if (optionalValues != null) {
value = optionalValues[i];
}
if (null == value) {
throw new TestNGException("Parameter '" + p + "' is required by "
+ methodAnnotation
+ " on method "
+ methodName
+ "\nbut has not been marked @Optional or defined "
+ (xmlSuite.getFileName() != null ? "in "
+ xmlSuite.getFileName() : ""));
+ xmlSuite.getFileName() : ""));
}
}

vResult.add(convertType(parameterTypes[i], value, p));
j++;
}

vResult.add(convertType(parameterTypes[i], value, p));
j++;
}
}

Expand All @@ -142,8 +145,10 @@ private static void checkParameterTypes(String methodName,
if(parameterNames.length == parameterTypes.length) return;

for(int i= parameterTypes.length - 1; i >= parameterNames.length; i--) {
if(!ITestContext.class.equals(parameterTypes[i])
&& !Method.class.equals(parameterTypes[i])) {
Class type = parameterTypes[i];
if(!ITestContext.class.equals(type)
&& !Method.class.equals(type)
&& !Object[].class.equals(type)) {
throw new TestNGException( "Method " + methodName + " requires "
+ parameterTypes.length + " parameters but "
+ parameterNames.length
Expand Down Expand Up @@ -269,9 +274,10 @@ private static Object[] createParameters(Method m, MethodParameters params,
// Try to find an @Parameters annotation
//
IParametersAnnotation annotation = (IParametersAnnotation) finder.findAnnotation(m, IParametersAnnotation.class);
Class<?>[] types = m.getParameterTypes();
if(null != annotation) {
String[] parameterNames = annotation.getValue();
extraParameters = createParameters(m.getName(), m.getParameterTypes(),
extraParameters = createParameters(m.getName(), types,
finder.findOptionalValues(m), atName, finder, parameterNames, params, xmlSuite);
}

Expand All @@ -282,15 +288,23 @@ private static Object[] createParameters(Method m, MethodParameters params,
IParameterizable a = (IParameterizable) finder.findAnnotation(m, annotationClass);
if(null != a && a.getParameters().length > 0) {
String[] parameterNames = a.getParameters();
extraParameters = createParameters(m.getName(), m.getParameterTypes(),
extraParameters = createParameters(m.getName(), types,
finder.findOptionalValues(m), atName, finder, parameterNames, params, xmlSuite);
}
else {
extraParameters = createParameters(m.getName(), m.getParameterTypes(),
extraParameters = createParameters(m.getName(), types,
finder.findOptionalValues(m), atName, finder, new String[0], params, xmlSuite);
}
}

// If the method declared an Object[] parameter and we have parameter values, inject them
for (int i = 0; i < types.length; i++) {
Class<?> type = types[i];
if (Object[].class.equals(type)) {
result.add(params.parameterValues);
}
}

//
// Add the extra parameters we found
//
Expand Down Expand Up @@ -371,19 +385,21 @@ public static class MethodParameters {
private final Map<String, String> xmlParameters;
private final Method currentTestMethod;
private final ITestContext context;
private Object[] parameterValues;

public MethodParameters(Map<String, String> params) {
this(params, null, null);
this(params, null, null, null);
}

public MethodParameters(Map<String, String> params, Method m) {
this(params, m, null);
this(params, null, m, null);
}

public MethodParameters(Map<String, String> params, Method m, ITestContext ctx) {
xmlParameters= params;
currentTestMethod= m;
context= ctx;
public MethodParameters(Map<String, String> params, Object[] pv, Method m, ITestContext ctx) {
xmlParameters = params;
currentTestMethod = m;
context = ctx;
parameterValues = pv;
}
}
}
2 changes: 2 additions & 0 deletions src/main/org/testng/internal/TestMethodWorker.java
Expand Up @@ -168,6 +168,7 @@ protected void invokeBeforeClassMethods(ITestClass testClass, IMethodInstance mi
testClass.getBeforeClassMethods(),
m_suite,
m_parameters,
null, /* no parameter values */
instance);
}
}
Expand Down Expand Up @@ -211,6 +212,7 @@ protected void invokeAfterClassMethods(ITestClass testClass, IMethodInstance mi)
afterClassMethods,
m_suite,
m_parameters,
null, /* no parameter values */
inst);
}
}
Expand Down

0 comments on commit facba56

Please sign in to comment.