KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > picocontainer > gems > adapters > 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.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 JavaDoc;
21 import java.util.List JavaDoc;
22
23
24 /**
25  * Test ThreadLocalComponentAdapterFactory.
26  *
27  * @author Jörg Schaible
28  */

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

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

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     /**
60      * Test creation of a CA failing ThreadLocal-behaviour.
61      *
62      * @throws InterruptedException
63      */

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

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     /**
89      * Test creation of a CA with ThreadLocal-behaviour works if the thread ensures creation.
90      *
91      * @throws InterruptedException
92      */

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

105             public void run() {
106                 final List JavaDoc newList = (List JavaDoc)componentAdapter.getComponentInstance(null);
107                 list2.addAll(newList);
108                 final Thread JavaDoc 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 JavaDoc e) {
116                             // Ignore
117
}
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