|
| 1 | +package test.objectfactory; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | + |
| 5 | +import org.testng.TestNG; |
| 6 | +import org.testng.TestNGException; |
| 7 | +import org.testng.annotations.DataProvider; |
| 8 | +import org.testng.annotations.Test; |
| 9 | +import org.testng.xml.XmlSuite; |
| 10 | +import test.SimpleBaseTest; |
| 11 | +import test.objectfactory.github1131.EmptyConstructorSample; |
| 12 | +import test.objectfactory.github1131.IntConstructorSample; |
| 13 | +import test.objectfactory.github1131.MyObjectFactory; |
| 14 | +import test.objectfactory.github1131.StringConstructorSample; |
| 15 | +import test.objectfactory.github1827.GitHub1827Sample; |
| 16 | +import test.objectfactory.issue2676.LocalSuiteAlteringListener; |
| 17 | +import test.objectfactory.issue2676.LoggingObjectFactorySample; |
| 18 | +import test.objectfactory.issue2676.TestClassSample; |
| 19 | + |
| 20 | +public class ObjectFactoryTest extends SimpleBaseTest { |
| 21 | + |
| 22 | + @Test(description = "GITHUB-2676") |
| 23 | + public void ensureObjectFactoryIsInvokedWhenAddedViaListeners() { |
| 24 | + TestNG testng = create(TestClassSample.class); |
| 25 | + testng.addListener(new LocalSuiteAlteringListener()); |
| 26 | + testng.run(); |
| 27 | + assertThat(LoggingObjectFactorySample.wasInvoked).isTrue(); |
| 28 | + } |
| 29 | + |
| 30 | + @Test( |
| 31 | + expectedExceptions = TestNGException.class, |
| 32 | + expectedExceptionsMessageRegExp = ".*Check to make sure it can be instantiated", |
| 33 | + description = "GITHUB-1827") |
| 34 | + public void ensureExceptionThrownWhenNoSuitableConstructorFound() { |
| 35 | + |
| 36 | + TestNG testng = create(GitHub1827Sample.class); |
| 37 | + testng.run(); |
| 38 | + } |
| 39 | + |
| 40 | + @Test(dataProvider = "dp", description = "GITHUB-1131") |
| 41 | + public void factoryWithEmptyConstructorShouldWork(boolean bool) { |
| 42 | + testFactory(bool, EmptyConstructorSample.class); |
| 43 | + assertThat(MyObjectFactory.allParams).containsExactly(new Object[] {}, new Object[] {}); |
| 44 | + } |
| 45 | + |
| 46 | + @Test(dataProvider = "dp", description = "GITHUB-1131") |
| 47 | + public void factoryWithIntConstructorShouldWork(boolean bool) { |
| 48 | + testFactory(bool, IntConstructorSample.class); |
| 49 | + assertThat(MyObjectFactory.allParams).containsExactly(new Object[] {1}, new Object[] {2}); |
| 50 | + } |
| 51 | + |
| 52 | + @Test(dataProvider = "dp", description = "GITHUB-1131") |
| 53 | + public void factoryWithStringConstructorShouldWork(boolean bool) { |
| 54 | + testFactory(bool, StringConstructorSample.class); |
| 55 | + assertThat(MyObjectFactory.allParams) |
| 56 | + .containsExactly(new Object[] {"foo"}, new Object[] {"bar"}); |
| 57 | + } |
| 58 | + |
| 59 | + private void testFactory(boolean onSuite, Class<?> sample) { |
| 60 | + MyObjectFactory.allParams.clear(); |
| 61 | + |
| 62 | + XmlSuite suite = createXmlSuite("Test IObjectFactory2", "TmpTest", sample); |
| 63 | + TestNG tng = create(suite); |
| 64 | + |
| 65 | + if (onSuite) { |
| 66 | + suite.setObjectFactoryClass(MyObjectFactory.class); |
| 67 | + } else { |
| 68 | + tng.setObjectFactory(MyObjectFactory.class); |
| 69 | + } |
| 70 | + |
| 71 | + tng.run(); |
| 72 | + } |
| 73 | + |
| 74 | + @DataProvider |
| 75 | + public static Object[][] dp() { |
| 76 | + return new Object[][] {new Object[] {true}, new Object[] {false}}; |
| 77 | + } |
| 78 | +} |
0 commit comments