[picocontainer-scm] [5783] java/2.x/trunk/pico/container/src/test/org/picocontainer/behaviors/ThreadCachingTestCase.java: futzing around with tests for threadcaching and setter injection
paul-yCVjj/[email protected] Sat, 30 Apr 2011 12:56:34 -0500 (CDT)
| Newsgroups | gmane.comp.java.picocontainer.cvs |
|---|---|
| Message-ID | <[email protected]> |
Revision 5783
Author paul
Date 2011-04-30 12:56:34 -0500 (Sat, 30 Apr 2011)
Log Message
futzing around with tests for threadcaching and setter injection
Modified Paths
- java/2.x/trunk/pico/container/src/test/org/picocontainer/behaviors/ThreadCachingTestCase.java
Diff
Modified: java/2.x/trunk/pico/container/src/test/org/picocontainer/behaviors/ThreadCachingTestCase.java (5782 => 5783)
--- java/2.x/trunk/pico/container/src/test/org/picocontainer/behaviors/ThreadCachingTestCase.java 2011-04-21 14:31:43 UTC (rev 5782)
+++ java/2.x/trunk/pico/container/src/test/org/picocontainer/behaviors/ThreadCachingTestCase.java 2011-04-30 17:56:34 UTC (rev 5783)
@@ -19,6 +19,7 @@
import org.picocontainer.DefaultPicoContainer;
import org.picocontainer.classname.DefaultClassLoadingPicoContainer;
import org.picocontainer.containers.EmptyPicoContainer;
+import org.picocontainer.injectors.SetterInjection;
import org.picocontainer.lifecycle.NullLifecycleStrategy;
import org.picocontainer.monitors.NullComponentMonitor;
@@ -30,6 +31,12 @@
}
}
+ public static class Baz {
+ public void setStringBuilder(StringBuilder sb) {
+ sb.append("<Baz");
+ }
+ }
+
public static class Bar {
private final Foo foo;
public Bar(StringBuilder sb, Foo foo) {
@@ -56,6 +63,24 @@
assertEquals("ThreadCached:ConstructorInjector-class org.picocontainer.behaviors.ThreadCachingTestCase$Foo", child.getComponentAdapter(Foo.class).toString());
}
+ @Test public void testThatForASingleThreadTheBehaviorIsTheSameAsPlainCachingWithSetterInjection() {
+
+ DefaultPicoContainer parent = new DefaultPicoContainer(new Caching());
+ DefaultPicoContainer child = new DefaultPicoContainer(new ThreadCaching().wrap(new SetterInjection()), new NullLifecycleStrategy(), parent);
+
+ parent.addComponent(StringBuilder.class);
+ child.addComponent(Baz.class);
+
+ StringBuilder sb = parent.getComponent(StringBuilder.class);
+ Baz baz = child.getComponent(Baz.class);
+ Baz baz2 = child.getComponent(Baz.class);
+ assertNotNull(baz);
+ assertNotNull(baz2);
+ assertEquals(baz,baz2);
+ assertEquals("<Baz", sb.toString());
+ assertEquals("ThreadCached:SetterInjector-class org.picocontainer.behaviors.ThreadCachingTestCase$Baz", child.getComponentAdapter(Baz.class).toString());
+ }
+
@Test public void testThatTwoThreadsHaveSeparatedCacheValues() {
final Foo[] foos = new Foo[4];
@@ -93,16 +118,53 @@
assertEquals("ThreadCached:ConstructorInjector-class org.picocontainer.behaviors.ThreadCachingTestCase$Foo", child.getComponentAdapter(Foo.class).toString());
}
+ @Test public void testThatTwoThreadsHaveSeparatedCacheValuesWithSetterInjection() {
+
+ final Baz[] bazs = new Baz[4];
+
+ DefaultPicoContainer parent = new DefaultPicoContainer(new Caching());
+ final DefaultPicoContainer child = new DefaultPicoContainer(new ThreadCaching().wrap(new SetterInjection()), new NullLifecycleStrategy(), parent);
+
+ parent.addComponent(StringBuilder.class);
+ child.addComponent(Baz.class);
+
+ StringBuilder sb = parent.getComponent(StringBuilder.class);
+ bazs[0] = child.getComponent(Baz.class);
+
+ Thread thread = new Thread() {
+ public void run() {
+ bazs[1] = child.getComponent(Baz.class);
+ bazs[3] = child.getComponent(Baz.class);
+ }
+ };
+ thread.start();
+ bazs[2] = child.getComponent(Baz.class);
+ try {
+ Thread.sleep(100);
+ } catch (InterruptedException e) {
+ }
+
+ assertNotNull(bazs[0]);
+ assertNotNull(bazs[1]);
+ assertNotNull(bazs[2]);
+ assertNotNull(bazs[3]);
+ assertSame(bazs[0],bazs[2]);
+ assertEquals(bazs[1],bazs[3]);
+ assertFalse(bazs[0] == bazs[1]);
+ assertEquals("<Baz<Baz", sb.toString());
+ assertEquals("ThreadCached:SetterInjector-class org.picocontainer.behaviors.ThreadCachingTestCase$Baz", child.getComponentAdapter(Baz.class).toString());
+ }
+
@Test public void testThatTwoThreadsHaveSeparatedCacheValuesWithInstanceRegistrationAndClassLoadingPicoContainer() {
final Foo[] foos = new Foo[4];
DefaultPicoContainer parent = new DefaultPicoContainer(new Caching());
parent.change(Characteristics.USE_NAMES);
+ parent.addComponent(StringBuilder.class);
+
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");
----------
To unsubscribe from this list please visit:
http://xircles.codehaus.org/manage_email