forked from testng-team/testng
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
175 additions
and
0 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
core/src/test/java/test/factory/github2560/ConstructorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package test.factory.github2560; | ||
|
||
import org.testng.annotations.*; | ||
|
||
public class ConstructorTest { | ||
|
||
private final int hashCode; | ||
|
||
@Factory(dataProvider = "constructorArguments") | ||
public ConstructorTest(int hashCode) { | ||
this.hashCode = hashCode; | ||
} | ||
|
||
@DataProvider | ||
public static Object[][] constructorArguments() { | ||
return new Object[][]{{0}, {1}, {2}}; | ||
} | ||
|
||
@BeforeClass | ||
public void beforeClass() { | ||
} | ||
|
||
@BeforeMethod | ||
public void beforeMethod() { | ||
} | ||
|
||
@Test | ||
public void test() { | ||
} | ||
|
||
@AfterMethod | ||
public void afterMethod() { | ||
} | ||
|
||
@AfterClass | ||
public void afterClass() { | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return hashCode; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
core/src/test/java/test/factory/github2560/FactoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package test.factory.github2560; | ||
|
||
import org.testng.annotations.Factory; | ||
|
||
public class FactoryTest { | ||
|
||
@Factory | ||
public static Object[] factory() { | ||
return new Object[]{ | ||
new TestClass(0), | ||
new TestClass(1), | ||
new TestClass(2) | ||
}; | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
core/src/test/java/test/factory/github2560/Github2560Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package test.factory.github2560; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import com.google.common.collect.ImmutableMap; | ||
import org.testng.Assert; | ||
import org.testng.TestNG; | ||
import org.testng.annotations.Test; | ||
import test.SimpleBaseTest; | ||
|
||
public class Github2560Test extends SimpleBaseTest { | ||
|
||
@Test | ||
public void staticFactory() { | ||
TestNG testng = create(FactoryTest.class); | ||
testng.setDefaultSuiteName("Static @Factory tests"); | ||
InvokedMethodListener invokedMethodListener = new InvokedMethodListener(); | ||
testng.addListener(invokedMethodListener); | ||
|
||
testng.run(); | ||
|
||
ImmutableMap<Integer, ImmutableList<String>> expected = ImmutableMap.of( | ||
0, ImmutableList.of("beforeClass", "beforeMethod", "test", "afterMethod", "afterClass"), | ||
1, ImmutableList.of("beforeClass", "beforeMethod", "test", "afterMethod", "afterClass"), | ||
2, ImmutableList.of("beforeClass", "beforeMethod", "test", "afterMethod", "afterClass") | ||
); | ||
Assert.assertEquals(invokedMethodListener.capturedBeforeInvocations, expected, "beforeInvocation"); | ||
Assert.assertEquals(invokedMethodListener.capturedAfterInvocations, expected, "afterInvocation"); | ||
} | ||
|
||
@Test | ||
public void constructorFactory() { | ||
TestNG testng = create(ConstructorTest.class); | ||
testng.setDefaultSuiteName("Constructor @Factory tests"); | ||
InvokedMethodListener invokedMethodListener = new InvokedMethodListener(); | ||
testng.addListener(invokedMethodListener); | ||
|
||
testng.run(); | ||
|
||
ImmutableMap<Integer, ImmutableList<String>> expected = ImmutableMap.of( | ||
0, ImmutableList.of("beforeClass", "beforeMethod", "test", "afterMethod", "afterClass"), | ||
1, ImmutableList.of("beforeClass", "beforeMethod", "test", "afterMethod", "afterClass"), | ||
2, ImmutableList.of("beforeClass", "beforeMethod", "test", "afterMethod", "afterClass") | ||
); | ||
Assert.assertEquals(invokedMethodListener.capturedBeforeInvocations, expected, "beforeInvocation"); | ||
Assert.assertEquals(invokedMethodListener.capturedAfterInvocations, expected, "afterInvocation"); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
core/src/test/java/test/factory/github2560/InvokedMethodListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package test.factory.github2560; | ||
|
||
import org.testng.*; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
public class InvokedMethodListener implements IInvokedMethodListener { | ||
|
||
final Map<Integer, List<String>> capturedBeforeInvocations = new ConcurrentHashMap<>(); | ||
final Map<Integer, List<String>> capturedAfterInvocations = new ConcurrentHashMap<>(); | ||
|
||
@Override | ||
public void beforeInvocation(IInvokedMethod method, ITestResult testResult, ITestContext context) { | ||
Assert.assertSame(method.getTestMethod().getInstance(), testResult.getInstance()); | ||
capturedBeforeInvocations.computeIfAbsent(testResult.getInstance().hashCode(), ignored -> new ArrayList<>()) | ||
.add(method.getTestMethod().getMethodName()); | ||
} | ||
|
||
@Override | ||
public void afterInvocation(IInvokedMethod method, ITestResult testResult, ITestContext context) { | ||
Assert.assertSame(method.getTestMethod().getInstance(), testResult.getInstance()); | ||
capturedAfterInvocations.computeIfAbsent(testResult.getInstance().hashCode(), ignored -> new ArrayList<>()) | ||
.add(method.getTestMethod().getMethodName()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package test.factory.github2560; | ||
|
||
import org.testng.annotations.*; | ||
|
||
public class TestClass { | ||
|
||
private final int hashCode; | ||
|
||
public TestClass(int hashCode) { | ||
this.hashCode = hashCode; | ||
} | ||
|
||
@BeforeClass | ||
public void beforeClass() { | ||
} | ||
|
||
@BeforeMethod | ||
public void beforeMethod() { | ||
} | ||
|
||
@Test | ||
public void test() { | ||
} | ||
|
||
@AfterMethod | ||
public void afterMethod() { | ||
} | ||
|
||
@AfterClass | ||
public void afterClass() { | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return hashCode; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters