[picocontainer-scm] [5769] java/2.x/trunk/pico/container/src/java/org/picocontainer: ThreadCaching behavior not working for addComponent(instance) situations
paul-yCVjj/[email protected] Wed, 2 Mar 2011 10:33:39 -0600 (CST)
| Newsgroups | gmane.comp.java.picocontainer.cvs |
|---|---|
| Message-ID | <[email protected]> |
Revision 5769
Author paul
Date 2011-03-02 10:33:37 -0600 (Wed, 02 Mar 2011)
Log Message
ThreadCaching behavior not working for addComponent(instance) situations
Modified Paths
- java/2.x/trunk/pico/container/src/java/org/picocontainer/DefaultPicoContainer.java
- java/2.x/trunk/pico/container/src/test/org/picocontainer/behaviors/ThreadCachingTestCase.java
Diff
Modified: java/2.x/trunk/pico/container/src/java/org/picocontainer/DefaultPicoContainer.java (5768 => 5769)
--- java/2.x/trunk/pico/container/src/java/org/picocontainer/DefaultPicoContainer.java 2011-02-28 06:48:59 UTC (rev 5768)
+++ java/2.x/trunk/pico/container/src/java/org/picocontainer/DefaultPicoContainer.java 2011-03-02 16:33:37 UTC (rev 5769)
@@ -454,6 +454,7 @@
/** {@inheritDoc} **/
public MutablePicoContainer addAdapter(final ComponentAdapter<?> componentAdapter, final Properties properties) {
Properties tmpProperties = (Properties)properties.clone();
+ AbstractBehaviorFactory.removePropertiesIfPresent(tmpProperties, Characteristics.USE_NAMES);
if (AbstractBehaviorFactory.removePropertiesIfPresent(tmpProperties, Characteristics.NONE) == false && componentFactory instanceof BehaviorFactory) {
MutablePicoContainer container = addAdapterInternal(((BehaviorFactory)componentFactory).addComponentAdapter(
componentMonitor,
Modified: java/2.x/trunk/pico/container/src/test/org/picocontainer/behaviors/ThreadCachingTestCase.java (5768 => 5769)
--- java/2.x/trunk/pico/container/src/test/org/picocontainer/behaviors/ThreadCachingTestCase.java 2011-02-28 06:48:59 UTC (rev 5768)
+++ java/2.x/trunk/pico/container/src/test/org/picocontainer/behaviors/ThreadCachingTestCase.java 2011-03-02 16:33:37 UTC (rev 5769)
@@ -15,9 +15,12 @@
import static org.junit.Assert.assertSame;
import org.junit.Test;
+import org.picocontainer.Characteristics;
import org.picocontainer.DefaultPicoContainer;
+import org.picocontainer.classname.DefaultClassLoadingPicoContainer;
import org.picocontainer.containers.EmptyPicoContainer;
import org.picocontainer.lifecycle.NullLifecycleStrategy;
+import org.picocontainer.monitors.NullComponentMonitor;
public class ThreadCachingTestCase {
@@ -90,6 +93,47 @@
assertEquals("ThreadCached:ConstructorInjector-class org.picocontainer.behaviors.ThreadCachingTestCase$Foo", child.getComponentAdapter(Foo.class).toString());
}
+ @Test public void testThatTwoThreadsHaveSeparatedCacheValuesWithInstanceRegistrationAndClassLoadingPicoContainer() {
+
+ final Foo[] foos = new Foo[4];
+
+ DefaultPicoContainer parent = new DefaultPicoContainer(new Caching());
+ parent.change(Characteristics.USE_NAMES);
+ final DefaultClassLoadingPicoContainer child = new DefaultClassLoadingPicoContainer(new ThreadCaching(), new NullLifecycleStrategy(), parent, this.getClass().getClassLoader(), new NullComponentMonitor());
+ child.change(Characteristics.USE_NAMES);
+
+ parent.addComponent(StringBuilder.class);
+ child.addComponent(Foo.class);
+ child.addComponent("hello");
+
+ StringBuilder sb = parent.getComponent(StringBuilder.class);
+ foos[0] = child.getComponent(Foo.class);
+
+ Thread thread = new Thread() {
+ public void run() {
+ foos[1] = child.getComponent(Foo.class);
+ foos[3] = child.getComponent(Foo.class);
+ }
+ };
+ thread.start();
+ foos[2] = child.getComponent(Foo.class);
+ try {
+ Thread.sleep(100);
+ } catch (InterruptedException e) {
+ }
+
+ assertNotNull(foos[0]);
+ assertNotNull(foos[1]);
+ assertNotNull(foos[2]);
+ assertNotNull(foos[3]);
+ assertSame(foos[0],foos[2]);
+ assertEquals(foos[1],foos[3]);
+ assertFalse(foos[0] == foos[1]);
+ assertEquals("<Foo<Foo", sb.toString());
+ assertEquals("ThreadCached:ConstructorInjector-class org.picocontainer.behaviors.ThreadCachingTestCase$Foo", child.getComponentAdapter(Foo.class).toString());
+ }
+
+
@Test public void testThatTwoThreadsHaveSeparatedCacheValuesForThreeScopeScenario() {
final Foo[] foos = new Foo[4];
----------
To unsubscribe from this list please visit:
http://xircles.codehaus.org/manage_email