KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > picocontainer > defaults > issues > Issue0199TestCase


1 package org.picocontainer.defaults.issues;
2
3 import junit.framework.TestCase;
4
5 import java.util.List JavaDoc;
6 import java.util.ArrayList JavaDoc;
7
8 import org.picocontainer.PicoContainer;
9 import org.picocontainer.defaults.ConstructorInjectionComponentAdapterFactory;
10 import org.picocontainer.defaults.DefaultPicoContainer;
11 import org.picocontainer.defaults.SynchronizedComponentAdapterFactory;
12
13 public class Issue0199TestCase extends TestCase {
14
15     public static class A {
16         public A(C c) {}
17     }
18
19     public static class B {
20         public B(C c) {}
21     }
22
23     public static class C {}
24
25     class Runner extends Thread JavaDoc {
26         private PicoContainer container;
27         private Object JavaDoc componentKey;
28         private Throwable JavaDoc throwable;
29         private boolean finished;
30
31         Runner(String JavaDoc name, PicoContainer container, Object JavaDoc componentKey) {
32             super(name);
33             this.container = container;
34             this.componentKey = componentKey;
35         }
36
37         public void run() {
38             try {
39                 report("Started instantiating " + componentKey.toString());
40                 container.getComponentInstance(componentKey);
41                 report("Finished instantiating " + componentKey.toString());
42                 finished = true;
43             } catch (Throwable JavaDoc t) {
44                 this.throwable = t;
45             }
46         }
47
48         private void report(String JavaDoc messsage) {
49             System.out.println(getName() + ": " + messsage);
50         }
51
52         public boolean isFinished() {
53             return finished;
54         }
55
56         public Throwable JavaDoc getThrowable() {
57             return throwable;
58         }
59     }
60
61     public void testPicoContainerCausesDeadlock() throws InterruptedException JavaDoc {
62         DefaultPicoContainer container = createContainer();
63         container.registerComponentImplementation("A", A.class);
64         container.registerComponentImplementation("B", B.class);
65         container.registerComponentImplementation("C", C.class);
66
67         final int THREAD_COUNT = 2;
68         List JavaDoc runnerList = new ArrayList JavaDoc(THREAD_COUNT);
69
70         for (int i = 0; i < THREAD_COUNT; ++i) {
71             Runner runner = new Runner("Runner " + i, container, (i % 2 == 0) ? "A" : "B");
72             runnerList.add(runner);
73             runner.start();
74         }
75
76         final long WAIT_TIME = 1000;
77
78         for (int i = 0; i < THREAD_COUNT; ++i) {
79             Runner runner = (Runner) runnerList.get(i);
80             runner.join(WAIT_TIME);
81             assertTrue("Deadlock occurred", runner.isFinished());
82         }
83     }
84
85     private DefaultPicoContainer createContainer() {
86         return new DefaultPicoContainer(
87                 new SynchronizedComponentAdapterFactory(
88                         new ConstructorInjectionComponentAdapterFactory()));
89     }
90 }
91
Popular Tags