Skip to content

Commit

Permalink
Fixing Review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
krmahadevan committed Mar 14, 2024
1 parent a3ec3a3 commit 5747e77
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 17 deletions.
9 changes: 0 additions & 9 deletions testng-core-api/src/main/java/org/testng/ITestNGMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.Callable;
import org.testng.annotations.CustomAttribute;
import org.testng.internal.ConstructorOrMethod;
Expand Down Expand Up @@ -297,12 +296,4 @@ default Class<?>[] getParameterTypes() {
default boolean isIgnoreFailure() {
return false;
}

/**
* @return - A <code>{@link UUID}</code> that represents a unique id which is associated with
* every test class object.
*/
default UUID getInstanceId() {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
import org.testng.xml.XmlTest;

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

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

Expand Down Expand Up @@ -157,7 +158,9 @@ public Object getInstance() {

@Override
public UUID getInstanceId() {
return m_instance.getInstanceId();
return Optional.ofNullable(m_instance)
.map(IObject.IdentifiableObject::getInstanceId)
.orElse(null);
}

/** {@inheritDoc} */
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.testng.internal;

import java.util.UUID;

public interface IInstanceIdentity {

/**
* @return - A <code>{@link UUID}</code> that represents a unique id which is associated with
* every test class object.
*/
UUID getInstanceId();

static Object getInstanceId(Object object) {
if (object instanceof IInstanceIdentity) {
return ((IInstanceIdentity) object).getInstanceId();
}
return object;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,18 @@
* generates a unique hashcode that is different from the original {@link ITestNGMethod} instance
* that it wraps.
*/
public class WrappedTestNGMethod implements ITestNGMethod {
public class WrappedTestNGMethod implements ITestNGMethod, IInstanceIdentity {
private final ITestNGMethod testNGMethod;
private final int multiplicationFactor = new Random().nextInt();

private final UUID uuid;

public WrappedTestNGMethod(ITestNGMethod testNGMethod) {
this.testNGMethod = testNGMethod;
uuid =
(testNGMethod instanceof BaseTestMethod)
? ((BaseTestMethod) testNGMethod).getInstanceId()
: UUID.randomUUID();
}

@Override
Expand Down Expand Up @@ -367,7 +373,7 @@ public String getQualifiedName() {

@Override
public UUID getInstanceId() {
return testNGMethod.getInstanceId();
return uuid;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
import javax.annotation.Nonnull;
import org.testng.ClassMethodMap;
import org.testng.IClassListener;
Expand Down Expand Up @@ -123,7 +122,7 @@ && doesTaskHavePreRequisites()

for (IMethodInstance testMethodInstance : m_methodInstances) {
ITestNGMethod testMethod = testMethodInstance.getMethod();
UUID key = Objects.requireNonNull(testMethod.getInstanceId());
Object key = Objects.requireNonNull(IInstanceIdentity.getInstanceId(testMethod));
if (canInvokeBeforeClassMethods()) {
try (KeyAwareAutoCloseableLock.AutoReleasable ignored = lock.lockForObject(key)) {
invokeBeforeClassMethods(testMethod.getTestClass(), testMethodInstance);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.testng.annotations.DataProvider;
import org.testng.annotations.Factory;
import org.testng.annotations.Test;
import org.testng.internal.IInstanceIdentity;

public class FactoryTestCase {

Expand Down Expand Up @@ -60,7 +61,8 @@ private static void record() {
ITestResult itr = Reporter.getCurrentTestResult();
ITestNGMethod itm = itr.getMethod();
objectMap
.computeIfAbsent(itm.getInstanceId(), k -> ConcurrentHashMap.newKeySet())
.computeIfAbsent(
(UUID) IInstanceIdentity.getInstanceId(itm), k -> ConcurrentHashMap.newKeySet())
.add(itm.getInstance());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.testng.internal.IInstanceIdentity;

public class SampleTestCase {
public static Map<UUID, Set<Object>> objectMap = new ConcurrentHashMap<>();
Expand Down Expand Up @@ -61,7 +62,8 @@ private static void record() {
ITestResult itr = Reporter.getCurrentTestResult();
ITestNGMethod itm = itr.getMethod();
objectMap
.computeIfAbsent(itm.getInstanceId(), k -> ConcurrentHashMap.newKeySet())
.computeIfAbsent(
(UUID) IInstanceIdentity.getInstanceId(itm), k -> ConcurrentHashMap.newKeySet())
.add(itm.getInstance());
}
}

0 comments on commit 5747e77

Please sign in to comment.