-
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 (#2677)
Closes #2676
- Loading branch information
1 parent
db17f3c
commit e8f17fe
Showing
13 changed files
with
153 additions
and
82 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
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
12 changes: 12 additions & 0 deletions
12
testng-core/src/main/java/org/testng/internal/objects/DefaultTestObjectFactory.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,12 @@ | ||
package org.testng.internal.objects; | ||
|
||
import org.testng.ITestObjectFactory; | ||
|
||
/** | ||
* Intended to be the default way of instantiating objects within TestNG. Intentionally does not | ||
* provide any specific implementation because the interface already defines the default behavior. | ||
* This class still exists to ensure that we dont use an anonymous object instantiation so that its | ||
* easy to find out what type of object factory is being injected into our object dispensing | ||
* mechanisms. | ||
*/ | ||
public class DefaultTestObjectFactory implements ITestObjectFactory {} |
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