Skip to content

Commit

Permalink
Fixing review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
krmahadevan committed Apr 24, 2024
1 parent 64f8711 commit 5462ee1
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.testng;

/** Represents the ability to retrieve the parameters associated with a factory method. */
public interface IParameterInfo {
public interface ITestClassInstance {

/** @return - The actual instance associated with a factory method */
Object getInstance();
Expand All @@ -22,14 +22,14 @@ public interface IParameterInfo {
* each of the invocations to this method would return a value from <code>0</code> to <code>3
* </code>
*/
int currentIndex();
int getInvocationIndex();

/** @return - The parameters associated with the factory method as an array. */
Object[] getParameters();

static Object embeddedInstance(Object original) {
if (original instanceof IParameterInfo) {
return ((IParameterInfo) original).getInstance();
if (original instanceof ITestClassInstance) {
return ((ITestClassInstance) original).getInstance();
}
return original;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package org.testng.internal;

import org.testng.ITestClassInstance;

/**
* Represents the ability to retrieve the parameters associated with a factory method.
*
* @deprecated - This interface stands deprecated as of TestNG <code>7.11.0</code>.
*/
@Deprecated
public interface IParameterInfo extends org.testng.IParameterInfo {}
public interface IParameterInfo extends ITestClassInstance {}
4 changes: 2 additions & 2 deletions testng-core/src/main/java/org/testng/TestClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ private void initTestClassesAndInstances() {
IObject.IdentifiableObject[] instances = getObjects(true, this.m_errorMsgPrefix);
Arrays.stream(instances)
.map(IdentifiableObject::getInstance)
.map(IParameterInfo::embeddedInstance)
.map(ITestClassInstance::embeddedInstance)
.filter(it -> it instanceof ITest)
.findFirst()
.ifPresent(it -> testName = ((ITest) it).getTestName());
Expand Down Expand Up @@ -208,7 +208,7 @@ private void initMethods() {
true,
xmlTest,
eachInstance);
Object instance = IParameterInfo.embeddedInstance(eachInstance.getInstance());
Object instance = ITestClassInstance.embeddedInstance(eachInstance.getInstance());
beforeClassConfig.put(instance, m_beforeClassMethods);
m_afterClassMethods =
ConfigurationMethod.createClassConfigurationMethods(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.testng.IClass;
import org.testng.IRetryAnalyzer;
import org.testng.ITestClass;
import org.testng.ITestClassInstance;
import org.testng.ITestNGMethod;
import org.testng.ITestObjectFactory;
import org.testng.ITestResult;
Expand Down Expand Up @@ -153,7 +154,7 @@ public String getMethodName() {
public Object getInstance() {
return Optional.ofNullable(m_instance)
.map(IObject.IdentifiableObject::getInstance)
.map(org.testng.IParameterInfo::embeddedInstance)
.map(ITestClassInstance::embeddedInstance)
.orElse(null);
}

Expand Down
3 changes: 2 additions & 1 deletion testng-core/src/main/java/org/testng/internal/ClassImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.Map;
import org.testng.IClass;
import org.testng.ITest;
import org.testng.ITestClassInstance;
import org.testng.ITestContext;
import org.testng.ITestObjectFactory;
import org.testng.annotations.ITestAnnotation;
Expand Down Expand Up @@ -162,7 +163,7 @@ public void addInstance(Object instance) {
}

private static int computeHashCode(Object instance) {
return org.testng.IParameterInfo.embeddedInstance(instance).hashCode();
return ITestClassInstance.embeddedInstance(instance).hashCode();
}

private DetailedAttributes newDetailedAttributes(boolean create, String errMsgPrefix) {
Expand Down
13 changes: 7 additions & 6 deletions testng-core/src/main/java/org/testng/internal/FactoryMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.testng.IDataProviderInterceptor;
import org.testng.IDataProviderListener;
import org.testng.IInstanceInfo;
import org.testng.ITestClassInstance;
import org.testng.ITestContext;
import org.testng.ITestMethodFinder;
import org.testng.ITestNGListener;
Expand Down Expand Up @@ -145,8 +146,8 @@ private static String[] getAllGroups(
return groups.toArray(new String[0]);
}

public org.testng.IParameterInfo[] invoke() {
List<org.testng.IParameterInfo> result = Lists.newArrayList();
public ITestClassInstance[] invoke() {
List<ITestClassInstance> result = Lists.newArrayList();

Map<String, String> allParameterNames = Maps.newHashMap();
Parameters.MethodParameters methodParameters =
Expand Down Expand Up @@ -175,7 +176,7 @@ public org.testng.IParameterInfo[] invoke() {
try {
List<Integer> indices = factoryAnnotation.getIndices();
int position = 0;
AtomicInteger counter = new AtomicInteger(0);
AtomicInteger invocationCounter = new AtomicInteger(0);
while (parameterIterator.hasNext()) {
Object[] parameters = parameterIterator.next();
if (parameters == null) {
Expand All @@ -201,15 +202,15 @@ public org.testng.IParameterInfo[] invoke() {
.map(
instance ->
new ParameterInfo(
instance, instancePosition, parameters, counter.getAndIncrement()))
instance, instancePosition, parameters, invocationCounter.getAndIncrement()))
.collect(Collectors.toList()));
} else {
for (Integer index : indices) {
int i = index - position;
if (i >= 0 && i < testInstances.length) {
result.add(
new ParameterInfo(
testInstances[i], position, parameters, counter.getAndIncrement()));
testInstances[i], position, parameters, invocationCounter.getAndIncrement()));
}
}
}
Expand All @@ -218,7 +219,7 @@ public org.testng.IParameterInfo[] invoke() {
if (indices == null || indices.isEmpty() || indices.contains(position)) {
Object instance = m_objectFactory.newInstance(com.getConstructor(), parameters);
result.add(
new ParameterInfo(instance, position, parameters, counter.getAndIncrement()));
new ParameterInfo(instance, position, parameters, invocationCounter.getAndIncrement()));
}
position++;
}
Expand Down
10 changes: 5 additions & 5 deletions testng-core/src/main/java/org/testng/internal/ParameterInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ public class ParameterInfo implements IParameterInfo {
private final Object instance;
private final int index;
private final Object[] parameters;
private final int currentIndex;
private final int invocationIndex;

public ParameterInfo(Object instance, int index, Object[] parameters, int currentIndex) {
public ParameterInfo(Object instance, int index, Object[] parameters, int invocationIndex) {
this.instance = instance;
this.index = index;
this.parameters = parameters;
this.currentIndex = currentIndex;
this.invocationIndex = invocationIndex;
}

@Override
Expand All @@ -24,8 +24,8 @@ public int getIndex() {
}

@Override
public int currentIndex() {
return currentIndex;
public int getInvocationIndex() {
return invocationIndex;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.testng.DataProviderHolder;
import org.testng.IClass;
import org.testng.IInstanceInfo;
import org.testng.ITestClassInstance;
import org.testng.ITestContext;
import org.testng.ITestObjectFactory;
import org.testng.TestNGException;
Expand Down Expand Up @@ -178,7 +179,7 @@ private ClassInfoMap processFactory(IClass ic, ConstructorOrMethod factoryMethod
// If the factory returned IInstanceInfo, get the class from it,
// otherwise, just call getClass() on the returned instances
int i = 0;
for (org.testng.IParameterInfo o : fm.invoke()) {
for (ITestClassInstance o : fm.invoke()) {
if (o == null) {
throw new TestNGException(
"The factory " + fm + " returned a null instance" + "at index " + i);
Expand Down Expand Up @@ -329,8 +330,8 @@ private <T> void addInstance(IInstanceInfo<T> ii) {

private void addInstance(IObject.IdentifiableObject o) {
Class<?> key = o.getInstance().getClass();
if (o.getInstance() instanceof org.testng.IParameterInfo) {
key = ((org.testng.IParameterInfo) o.getInstance()).getInstance().getClass();
if (o.getInstance() instanceof ITestClassInstance) {
key = ((ITestClassInstance) o.getInstance()).getInstance().getClass();
}
addInstance(key, o);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import java.util.List;
import java.util.stream.Collectors;
import org.testng.Assert;
import org.testng.IParameterInfo;
import org.testng.ITestClassInstance;
import org.testng.ITestListener;
import org.testng.ITestResult;
import org.testng.TestListenerAdapter;
Expand Down Expand Up @@ -82,7 +82,7 @@ public void doubleFactoryMethodShouldWork() {

@Test(dataProvider = "testdata", description = "GITHUB-3111")
public void ensureCurrentIndexWorksForFactoryPoweredTests(Class<?> klass, Integer[] expected) {
List<IParameterInfo> params = new ArrayList<>();
List<ITestClassInstance> params = new ArrayList<>();
TestNG testng = create(klass);
testng.addListener(
new ITestListener() {
Expand All @@ -93,7 +93,10 @@ public void onTestSuccess(ITestResult result) {
});
testng.run();
List<Integer> actualIndices =
params.stream().map(IParameterInfo::currentIndex).sorted().collect(Collectors.toList());
params.stream()
.map(ITestClassInstance::getInvocationIndex)
.sorted()
.collect(Collectors.toList());
assertThat(actualIndices).containsExactly(expected);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.testng.IAttributes;
import org.testng.IClass;
import org.testng.ITest;
import org.testng.ITestClassInstance;
import org.testng.ITestContext;
import org.testng.ITestNGMethod;
import org.testng.ITestResult;
Expand Down Expand Up @@ -298,7 +299,7 @@ public void setParameters(Object[] parameters) {

@Override
public Object getInstance() {
return org.testng.IParameterInfo.embeddedInstance(this.m_method.getInstance());
return ITestClassInstance.embeddedInstance(this.m_method.getInstance());
}

@Override
Expand Down

0 comments on commit 5462ee1

Please sign in to comment.