Skip to content

Commit 7535dd3

Browse files
committed
refacto: proposal implementation for testng-team#3111
1 parent f11c4fc commit 7535dd3

File tree

68 files changed

+419
-474
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+419
-474
lines changed
Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
package org.testng;
22

3-
import java.util.Optional;
4-
53
/** Represents a factory method */
64
public interface IFactoryMethod {
75

8-
/**
9-
* @return - Returns parameters associated with a factory method wrapped within a {@link Optional}
10-
*/
11-
Optional<Object[]> getParameters();
6+
/** @return - Returns parameters associated with a factory method */
7+
Object[] getParameters();
128
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package org.testng;
22

3+
import java.util.UUID;
4+
35
/**
46
* This class defines a pair of instance/class. A method with @Factory can return an array of these
57
* objects instead of Object[] so that instances can be dynamic proxies or mock objects and still
68
* provide enough information to TestNG to figure out what classes the annotations should be looked
79
* up in.
8-
*
9-
* @author <a href="mailto:[email protected]">Cedric Beust</a>
1010
*/
1111
public interface IInstanceInfo<T> {
12+
UUID getUid();
1213

1314
/** @return The instance on which the tests will be invoked. */
1415
T getInstance();

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ public interface IMethodInstance {
55

66
ITestNGMethod getMethod();
77

8+
// TODO deprecate return IInstanceInfo<?>
89
Object getInstance();
910
}

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package org.testng;
22

33
/** Represents the ability to retrieve the parameters associated with a factory method. */
4-
public interface ITestClassInstance<T> {
5-
6-
/** @return - The actual instance associated with a factory method */
7-
T getInstance();
4+
public interface ITestClassInstance<T> extends IInstanceInfo<T> {
85

96
/**
107
* @return - The actual index of instance associated with a factory method. This index has a 1:1
@@ -24,6 +21,8 @@ public interface ITestClassInstance<T> {
2421
*/
2522
int getInvocationIndex();
2623

24+
IFactoryMethod getFactoryMethod();
25+
2726
static Object embeddedInstance(Object original) {
2827
if (original instanceof ITestClassInstance) {
2928
return ((ITestClassInstance) original).getInstance();

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

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import java.util.List;
44
import java.util.Map;
5-
import java.util.Optional;
65
import java.util.Set;
76
import java.util.concurrent.Callable;
87
import org.testng.annotations.CustomAttribute;
@@ -39,6 +38,7 @@ public interface ITestNGMethod extends Cloneable {
3938
*/
4039
String getMethodName();
4140

41+
// TODO deprecate return IInstanceInfo<?>
4242
Object getInstance();
4343

4444
/**
@@ -266,18 +266,11 @@ default boolean isDataDriven() {
266266
* @return - A {@link IParameterInfo} object that represents details about the parameters
267267
* associated with the factory method.
268268
*/
269+
@Deprecated
269270
default IParameterInfo getFactoryMethodParamsInfo() {
270271
return null;
271272
}
272273

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-
281274
/**
282275
* @return - An array of {@link CustomAttribute} that represents the custom attributes associated
283276
* with a test.

testng-core-api/src/main/java/org/testng/internal/IParameterInfo.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
package org.testng.internal;
22

33
import org.testng.ITestClassInstance;
4-
import org.testng.ITestNGMethod;
54

65
/**
76
* Represents the ability to retrieve the parameters associated with a factory method.
87
*
98
* @deprecated - This interface stands deprecated as of TestNG <code>7.11.0</code>.
109
*/
1110
@Deprecated
12-
public interface IParameterInfo extends ITestClassInstance {
11+
public interface IParameterInfo<T> extends ITestClassInstance<T> {
1312
/**
1413
* @return - The parameters associated with the factory method as an array.
1514
* @deprecated - This method stands deprecated as of TestNG <code>7.11.0</code> Please use {@link
16-
* ITestNGMethod#getFactoryMethod()} to retrieve the parameters.
15+
* ITestClassInstance#getFactoryMethod()} to retrieve the parameters.
1716
*/
1817
@Deprecated
1918
Object[] getParameters();

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
* @author <a href='mailto:the[dot]mindstorm[at]gmail[dot]com'>Alex Popescu</a>
1717
*/
1818
public class ClassMethodMap {
19-
private final Map<Object, Collection<ITestNGMethod>> classMap = Maps.newConcurrentMap();
19+
private final Map<IInstanceInfo<?>, Collection<ITestNGMethod>> classMap = Maps.newConcurrentMap();
2020
// These two variables are used throughout the workers to keep track
2121
// of what beforeClass/afterClass methods have been invoked
22-
private final Map<ITestClass, Set<Object>> beforeClassMethods = Maps.newConcurrentMap();
23-
private final Map<ITestClass, Set<Object>> afterClassMethods = Maps.newConcurrentMap();
22+
private final Map<ITestClass, Set<IInstanceInfo<?>>> beforeClassMethods = Maps.newConcurrentMap();
23+
private final Map<ITestClass, Set<IInstanceInfo<?>>> afterClassMethods = Maps.newConcurrentMap();
2424

2525
public ClassMethodMap(List<ITestNGMethod> methods, XmlMethodSelector xmlMethodSelector) {
2626
for (ITestNGMethod m : methods) {
@@ -31,7 +31,7 @@ public ClassMethodMap(List<ITestNGMethod> methods, XmlMethodSelector xmlMethodSe
3131
continue;
3232
}
3333

34-
Object instance = m.getInstance();
34+
IInstanceInfo<?> instance = (IInstanceInfo<?>) m.getInstance();
3535
classMap.computeIfAbsent(instance, k -> new ConcurrentLinkedQueue<>()).add(m);
3636
}
3737
}
@@ -43,7 +43,7 @@ public ClassMethodMap(List<ITestNGMethod> methods, XmlMethodSelector xmlMethodSe
4343
* @param instance The test instance
4444
* @return true if it is the last of its class
4545
*/
46-
public boolean removeAndCheckIfLast(ITestNGMethod m, Object instance) {
46+
public boolean removeAndCheckIfLast(ITestNGMethod m, IInstanceInfo<?> instance) {
4747
Collection<ITestNGMethod> l = classMap.get(instance);
4848
if (l == null) {
4949
throw new IllegalStateException(
@@ -60,19 +60,19 @@ public boolean removeAndCheckIfLast(ITestNGMethod m, Object instance) {
6060
return true;
6161
}
6262

63-
public Map<ITestClass, Set<Object>> getInvokedBeforeClassMethods() {
63+
public Map<ITestClass, Set<IInstanceInfo<?>>> getInvokedBeforeClassMethods() {
6464
return beforeClassMethods;
6565
}
6666

67-
public Map<ITestClass, Set<Object>> getInvokedAfterClassMethods() {
67+
public Map<ITestClass, Set<IInstanceInfo<?>>> getInvokedAfterClassMethods() {
6868
return afterClassMethods;
6969
}
7070

7171
public void clear() {
72-
for (Set<Object> instances : beforeClassMethods.values()) {
72+
for (Set<IInstanceInfo<?>> instances : beforeClassMethods.values()) {
7373
instances.clear();
7474
}
75-
for (Set<Object> instances : afterClassMethods.values()) {
75+
for (Set<IInstanceInfo<?>> instances : afterClassMethods.values()) {
7676
instances.clear();
7777
}
7878
beforeClassMethods.clear();

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

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -103,49 +103,47 @@ private static boolean belongToDifferentClassHierarchy(
103103

104104
private static boolean hasInstance(
105105
ITestNGMethod baseClassMethod, ITestNGMethod derivedClassMethod) {
106-
Object baseInstance = baseClassMethod.getInstance();
107-
Object derivedInstance = derivedClassMethod.getInstance();
106+
IInstanceInfo<?> baseInstance = (IInstanceInfo<?>) baseClassMethod.getInstance();
107+
IInstanceInfo<?> derivedInstance = (IInstanceInfo<?>) derivedClassMethod.getInstance();
108108
boolean result = derivedInstance != null || baseInstance != null;
109-
boolean params =
110-
baseClassMethod.getFactoryMethod().flatMap(IFactoryMethod::getParameters).isPresent();
111-
112-
if (result && params && RuntimeBehavior.enforceThreadAffinity()) {
113-
return hasSameParameters(baseClassMethod, derivedClassMethod);
109+
if (RuntimeBehavior.enforceThreadAffinity()
110+
&& result
111+
&& baseClassMethod instanceof ITestClassInstance
112+
&& derivedClassMethod instanceof ITestClassInstance) {
113+
return hasSameParameters(
114+
(ITestClassInstance<?>) baseClassMethod, (ITestClassInstance<?>) derivedClassMethod);
114115
}
115116
return result;
116117
}
117118

118-
private static boolean hasSameParameters(
119-
ITestNGMethod baseClassMethod, ITestNGMethod derivedClassMethod) {
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;
131-
}
132-
133119
private static boolean isSameInstance(
134120
ITestNGMethod baseClassMethod, ITestNGMethod derivedClassMethod) {
135-
Object baseInstance = baseClassMethod.getInstance();
136-
Object derivedInstance = derivedClassMethod.getInstance();
121+
IInstanceInfo<?> baseInstance = (IInstanceInfo<?>) baseClassMethod.getInstance();
122+
IInstanceInfo<?> derivedInstance = (IInstanceInfo<?>) derivedClassMethod.getInstance();
137123
boolean nonNullInstances = derivedInstance != null && baseInstance != null;
138124
if (!nonNullInstances) {
139125
return false;
140126
}
141-
if (null != baseClassMethod.getFactoryMethodParamsInfo()
142-
&& RuntimeBehavior.enforceThreadAffinity()) {
127+
if (RuntimeBehavior.enforceThreadAffinity()
128+
&& baseClassMethod instanceof ITestClassInstance
129+
&& derivedClassMethod instanceof ITestClassInstance) {
143130
return baseInstance.getClass().isAssignableFrom(derivedInstance.getClass())
144-
&& hasSameParameters(baseClassMethod, derivedClassMethod);
131+
&& hasSameParameters(
132+
(ITestClassInstance<?>) baseClassMethod, (ITestClassInstance<?>) derivedClassMethod);
145133
}
146134
return baseInstance.getClass().isAssignableFrom(derivedInstance.getClass());
147135
}
148136

137+
private static boolean hasSameParameters(
138+
ITestClassInstance<?> baseClassMethod, ITestClassInstance<?> derivedClassMethod) {
139+
Object[] firstParams = baseClassMethod.getFactoryMethod().getParameters();
140+
Object[] secondParams = derivedClassMethod.getFactoryMethod().getParameters();
141+
if (firstParams.length == 0 || secondParams.length == 0) {
142+
return false;
143+
}
144+
return firstParams[0].equals(secondParams[0]);
145+
}
146+
149147
private static String constructMethodNameUsingTestClass(
150148
String currentMethodName, ITestNGMethod m) {
151149
int lastIndex = currentMethodName.lastIndexOf('.');

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ public List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestConte
1515

1616
/** The default method interceptor which sorts methods by instances (i.e. by class). */
1717
private List<IMethodInstance> groupMethodsByInstance(List<IMethodInstance> methods) {
18-
List<Object> instanceList = Lists.newArrayList();
18+
List<IInstanceInfo<?>> instanceList = Lists.newArrayList();
1919
Map<Object, List<IMethodInstance>> map = Maps.newLinkedHashMap();
2020
for (IMethodInstance mi : methods) {
21-
Object instance = mi.getInstance();
21+
IInstanceInfo<?> instance = (IInstanceInfo<?>) mi.getInstance();
2222
if (!instanceList.contains(instance)) {
2323
instanceList.add(instance);
2424
}

0 commit comments

Comments
 (0)