KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > picocontainer > gems > ThreadLocalReferenceTest


1 /*****************************************************************************
2  * Copyright (c) PicoContainer Organization. All rights reserved. *
3  * ------------------------------------------------------------------------- *
4  * The software in this package is published under the terms of the BSD *
5  * style license a copy of which has been included with this distribution in *
6  * the LICENSE.txt file. *
7  * *
8  * Original code by Joerg Schaible *
9  *****************************************************************************/

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 JavaDoc;
17 import java.util.Collections JavaDoc;
18 import java.util.List JavaDoc;
19
20
21 /**
22  * Unit test for ThreadLocalReference
23  *
24  * @author Jörg Schaible
25  */

26 public class ThreadLocalReferenceTest
27         extends TestCase {
28
29     private List JavaDoc m_exceptionList;
30
31     /**
32      * @see junit.framework.TestCase#setUp()
33      */

34     protected void setUp() throws Exception JavaDoc {
35         super.setUp();
36         m_exceptionList = Collections.synchronizedList(new ArrayList JavaDoc());
37     }
38
39     final class RunIt
40             implements Runnable JavaDoc {
41
42         private ObjectReference m_reference;
43
44         /**
45          * Construct an instance.
46          *
47          * @param reference
48          */

49         public RunIt(ObjectReference reference) {
50             super();
51             m_reference = reference;
52         }
53
54         /**
55          * @see java.lang.Runnable#run()
56          */

57         public void run() {
58             try {
59                 final Thread JavaDoc 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 JavaDoc e) {
66                 m_exceptionList.add(e);
67             }
68         }
69     }
70
71     /**
72      * Test working ThreadLocalReference
73      *
74      * @throws InterruptedException
75      */

76     public final void testThreadLocalReference() throws InterruptedException JavaDoc {
77         final ThreadLocalReference reference = new ThreadLocalReference();
78         final Thread JavaDoc[] threads = new Thread JavaDoc[]{
79             new Thread JavaDoc(new RunIt(reference), "junit-TLR-1"),
80             new Thread JavaDoc(new RunIt(reference), "junit-TLR-2"),
81             new Thread JavaDoc(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