1 package test.objectfactory;
2 
3 import org.testng.TestNG;
4 import org.testng.TestNGException;
5 import org.testng.annotations.AfterMethod;
6 import org.testng.annotations.Test;
7 import org.testng.xml.XmlClass;
8 import org.testng.xml.XmlSuite;
9 import test.TestHelper;
10 
11 /**
12  * @author Hani Suleiman Date: Mar 6, 2007 Time: 3:52:19 PM
13  */
14 public class CustomFactoryTest {
15   @Test
setFactoryOnTestNG()16   public void setFactoryOnTestNG() {
17     XmlSuite suite = TestHelper.createSuite("test.objectfactory.Simple", "objectfactory");
18     // suite.setObjectFactory(new LoggingObjectFactory());
19     TestNG tng = TestHelper.createTestNG(suite);
20     tng.setObjectFactory(LoggingObjectFactory.class);
21     tng.run();
22     assert LoggingObjectFactory.invoked == 1 : "Logging factory invoked "
23         + LoggingObjectFactory.invoked + " times";
24   }
25 
26   @AfterMethod
resetCount()27   public void resetCount() {
28     LoggingObjectFactory.invoked = 0;
29   }
30 
31   @Test
setFactoryOnSuite()32   public void setFactoryOnSuite() {
33     XmlSuite suite = TestHelper.createSuite("test.objectfactory.Simple", "objectfactory");
34     suite.setObjectFactory(new LoggingObjectFactory());
35     TestNG tng = TestHelper.createTestNG(suite);
36     tng.run();
37     assert LoggingObjectFactory.invoked == 1 : "Logging factory invoked "
38         + LoggingObjectFactory.invoked + " times";
39   }
40 
41   @Test(enabled = false, description = "This broke after I made the change to enable AbstractTest")
setFactoryByAnnotation()42   public void setFactoryByAnnotation() {
43     XmlSuite suite = TestHelper.createSuite("test.objectfactory.Simple", "objectfactory");
44     suite.getTests().get(0).getXmlClasses()
45         .add(new XmlClass("test.objectfactory.MyFactoryFactory"));
46     TestNG tng = TestHelper.createTestNG(suite);
47     tng.run();
48     assert LoggingObjectFactory.invoked == 1 : "Logging factory invoked "
49         + LoggingObjectFactory.invoked + " times";
50   }
51 
52   @Test
factoryReceivesContext()53   public void factoryReceivesContext() {
54     XmlSuite suite = TestHelper.createSuite("test.objectfactory.Simple", "objectfactory");
55     suite.getTests().get(0).getXmlClasses()
56         .add(new XmlClass("test.objectfactory.ContextAwareFactoryFactory"));
57     TestNG tng = TestHelper.createTestNG(suite);
58     tng.run();
59   }
60 
61   @Test(expectedExceptions = TestNGException.class)
setInvalidMethodFactoryByAnnotation()62   public void setInvalidMethodFactoryByAnnotation() {
63     XmlSuite suite = TestHelper.createSuite("test.objectfactory.Simple", "objectfactory");
64     suite.getTests().get(0).getXmlClasses()
65         .add(new XmlClass("test.objectfactory.BadMethodFactoryFactory"));
66     TestNG tng = TestHelper.createTestNG(suite);
67     tng.run();
68   }
69 }
70