Skip to content

Commit

Permalink
Merge pull request #1398 from krmahadevan/krmahadevan-fix-1394
Browse files Browse the repository at this point in the history
Exclude Object class from ClassHelper.getAvailableMethods()
  • Loading branch information
cbeust authored Mar 29, 2017
2 parents 0418645 + 42f95bd commit 76c52be
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Current
Fixed: GITHUB-1394: Optimize ClassHelper.getAvailableMethods() to exclude Object class(Nathan Reynolds & Krishnan Mahadevan)
Fixed: GITHUB-1396: Order established by IMethodInterceptor not honored when running with parallel='instances' (Ryan Scott)
Fixed: GITHUB-1287: Parallel (methods) execution with dependsOn running in unexpected order (Kevyn Reinholt)
Fixed: GITHUB-1362: Ensure AfterGroups methods get executed when involving Method Interceptors (Krishnan Mahadevan)
Expand Down
20 changes: 11 additions & 9 deletions src/main/java/org/testng/internal/ClassHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,17 +182,19 @@ public static Set<Method> getAvailableMethods(Class<?> clazz) {
}

Class<?> parent = clazz.getSuperclass();
while (null != parent) {
Set<Map.Entry<String, Set<Method>>> extractedMethods = extractMethods(clazz, parent, methods).entrySet();
for (Map.Entry<String, Set<Method>> extractedMethod : extractedMethods){
Set<Method> m = methods.get(extractedMethod.getKey());
if (m == null) {
methods.put(extractedMethod.getKey(), extractedMethod.getValue());
} else {
m.addAll(extractedMethod.getValue());
if (null != parent) {
while (!Object.class.equals(parent)) {
Set<Map.Entry<String, Set<Method>>> extractedMethods = extractMethods(clazz, parent, methods).entrySet();
for (Map.Entry<String, Set<Method>> extractedMethod : extractedMethods) {
Set<Method> m = methods.get(extractedMethod.getKey());
if (m == null) {
methods.put(extractedMethod.getKey(), extractedMethod.getValue());
} else {
m.addAll(extractedMethod.getValue());
}
}
parent = parent.getSuperclass();
}
parent = parent.getSuperclass();
}

Set<Method> returnValue = Sets.newHashSet();
Expand Down
27 changes: 27 additions & 0 deletions src/test/java/test/issue1339/BabyPanda.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package test.issue1339;

public class BabyPanda extends LittlePanda {
String name;

@Override
public String toString() {
return "BabyPanda{" +
"name='" + name + '\'' +
'}';
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

BabyPanda babyPanda = (BabyPanda) o;

return name != null ? name.equals(babyPanda.name) : babyPanda.name == null;
}

@Override
public int hashCode() {
return name != null ? name.hashCode() : 0;
}
}
31 changes: 24 additions & 7 deletions src/test/java/test/issue1339/ClassHelperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.testng.internal.ClassHelper;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
Expand All @@ -13,20 +14,36 @@ public class ClassHelperTest {

@Test
public void testGetAvailableMethods() {
List<String> expected = Arrays.asList(
"finalize", "toString", "clone", "announcer", "hashCode", "equals",
"announcer", "notify", "wait", "wait", "wait", "getClass",
"inheritable", "inheritable", "notifyAll"
);
Set<Method> methods = ClassHelper.getAvailableMethods(LittlePanda.class);
runTest(getExpected(), LittlePanda.class);
}

@Test
public void testGetAvailableMethodsWhenOverrdingIsInvolved() {
List<String> expected = getExpected("equals","hashCode","toString");
runTest(expected, BabyPanda.class);
}

private static void runTest(List<String> expected, Class<?> whichClass) {
Set<Method> methods = ClassHelper.getAvailableMethods(whichClass);
//Intentionally not using assertEquals because when this test is executed via gradle an additional method
//called "jacocoInit()" is getting added, which does not get added when this test is executed individually
Assert.assertTrue(methods.size() >= 15, "Number of methods found should have been atleast 15.");
int size = expected.size();
Assert.assertTrue(methods.size() >= size, "Number of methods found should have been atleast " + size);
for (Method method : methods) {
if ("$jacocoInit".equalsIgnoreCase(method.getName())) {
continue;
}
Assert.assertTrue(expected.contains(method.getName()));
}
}

private static List<String> getExpected(String... additionalMethods) {
String[] defaultMethods = new String[]{"announcer", "announcer", "inheritable", "inheritable"};
if (additionalMethods == null) {
return Arrays.asList(defaultMethods);
}
List<String> expected = new ArrayList<>(Arrays.asList(defaultMethods));
expected.addAll(Arrays.asList(additionalMethods));
return expected;
}
}

0 comments on commit 76c52be

Please sign in to comment.