-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow ITestObjectFactory injection via listeners
Closes #2676
- Loading branch information
1 parent
4c28cd6
commit 2c4e019
Showing
11 changed files
with
141 additions
and
81 deletions.
There are no files selected for viewing
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
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
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
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
78 changes: 78 additions & 0 deletions
78
testng-core/src/test/java/test/objectfactory/ObjectFactoryTest.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,78 @@ | ||
package test.objectfactory; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import org.testng.TestNG; | ||
import org.testng.TestNGException; | ||
import org.testng.annotations.DataProvider; | ||
import org.testng.annotations.Test; | ||
import org.testng.xml.XmlSuite; | ||
import test.SimpleBaseTest; | ||
import test.objectfactory.github1131.EmptyConstructorSample; | ||
import test.objectfactory.github1131.IntConstructorSample; | ||
import test.objectfactory.github1131.MyObjectFactory; | ||
import test.objectfactory.github1131.StringConstructorSample; | ||
import test.objectfactory.github1827.GitHub1827Sample; | ||
import test.objectfactory.issue2676.LocalSuiteAlteringListener; | ||
import test.objectfactory.issue2676.LoggingObjectFactorySample; | ||
import test.objectfactory.issue2676.TestClassSample; | ||
|
||
public class ObjectFactoryTest extends SimpleBaseTest { | ||
|
||
@Test(description = "GITHUB-2676") | ||
public void ensureObjectFactoryIsInvokedWhenAddedViaListeners() { | ||
TestNG testng = create(TestClassSample.class); | ||
testng.addListener(new LocalSuiteAlteringListener()); | ||
testng.run(); | ||
assertThat(LoggingObjectFactorySample.wasInvoked).isTrue(); | ||
} | ||
|
||
@Test( | ||
expectedExceptions = TestNGException.class, | ||
expectedExceptionsMessageRegExp = ".*Check to make sure it can be instantiated", | ||
description = "GITHUB-1827") | ||
public void ensureExceptionThrownWhenNoSuitableConstructorFound() { | ||
|
||
TestNG testng = create(GitHub1827Sample.class); | ||
testng.run(); | ||
} | ||
|
||
@Test(dataProvider = "dp", description = "GITHUB-1131") | ||
public void factoryWithEmptyConstructorShouldWork(boolean bool) { | ||
testFactory(bool, EmptyConstructorSample.class); | ||
assertThat(MyObjectFactory.allParams).containsExactly(new Object[] {}, new Object[] {}); | ||
} | ||
|
||
@Test(dataProvider = "dp", description = "GITHUB-1131") | ||
public void factoryWithIntConstructorShouldWork(boolean bool) { | ||
testFactory(bool, IntConstructorSample.class); | ||
assertThat(MyObjectFactory.allParams).containsExactly(new Object[] {1}, new Object[] {2}); | ||
} | ||
|
||
@Test(dataProvider = "dp", description = "GITHUB-1131") | ||
public void factoryWithStringConstructorShouldWork(boolean bool) { | ||
testFactory(bool, StringConstructorSample.class); | ||
assertThat(MyObjectFactory.allParams) | ||
.containsExactly(new Object[] {"foo"}, new Object[] {"bar"}); | ||
} | ||
|
||
private void testFactory(boolean onSuite, Class<?> sample) { | ||
MyObjectFactory.allParams.clear(); | ||
|
||
XmlSuite suite = createXmlSuite("Test IObjectFactory2", "TmpTest", sample); | ||
TestNG tng = create(suite); | ||
|
||
if (onSuite) { | ||
suite.setObjectFactoryClass(MyObjectFactory.class); | ||
} else { | ||
tng.setObjectFactory(MyObjectFactory.class); | ||
} | ||
|
||
tng.run(); | ||
} | ||
|
||
@DataProvider | ||
public static Object[][] dp() { | ||
return new Object[][] {new Object[] {true}, new Object[] {false}}; | ||
} | ||
} |
51 changes: 0 additions & 51 deletions
51
testng-core/src/test/java/test/objectfactory/github1131/GitHub1131Test.java
This file was deleted.
Oops, something went wrong.
19 changes: 0 additions & 19 deletions
19
testng-core/src/test/java/test/objectfactory/github1827/GitHub1827Test.java
This file was deleted.
Oops, something went wrong.
13 changes: 13 additions & 0 deletions
13
testng-core/src/test/java/test/objectfactory/issue2676/LocalSuiteAlteringListener.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,13 @@ | ||
package test.objectfactory.issue2676; | ||
|
||
import java.util.List; | ||
import org.testng.IAlterSuiteListener; | ||
import org.testng.xml.XmlSuite; | ||
|
||
public class LocalSuiteAlteringListener implements IAlterSuiteListener { | ||
|
||
@Override | ||
public void alter(List<XmlSuite> suites) { | ||
suites.forEach(each -> each.setObjectFactoryClass(LoggingObjectFactorySample.class)); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
testng-core/src/test/java/test/objectfactory/issue2676/LoggingObjectFactorySample.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,16 @@ | ||
package test.objectfactory.issue2676; | ||
|
||
import java.lang.reflect.Constructor; | ||
import org.testng.ITestObjectFactory; | ||
import org.testng.internal.objects.InstanceCreator; | ||
|
||
public class LoggingObjectFactorySample implements ITestObjectFactory { | ||
|
||
public static boolean wasInvoked = false; | ||
|
||
@Override | ||
public <T> T newInstance(Constructor<T> constructor, Object... parameters) { | ||
wasInvoked = true; | ||
return InstanceCreator.newInstance(constructor, parameters); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
testng-core/src/test/java/test/objectfactory/issue2676/TestClassSample.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,9 @@ | ||
package test.objectfactory.issue2676; | ||
|
||
import org.testng.annotations.Test; | ||
|
||
public class TestClassSample { | ||
|
||
@Test | ||
public void sampleTestMethod() {} | ||
} |
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