01: package test.objectfactory;
02:
03: import org.testng.annotations.Test;
04: import org.testng.annotations.AfterMethod;
05: import org.testng.TestNG;
06: import org.testng.TestNGException;
07: import org.testng.xml.XmlSuite;
08: import org.testng.xml.XmlClass;
09: import test.TestHelper;
10:
11: /**
12: * @author Hani Suleiman
13: * Date: Mar 6, 2007
14: * Time: 3:52:19 PM
15: */
16: public class CustomFactoryTest {
17: @Test
18: public void setFactoryOnTestNG() {
19: XmlSuite suite = TestHelper.createSuite(
20: "test.objectfactory.Simple", "objectfactory");
21: //suite.setObjectFactory(new LoggingObjectFactory());
22: TestNG tng = TestHelper.createTestNG(suite);
23: tng.setObjectFactory(LoggingObjectFactory.class);
24: tng.run();
25: assert LoggingObjectFactory.invoked == 1 : "Logging factory invoked "
26: + LoggingObjectFactory.invoked + " times";
27: }
28:
29: @AfterMethod
30: public void resetCount() {
31: LoggingObjectFactory.invoked = 0;
32: }
33:
34: @Test
35: public void setFactoryOnSuite() {
36: XmlSuite suite = TestHelper.createSuite(
37: "test.objectfactory.Simple", "objectfactory");
38: suite.setObjectFactory(new LoggingObjectFactory());
39: TestNG tng = TestHelper.createTestNG(suite);
40: tng.run();
41: assert LoggingObjectFactory.invoked == 1 : "Logging factory invoked "
42: + LoggingObjectFactory.invoked + " times";
43: }
44:
45: @Test
46: public void setFactoryByAnnotation() {
47: XmlSuite suite = TestHelper.createSuite(
48: "test.objectfactory.Simple", "objectfactory");
49: suite.getTests().get(0).getXmlClasses().add(
50: new XmlClass("test.objectfactory.MyFactoryFactory"));
51: TestNG tng = TestHelper.createTestNG(suite);
52: tng.run();
53: assert LoggingObjectFactory.invoked == 1 : "Logging factory invoked "
54: + LoggingObjectFactory.invoked + " times";
55: }
56:
57: @Test
58: public void factoryReceivesContext() {
59: XmlSuite suite = TestHelper.createSuite(
60: "test.objectfactory.Simple", "objectfactory");
61: suite
62: .getTests()
63: .get(0)
64: .getXmlClasses()
65: .add(
66: new XmlClass(
67: "test.objectfactory.ContextAwareFactoryFactory"));
68: TestNG tng = TestHelper.createTestNG(suite);
69: tng.run();
70: }
71:
72: @Test(expectedExceptions=TestNGException.class)
73: public void setInvalidMethodFactoryByAnnotation() {
74: XmlSuite suite = TestHelper.createSuite(
75: "test.objectfactory.Simple", "objectfactory");
76: suite.getTests().get(0).getXmlClasses().add(
77: new XmlClass(
78: "test.objectfactory.BadMethodFactoryFactory"));
79: TestNG tng = TestHelper.createTestNG(suite);
80: tng.run();
81: }
82: }
|