MentaContainer

Compare Revisions

Ignore whitespace Rev 92 → Rev 93

/trunk/src/test/java/org/mentacontainer/impl/ClassFactoryTest.java
30,10 → 30,12
@Test
public void testFactory() {
Factory c1 = new ClassFactory(User.class).addInitValue("saoj");
Container container = new MentaContainer();
Factory c2 = new ClassFactory(User.class).addPropertyValue("username", "soliveira");
Factory c1 = new ClassFactory(container, User.class).addInitValue("saoj");
Factory c2 = new ClassFactory(container, User.class).addPropertyValue("username", "soliveira");
User u1 = c1.getInstance();
User u2 = c2.getInstance();
116,8 → 118,10
@Test
public void testPrimitivePropertyValue1() {
Factory f = new ClassFactory(TestObj2.class).addPropertyValue("integer", 20);
Container container = new MentaContainer();
Factory f = new ClassFactory(container, TestObj2.class).addPropertyValue("integer", 20);
TestObj2 o = f.getInstance();
Assert.assertEquals(20, o.x);
126,8 → 130,10
@Test
public void testPrimitivePropertyValue2() {
Factory f = new ClassFactory(TestObj2.class).addPropertyValue("integer", new Integer(20));
Container container = new MentaContainer();
Factory f = new ClassFactory(container, TestObj2.class).addPropertyValue("integer", new Integer(20));
TestObj2 o = f.getInstance();
Assert.assertEquals(20, o.x);
147,8 → 153,10
@Test
public void testPrimitivePropertyValue3() {
Factory f = new ClassFactory(TestObj2_1.class).addPropertyValue("integer", 20);
Container container = new MentaContainer();
Factory f = new ClassFactory(container, TestObj2_1.class).addPropertyValue("integer", 20);
TestObj2_1 o = f.getInstance();
Assert.assertEquals(20, o.x);
157,8 → 165,10
@Test
public void testPrimitivePropertyValue4() {
Factory f = new ClassFactory(TestObj2_1.class).addPropertyValue("integer", new Integer(20));
Container container = new MentaContainer();
Factory f = new ClassFactory(container, TestObj2_1.class).addPropertyValue("integer", new Integer(20));
TestObj2_1 o = f.getInstance();
Assert.assertEquals(20, o.x);
177,8 → 187,10
@Test
public void testPrimitiveInitValue1() {
Factory f = new ClassFactory(TestObj3.class).addInitValue(20);
Container container = new MentaContainer();
Factory f = new ClassFactory(container, TestObj3.class).addInitValue(20);
TestObj3 o = f.getInstance();
Assert.assertEquals(20, o.x);
195,8 → 207,10
@Test
public void testPrimitiveInitValue2() {
Container container = new MentaContainer();
Factory f = new ClassFactory(TestObj3_1.class).addInitValue(20);
Factory f = new ClassFactory(container, TestObj3_1.class).addInitValue(20);
TestObj3_1 o = f.getInstance();
217,8 → 231,10
@Test
public void testPrimitiveInitValue3() {
Factory f = new ClassFactory(TestObj3_2.class).addInitValue(20).addInitPrimitive(30L);
Container container = new MentaContainer();
Factory f = new ClassFactory(container, TestObj3_2.class).addInitValue(20).addInitPrimitive(30L);
TestObj3_2 o = f.getInstance();
Assert.assertEquals(20, o.x);
243,8 → 259,10
@Test
public void testMixInitValues() {
Factory f = new ClassFactory(TestObj4.class).addInitPrimitive(20).addInitValue("hello").addInitValue(20L).addInitPrimitive(20L);
Container container = new MentaContainer();
Factory f = new ClassFactory(container, TestObj4.class).addInitPrimitive(20).addInitValue("hello").addInitValue(20L).addInitPrimitive(20L);
TestObj4 o = f.getInstance();
Assert.assertEquals(20, o.x);
/trunk/src/main/java/org/mentacontainer/impl/ClassFactory.java
13,11 → 13,11
import org.mentacontainer.util.FindMethod;
 
/**
* A simple implementation of the Component interface.
* The implementation of the Configurable Factory.
*
* @author sergio.oliveira.jr@gmail.com
*/
public class ClassFactory implements ConfigurableFactory {
class ClassFactory implements ConfigurableFactory {
private final Container container;
33,11 → 33,6
private Map<String, Method> cache = null;
public ClassFactory(Class<? extends Object> klass) {
this(null, klass);
}
public ClassFactory(Container container, Class<? extends Object> klass) {
this.container = container;
63,16 → 58,12
@Override
public ConfigurableFactory addPropertyDependency(String property, String key) {
if (container == null) throw new IllegalStateException("Cannot set property dependency because factory does not have container!");
return addPropertyValue(property, new DependencyKey(key));
}
@Override
public ConfigurableFactory addInitDependency(String key) {
if (container == null) throw new IllegalStateException("Cannot set constructor dependency because factory does not have container!");
return addInitValue(new DependencyKey(key), container.getType(key));
}
/trunk/src/main/java/org/mentacontainer/Container.java
54,14 → 54,14
public ConfigurableFactory ioc(String key, Class<?extends Object> klass);
/**
* Set up IoC based on the component passed. The scope assumed is NONE.
* Set up IoC based on the factory passed. The scope assumed is NONE.
*
* @param key The key representing the bean to return. The name of the bean in the container.
* @param component The component for the IoC.
* @param factory The component for the IoC.
* @return The component passed as a parameter.
* @see Factory
*/
public Factory ioc(String key, Factory component);
public Factory ioc(String key, Factory factory);
/**
* Set up IoC based on the component passed. Specify the scope of the component.