Skip to content

Commit f11c4fc

Browse files
committed
Fixing review comments
1 parent ac7ab96 commit f11c4fc

File tree

9 files changed

+83
-25
lines changed

9 files changed

+83
-25
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.testng;
2+
3+
import java.util.Optional;
4+
5+
/** Represents a factory method */
6+
public interface IFactoryMethod {
7+
8+
/**
9+
* @return - Returns parameters associated with a factory method wrapped within a {@link Optional}
10+
*/
11+
Optional<Object[]> getParameters();
12+
}

testng-core-api/src/main/java/org/testng/ITestClassInstance.java

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package org.testng;
22

33
/** Represents the ability to retrieve the parameters associated with a factory method. */
4-
public interface ITestClassInstance {
4+
public interface ITestClassInstance<T> {
55

66
/** @return - The actual instance associated with a factory method */
7-
Object getInstance();
7+
T getInstance();
88

99
/**
1010
* @return - The actual index of instance associated with a factory method. This index has a 1:1
@@ -24,9 +24,6 @@ public interface ITestClassInstance {
2424
*/
2525
int getInvocationIndex();
2626

27-
/** @return - The parameters associated with the factory method as an array. */
28-
Object[] getParameters();
29-
3027
static Object embeddedInstance(Object original) {
3128
if (original instanceof ITestClassInstance) {
3229
return ((ITestClassInstance) original).getInstance();

testng-core-api/src/main/java/org/testng/ITestNGMethod.java

+9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.List;
44
import java.util.Map;
5+
import java.util.Optional;
56
import java.util.Set;
67
import java.util.concurrent.Callable;
78
import org.testng.annotations.CustomAttribute;
@@ -269,6 +270,14 @@ default IParameterInfo getFactoryMethodParamsInfo() {
269270
return null;
270271
}
271272

273+
/**
274+
* @return - A {@link IFactoryMethod} implementation that contains attributes associated with a
275+
* factory method, wrapped within an {@link Optional}.
276+
*/
277+
default Optional<IFactoryMethod> getFactoryMethod() {
278+
return Optional.empty();
279+
}
280+
272281
/**
273282
* @return - An array of {@link CustomAttribute} that represents the custom attributes associated
274283
* with a test.
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
package org.testng.internal;
22

33
import org.testng.ITestClassInstance;
4+
import org.testng.ITestNGMethod;
45

56
/**
67
* Represents the ability to retrieve the parameters associated with a factory method.
78
*
89
* @deprecated - This interface stands deprecated as of TestNG <code>7.11.0</code>.
910
*/
1011
@Deprecated
11-
public interface IParameterInfo extends ITestClassInstance {}
12+
public interface IParameterInfo extends ITestClassInstance {
13+
/**
14+
* @return - The parameters associated with the factory method as an array.
15+
* @deprecated - This method stands deprecated as of TestNG <code>7.11.0</code> Please use {@link
16+
* ITestNGMethod#getFactoryMethod()} to retrieve the parameters.
17+
*/
18+
@Deprecated
19+
Object[] getParameters();
20+
}

testng-core/src/main/java/org/testng/DependencyMap.java

+12-6
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,7 @@ private static boolean hasInstance(
107107
Object derivedInstance = derivedClassMethod.getInstance();
108108
boolean result = derivedInstance != null || baseInstance != null;
109109
boolean params =
110-
null != baseClassMethod.getFactoryMethodParamsInfo()
111-
&& null != derivedClassMethod.getFactoryMethodParamsInfo().getParameters();
110+
baseClassMethod.getFactoryMethod().flatMap(IFactoryMethod::getParameters).isPresent();
112111

113112
if (result && params && RuntimeBehavior.enforceThreadAffinity()) {
114113
return hasSameParameters(baseClassMethod, derivedClassMethod);
@@ -118,10 +117,17 @@ private static boolean hasInstance(
118117

119118
private static boolean hasSameParameters(
120119
ITestNGMethod baseClassMethod, ITestNGMethod derivedClassMethod) {
121-
return baseClassMethod
122-
.getFactoryMethodParamsInfo()
123-
.getParameters()[0]
124-
.equals(derivedClassMethod.getFactoryMethodParamsInfo().getParameters()[0]);
120+
Optional<IFactoryMethod> first = baseClassMethod.getFactoryMethod();
121+
Optional<IFactoryMethod> second = derivedClassMethod.getFactoryMethod();
122+
if (first.isPresent() && second.isPresent()) {
123+
Optional<Object[]> firstParams = first.get().getParameters();
124+
Optional<Object[]> secondParams = second.get().getParameters();
125+
if (firstParams.isPresent() && secondParams.isPresent()) {
126+
return firstParams.get()[0].equals(secondParams.get()[0]);
127+
}
128+
return false;
129+
}
130+
return false;
125131
}
126132

127133
private static boolean isSameInstance(

testng-core/src/main/java/org/testng/internal/BaseTestMethod.java

+22-5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import java.util.regex.Pattern;
1818
import java.util.stream.Collectors;
1919
import org.testng.IClass;
20+
import org.testng.IFactoryMethod;
2021
import org.testng.IRetryAnalyzer;
2122
import org.testng.ITestClass;
2223
import org.testng.ITestClassInstance;
@@ -104,6 +105,10 @@ public BaseTestMethod(
104105
m_instance = instance;
105106
}
106107

108+
protected final IObject.IdentifiableObject identifiableObject() {
109+
return m_instance;
110+
}
111+
107112
/** {@inheritDoc} */
108113
@Override
109114
public boolean isAlwaysRun() {
@@ -300,6 +305,19 @@ public void setTimeOut(long timeOut) {
300305
m_timeOut = timeOut;
301306
}
302307

308+
@Override
309+
public Optional<IFactoryMethod> getFactoryMethod() {
310+
IObject.IdentifiableObject identifiable = identifiableObject();
311+
if (identifiable == null) {
312+
return Optional.empty();
313+
}
314+
Object instance = identifiableObject().getInstance();
315+
if (instance instanceof ParameterInfo) {
316+
return Optional.of(() -> Optional.of(((ParameterInfo) instance).getParameters()));
317+
}
318+
return ITestNGMethod.super.getFactoryMethod();
319+
}
320+
303321
/**
304322
* {@inheritDoc}
305323
*
@@ -539,11 +557,10 @@ public String getSimpleName() {
539557
}
540558

541559
private String instanceParameters() {
542-
IParameterInfo instance = getFactoryMethodParamsInfo();
543-
if (instance != null) {
544-
return ", instance params:" + Arrays.toString(instance.getParameters());
545-
}
546-
return "";
560+
return getFactoryMethod()
561+
.flatMap(IFactoryMethod::getParameters)
562+
.map(it -> ", instance params:" + Arrays.toString(it))
563+
.orElse("");
547564
}
548565

549566
protected String getSignature() {

testng-core/src/main/java/org/testng/internal/MethodSorting.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.util.Objects;
66
import java.util.Optional;
77
import java.util.UUID;
8+
import org.testng.IFactoryMethod;
89
import org.testng.ITestNGMethod;
910

1011
public enum MethodSorting implements Comparator<ITestNGMethod> {
@@ -31,8 +32,10 @@ public int compare(ITestNGMethod o1, ITestNGMethod o2) {
3132
.thenComparing(Object::toString)
3233
.thenComparing(
3334
method ->
34-
Optional.ofNullable(method.getFactoryMethodParamsInfo())
35-
.map(it -> Arrays.toString(it.getParameters()))
35+
method
36+
.getFactoryMethod()
37+
.flatMap(IFactoryMethod::getParameters)
38+
.map(Arrays::toString)
3639
.orElse(""))
3740
.thenComparing(this::objectEquality);
3841
return comparator.compare(o1, o2);

testng-core/src/test/java/test/configuration/issue2426/MyMethodListener.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.util.HashMap;
44
import java.util.Map;
55
import org.testng.IConfigurationListener;
6+
import org.testng.IFactoryMethod;
67
import org.testng.ITestResult;
78
import org.testng.annotations.*;
89

@@ -12,7 +13,11 @@ public class MyMethodListener implements IConfigurationListener {
1213

1314
@Override
1415
public void onConfigurationSuccess(ITestResult tr) {
15-
Object[] values = tr.getMethod().getFactoryMethodParamsInfo().getParameters();
16+
Object[] values =
17+
tr.getMethod()
18+
.getFactoryMethod()
19+
.flatMap(IFactoryMethod::getParameters)
20+
.orElse(new Object[0]);
1621
if (tr.getMethod().isBeforeSuiteConfiguration()) {
1722
contents.put(BeforeSuite.class, values);
1823
}

testng-runner-api/src/main/java/org/testng/internal/TestResult.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import javax.annotation.Nonnull;
1313
import org.testng.IAttributes;
1414
import org.testng.IClass;
15+
import org.testng.IFactoryMethod;
1516
import org.testng.ITest;
1617
import org.testng.ITestClassInstance;
1718
import org.testng.ITestContext;
@@ -304,11 +305,10 @@ public Object getInstance() {
304305

305306
@Override
306307
public Object[] getFactoryParameters() {
307-
IParameterInfo instance = this.m_method.getFactoryMethodParamsInfo();
308-
if (instance != null) {
309-
return instance.getParameters();
310-
}
311-
return new Object[0];
308+
return this.m_method
309+
.getFactoryMethod()
310+
.flatMap(IFactoryMethod::getParameters)
311+
.orElse(new Object[0]);
312312
}
313313

314314
@Override

0 commit comments

Comments
 (0)