1 10 package org.picocontainer.gems; 11 12 import junit.framework.Assert; 13 import junit.framework.TestCase; 14 import org.picocontainer.defaults.ObjectReference; 15 16 import java.util.ArrayList ; 17 import java.util.Collections ; 18 import java.util.List ; 19 20 21 26 public class ThreadLocalReferenceTest 27 extends TestCase { 28 29 private List m_exceptionList; 30 31 34 protected void setUp() throws Exception { 35 super.setUp(); 36 m_exceptionList = Collections.synchronizedList(new ArrayList ()); 37 } 38 39 final class RunIt 40 implements Runnable { 41 42 private ObjectReference m_reference; 43 44 49 public RunIt(ObjectReference reference) { 50 super(); 51 m_reference = reference; 52 } 53 54 57 public void run() { 58 try { 59 final Thread thread = Thread.currentThread(); 60 m_reference.set(thread.getName()); 61 synchronized (thread) { 62 thread.wait(); 63 } 64 Assert.assertEquals(thread.getName(), m_reference.get()); 65 } catch (InterruptedException e) { 66 m_exceptionList.add(e); 67 } 68 } 69 } 70 71 76 public final void testThreadLocalReference() throws InterruptedException { 77 final ThreadLocalReference reference = new ThreadLocalReference(); 78 final Thread [] threads = new Thread []{ 79 new Thread (new RunIt(reference), "junit-TLR-1"), 80 new Thread (new RunIt(reference), "junit-TLR-2"), 81 new Thread (new RunIt(reference), "junit-TLR-3")}; 82 reference.set("Hello"); 83 for (int i = 0; i < threads.length; i++) { 84 threads[i].start(); 85 } 86 Thread.sleep(50); 87 assertEquals("Hello", reference.get()); 88 for (int i = 0; i < threads.length; i++) { 89 synchronized (threads[i]) { 90 threads[i].notify(); 91 } 92 } 93 Thread.sleep(50); 94 assertEquals("Unexpected Exceptions: " + m_exceptionList, 0, m_exceptionList.size()); 95 } 96 } 97
| Popular Tags
|