Skip to content

Commit afc7b6c

Browse files
committed
Fixing Review comments
1 parent a3ec3a3 commit afc7b6c

File tree

7 files changed

+39
-15
lines changed

7 files changed

+39
-15
lines changed

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

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import java.util.List;
44
import java.util.Map;
55
import java.util.Set;
6-
import java.util.UUID;
76
import java.util.concurrent.Callable;
87
import org.testng.annotations.CustomAttribute;
98
import org.testng.internal.ConstructorOrMethod;
@@ -297,12 +296,4 @@ default Class<?>[] getParameterTypes() {
297296
default boolean isIgnoreFailure() {
298297
return false;
299298
}
300-
301-
/**
302-
* @return - A <code>{@link UUID}</code> that represents a unique id which is associated with
303-
* every test class object.
304-
*/
305-
default UUID getInstanceId() {
306-
return null;
307-
}
308299
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
import org.testng.xml.XmlTest;
3636

3737
/** Superclass to represent both &#64;Test and &#64;Configuration methods. */
38-
public abstract class BaseTestMethod implements ITestNGMethod, IInvocationStatus {
38+
public abstract class BaseTestMethod
39+
implements ITestNGMethod, IInvocationStatus, IInstanceIdentity {
3940

4041
private static final Pattern SPACE_SEPARATOR_PATTERN = Pattern.compile(" +");
4142

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package org.testng.internal;
2+
3+
import java.util.Optional;
4+
import java.util.UUID;
5+
import org.testng.log4testng.Logger;
6+
7+
public interface IInstanceIdentity {
8+
9+
/**
10+
* @return - A <code>{@link UUID}</code> that represents a unique id which is associated with
11+
* every test class object.
12+
*/
13+
UUID getInstanceId();
14+
15+
String fmt = "[Attention] %s does not implement %s. Generating an on the fly UUID.";
16+
Logger logger = Logger.getLogger(IInstanceIdentity.class);
17+
18+
static UUID getInstanceId(Object object) {
19+
if (object instanceof IInstanceIdentity) {
20+
return ((IInstanceIdentity) object).getInstanceId();
21+
}
22+
if (logger.isDebugEnabled()) {
23+
String className =
24+
Optional.ofNullable(object).map(it -> it.getClass().getName()).orElse("[Unknown Class]");
25+
String msg = String.format(fmt, className, IInstanceIdentity.class.getName());
26+
logger.debug(msg);
27+
}
28+
return UUID.randomUUID();
29+
}
30+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* generates a unique hashcode that is different from the original {@link ITestNGMethod} instance
1818
* that it wraps.
1919
*/
20-
public class WrappedTestNGMethod implements ITestNGMethod {
20+
public class WrappedTestNGMethod implements ITestNGMethod, IInstanceIdentity {
2121
private final ITestNGMethod testNGMethod;
2222
private final int multiplicationFactor = new Random().nextInt();
2323

@@ -367,7 +367,7 @@ public String getQualifiedName() {
367367

368368
@Override
369369
public UUID getInstanceId() {
370-
return testNGMethod.getInstanceId();
370+
return IInstanceIdentity.getInstanceId(testNGMethod);
371371
}
372372

373373
@Override

testng-core/src/main/java/org/testng/internal/invokers/TestMethodWorker.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ && doesTaskHavePreRequisites()
123123

124124
for (IMethodInstance testMethodInstance : m_methodInstances) {
125125
ITestNGMethod testMethod = testMethodInstance.getMethod();
126-
UUID key = Objects.requireNonNull(testMethod.getInstanceId());
126+
UUID key = Objects.requireNonNull(IInstanceIdentity.getInstanceId(testMethod));
127127
if (canInvokeBeforeClassMethods()) {
128128
try (KeyAwareAutoCloseableLock.AutoReleasable ignored = lock.lockForObject(key)) {
129129
invokeBeforeClassMethods(testMethod.getTestClass(), testMethodInstance);

testng-core/src/test/java/test/factory/issue3079/FactoryTestCase.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import org.testng.annotations.DataProvider;
1616
import org.testng.annotations.Factory;
1717
import org.testng.annotations.Test;
18+
import org.testng.internal.IInstanceIdentity;
1819

1920
public class FactoryTestCase {
2021

@@ -60,7 +61,7 @@ private static void record() {
6061
ITestResult itr = Reporter.getCurrentTestResult();
6162
ITestNGMethod itm = itr.getMethod();
6263
objectMap
63-
.computeIfAbsent(itm.getInstanceId(), k -> ConcurrentHashMap.newKeySet())
64+
.computeIfAbsent(IInstanceIdentity.getInstanceId(itm), k -> ConcurrentHashMap.newKeySet())
6465
.add(itm.getInstance());
6566
}
6667
}

testng-core/src/test/java/test/factory/issue3079/SampleTestCase.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.testng.annotations.BeforeMethod;
1414
import org.testng.annotations.DataProvider;
1515
import org.testng.annotations.Test;
16+
import org.testng.internal.IInstanceIdentity;
1617

1718
public class SampleTestCase {
1819
public static Map<UUID, Set<Object>> objectMap = new ConcurrentHashMap<>();
@@ -61,7 +62,7 @@ private static void record() {
6162
ITestResult itr = Reporter.getCurrentTestResult();
6263
ITestNGMethod itm = itr.getMethod();
6364
objectMap
64-
.computeIfAbsent(itm.getInstanceId(), k -> ConcurrentHashMap.newKeySet())
65+
.computeIfAbsent(IInstanceIdentity.getInstanceId(itm), k -> ConcurrentHashMap.newKeySet())
6566
.add(itm.getInstance());
6667
}
6768
}

0 commit comments

Comments
 (0)