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.Iterator;
021: import java.util.Set;
022:
023: import javax.management.MBeanServer;
024: import javax.management.ObjectName;
025: import javax.xml.namespace.QName;
026: import javax.xml.ws.Endpoint;
027:
028: import org.apache.cxf.Bus;
029: import org.apache.cxf.BusFactory;
030: import org.apache.cxf.bus.spring.SpringBusFactory;
031: import org.apache.cxf.management.InstrumentationManager;
032: import org.apache.cxf.management.ManagementConstants;
033: import org.apache.cxf.management.counters.CounterRepository;
034: import org.apache.cxf.management.jmx.InstrumentationManagerImpl;
035: import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase;
036: import org.apache.cxf.testutil.common.AbstractBusTestServerBase;
037: import org.apache.hello_world_soap_http.Greeter;
038: import org.apache.hello_world_soap_http.GreeterImpl;
039: import org.apache.hello_world_soap_http.SOAPService;
040: import org.junit.AfterClass;
041: import org.junit.BeforeClass;
042: import org.junit.Test;
043:
044: public class CountersClientServerTest extends
045: AbstractBusClientServerTestBase {
046:
047: private final QName portName = new QName(
048: "http://apache.org/hello_world_soap_http", "SoapPort");
049:
050: public static class Server extends AbstractBusTestServerBase {
051:
052: protected void run() {
053: SpringBusFactory bf = new SpringBusFactory();
054: Bus bus = bf
055: .createBus(
056: "org/apache/cxf/systest/management/counter-spring.xml",
057: true);
058: BusFactory.setDefaultBus(bus);
059: Object implementor = new GreeterImpl();
060: Endpoint.publish(
061: "http://localhost:9000/SoapContext/SoapPort",
062: implementor);
063: }
064:
065: public static void main(String[] args) {
066: try {
067: Server s = new Server();
068: s.start();
069: } catch (Exception ex) {
070: ex.printStackTrace();
071: System.exit(-1);
072: } finally {
073: System.out.println("done!");
074: }
075: }
076: }
077:
078: @BeforeClass
079: public static void startServers() throws Exception {
080: assertTrue("server did not launch correctly", launchServer(
081: Server.class, true));
082: }
083:
084: @AfterClass
085: public static void shutdownBus() throws Exception {
086: BusFactory.getDefaultBus().shutdown(false);
087: }
088:
089: @Test
090: public void testCountersWithInstrumentationManager()
091: throws Exception {
092: // create Client with other bus
093: Bus bus = BusFactory.getDefaultBus();
094:
095: CounterRepository cr = bus
096: .getExtension(CounterRepository.class);
097: InstrumentationManager im = bus
098: .getExtension(InstrumentationManager.class);
099: assertNotNull(im);
100: InstrumentationManagerImpl impl = (InstrumentationManagerImpl) im;
101: assertTrue(impl.isEnabled());
102: assertNotNull(impl.getMBeanServer());
103:
104: MBeanServer mbs = im.getMBeanServer();
105: ObjectName name = new ObjectName(
106: ManagementConstants.DEFAULT_DOMAIN_NAME + ":"
107: + ManagementConstants.BUS_ID_PROP + "=cxf"
108: + bus.hashCode() + ",*");
109:
110: SOAPService service = new SOAPService();
111: assertNotNull(service);
112:
113: Greeter greeter = service.getPort(portName, Greeter.class);
114:
115: String response = new String("Bonjour");
116: String reply = greeter.sayHi();
117:
118: //assertNotNull("no response received from service", reply);
119: //assertEquals(response, reply);
120:
121: assertEquals("The Counters are not create yet", 4, cr
122: .getCounters().size());
123: Set counterNames = mbs.queryNames(name, null);
124: assertEquals("The Counters are not export to JMX ", 4 + 2,
125: counterNames.size());
126:
127: ObjectName sayHiCounter = new ObjectName(
128: ManagementConstants.DEFAULT_DOMAIN_NAME
129: + ":operation=\"{http://apache.org/hello_world_soap_http}sayHi\",*");
130:
131: Set s = mbs.queryNames(sayHiCounter, null);
132: Iterator it = s.iterator();
133:
134: while (it.hasNext()) {
135: ObjectName counterName = (ObjectName) it.next();
136: Object val = mbs
137: .getAttribute(counterName, "NumInvocations");
138: assertEquals("Wrong Counters Number of Invocations", val, 1);
139: }
140:
141: reply = greeter.sayHi();
142: assertNotNull("no response received from service", reply);
143: assertEquals(response, reply);
144:
145: s = mbs.queryNames(sayHiCounter, null);
146: it = s.iterator();
147:
148: while (it.hasNext()) {
149: ObjectName counterName = (ObjectName) it.next();
150: Object val = mbs
151: .getAttribute(counterName, "NumInvocations");
152: assertEquals("Wrong Counters Number of Invocations", val, 2);
153: }
154:
155: greeter.greetMeOneWay("hello");
156: assertEquals("The Counters are not create yet", 6, cr
157: .getCounters().size());
158: counterNames = mbs.queryNames(name, null);
159: assertEquals("The Counters are not export to JMX ", 6 + 2,
160: counterNames.size());
161:
162: ObjectName greetMeOneWayCounter = new ObjectName(
163: ManagementConstants.DEFAULT_DOMAIN_NAME
164: + ":operation=\"{http://apache.org/hello_world_soap_http}greetMeOneWay\",*");
165:
166: s = mbs.queryNames(greetMeOneWayCounter, null);
167: it = s.iterator();
168:
169: while (it.hasNext()) {
170: ObjectName counterName = (ObjectName) it.next();
171: Object val = mbs
172: .getAttribute(counterName, "NumInvocations");
173: assertEquals("Wrong Counters Number of Invocations", val, 1);
174: }
175:
176: }
177:
178: }
|