1 10 11 package org.picocontainer.gems.adapters; 12 13 import junit.framework.TestCase; 14 15 import org.picocontainer.ComponentAdapter; 16 import org.picocontainer.Parameter; 17 import org.picocontainer.defaults.ComponentAdapterFactory; 18 import org.picocontainer.defaults.ConstructorInjectionComponentAdapterFactory; 19 20 import java.util.ArrayList ; 21 import java.util.List ; 22 23 24 29 public class ThreadLocalComponentAdapterFactoryTest extends TestCase { 30 31 36 public final void testCreateComponentAdapterEnsuringThreadLocal() throws InterruptedException { 37 final ComponentAdapterFactory componentAdapterFactory = new ThreadLocalComponentAdapterFactory( 38 new ConstructorInjectionComponentAdapterFactory()); 39 final ComponentAdapter componentAdapter = componentAdapterFactory.createComponentAdapter( 40 List .class, ArrayList .class, new Parameter[]{}); 41 final List list = (List )componentAdapter.getComponentInstance(null); 42 list.add(this); 43 final List list2 = new ArrayList (); 44 final Thread thread = new Thread (new Runnable () { 45 48 public void run() { 49 list2.addAll(list); 50 list2.add(Thread.currentThread()); 51 } 52 }, "junit"); 53 thread.start(); 54 thread.join(); 55 assertEquals(1, list2.size()); 56 assertSame(thread, list2.get(0)); 57 } 58 59 64 public final void testCreateComponentAdapterFailingThreadLocal() throws InterruptedException { 65 final ComponentAdapterFactory componentAdapterFactory = new ThreadLocalComponentAdapterFactory( 66 new ConstructorInjectionComponentAdapterFactory(), ThreadLocalComponentAdapterFactory.THREAD_ENSURES_LOCALITY); 67 final ComponentAdapter componentAdapter = componentAdapterFactory.createComponentAdapter( 68 List .class, ArrayList .class, new Parameter[]{}); 69 final List list = (List )componentAdapter.getComponentInstance(null); 70 list.add(this); 71 final List list2 = new ArrayList (); 72 final Thread thread = new Thread (new Runnable () { 73 76 public void run() { 77 list2.addAll(list); 78 list2.add(Thread.currentThread()); 79 } 80 }, "junit"); 81 thread.start(); 82 thread.join(); 83 assertEquals(2, list2.size()); 84 assertSame(this, list2.get(0)); 85 assertSame(thread, list2.get(1)); 86 } 87 88 93 public final void testCreateComponentAdapterWorksForDifferentThreads() throws InterruptedException { 94 final ComponentAdapterFactory componentAdapterFactory = new ThreadLocalComponentAdapterFactory( 95 new ConstructorInjectionComponentAdapterFactory(), ThreadLocalComponentAdapterFactory.THREAD_ENSURES_LOCALITY); 96 final ComponentAdapter componentAdapter = componentAdapterFactory.createComponentAdapter( 97 List .class, ArrayList .class, new Parameter[]{}); 98 final List list = (List )componentAdapter.getComponentInstance(null); 99 list.add(this); 100 final List list2 = new ArrayList (); 101 final Thread thread = new Thread (new Runnable () { 102 105 public void run() { 106 final List newList = (List )componentAdapter.getComponentInstance(null); 107 list2.addAll(newList); 108 final Thread junitThread = Thread.currentThread(); 109 list2.add(junitThread); 110 if (newList.size() == 0) { 111 synchronized (junitThread) { 112 junitThread.notify(); 113 try { 114 junitThread.wait(); 115 } catch (InterruptedException e) { 116 } 118 } 119 newList.add(list2); 120 run(); 121 } 122 } 123 }, "junit"); 124 synchronized (thread) { 125 thread.start(); 126 thread.wait(); 127 } 128 assertEquals(1, list2.size()); 129 assertSame(thread, list2.get(0)); 130 synchronized (thread) { 131 thread.notify(); 132 } 133 thread.join(); 134 assertEquals(3, list2.size()); 135 assertSame(thread, list2.get(0)); 136 assertSame(list2, list2.get(1)); 137 assertSame(thread, list2.get(2)); 138 } 139 } 140 | Popular Tags |