Skip to content

Commit

Permalink
Merge pull request #1479 from krmahadevan/krmahadevan-fix-1405
Browse files Browse the repository at this point in the history
Skip considering main() method
  • Loading branch information
cbeust authored Jul 17, 2017
2 parents 3a0b101 + 65fa5fc commit 5846f06
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 2 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-1405: Skip considering main() method when @Test used at class level (Krishnan Mahadevan)
Fixed: GITHUB-799: @Factory with dataProvider changes order of iterations (Krishnan Mahadevan & Julien Herr)
New: Enhance XML Reporter to be able to customize the file name (Krishnan Mahadevan)
Fixed: GITHUB-1417: Class param injection is not working with @BeforeClass (Krishnan Mahadevan)
Expand Down
32 changes: 31 additions & 1 deletion src/main/java/org/testng/internal/reflect/ReflectionHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class ReflectionHelper {
*/
public static Method[] getLocalMethods(Class<?> clazz) {
Method[] result;
Method[] declaredMethods = clazz.getDeclaredMethods();
Method[] declaredMethods = excludingMain(clazz);
List<Method> defaultMethods = getDefaultMethods(clazz);
if (defaultMethods != null) {
result = new Method[declaredMethods.length + defaultMethods.size()];
Expand All @@ -38,6 +38,36 @@ public static Method[] getLocalMethods(Class<?> clazz) {
return result;
}

/**
* @return An array of all locally declared methods or equivalent thereof
* (such as default methods on Java 8 based interfaces that the given class
* implements) but excludes the <code>main()</code> method alone.
*/
public static Method[] excludingMain(Class<?> clazz) {
Method[] declaredMethods = clazz.getDeclaredMethods();
List<Method> pruned = new LinkedList<>();
for (Method declaredMethod :declaredMethods) {
if ("main".equals(declaredMethod.getName()) && isStaticVoid(declaredMethod) && acceptsStringArray(declaredMethod)) {
continue;
}
pruned.add(declaredMethod);
}
return pruned.toArray(new Method[pruned.size()]);
}

private static boolean isStaticVoid(Method method) {
return method.getReturnType().equals(void.class) && Modifier.isStatic(method.getModifiers());
}

private static boolean acceptsStringArray(Method method) {
Class<?>[] paramTypes = method.getParameterTypes();
if (paramTypes.length == 0) {
return false;
}
Class<?> paramType = paramTypes[0];
return paramType.isArray() && paramType.isInstance(new String[0]);
}

private static List<Method> getDefaultMethods(Class<?> clazz) {
List<Method> result = null;
for (Class<?> ifc : clazz.getInterfaces()) {
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/testng/junit/JUnitMethodFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.testng.internal.ConstructorOrMethod;
import org.testng.internal.TestNGMethod;
import org.testng.internal.annotations.IAnnotationFinder;
import org.testng.internal.reflect.ReflectionHelper;
import org.testng.xml.XmlTest;

import java.lang.reflect.Constructor;
Expand Down Expand Up @@ -80,7 +81,7 @@ private ITestNGMethod[] privateFindTestMethods(INameFilter filter, Class cls) {
//
Class current = cls;
while(!(current == Object.class)) {
Method[] allMethods = current.getDeclaredMethods();
Method[] allMethods = ReflectionHelper.excludingMain(current);
for(Method allMethod : allMethods) {
ITestNGMethod m = new TestNGMethod(/* allMethods[i].getDeclaringClass(), */ allMethod,
m_annotationFinder, null,
Expand Down
5 changes: 5 additions & 0 deletions src/test/java/test/ReflectionHelperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.testng.annotations.Test;
import org.testng.collections.Lists;
import org.testng.internal.reflect.ReflectionHelper;
import test.github1405.TestClassSample;
import test.github765.DuplicateCallsSample;

import java.lang.reflect.Method;
Expand All @@ -28,6 +29,10 @@ public void testMethodCount() {
//Abstract methods should be included.
methods = prune(ReflectionHelper.getLocalMethods(Dragon.class));
Assert.assertEquals(methods.length, 2);

//main methods should be pruned
methods = prune(ReflectionHelper.getLocalMethods(TestClassSample.class));
Assert.assertEquals(methods.length, 1);
}

/**
Expand Down
12 changes: 12 additions & 0 deletions src/test/java/test/github1405/JUnitTestClassSample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package test.github1405;

import org.junit.Test;

public class JUnitTestClassSample {
@Test
public void testMethod() {}
@Test
public static void main(String[] args) {

}
}
14 changes: 14 additions & 0 deletions src/test/java/test/github1405/TestClassSample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package test.github1405;


import org.testng.annotations.Test;

@Test
public class TestClassSample {
public static void main(String[] args) {
}

public void testMethod() {
}

}
28 changes: 28 additions & 0 deletions src/test/java/test/github1405/TestExclusionOfMainMethod.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package test.github1405;

import org.testng.Assert;
import org.testng.TestNG;
import org.testng.annotations.Test;
import org.testng.xml.XmlSuite;
import test.SimpleBaseTest;

import java.util.Collections;

public class TestExclusionOfMainMethod extends SimpleBaseTest {
@Test
public void testMainMethodExclusion() {
TestNG tng = create(TestClassSample.class);
tng.run();
Assert.assertEquals(tng.getStatus(), 0);
}

@Test
public void testMainMethodExclusionForJunit() {
XmlSuite xmlSuite = createXmlSuite("suite");
xmlSuite.setJunit(true);
createXmlTest(xmlSuite, "test", JUnitTestClassSample.class);
TestNG tng = create(xmlSuite);
tng.run();
Assert.assertEquals(tng.getStatus(), 0);
}
}
1 change: 1 addition & 0 deletions src/test/resources/testng.xml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
<class name="test.thread.MultiThreadedDependentTest" />
<class name="test.thread.TrueParallelTest" />
<class name="test.github765.ExcludeSyntheticMethodsFromTemplateCallsTest"/>
<class name="test.github1405.TestExclusionOfMainMethod"/>
<!--
<class name="test.thread.ParallelSuiteTest"/>
-->
Expand Down

0 comments on commit 5846f06

Please sign in to comment.