001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.bus;
019:
020: import java.util.HashMap;
021: import java.util.Map;
022:
023: import org.apache.cxf.Bus;
024: import org.apache.cxf.BusException;
025: import org.apache.cxf.binding.BindingFactoryManager;
026: import org.apache.cxf.bus.extension.ExtensionManagerBus;
027: import org.apache.cxf.buslifecycle.BusLifeCycleListener;
028: import org.apache.cxf.buslifecycle.BusLifeCycleManager;
029: import org.apache.cxf.event.EventProcessor;
030: import org.apache.cxf.management.InstrumentationManager;
031: import org.apache.cxf.phase.PhaseManager;
032: import org.apache.cxf.transport.ConduitInitiatorManager;
033: import org.apache.cxf.transport.DestinationFactoryManager;
034: import org.apache.cxf.wsdl.WSDLManager;
035: import org.easymock.classextension.EasyMock;
036: import org.easymock.classextension.IMocksControl;
037: import org.junit.Assert;
038: import org.junit.Test;
039:
040: public class CXFBusImplTest extends Assert {
041:
042: @Test
043: public void testConstructionWithoutExtensions() throws BusException {
044:
045: CXFBusImpl bus = new ExtensionManagerBus();
046: assertNotNull(bus.getExtension(BindingFactoryManager.class));
047: assertNotNull(bus.getExtension(ConduitInitiatorManager.class));
048: assertNotNull(bus.getExtension(DestinationFactoryManager.class));
049: assertNotNull(bus.getExtension(WSDLManager.class));
050: assertNotNull(bus.getExtension(PhaseManager.class));
051: }
052:
053: @Test
054: public void testConstructionWithExtensions() throws BusException {
055:
056: IMocksControl control;
057: BindingFactoryManager bindingFactoryManager;
058: WSDLManager wsdlManager;
059: EventProcessor eventProcessor;
060: InstrumentationManager instrumentationManager;
061: PhaseManager phaseManager;
062:
063: control = EasyMock.createNiceControl();
064:
065: Map<Class, Object> extensions = new HashMap<Class, Object>();
066: bindingFactoryManager = control
067: .createMock(BindingFactoryManager.class);
068: wsdlManager = control.createMock(WSDLManager.class);
069: eventProcessor = control.createMock(EventProcessor.class);
070: instrumentationManager = control
071: .createMock(InstrumentationManager.class);
072: phaseManager = control.createMock(PhaseManager.class);
073:
074: extensions.put(BindingFactoryManager.class,
075: bindingFactoryManager);
076: extensions.put(WSDLManager.class, wsdlManager);
077: extensions.put(EventProcessor.class, eventProcessor);
078: extensions.put(InstrumentationManager.class,
079: instrumentationManager);
080: extensions.put(PhaseManager.class, phaseManager);
081:
082: CXFBusImpl bus = new CXFBusImpl(extensions);
083:
084: assertSame(bindingFactoryManager, bus
085: .getExtension(BindingFactoryManager.class));
086: assertSame(wsdlManager, bus.getExtension(WSDLManager.class));
087: assertSame(eventProcessor, bus
088: .getExtension(EventProcessor.class));
089: assertSame(instrumentationManager, bus
090: .getExtension(InstrumentationManager.class));
091: assertSame(phaseManager, bus.getExtension(PhaseManager.class));
092:
093: }
094:
095: @Test
096: public void testExtensions() {
097: CXFBusImpl bus = new CXFBusImpl();
098: String extension = "CXF";
099: bus.setExtension(extension, String.class);
100: assertSame(extension, bus.getExtension(String.class));
101: }
102:
103: @Test
104: public void testBusID() {
105: CXFBusImpl bus = new CXFBusImpl();
106: String id = bus.getId();
107: assertEquals("The bus id should be cxf", id, Bus.DEFAULT_BUS_ID
108: + bus.hashCode());
109: bus.setId("test");
110: assertEquals("The bus id should be changed", bus.getId(),
111: "test");
112: }
113:
114: @Test
115: public void testRun() {
116: final CXFBusImpl bus = new CXFBusImpl();
117: Thread t = new Thread() {
118: public void run() {
119: bus.run();
120: }
121: };
122: t.start();
123: try {
124: Thread.sleep(100);
125: } catch (InterruptedException ex) {
126: // ignore;
127: }
128: try {
129: t.join(400);
130: } catch (InterruptedException ex) {
131: // ignore
132: }
133: assertEquals(BusState.RUNNING, bus.getState());
134: }
135:
136: @Test
137: public void testShutdown() {
138: final CXFBusImpl bus = new CXFBusImpl();
139: Thread t = new Thread() {
140: public void run() {
141: bus.run();
142: }
143: };
144: t.start();
145: try {
146: Thread.sleep(100);
147: } catch (InterruptedException ex) {
148: // ignore;
149: }
150: bus.shutdown(true);
151: try {
152: t.join();
153: } catch (InterruptedException ex) {
154: // ignore
155: }
156: assertEquals(BusState.SHUTDOWN, bus.getState());
157:
158: }
159:
160: @Test
161: public void testShutdownWithBusLifecycle() {
162: final CXFBusImpl bus = new ExtensionManagerBus();
163: BusLifeCycleManager lifeCycleManager = bus
164: .getExtension(BusLifeCycleManager.class);
165: BusLifeCycleListener listener = EasyMock
166: .createMock(BusLifeCycleListener.class);
167: EasyMock.reset(listener);
168: listener.preShutdown();
169: EasyMock.expectLastCall();
170: listener.postShutdown();
171: EasyMock.expectLastCall();
172: EasyMock.replay(listener);
173: lifeCycleManager.registerLifeCycleListener(listener);
174: bus.shutdown(true);
175: EasyMock.verify(listener);
176:
177: }
178:
179: }
|