Home | History | Annotate | Download | only in objectfactory
      1 package test.objectfactory;
      2 
      3 import org.testng.Assert;
      4 import org.testng.IObjectFactory;
      5 import org.testng.annotations.ObjectFactory;
      6 import org.testng.annotations.Test;
      7 
      8 import java.lang.reflect.Constructor;
      9 
     10 @SuppressWarnings("serial")
     11 public class CombinedTestAndObjectFactorySample implements IObjectFactory{
     12   private boolean configured = false;
     13 
     14   @ObjectFactory public IObjectFactory create() {
     15     return new CombinedTestAndObjectFactorySample();
     16   }
     17 
     18   @Test public void isConfigured() {
     19     Assert.assertTrue(configured, "Should have been configured by object factory");
     20   }
     21 
     22   @Override
     23   @SuppressWarnings("unchecked")
     24   public Object newInstance(Constructor constructor, Object... params)  {
     25     Object o;
     26     try {
     27       o = constructor.newInstance(params);
     28     } catch (Exception e) {
     29       throw new RuntimeException(e);
     30     }
     31     if (CombinedTestAndObjectFactorySample.class.equals(o.getClass())) {
     32       CombinedTestAndObjectFactorySample s = (CombinedTestAndObjectFactorySample) o;
     33       s.configured = true;
     34     }
     35     return o;
     36   }
     37 }
     38