1 10 11 package org.picocontainer.gems; 12 13 import junit.framework.TestCase; 14 import org.picocontainer.ComponentAdapter; 15 import org.picocontainer.Parameter; 16 import org.picocontainer.defaults.ComponentAdapterFactory; 17 import org.picocontainer.defaults.ConstructorInjectionComponentAdapterFactory; 18 19 import java.util.ArrayList ; 20 import java.util.List ; 21 22 23 27 public class ThreadLocalComponentAdapterFactoryTest extends TestCase { 28 29 33 public final void testCreateComponentAdapterEnsuringThreadLocal() throws InterruptedException { 34 final ComponentAdapterFactory componentAdapterFactory = new ThreadLocalComponentAdapterFactory( 35 new ConstructorInjectionComponentAdapterFactory()); 36 final ComponentAdapter componentAdapter = componentAdapterFactory.createComponentAdapter( 37 List .class, ArrayList .class, new Parameter[]{}); 38 final List list = (List )componentAdapter.getComponentInstance(null); 39 list.add(this); 40 final List list2 = new ArrayList (); 41 final Thread thread = new Thread (new Runnable () { 42 45 public void run() { 46 list2.addAll(list); 47 list2.add(Thread.currentThread()); 48 } 49 }, "junit"); 50 thread.start(); 51 thread.join(); 52 assertEquals(1, list2.size()); 53 assertSame(thread, list2.get(0)); 54 } 55 56 60 public final void testCreateComponentAdapterFailingThreadLocal() throws InterruptedException { 61 final ComponentAdapterFactory componentAdapterFactory = new ThreadLocalComponentAdapterFactory( 62 new ConstructorInjectionComponentAdapterFactory(), ThreadLocalComponentAdapterFactory.THREAD_ENSURES_LOCALITY); 63 final ComponentAdapter componentAdapter = componentAdapterFactory.createComponentAdapter( 64 List .class, ArrayList .class, new Parameter[]{}); 65 final List list = (List )componentAdapter.getComponentInstance(null); 66 list.add(this); 67 final List list2 = new ArrayList (); 68 final Thread thread = new Thread (new Runnable () { 69 72 public void run() { 73 list2.addAll(list); 74 list2.add(Thread.currentThread()); 75 } 76 }, "junit"); 77 thread.start(); 78 thread.join(); 79 assertEquals(2, list2.size()); 80 assertSame(this, list2.get(0)); 81 assertSame(thread, list2.get(1)); 82 } 83 84 88 public final void testCreateComponentAdapterWorksForDifferentThreads() throws InterruptedException { 89 final ComponentAdapterFactory componentAdapterFactory = new ThreadLocalComponentAdapterFactory( 90 new ConstructorInjectionComponentAdapterFactory(), ThreadLocalComponentAdapterFactory.THREAD_ENSURES_LOCALITY); 91 final ComponentAdapter componentAdapter = componentAdapterFactory.createComponentAdapter( 92 List .class, ArrayList .class, new Parameter[]{}); 93 final List list = (List )componentAdapter.getComponentInstance(null); 94 list.add(this); 95 final List list2 = new ArrayList (); 96 final Thread thread = new Thread (new Runnable () { 97 100 public void run() { 101 final List newList = (List )componentAdapter.getComponentInstance(null); 102 list2.addAll(newList); 103 final Thread junitThread = Thread.currentThread(); 104 list2.add(junitThread); 105 if (newList.size() == 0) { 106 synchronized (junitThread) { 107 junitThread.notify(); 108 try { 109 junitThread.wait(); 110 } catch (InterruptedException e) { 111 } 113 } 114 newList.add(list2); 115 run(); 116 } 117 } 118 }, "junit"); 119 synchronized (thread) { 120 thread.start(); 121 thread.wait(); 122 } 123 assertEquals(1, list2.size()); 124 assertSame(thread, list2.get(0)); 125 synchronized (thread) { 126 thread.notify(); 127 } 128 thread.join(); 129 assertEquals(3, list2.size()); 130 assertSame(thread, list2.get(0)); 131 assertSame(list2, list2.get(1)); 132 assertSame(thread, list2.get(2)); 133 } 134 } 135
| Popular Tags
|