Created
March 25, 2010 12:25
-
-
Save bonnefoa/343491 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.izforge.izpack.core.container; | |
import com.izforge.izpack.api.container.BindeableContainer; | |
import com.izforge.izpack.api.container.DependenciesFillerContainer; | |
import org.picocontainer.Characteristics; | |
import org.picocontainer.MutablePicoContainer; | |
/** | |
* Abstract container | |
*/ | |
public abstract class AbstractContainer implements BindeableContainer | |
{ | |
protected MutablePicoContainer pico; | |
public <T> void addComponent(Class<T> componentType) | |
{ | |
pico.as(Characteristics.USE_NAMES).addComponent(componentType); | |
} | |
public void addComponent(Object componentType, Object implementation) | |
{ | |
pico.addComponent(componentType, implementation); | |
} | |
public <T> T getComponent(Class<T> componentType) | |
{ | |
return pico.getComponent(componentType); | |
} | |
public Object getComponent(Object componentKeyOrType) | |
{ | |
return pico.getComponent(componentKeyOrType); | |
} | |
public void addConfig(String name, Object val) | |
{ | |
pico.addConfig(name, val); | |
} | |
public void dispose() | |
{ | |
pico.dispose(); | |
} | |
public MutablePicoContainer fillContainer(DependenciesFillerContainer fillerContainer) | |
{ | |
return fillerContainer.fillContainer(pico); | |
} | |
public MutablePicoContainer makeChildContainer() | |
{ | |
return pico.makeChildContainer(); | |
} | |
public MutablePicoContainer getContainer() | |
{ | |
return pico; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.izforge.izpack.api.container; | |
import org.picocontainer.MutablePicoContainer; | |
/** | |
* Interface for applcation container using Pico | |
* | |
* @author Anthonin Bonnefoy | |
*/ | |
public interface BindeableContainer | |
{ | |
<T> void addComponent(Class<T> componentType); | |
<T> T getComponent(final Class<T> componentType); | |
void addComponent(Object componentType, Object implementation); | |
Object getComponent(Object componentKeyOrType); | |
void initBindings() throws Exception; | |
void dispose(); | |
MutablePicoContainer getContainer(); | |
MutablePicoContainer fillContainer(DependenciesFillerContainer fillerContainer); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.izforge.izpack.test.junit; | |
import com.izforge.izpack.api.container.BindeableContainer; | |
import com.izforge.izpack.test.Container; | |
import org.junit.runners.BlockJUnit4ClassRunner; | |
import org.junit.runners.model.InitializationError; | |
import java.lang.reflect.Constructor; | |
import java.lang.reflect.InvocationTargetException; | |
import java.util.List; | |
/** | |
* Custom runner for getting dependencies injected in test with PicoContainer | |
* | |
* @author Anthonin Bonnefoy | |
*/ | |
public class | |
PicoRunner extends BlockJUnit4ClassRunner | |
{ | |
private Class<?> klass; | |
public PicoRunner(Class<?> klass) | |
throws InitializationError | |
{ | |
super(klass); | |
this.klass = klass; | |
} | |
@Override | |
protected void validateConstructor(List<Throwable> errors) | |
{ | |
} | |
@Override | |
protected Object createTest() throws Exception | |
{ | |
Class<? extends BindeableContainer> containerClass = getTestClass().getJavaClass().getAnnotation(Container.class).value(); | |
BindeableContainer installerContainer = getContainerInstance(containerClass); | |
installerContainer.initBindings(); | |
installerContainer.addComponent(klass); | |
Object component = installerContainer.getComponent(klass); | |
return component; | |
} | |
private BindeableContainer getContainerInstance(Class<? extends BindeableContainer> containerClass) throws InvocationTargetException, IllegalAccessException, InstantiationException | |
{ | |
Constructor<? extends BindeableContainer> constructor = getUniqueConstructor(containerClass); | |
if (constructor.getParameterTypes().length == 1) | |
{ | |
return constructor.newInstance(klass); | |
} | |
return constructor.newInstance(); | |
} | |
private Constructor<? extends BindeableContainer> getUniqueConstructor(Class<? extends BindeableContainer> containerClass) | |
{ | |
Constructor<?>[] constructors = containerClass.getConstructors(); | |
if (constructors.length > 1) | |
{ | |
throw new IllegalArgumentException("There should be only one constructor for " + containerClass); | |
} | |
return (Constructor<? extends BindeableContainer>) constructors[0]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment