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
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)
{
fixture = super.getFixture(name, params);
injector.injectMembers(fixture.getTarget());
}
else
{
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 |
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 |
| GET |
/messages/0 |
| check |
response status |
200 |
| check |
response body |
Hello GreenPepper |
| set request header |
Accept |
as |
application/xml |
| GET |
/messages/0 |
| check |
response status |
200 |
| check |
response body |
<?xml version="1.0"><message id="0" value="Hello GreenPepper" />
|
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 |
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)
{
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 :
- You must add GreenPepper.Extensions.Castle.Windsor assembly in your configuration assemblies
- 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
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);
}
}
}
}
}