001: package org.objectweb.celtix;
002:
003: import java.util.HashMap;
004: import java.util.Map;
005: import javax.xml.ws.Holder;
006: import junit.framework.*;
007: import org.objectweb.celtix.bindings.BindingFactory;
008: import org.objectweb.celtix.bindings.BindingManager;
009: import org.objectweb.celtix.bus.busimpl.CeltixBus;
010:
011: public class BusTest extends TestCase {
012:
013: public void tearDown() {
014: Bus.clearCurrent();
015: Bus.clearDefault();
016: }
017:
018: public void testBusInit() throws Exception {
019:
020: Bus bus = Bus.init(null, new HashMap<String, Object>());
021: assertNotNull(bus);
022: assertTrue("Bus not a Celtix bus", bus instanceof CeltixBus);
023:
024: Map<String, Object> map = new HashMap<String, Object>();
025: map.put(Bus.BUS_CLASS_PROPERTY, "com.foo.bar.Bus");
026: try {
027: bus = Bus.init(null, map);
028: fail("Should have thrown an exception");
029: } catch (BusException ex) {
030: //ignore -expected
031: } finally {
032: Thread.sleep(100);
033: bus.shutdown(true);
034: }
035: }
036:
037: /*
038: * Test method for 'org.objectweb.celtix.Bus.getCurrent()'
039: */
040: public void testBusGetCurrent() throws Exception {
041:
042: Bus bus1 = Bus.init(null, new HashMap<String, Object>());
043: assertNotNull(bus1);
044:
045: assertSame(
046: "getCurrent should have returned the same bus handle.",
047: bus1, Bus.getCurrent());
048:
049: //Create another bus
050: Bus bus2 = Bus.init(null, new HashMap<String, Object>());
051: assertNotSame(
052: "getCurrent should have returned a different bus handle.",
053: bus1, Bus.getCurrent());
054: assertSame("last bus initilialised should be the current bus ",
055: bus2, Bus.getCurrent());
056:
057: bus1.shutdown(true);
058: bus2.shutdown(true);
059: }
060:
061: public void testBusGetCurrentDefaultMulitpleThreads()
062: throws Exception {
063:
064: final Bus bus1 = Bus.getCurrent();
065:
066: Thread t = new Thread() {
067: public void run() {
068: Bus bus2 = Bus.getCurrent();
069: assertSame("default bus not visible on all threads",
070: bus1, bus2);
071: try {
072: try {
073: Thread.sleep(100);
074: } catch (InterruptedException e) {
075: // do nothing
076: }
077: bus2.shutdown(true);
078: } catch (BusException e) {
079: // TODO Auto-generated catch block
080: e.printStackTrace();
081: }
082: }
083: };
084:
085: t.start();
086: t.join();
087: // bus1 and bus2 are the same bus
088: }
089:
090: public void testBusGetCurrentPreInitMulitpleThreads()
091: throws Exception {
092:
093: final Bus bus1 = Bus.init(null, new HashMap<String, Object>());
094: assertNotNull(bus1);
095: assertTrue("Bus not a Celtix bus", bus1 instanceof CeltixBus);
096:
097: //Last Created bus should always be returned.
098: assertSame(
099: "getCurrent should have returned the same bus handle.",
100: bus1, Bus.getCurrent());
101:
102: final Holder<Bus> busHolder = new Holder<Bus>();
103: Thread t = new Thread() {
104: public void run() {
105: busHolder.value = Bus.getCurrent();
106: }
107: };
108:
109: t.start();
110: t.join();
111:
112: assertSame("initialised bus not visible on all threads", bus1,
113: busHolder.value);
114: Thread.sleep(100);
115: bus1.shutdown(true);
116: }
117:
118: public void testBusGetCurrentDefault() throws Exception {
119:
120: Bus bus1 = Bus.getCurrent();
121: assertNotNull("getCurrent did not return default bus", bus1);
122: Bus bus2 = Bus.getCurrent();
123: assertNotNull("getCurrent did not return default bus", bus2);
124: assertSame("calls to get default bus returned different buses",
125: bus1, bus2);
126:
127: Thread.sleep(100);
128: bus1.shutdown(true);
129:
130: }
131:
132: /*
133: * Test method for 'org.objectweb.celtix.Bus.getCurrent()'
134: */
135: public void testBusGetBindingManager() throws Exception {
136:
137: Bus bus = Bus.init(null, new HashMap<String, Object>());
138: assertNotNull(bus);
139:
140: BindingManager bindingManager = bus.getBindingManager();
141: assertNotNull(bindingManager);
142:
143: BindingFactory factory = bindingManager
144: .getBindingFactory("http://schemas.xmlsoap.org/wsdl/soap/");
145: assertNotNull(factory);
146: //Last Created bus should always be returned.
147: Thread.sleep(100);
148: bus.shutdown(true);
149: }
150:
151: public void testBusRun() throws Exception {
152:
153: final Bus bus = Bus.init();
154: Thread th = new Thread() {
155: public void run() {
156: try {
157: Thread.sleep(100);
158: bus.shutdown(true);
159: } catch (InterruptedException e) {
160: // TODO Auto-generated catch block
161: e.printStackTrace();
162: } catch (BusException e) {
163: // TODO Auto-generated catch block
164: e.printStackTrace();
165: }
166: }
167: };
168: th.start();
169: bus.run();
170: }
171:
172: public void testBusInitCommand() throws Exception {
173: // just for test the celtix Bus init(String[] args)
174: String[] args = { "Bus", "test" };
175: final Bus bus = Bus.init(args);
176: assertNotNull(bus);
177: assertTrue("Bus not a Celtix bus", bus instanceof CeltixBus);
178: Thread.sleep(1000);
179: bus.shutdown(true);
180: }
181: }
|