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.systest.lifecycle;
019:
020: import java.util.HashMap;
021: import java.util.List;
022: import java.util.Map;
023:
024: import javax.xml.ws.Endpoint;
025:
026: import org.apache.cxf.Bus;
027: import org.apache.cxf.BusFactory;
028: import org.apache.cxf.bus.spring.SpringBusFactory;
029: import org.apache.cxf.endpoint.Server;
030: import org.apache.cxf.endpoint.ServerLifeCycleListener;
031: import org.apache.cxf.endpoint.ServerLifeCycleManager;
032: import org.apache.cxf.feature.AbstractFeature;
033: import org.apache.cxf.greeter_control.ControlImpl;
034: import org.apache.cxf.systest.ws.addressing.GreeterImpl;
035: import org.apache.cxf.ws.addressing.WSAddressingFeature;
036:
037: import org.junit.After;
038: import org.junit.Assert;
039: import org.junit.Before;
040: import org.junit.Test;
041:
042: public class LifeCycleTest extends Assert {
043: private static final int RECURSIVE_LIMIT = 3;
044: private static final String[] ADDRESSES = {
045: "http://localhost:9056/SoapContext/SoapPort",
046: "http://localhost:9057/SoapContext/SoapPort",
047: "http://localhost:9058/SoapContext/SoapPort",
048: "http://localhost:9059/SoapContext/SoapPort" };
049: private static final String CONFIG = "org/apache/cxf/systest/lifecycle/cxf.xml";
050:
051: private Bus bus;
052: private ServerLifeCycleManager manager;
053: private int recursiveCount;
054: private Endpoint[] recursiveEndpoints;
055: private Map<String, Integer> startNotificationMap;
056: private Map<String, Integer> stopNotificationMap;
057:
058: @Before
059: public void setUp() throws Exception {
060: SpringBusFactory bf = new SpringBusFactory();
061: bus = bf.createBus(CONFIG);
062: BusFactory.setDefaultBus(bus);
063: manager = bus.getExtension(ServerLifeCycleManager.class);
064: recursiveCount = 0;
065: recursiveEndpoints = new Endpoint[RECURSIVE_LIMIT];
066: startNotificationMap = new HashMap<String, Integer>();
067: stopNotificationMap = new HashMap<String, Integer>();
068: }
069:
070: @After
071: public void tearDown() throws Exception {
072: bus.shutdown(true);
073: }
074:
075: @Test
076: public void testRecursive() {
077: assertNotNull("unexpected non-null ServerLifeCycleManager",
078: manager);
079:
080: manager.registerListener(new ServerLifeCycleListener() {
081: public void startServer(Server server) {
082: String address = server.getEndpoint().getEndpointInfo()
083: .getAddress();
084: verifyNotification(startNotificationMap, address, 0);
085: updateMap(startNotificationMap, address);
086: if (recursiveCount < RECURSIVE_LIMIT) {
087: recursiveEndpoints[recursiveCount++] = Endpoint
088: .publish(ADDRESSES[recursiveCount],
089: new GreeterImpl());
090: }
091: }
092:
093: public void stopServer(Server server) {
094: String address = server.getEndpoint().getEndpointInfo()
095: .getAddress();
096: verifyNotification(stopNotificationMap, address, 0);
097: updateMap(stopNotificationMap, address);
098: if (recursiveCount > 0) {
099: recursiveEndpoints[--recursiveCount].stop();
100: }
101: }
102: });
103:
104: Endpoint.publish(ADDRESSES[0], new GreeterImpl()).stop();
105: for (int i = 0; i < ADDRESSES.length; i++) {
106: verifyNotification(startNotificationMap, ADDRESSES[i], 1);
107: verifyNotification(stopNotificationMap, ADDRESSES[i], 1);
108: }
109: }
110:
111: @Test
112: public void testGetActiveFeatures() {
113: assertNotNull("unexpected non-null ServerLifeCycleManager",
114: manager);
115:
116: manager.registerListener(new ServerLifeCycleListener() {
117: public void startServer(Server server) {
118: org.apache.cxf.endpoint.Endpoint endpoint = server
119: .getEndpoint();
120: updateMap(startNotificationMap, endpoint
121: .getEndpointInfo().getAddress());
122: String portName = endpoint.getEndpointInfo().getName()
123: .getLocalPart();
124: if ("SoapPort".equals(portName)) {
125:
126: List<AbstractFeature> active = endpoint
127: .getActiveFeatures();
128: assertNotNull(active);
129: assertEquals(1, active.size());
130: assertTrue(active.get(0) instanceof WSAddressingFeature);
131: assertSame(active.get(0), AbstractFeature
132: .getActive(active,
133: WSAddressingFeature.class));
134: } else {
135: List<AbstractFeature> active = endpoint
136: .getActiveFeatures();
137: assertNotNull(active);
138: assertEquals(0, active.size());
139: assertNull(AbstractFeature.getActive(active,
140: WSAddressingFeature.class));
141: }
142: }
143:
144: public void stopServer(Server server) {
145: updateMap(stopNotificationMap, server.getEndpoint()
146: .getEndpointInfo().getAddress());
147: }
148: });
149:
150: Endpoint greeter = Endpoint.publish(ADDRESSES[0],
151: new GreeterImpl());
152: Endpoint control = Endpoint.publish(ADDRESSES[1],
153: new ControlImpl());
154: greeter.stop();
155: control.stop();
156: for (int i = 0; i < 2; i++) {
157: verifyNotification(startNotificationMap, ADDRESSES[0], 1);
158: verifyNotification(stopNotificationMap, ADDRESSES[0], 1);
159: }
160: }
161:
162: private void verifyNotification(
163: Map<String, Integer> notificationMap, String address,
164: int expected) {
165: synchronized (notificationMap) {
166: Integer count = notificationMap.get(address);
167: if (expected == 0) {
168: assertNull("unexpected prior notification for: "
169: + address, count);
170: } else {
171: assertEquals("unexpected prior notification for: "
172: + address, expected, count.intValue());
173: }
174: }
175: }
176:
177: private void updateMap(Map<String, Integer> notificationMap,
178: String address) {
179: synchronized (notificationMap) {
180: Integer count = notificationMap.get(address);
181: if (count != null) {
182: notificationMap.put(address, new Integer(count
183: .intValue() + 1));
184: } else {
185: notificationMap.put(address, new Integer(1));
186: }
187: }
188: }
189:
190: }
|