KickJava   Java API By Example, From Geeks To Geeks.

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


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
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 JavaDoc;
20 import java.util.List JavaDoc;
21
22
23 /**
24  * Test ThreadLocalComponentAdapterFactory.
25  * @author Jörg Schaible
26  */

27 public class ThreadLocalComponentAdapterFactoryTest extends TestCase {
28
29     /**
30      * Test creation of a CA ensuring ThreadLocal-behaviour.
31      * @throws InterruptedException
32      */

33     public final void testCreateComponentAdapterEnsuringThreadLocal() throws InterruptedException JavaDoc {
34         final ComponentAdapterFactory componentAdapterFactory = new ThreadLocalComponentAdapterFactory(
35                 new ConstructorInjectionComponentAdapterFactory());
36         final ComponentAdapter componentAdapter = componentAdapterFactory.createComponentAdapter(
37                 List JavaDoc.class, ArrayList JavaDoc.class, new Parameter[]{});
38         final List JavaDoc list = (List JavaDoc)componentAdapter.getComponentInstance(null);
39         list.add(this);
40         final List JavaDoc list2 = new ArrayList JavaDoc();
41         final Thread JavaDoc thread = new Thread JavaDoc(new Runnable JavaDoc() {
42             /**
43              * @see java.lang.Runnable#run()
44              */

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     /**
57      * Test creation of a CA failing ThreadLocal-behaviour.
58      * @throws InterruptedException
59      */

60     public final void testCreateComponentAdapterFailingThreadLocal() throws InterruptedException JavaDoc {
61         final ComponentAdapterFactory componentAdapterFactory = new ThreadLocalComponentAdapterFactory(
62                 new ConstructorInjectionComponentAdapterFactory(), ThreadLocalComponentAdapterFactory.THREAD_ENSURES_LOCALITY);
63         final ComponentAdapter componentAdapter = componentAdapterFactory.createComponentAdapter(
64                 List JavaDoc.class, ArrayList JavaDoc.class, new Parameter[]{});
65         final List JavaDoc list = (List JavaDoc)componentAdapter.getComponentInstance(null);
66         list.add(this);
67         final List JavaDoc list2 = new ArrayList JavaDoc();
68         final Thread JavaDoc thread = new Thread JavaDoc(new Runnable JavaDoc() {
69             /**
70              * @see java.lang.Runnable#run()
71              */

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     /**
85      * Test creation of a CA with ThreadLocal-behaviour works if the thread ensures creation.
86      * @throws InterruptedException
87      */

88     public final void testCreateComponentAdapterWorksForDifferentThreads() throws InterruptedException JavaDoc {
89         final ComponentAdapterFactory componentAdapterFactory = new ThreadLocalComponentAdapterFactory(
90                 new ConstructorInjectionComponentAdapterFactory(), ThreadLocalComponentAdapterFactory.THREAD_ENSURES_LOCALITY);
91         final ComponentAdapter componentAdapter = componentAdapterFactory.createComponentAdapter(
92                 List JavaDoc.class, ArrayList JavaDoc.class, new Parameter[]{});
93         final List JavaDoc list = (List JavaDoc)componentAdapter.getComponentInstance(null);
94         list.add(this);
95         final List JavaDoc list2 = new ArrayList JavaDoc();
96         final Thread JavaDoc thread = new Thread JavaDoc(new Runnable JavaDoc() {
97             /**
98              * @see java.lang.Runnable#run()
99              */

100             public void run() {
101                 final List JavaDoc newList = (List JavaDoc)componentAdapter.getComponentInstance(null);
102                 list2.addAll(newList);
103                 final Thread JavaDoc 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 JavaDoc e) {
111                             // Ignore
112
}
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