KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > celtix > BusTest


1 package org.objectweb.celtix;
2
3
4
5 import java.util.HashMap JavaDoc;
6 import java.util.Map JavaDoc;
7 import javax.xml.ws.Holder;
8 import junit.framework.*;
9 import org.objectweb.celtix.bindings.BindingFactory;
10 import org.objectweb.celtix.bindings.BindingManager;
11 import org.objectweb.celtix.bus.busimpl.CeltixBus;
12
13 public class BusTest extends TestCase {
14
15     public void tearDown() {
16         Bus.clearCurrent();
17         Bus.clearDefault();
18     }
19
20     public void testBusInit() throws Exception JavaDoc {
21         
22         Bus bus = Bus.init(null, new HashMap JavaDoc<String JavaDoc, Object JavaDoc>());
23         assertNotNull(bus);
24         assertTrue("Bus not a Celtix bus", bus instanceof CeltixBus);
25
26         Map JavaDoc<String JavaDoc, Object JavaDoc> map = new HashMap JavaDoc<String JavaDoc, Object JavaDoc>();
27         map.put(Bus.BUS_CLASS_PROPERTY, "com.foo.bar.Bus");
28         try {
29             bus = Bus.init(null, map);
30             fail("Should have thrown an exception");
31         } catch (BusException ex) {
32             //ignore -expected
33
} finally {
34             Thread.sleep(100);
35             bus.shutdown(true);
36         }
37     }
38     
39     /*
40      * Test method for 'org.objectweb.celtix.Bus.getCurrent()'
41      */

42     public void testBusGetCurrent() throws Exception JavaDoc {
43         
44         Bus bus1 = Bus.init(null, new HashMap JavaDoc<String JavaDoc, Object JavaDoc>());
45         assertNotNull(bus1);
46
47         assertSame("getCurrent should have returned the same bus handle.", bus1, Bus.getCurrent());
48
49         //Create another bus
50
Bus bus2 = Bus.init(null, new HashMap JavaDoc<String JavaDoc, Object JavaDoc>());
51         assertNotSame("getCurrent should have returned a different bus handle.", bus1, Bus.getCurrent());
52         assertSame("last bus initilialised should be the current bus ", bus2, Bus.getCurrent());
53         
54         bus1.shutdown(true);
55         bus2.shutdown(true);
56     }
57     
58     public void testBusGetCurrentDefaultMulitpleThreads() throws Exception JavaDoc {
59         
60         final Bus bus1 = Bus.getCurrent();
61
62         Thread JavaDoc t = new Thread JavaDoc() {
63                 public void run() {
64                     Bus bus2 = Bus.getCurrent();
65                     assertSame("default bus not visible on all threads", bus1, bus2);
66                     try {
67                         try {
68                             Thread.sleep(100);
69                         } catch (InterruptedException JavaDoc e) {
70                             // do nothing
71
}
72                         bus2.shutdown(true);
73                     } catch (BusException e) {
74                         // TODO Auto-generated catch block
75
e.printStackTrace();
76                     }
77                 }
78             };
79
80         t.start();
81         t.join();
82         // bus1 and bus2 are the same bus
83
}
84
85     public void testBusGetCurrentPreInitMulitpleThreads() throws Exception JavaDoc {
86         
87         final Bus bus1 = Bus.init(null, new HashMap JavaDoc<String JavaDoc, Object JavaDoc>());
88         assertNotNull(bus1);
89         assertTrue("Bus not a Celtix bus", bus1 instanceof CeltixBus);
90
91         //Last Created bus should always be returned.
92
assertSame("getCurrent should have returned the same bus handle.", bus1, Bus.getCurrent());
93
94         final Holder<Bus> busHolder = new Holder<Bus>();
95         Thread JavaDoc t = new Thread JavaDoc() {
96                 public void run() {
97                     busHolder.value = Bus.getCurrent();
98                 }
99             };
100
101         t.start();
102         t.join();
103         
104         assertSame("initialised bus not visible on all threads", bus1, busHolder.value);
105         Thread.sleep(100);
106         bus1.shutdown(true);
107     }
108
109
110     public void testBusGetCurrentDefault() throws Exception JavaDoc {
111         
112         Bus bus1 = Bus.getCurrent();
113         assertNotNull("getCurrent did not return default bus", bus1);
114         Bus bus2 = Bus.getCurrent();
115         assertNotNull("getCurrent did not return default bus", bus2);
116         assertSame("calls to get default bus returned different buses", bus1, bus2);
117         
118         Thread.sleep(100);
119         bus1.shutdown(true);
120         
121     }
122
123     /*
124      * Test method for 'org.objectweb.celtix.Bus.getCurrent()'
125      */

126     public void testBusGetBindingManager() throws Exception JavaDoc {
127         
128         Bus bus = Bus.init(null, new HashMap JavaDoc<String JavaDoc, Object JavaDoc>());
129         assertNotNull(bus);
130
131         BindingManager bindingManager = bus.getBindingManager();
132         assertNotNull(bindingManager);
133         
134         BindingFactory factory = bindingManager.getBindingFactory("http://schemas.xmlsoap.org/wsdl/soap/");
135         assertNotNull(factory);
136         //Last Created bus should always be returned.
137
Thread.sleep(100);
138         bus.shutdown(true);
139     }
140     
141     
142     
143     public void testBusRun() throws Exception JavaDoc {
144         
145         final Bus bus = Bus.init();
146         Thread JavaDoc th = new Thread JavaDoc() {
147             public void run() {
148                 try {
149                     Thread.sleep(100);
150                     bus.shutdown(true);
151                 } catch (InterruptedException JavaDoc e) {
152                     // TODO Auto-generated catch block
153
e.printStackTrace();
154                 } catch (BusException e) {
155                     // TODO Auto-generated catch block
156
e.printStackTrace();
157                 }
158             }
159         };
160         th.start();
161         bus.run();
162     }
163         
164     public void testBusInitCommand() throws Exception JavaDoc {
165        // just for test the celtix Bus init(String[] args)
166
String JavaDoc [] args = {"Bus" , "test"};
167         final Bus bus = Bus.init(args);
168         assertNotNull(bus);
169         assertTrue("Bus not a Celtix bus", bus instanceof CeltixBus);
170         Thread.sleep(1000);
171         bus.shutdown(true);
172     }
173 }
174
175
176
Popular Tags