Dashboard > GreenPepper Open Documentation Refactoring > ... > Developer Guide > 8. Developer examples
  GreenPepper Open Documentation Refactoring Log In View a printable version of the current page.  
  8. Developer examples
Added by Laurent Cobos, last edited by Francois Denommee on Oct 29, 2009  (view change)
Labels: 
(None)

8. Developer examples

The Guice container system under development

Google Guice is an Inversion Of Control (IOC) container get more information about it on Google Guice website

GuiceSystemUnderDevelopment code can be found in greenpepper-extension-guice: GuiceSystemUnderDevelopment.java

Guice system under development sample
package com.greenpepper.extensions.guice;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Module;
import com.greenpepper.reflect.DefaultFixture;
import com.greenpepper.reflect.Fixture;
import com.greenpepper.systemunderdevelopment.DefaultSystemUnderDevelopment;

public class GuiceSystemUnderDevelopment extends DefaultSystemUnderDevelopment
{

	private Injector injector;
	private List<String> moduleNames = new ArrayList<String>();

	public GuiceSystemUnderDevelopment()
	{
	}

	public GuiceSystemUnderDevelopment(String... moduleNames)
	{
		Collections.addAll(this.moduleNames, moduleNames);
	}

	@Override
	public Fixture getFixture(String name, String... params)
			throws Throwable
	{
		lazilyInstantiateModulesAndInjector();

		Fixture fixture;

		if (params.length != 0)
		{
			// When params are used, we use the PlainOldSystemUnderDevelopment to instantiate the fixture
			// these params are passed to the constructor
			// Then we use Guice to inject members
			fixture = super.getFixture(name, params);
			injector.injectMembers(fixture.getTarget());
		}
		else
		{
			// Use Guice to instantiate
			Class<?> klass = loadType(name).getUnderlyingClass();
			Object target = injector.getInstance(klass);
			fixture = new DefaultFixture(target);
		}

		return fixture;
	}

	private void lazilyInstantiateModulesAndInjector()
			throws Exception
	{
		if (injector == null)
		{
			List<Module> modules = convertModuleNamesToModules();
			injector = Guice.createInjector(modules);
		}
	}

	private List<Module> convertModuleNamesToModules()
			throws Exception
	{
		List<Module> modules = new ArrayList<Module>(moduleNames.size());
		for (String moduleName : moduleNames)
		{
			Class<?> klass = loadType(moduleName).getUnderlyingClass();
			modules.add((Module)klass.newInstance());
		}
		return modules;
	}

	public void addModules(String... moduleNames)
	{
		if (injector == null)
		{
			Collections.addAll(this.moduleNames, moduleNames);
		}
		else
		{
			throw new IllegalStateException("Cannot add module after a fixture has been instantiated");
		}
	}
}

Rest Fixture sample

Only available in Java

Here is an example of a fixture to test Rest web services.
RestFixture code can be found in greenpepper-extension-sandbox : RestFixture.java

RestFixture support GET POST DELETE PUT HEAD HTTP methods.
Available readable properties are responseHeader(headerName) responseBody responseStatus

Import
com.greenpepper.extensions
do with Rest Fixture on http://localhost:9998

GET example

GET /helloworld
check response status 200
check response header Content-Type text/plain
check response body Hello World
GET /unknown
check response status 404

POST example (create resource)

set request body Hello GreenPepper
POST /messages
check response status 201
check response header Location http://localhost:9998/messages/0
GET /messages/0
check response status 200
check response body Hello GreenPepper

Request with custom header example

set request header Accept as application/xml
GET /messages/0
check response status 200
check response body
&#60;?xml version="1.0"&#62;&#60;message id="0" value="Hello GreenPepper" /&#62;

PUT example (update resource)

set request body Hello from GreenPepper
PUT /messages/0
check response status 200
GET /messages/0
check response body Hello from GreenPepper

HEAD example (check if specified resource exists)

HEAD /messages/0
check response status 200
HEAD /messages/1
check response status 404

DELETE example

DELETE /messages/0
check response status 200
GET /messages/0
check response status 404
end

The Spring container system under development

Spring is an Inversion Of Control (IOC) container get more information about it on Spring website

SpringSystemUnderDevelopment code can be found in greenpepper-extensions-spring: SpringSystemUnderDevelopment.java

package com.greenpepper.extensions.spring;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.RootBeanDefinition;

import com.greenpepper.reflect.DefaultFixture;
import com.greenpepper.reflect.Fixture;
import com.greenpepper.systemunderdevelopment.DefaultSystemUnderDevelopment;

public class SpringSystemUnderDevelopment extends DefaultSystemUnderDevelopment
{
    private BeanFactory beanFactory;

    public SpringSystemUnderDevelopment(String... applicationCtxes)
    {
        this.beanFactory = new GreenPepperXMLAplicationContext(applicationCtxes).getBeanFactory();
    }
	
    public SpringSystemUnderDevelopment(BeanFactory beanFactory)
    {
        this.beanFactory = beanFactory;
    }

    @Override
    public Fixture getFixture(String fixtureName, String... params) throws Throwable
    {
		Fixture fixture;
		if (params.length != 0)
        {
		    // When params are used, we use the PlainOldSystemUnderDevelopment to instantiate the fixture
			// these params are passed to the constructor
            fixture = super.getFixture(fixtureName, params);
        }
		else
		{
	        try
	        {
	            fixture = new DefaultFixture(beanFactory.getBean(fixtureName));
	        }
	        catch (NoSuchBeanDefinitionException e)
	        {
	            fixture = new DefaultFixture(instantiateAsAutowiredBean(fixtureName));
	        }
		}
		
		return fixture;
    }

    private Object instantiateAsAutowiredBean(String fixtureName) throws Exception
    {
        Class fixtureClass = loadType(fixtureName).getUnderlyingClass();

        BeanDefinition beanDef = new RootBeanDefinition(fixtureClass, RootBeanDefinition.AUTOWIRE_AUTODETECT);
        
        DefaultListableBeanFactory fallFactory = new DefaultListableBeanFactory(beanFactory);
        fallFactory.registerBeanDefinition(fixtureName, beanDef);

        return fallFactory.getBean(fixtureName);
    }
}

The Windsor castle container system under development

Windsor container is an Inversion Of Control (IOC) container get more information about it on Windsor Castle website

New in GreenPepper .Net 1.3
Import
GreenPepper.WindsorCastle.Fixtures


A Castle basic Xml configuration file
<configuration>
    <components>
        <component id="Windsor.HelloWorld" type="GreenPepper.WindsorCastle.Fixtures.WindsorHelloWorld, GreenPepper.Extensions">
            <parameters>
            	<injectedConstructor>hello constructor</injectedConstructor>
                <injectedProperty>hello property</injectedProperty>
            </parameters>
        </component>

        <component id="Windsor.component1" 
        	type="GreenPepper.WindsorCastle.Fixtures.MyComponent1, GreenPepper.Extensions">
        </component>
        
        <component id="Windsor.component2"
        	type="GreenPepper.WindsorCastle.Fixtures.MyComponent2, GreenPepper.Extensions">
        </component>
        
        <component id="Windsor.AutoWired" type="GreenPepper.WindsorCastle.Fixtures.WindsorAutoWiredFixture, GreenPepper.Extensions" />
    </components>
</configuration>
How to use Windsor system under development :
Begin Info
  • Requirements :
  1. You must add GreenPepper.Extensions.Castle.Windsor assembly in your configuration assemblies
  2. You must notify GreenPepper Core to use Windsor system under development : GreenPepper.WindsorCastle.WindsorSystemUnderDevelopment

(You can find more about system under development configuration here : Using a custom SystemUnderDevelopment)

  • If you use a Windsor xml configuration file :
    Just add the path to your Windsor configuration file in the system under development arguments
End Info
WindsorCastle system under development
using System;
using Castle.MicroKernel;
using Castle.Windsor;
using GreenPepper.Fixtures;
using GreenPepper.Reflect;

namespace GreenPepper.WindsorCastle
{
    public class WindsorSystemUnderDevelopment : PlainOldSystemUnderDevelopment
    {
        private IWindsorContainer container;

        public WindsorSystemUnderDevelopment(IWindsorContainer container)
        {
            this.container = container;
        }

        public WindsorSystemUnderDevelopment():this(new WindsorContainer())
        {    
        }

        public WindsorSystemUnderDevelopment(params string[] xmlConfigurationFile)
            : this(new WindsorContainer(xmlConfigurationFile[0]))
        {
        }

        public override IFixture GetFixture(string fixtureName, params string[] parameters)
        {
            try
            {
                Type type = TypeForName(fixtureName);
                container.AddComponent(type.FullName, type);
                return new DefaultFixture(container.Resolve(type));
            }
            catch (Exception)
            {
                try
                {
                    return new DefaultFixture(container.Resolve(fixtureName));   
                }
                catch (ComponentNotFoundException)
                {
                    return base.GetFixture(fixtureName, parameters);
                }
            }
        }
    }
}
Guice Example (GreenPepper Open Documentation Refactoring )
RestFixture (GreenPepper Open Documentation Refactoring )
Spring Example (GreenPepper Open Documentation Refactoring )
Windsor Castle System Under Development (GreenPepper Open Documentation Refactoring )

DEMONSTRATION LICENSE - This Confluence site is for demonstration purposes only. Evaluate Confluence today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.4.3 Build:#705 Mar 21, 2007) - Bug/feature request - Contact Administrators