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.management;
019:
020: import java.util.Set;
021:
022: import javax.management.MBeanServer;
023: import javax.management.ObjectName;
024: import javax.xml.namespace.QName;
025: import javax.xml.ws.Endpoint;
026:
027: import org.apache.cxf.Bus;
028: import org.apache.cxf.BusFactory;
029: import org.apache.cxf.bus.spring.SpringBusFactory;
030: import org.apache.cxf.management.InstrumentationManager;
031: import org.apache.cxf.management.ManagementConstants;
032: import org.apache.cxf.management.jmx.InstrumentationManagerImpl;
033: import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase;
034: import org.apache.cxf.testutil.common.AbstractBusTestServerBase;
035: import org.apache.hello_world_soap_http.Greeter;
036: import org.apache.hello_world_soap_http.GreeterImpl;
037: import org.apache.hello_world_soap_http.SOAPService;
038: import org.junit.AfterClass;
039: import org.junit.BeforeClass;
040: import org.junit.Test;
041:
042: public class ManagedClientServerTest extends
043: AbstractBusClientServerTestBase {
044:
045: private final QName portName = new QName(
046: "http://apache.org/hello_world_soap_http", "SoapPort");
047:
048: public static class Server extends AbstractBusTestServerBase {
049:
050: protected void run() {
051: SpringBusFactory bf = new SpringBusFactory();
052: Bus bus = bf
053: .createBus(
054: "org/apache/cxf/systest/management/managed-spring.xml",
055: true);
056: BusFactory.setDefaultBus(bus);
057: Object implementor = new GreeterImpl();
058: Endpoint.publish(null, implementor);
059: }
060:
061: public static void main(String[] args) {
062: try {
063: Server s = new Server();
064: s.start();
065: } catch (Exception ex) {
066: ex.printStackTrace();
067: System.exit(-1);
068: } finally {
069: System.out.println("done!");
070: }
071: }
072: }
073:
074: @BeforeClass
075: public static void startServers() throws Exception {
076: assertTrue("server did not launch correctly", launchServer(
077: Server.class, true));
078: }
079:
080: @AfterClass
081: public static void shutdownBus() throws Exception {
082: BusFactory.getDefaultBus().shutdown(false);
083: }
084:
085: @Test
086: public void testManagedEndpoint() throws Exception {
087: Bus bus = SpringBusFactory.getDefaultBus();
088: InstrumentationManager im = bus
089: .getExtension(InstrumentationManager.class);
090: assertNotNull(im);
091: InstrumentationManagerImpl impl = (InstrumentationManagerImpl) im;
092: assertTrue(impl.isEnabled());
093: assertNotNull(impl.getMBeanServer());
094:
095: MBeanServer mbs = im.getMBeanServer();
096: ObjectName name = new ObjectName(
097: ManagementConstants.DEFAULT_DOMAIN_NAME
098: + ":type=Bus.Service.Endpoint,*");
099: Set s = mbs.queryNames(name, null);
100: assertTrue(s.size() == 1);
101: name = (ObjectName) s.iterator().next();
102:
103: Object val = mbs.invoke(name, "getState", new Object[0],
104: new String[0]);
105: assertEquals("Service should have been started.", "STARTED",
106: val);
107:
108: SOAPService service = new SOAPService();
109: assertNotNull(service);
110:
111: Greeter greeter = service.getPort(portName, Greeter.class);
112:
113: String response = new String("Bonjour");
114: String reply = greeter.sayHi();
115: assertNotNull("no response received from service", reply);
116: assertEquals(response, reply);
117:
118: mbs.invoke(name, "stop", new Object[0], new String[0]);
119:
120: val = mbs.getAttribute(name, "State");
121:
122: assertEquals("Service should have been stopped.", "STOPPED",
123: val);
124:
125: try {
126: reply = greeter.sayHi();
127: fail("Endpoint should not be active at this point.");
128: } catch (Exception ex) {
129: //Expected
130: }
131:
132: mbs.invoke(name, "start", new Object[0], new String[0]);
133:
134: val = mbs
135: .invoke(name, "getState", new Object[0], new String[0]);
136: assertEquals("Service should have been started.", "STARTED",
137: val);
138:
139: reply = greeter.sayHi();
140: assertNotNull("no response received from service", reply);
141: assertEquals(response, reply);
142:
143: }
144:
145: }
|