/trunk/src/test/java/org/mentacontainer/impl/InstanceComponentTest.java |
---|
New file |
0,0 → 1,31 |
package org.mentacontainer.impl; |
import junit.framework.Assert; |
import org.junit.Test; |
import org.mentacontainer.Component; |
import org.mentacontainer.Container; |
public class InstanceComponentTest { |
@Test |
public void testInstanceComponent() throws Exception { |
String s = new String("saoj"); |
Component ic = new InstanceComponent(s); |
Container c = new MentaContainer(); |
c.ioc("myString", ic); |
String s1 = c.get("myString"); |
String s2 = c.get("myString"); |
Assert.assertNotNull(s1); |
Assert.assertNotNull(s2); |
Assert.assertTrue(s == s1); |
Assert.assertTrue(s1 == s2); |
} |
} |
/trunk/src/test/java/org/mentacontainer/impl/MentaContainerTest.java |
---|
131,6 → 131,43 |
} |
@Test |
public void testScopes() { |
Container c = new MentaContainer(); |
// if you don't provide a scope the Scope is NONE |
// meaning a new instance is created on every |
// request for the component |
c.ioc("myString", String.class).addInitValue("saoj"); |
String s1 = c.get("myString"); |
String s2 = c.get("myString"); |
Assert.assertTrue(s1 != s2); |
// then you can use SINGLETON |
// always get the same instance no matter what |
c.ioc("myString", String.class, Scope.SINGLETON).addInitValue("saoj"); |
s1 = c.get("myString"); |
s2 = c.get("myString"); |
Assert.assertTrue(s1 == s2); |
// then you can use THREAD |
// each thread will get a different instance |
c.ioc("myString", String.class, Scope.THREAD).addInitValue("saoj"); |
s1 = c.get("myString"); |
s2 = c.get("myString"); |
Assert.assertTrue(s1 == s2); // same thread |
} |
@Test |
public void testThreadLocal() throws Exception { |
final Container c = new MentaContainer(); |
/trunk/src/main/java/org/mentacontainer/Container.java |
---|
131,7 → 131,8 |
/** |
* Clear all cached instances for that scope. If you have a thread pool for example you will |
* have to clear the THREAD scope when your thread is returned to the pool. |
* have to clear the THREAD scope when your thread is returned to the pool. It does not make |
* sense to clear a NONE scope (the method returns doing nothing). |
* |
* @param scope The scope to be cleared. |
*/ |
141,7 → 142,7 |
* Clear a single key from cache and return the instance that was cached. |
* |
* @param key The key representing the bean inside the container. |
* @return The value that was cached and it is not anymore (was cleared) |
* @return The value that was cached and it is not anymore (was cleared) or null if nothing was cleared |
*/ |
public <T> T clear(String key); |
} |
/trunk/src/main/java/org/mentacontainer/Clearable.java |
---|
1,5 → 1,24 |
package org.mentacontainer; |
/** |
* Some components can also implement this interface to perform some cleanup |
* when the instance is cleared. For example, a connection pool will want |
* to know when the connection instance is cleared so it can return it to |
* the pool. |
* |
* It makes more sense to use this interface for components that will be placed |
* in the THREAD scope, but you can also use it with components in the SINGLETON |
* scope. |
* |
* This is particular useful for the THREAD scope for dealing with thread pools, so |
* when the thread is returned to the thread pool you will want to clear the THREAD |
* scope. That's pretty much how web containers work: one thread per request coming from |
* a thread pool. |
* |
* @author sergio.oliveira.jr@gmail.com |
* |
* @param <E> |
*/ |
public interface Clearable<E> { |
public void onCleared(E clearedObject); |