object factory

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

object factory

Hani Suleiman

Since I didn't hear any negative feedback, I went ahead and  
implemented this feature. I'm not checking it in just yet so as to  
solicit feedback/objections before I do so. Documentation of the  
feature is below.

----

TestNG allows for plugging in factory objects to handle the actual  
instantiation of tests. This can be configured through Ant, the  
testng.xml configuration file, programatically for cases where TestNG  
is created at runtime (for example, inside of a container) or through  
the @ObjectFactory annotation.

The object factory itself has to implement the  
org.testng.IObjectFactory interface, the relevant method is:

Object newInstance(Constructor constructor, Object... params);

Once we have a factory, the next step is to hook it into TestNG using  
any of the mechanisms above:

Ant: The ant task has a 'objectFactory' attribute that expects a  
fully qualified class name
Command line: -objectfactory parameter allows a fully qualified class  
name
testng.xml: suite attribute 'object-factory'
annotations: @ObjectFactory.

Note that obviously, the class that contains the annotation will  
itself not go through the factory, and will instead be constructed by  
TestNG itself.

For the annotated method, the return type must be  
org.testng.IObjectFactory. If the method declares any other type,  
then TestNG will throw an error. Also, if there are duplicate methods  
with this annotation, then only one will be used, whichever is  
discovered first. Note that it makes no sense to have more than one,  
so there is no reason to need to specify this annotation twice.

An obvious use for this functionality is in allowing tests to be  
wired by via Spring. So an example Spring based object factory would be:

public class SpringObjectFactory extends ObjectFactoryImpl {

   private ApplicationContext context;

   public SpringObjectFactory(ApplicationContext context) {
     this.context = context;
   }

   public Object newInstance(Constructor constructor, Object...  
params) {
    Object created = super.newInstance(constructor, params);
     return context.configureBean(created, null);
   }
}

then anywhere else in the test classes:

@ObjectFactory
public IObjectFactory initFactory() {
   return new SpringObjectFactory(new ClassPathXmlApplicationContext
("spring.xml"));
}
---

Feedback appreciated!

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "testng-dev" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/testng-dev?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: object factory

Alexandru Popescu ☀

Sorry for the delay. I will try to check it shortly and follow up with
my comments. However, in the past I have discussed about this feature
with Cedric and we agreed it may become necessary... so here it is
:-).

./alex
--
.w( the_mindstorm )p.
  TestNG co-founder
EclipseTestNG Creator


On 3/7/07, Hani Suleiman <[hidden email]> wrote:

>
> Since I didn't hear any negative feedback, I went ahead and
> implemented this feature. I'm not checking it in just yet so as to
> solicit feedback/objections before I do so. Documentation of the
> feature is below.
>
> ----
>
> TestNG allows for plugging in factory objects to handle the actual
> instantiation of tests. This can be configured through Ant, the
> testng.xml configuration file, programatically for cases where TestNG
> is created at runtime (for example, inside of a container) or through
> the @ObjectFactory annotation.
>
> The object factory itself has to implement the
> org.testng.IObjectFactory interface, the relevant method is:
>
> Object newInstance(Constructor constructor, Object... params);
>
> Once we have a factory, the next step is to hook it into TestNG using
> any of the mechanisms above:
>
> Ant: The ant task has a 'objectFactory' attribute that expects a
> fully qualified class name
> Command line: -objectfactory parameter allows a fully qualified class
> name
> testng.xml: suite attribute 'object-factory'
> annotations: @ObjectFactory.
>
> Note that obviously, the class that contains the annotation will
> itself not go through the factory, and will instead be constructed by
> TestNG itself.
>
> For the annotated method, the return type must be
> org.testng.IObjectFactory. If the method declares any other type,
> then TestNG will throw an error. Also, if there are duplicate methods
> with this annotation, then only one will be used, whichever is
> discovered first. Note that it makes no sense to have more than one,
> so there is no reason to need to specify this annotation twice.
>
> An obvious use for this functionality is in allowing tests to be
> wired by via Spring. So an example Spring based object factory would be:
>
> public class SpringObjectFactory extends ObjectFactoryImpl {
>
>    private ApplicationContext context;
>
>    public SpringObjectFactory(ApplicationContext context) {
>      this.context = context;
>    }
>
>    public Object newInstance(Constructor constructor, Object...
> params) {
>     Object created = super.newInstance(constructor, params);
>      return context.configureBean(created, null);
>    }
> }
>
> then anywhere else in the test classes:
>
> @ObjectFactory
> public IObjectFactory initFactory() {
>    return new SpringObjectFactory(new ClassPathXmlApplicationContext
> ("spring.xml"));
> }
> ---
>
> Feedback appreciated!
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "testng-dev" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/testng-dev?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: object factory

Hani Suleiman

It's checked in now, so you can look over the code and see if it  
looks reasonable!

On Mar 7, 2007, at 7:52 AM, Alexandru Popescu wrote:

>
> Sorry for the delay. I will try to check it shortly and follow up with
> my comments. However, in the past I have discussed about this feature
> with Cedric and we agreed it may become necessary... so here it is
> :-).
>
> ./alex
> --
> .w( the_mindstorm )p.
>   TestNG co-founder
> EclipseTestNG Creator
>
>
> On 3/7/07, Hani Suleiman <[hidden email]> wrote:
>>
>> Since I didn't hear any negative feedback, I went ahead and
>> implemented this feature. I'm not checking it in just yet so as to
>> solicit feedback/objections before I do so. Documentation of the
>> feature is below.
>>
>> ----
>>
>> TestNG allows for plugging in factory objects to handle the actual
>> instantiation of tests. This can be configured through Ant, the
>> testng.xml configuration file, programatically for cases where TestNG
>> is created at runtime (for example, inside of a container) or through
>> the @ObjectFactory annotation.
>>
>> The object factory itself has to implement the
>> org.testng.IObjectFactory interface, the relevant method is:
>>
>> Object newInstance(Constructor constructor, Object... params);
>>
>> Once we have a factory, the next step is to hook it into TestNG using
>> any of the mechanisms above:
>>
>> Ant: The ant task has a 'objectFactory' attribute that expects a
>> fully qualified class name
>> Command line: -objectfactory parameter allows a fully qualified class
>> name
>> testng.xml: suite attribute 'object-factory'
>> annotations: @ObjectFactory.
>>
>> Note that obviously, the class that contains the annotation will
>> itself not go through the factory, and will instead be constructed by
>> TestNG itself.
>>
>> For the annotated method, the return type must be
>> org.testng.IObjectFactory. If the method declares any other type,
>> then TestNG will throw an error. Also, if there are duplicate methods
>> with this annotation, then only one will be used, whichever is
>> discovered first. Note that it makes no sense to have more than one,
>> so there is no reason to need to specify this annotation twice.
>>
>> An obvious use for this functionality is in allowing tests to be
>> wired by via Spring. So an example Spring based object factory  
>> would be:
>>
>> public class SpringObjectFactory extends ObjectFactoryImpl {
>>
>>    private ApplicationContext context;
>>
>>    public SpringObjectFactory(ApplicationContext context) {
>>      this.context = context;
>>    }
>>
>>    public Object newInstance(Constructor constructor, Object...
>> params) {
>>     Object created = super.newInstance(constructor, params);
>>      return context.configureBean(created, null);
>>    }
>> }
>>
>> then anywhere else in the test classes:
>>
>> @ObjectFactory
>> public IObjectFactory initFactory() {
>>    return new SpringObjectFactory(new ClassPathXmlApplicationContext
>> ("spring.xml"));
>> }
>> ---
>>
>> Feedback appreciated!
>>
>>>
>>
>
> >


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "testng-dev" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/testng-dev?hl=en
-~----------~----~----~----~------~----~------~--~---