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 92d1313 commit 73308ee
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 78 deletions.
45 changes: 18 additions & 27 deletions testng-core/src/main/java/org/testng/TestClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.util.Arrays;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.stream.Stream;
import org.testng.collections.Lists;
import org.testng.collections.Objects;
import org.testng.internal.*;
Expand Down Expand Up @@ -210,7 +209,7 @@ private void initMethods() {
xmlTest,
eachInstance);
Object instance = IParameterInfo.embeddedInstance(eachInstance.getInstance());
beforeClassConfig.put(instance, Arrays.asList(m_beforeClassMethods));
beforeClassConfig.put(instance, m_beforeClassMethods);
m_afterClassMethods =
ConfigurationMethod.createClassConfigurationMethods(
objectFactory,
Expand All @@ -219,7 +218,7 @@ private void initMethods() {
false,
xmlTest,
eachInstance);
afterClassConfig.put(instance, Arrays.asList(m_afterClassMethods));
afterClassConfig.put(instance, m_afterClassMethods);
m_beforeGroupsMethods =
ConfigurationMethod.createBeforeConfigurationMethods(
objectFactory,
Expand All @@ -234,30 +233,22 @@ private void initMethods() {
annotationFinder,
false,
eachInstance);
m_beforeTestMethods =
Stream.of(
m_beforeTestMethods,
ConfigurationMethod.createTestMethodConfigurationMethods(
objectFactory,
testMethodFinder.getBeforeTestMethods(m_testClass),
annotationFinder,
true,
xmlTest,
eachInstance))
.flatMap(Stream::of)
.toArray(ITestNGMethod[]::new);
m_afterTestMethods =
Stream.of(
m_afterTestMethods,
ConfigurationMethod.createTestMethodConfigurationMethods(
objectFactory,
testMethodFinder.getAfterTestMethods(m_testClass),
annotationFinder,
false,
xmlTest,
eachInstance))
.flatMap(Stream::of)
.toArray(ITestNGMethod[]::new);
m_beforeTestMethods.addAll(
ConfigurationMethod.createTestMethodConfigurationMethods(
objectFactory,
testMethodFinder.getBeforeTestMethods(m_testClass),
annotationFinder,
true,
xmlTest,
eachInstance));
m_afterTestMethods.addAll(
ConfigurationMethod.createTestMethodConfigurationMethods(
objectFactory,
testMethodFinder.getAfterTestMethods(m_testClass),
annotationFinder,
false,
xmlTest,
eachInstance));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import org.testng.ITestNGMethod;
import org.testng.ITestObjectFactory;
import org.testng.annotations.IAnnotation;
Expand Down Expand Up @@ -143,7 +144,7 @@ public ConfigurationMethod(
setXmlTest(xmlTest);
}

private static ITestNGMethod[] createMethods(
private static List<ITestNGMethod> createMethods(
ITestObjectFactory objectFactory,
ITestNGMethod[] methods,
IAnnotationFinder finder,
Expand Down Expand Up @@ -188,10 +189,10 @@ private static ITestNGMethod[] createMethods(
instance));
}

return result.toArray(new ITestNGMethod[0]);
return result;
}

public static ITestNGMethod[] createSuiteConfigurationMethods(
public static List<ITestNGMethod> createSuiteConfigurationMethods(
ITestObjectFactory objectFactory,
ITestNGMethod[] methods,
IAnnotationFinder annotationFinder,
Expand All @@ -214,7 +215,7 @@ public static ITestNGMethod[] createSuiteConfigurationMethods(
instance);
}

public static ITestNGMethod[] createTestConfigurationMethods(
public static List<ITestNGMethod> createTestConfigurationMethods(
ITestObjectFactory objectFactory,
ITestNGMethod[] methods,
IAnnotationFinder annotationFinder,
Expand All @@ -237,7 +238,7 @@ public static ITestNGMethod[] createTestConfigurationMethods(
instance);
}

public static ITestNGMethod[] createClassConfigurationMethods(
public static List<ITestNGMethod> createClassConfigurationMethods(
ITestObjectFactory objectFactory,
ITestNGMethod[] methods,
IAnnotationFinder annotationFinder,
Expand Down Expand Up @@ -291,7 +292,7 @@ public static ITestNGMethod[] createBeforeConfigurationMethods(
return result;
}

public static ITestNGMethod[] createAfterConfigurationMethods(
public static List<ITestNGMethod> createAfterConfigurationMethods(
ITestObjectFactory objectFactory,
ITestNGMethod[] methods,
IAnnotationFinder annotationFinder,
Expand All @@ -318,10 +319,10 @@ public static ITestNGMethod[] createAfterConfigurationMethods(
isBefore ? m.getBeforeGroups() : m.getAfterGroups(),
null,
instance))
.toArray(ITestNGMethod[]::new);
.collect(Collectors.toList());
}

public static ITestNGMethod[] createTestMethodConfigurationMethods(
public static List<ITestNGMethod> createTestMethodConfigurationMethods(
ITestObjectFactory objectFactory,
ITestNGMethod[] methods,
IAnnotationFinder annotationFinder,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,9 @@ default List<ITestNGMethod> getAllAfterClassMethods() {
* @param instance object hashcode
* @return All before class methods of instance
*/
default List<ITestNGMethod> getInstanceBeforeClassMethods(Object instance) {
return Lists.newArrayList();
}
List<ITestNGMethod> getInstanceBeforeClassMethods(Object instance);

default List<ITestNGMethod> getInstanceAfterClassMethods(Object instance) {
return Lists.newArrayList();
}
List<ITestNGMethod> getInstanceAfterClassMethods(Object instance);

static List<ITestNGMethod> allBeforeClassMethods(ITestClass tc) {
if (tc instanceof ITestClassConfigInfo) {
Expand Down
60 changes: 31 additions & 29 deletions testng-core/src/main/java/org/testng/internal/NoOpTestClass.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package org.testng.internal;

import java.util.List;
import org.testng.ITestClass;
import org.testng.ITestNGMethod;
import org.testng.collections.Lists;
import org.testng.xml.XmlClass;
import org.testng.xml.XmlTest;

Expand All @@ -10,17 +12,17 @@ public class NoOpTestClass implements ITestClass, IObject {
protected Class<?> m_testClass = null;

// Test methods
protected ITestNGMethod[] m_beforeClassMethods = new ITestNGMethod[0];
protected ITestNGMethod[] m_beforeTestMethods = new ITestNGMethod[0];
protected List<ITestNGMethod> m_beforeClassMethods = Lists.newArrayList();
protected List<ITestNGMethod> m_beforeTestMethods = Lists.newArrayList();
protected ITestNGMethod[] m_testMethods = new ITestNGMethod[0];
protected ITestNGMethod[] m_afterClassMethods = new ITestNGMethod[0];
protected ITestNGMethod[] m_afterTestMethods = new ITestNGMethod[0];
protected ITestNGMethod[] m_beforeSuiteMethods = new ITestNGMethod[0];
protected ITestNGMethod[] m_afterSuiteMethods = new ITestNGMethod[0];
protected ITestNGMethod[] m_beforeTestConfMethods = new ITestNGMethod[0];
protected ITestNGMethod[] m_afterTestConfMethods = new ITestNGMethod[0];
protected List<ITestNGMethod> m_afterClassMethods = Lists.newArrayList();
protected List<ITestNGMethod> m_afterTestMethods = Lists.newArrayList();
protected List<ITestNGMethod> m_beforeSuiteMethods = Lists.newArrayList();
protected List<ITestNGMethod> m_afterSuiteMethods = Lists.newArrayList();
protected List<ITestNGMethod> m_beforeTestConfMethods = Lists.newArrayList();
protected List<ITestNGMethod> m_afterTestConfMethods = Lists.newArrayList();
protected ITestNGMethod[] m_beforeGroupsMethods = new ITestNGMethod[0];
protected ITestNGMethod[] m_afterGroupsMethods = new ITestNGMethod[0];
protected List<ITestNGMethod> m_afterGroupsMethods = Lists.newArrayList();

private final IdentifiableObject[] m_instances;
private final long[] m_instanceHashes;
Expand All @@ -37,52 +39,52 @@ protected NoOpTestClass() {

public NoOpTestClass(ITestClass testClass) {
m_testClass = testClass.getRealClass();
m_beforeSuiteMethods = testClass.getBeforeSuiteMethods();
m_beforeTestConfMethods = testClass.getBeforeTestConfigurationMethods();
m_beforeSuiteMethods = List.of(testClass.getBeforeSuiteMethods());
m_beforeTestConfMethods = List.of(testClass.getBeforeTestConfigurationMethods());
m_beforeGroupsMethods = testClass.getBeforeGroupsMethods();
m_beforeClassMethods = testClass.getBeforeClassMethods();
m_beforeTestMethods = testClass.getBeforeTestMethods();
m_afterSuiteMethods = testClass.getAfterSuiteMethods();
m_afterTestConfMethods = testClass.getAfterTestConfigurationMethods();
m_afterGroupsMethods = testClass.getAfterGroupsMethods();
m_afterClassMethods = testClass.getAfterClassMethods();
m_afterTestMethods = testClass.getAfterTestMethods();
m_beforeClassMethods = List.of(testClass.getBeforeClassMethods());
m_beforeTestMethods = List.of(testClass.getBeforeTestMethods());
m_afterSuiteMethods = List.of(testClass.getAfterSuiteMethods());
m_afterTestConfMethods = List.of(testClass.getAfterTestConfigurationMethods());
m_afterGroupsMethods = List.of(testClass.getAfterGroupsMethods());
m_afterClassMethods = List.of(testClass.getAfterClassMethods());
m_afterTestMethods = List.of(testClass.getAfterTestMethods());
m_instances = IObject.objects(testClass, true);
m_instanceHashes = IObject.instanceHashCodes(testClass);
m_xmlTest = testClass.getXmlTest();
m_xmlClass = testClass.getXmlClass();
}

public void setBeforeTestMethods(ITestNGMethod[] beforeTestMethods) {
m_beforeTestMethods = beforeTestMethods;
m_beforeTestMethods = List.of(beforeTestMethods);
}

public void setAfterTestMethod(ITestNGMethod[] afterTestMethods) {
m_afterTestMethods = afterTestMethods;
m_afterTestMethods = List.of(afterTestMethods);
}

/** @return Returns the afterClassMethods. */
@Override
public ITestNGMethod[] getAfterClassMethods() {
return m_afterClassMethods;
return m_afterClassMethods.toArray(ITestNGMethod[]::new);
}

/** @return Returns the afterTestMethods. */
@Override
public ITestNGMethod[] getAfterTestMethods() {
return m_afterTestMethods;
return m_afterTestMethods.toArray(ITestNGMethod[]::new);
}

/** @return Returns the beforeClassMethods. */
@Override
public ITestNGMethod[] getBeforeClassMethods() {
return m_beforeClassMethods;
return m_beforeClassMethods.toArray(ITestNGMethod[]::new);
}

/** @return Returns the beforeTestMethods. */
@Override
public ITestNGMethod[] getBeforeTestMethods() {
return m_beforeTestMethods;
return m_beforeTestMethods.toArray(ITestNGMethod[]::new);
}

/** @return Returns the testMethods. */
Expand All @@ -93,22 +95,22 @@ public ITestNGMethod[] getTestMethods() {

@Override
public ITestNGMethod[] getBeforeSuiteMethods() {
return m_beforeSuiteMethods;
return m_beforeSuiteMethods.toArray(ITestNGMethod[]::new);
}

@Override
public ITestNGMethod[] getAfterSuiteMethods() {
return m_afterSuiteMethods;
return m_afterSuiteMethods.toArray(ITestNGMethod[]::new);
}

@Override
public ITestNGMethod[] getBeforeTestConfigurationMethods() {
return m_beforeTestConfMethods;
return m_beforeTestConfMethods.toArray(ITestNGMethod[]::new);
}

@Override
public ITestNGMethod[] getAfterTestConfigurationMethods() {
return m_afterTestConfMethods;
return m_afterTestConfMethods.toArray(ITestNGMethod[]::new);
}

/** @return all @Configuration methods that should be invoked before certain groups */
Expand All @@ -120,7 +122,7 @@ public ITestNGMethod[] getBeforeGroupsMethods() {
/** @return all @Configuration methods that should be invoked after certain groups */
@Override
public ITestNGMethod[] getAfterGroupsMethods() {
return m_afterGroupsMethods;
return m_afterGroupsMethods.toArray(ITestNGMethod[]::new);
}

/** @see org.testng.internal.IObject#getInstanceHashCodes() */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.util.List;
import java.util.Optional;
import java.util.function.BiPredicate;
import java.util.function.Predicate;
import org.testng.IClass;
import org.testng.ITestClass;
import org.testng.ITestNGMethod;
Expand Down Expand Up @@ -87,15 +86,9 @@ static ITestNGMethod[] filterMethods(
ITestNGMethod[] methods,
BiPredicate<ITestNGMethod, IClass> predicate) {
List<ITestNGMethod> vResult = Lists.newArrayList();
Predicate<ITestNGMethod> sameInstance =
tm ->
instance == null
|| instance.equals(
Optional.ofNullable(tm).map(ITestNGMethod::getInstance).orElse(new Object()));

for (ITestNGMethod tm : methods) {
String msg;
if ((predicate.test(tm, testClass) && sameInstance.test(tm))
if ((predicate.test(tm, testClass) && isSameInstance(tm, instance))
&& (!TestNgMethodUtils.containsConfigurationMethod(tm, vResult))) {
msg = Utils.getVerbose() < 10 ? "" : "Keeping method " + tm + " for class " + testClass;
vResult.add(tm);
Expand All @@ -108,6 +101,14 @@ static ITestNGMethod[] filterMethods(
return vResult.toArray(new ITestNGMethod[0]);
}

private static boolean isSameInstance(ITestNGMethod tm, Object instance) {
if (instance == null) {
return true;
}
Object tmObject = Optional.ofNullable(tm).map(ITestNGMethod::getInstance).orElse(new Object());
return instance.equals(tmObject);
}

static ITestNGMethod[] filterSetupConfigurationMethods(
ITestNGMethod tm, ITestNGMethod[] methods) {
List<ITestNGMethod> result = Lists.newArrayList();
Expand Down

0 comments on commit 73308ee

Please sign in to comment.