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.management.counters;
019:
020: import javax.management.ObjectName;
021:
022: import org.apache.cxf.Bus;
023: import org.apache.cxf.management.InstrumentationManager;
024: import org.easymock.classextension.EasyMock;
025: import org.junit.Assert;
026: import org.junit.Before;
027: import org.junit.Test;
028:
029: public class CounterRepositoryTest extends Assert {
030: private Bus bus;
031: //private InstrumentationManager im;
032: private ObjectName serviceCounter;
033: private ObjectName operationCounter;
034:
035: @Before
036: public void setUp() throws Exception {
037:
038: serviceCounter = new ObjectName(
039: "tandoori:type=counter,service=help");
040: operationCounter = new ObjectName(
041: "tandoori:type=counter,service=help,operation=me");
042: bus = EasyMock.createMock(Bus.class);
043: bus.getExtension(InstrumentationManager.class);
044: EasyMock.expectLastCall().andReturn(null).anyTimes();
045: EasyMock.replay(bus);
046: }
047:
048: @Test
049: public void testIncreaseOneWayResponseCounter() throws Exception {
050:
051: CounterRepository cr = new CounterRepository();
052: cr.setBus(bus);
053: //cr.createCounter(operationCounter, true);
054: MessageHandlingTimeRecorder mhtr = EasyMock
055: .createMock(MessageHandlingTimeRecorder.class);
056: EasyMock.expect(mhtr.isOneWay()).andReturn(true).anyTimes();
057: EasyMock.expect(mhtr.getEndTime()).andReturn((long) 100000000)
058: .anyTimes();
059: EasyMock.expect(mhtr.getHandlingTime()).andReturn((long) 1000)
060: .anyTimes();
061:
062: EasyMock.replay(mhtr);
063: cr.increaseCounter(serviceCounter, mhtr);
064: cr.increaseCounter(operationCounter, mhtr);
065: ResponseTimeCounter opCounter = (ResponseTimeCounter) cr
066: .getCounter(operationCounter);
067: ResponseTimeCounter sCounter = (ResponseTimeCounter) cr
068: .getCounter(serviceCounter);
069:
070: assertEquals("The operation counter isn't increased", opCounter
071: .getNumInvocations(), 1);
072: assertEquals("The Service counter isn't increased", sCounter
073: .getNumInvocations(), 1);
074:
075: EasyMock.verify(bus);
076: EasyMock.verify(mhtr);
077: }
078:
079: @Test
080: public void testIncreaseOneWayNoResponseCounter() throws Exception {
081:
082: CounterRepository cr = new CounterRepository();
083: cr.setBus(bus);
084: //cr.createCounter(operationCounter, true);
085: MessageHandlingTimeRecorder mhtr = EasyMock
086: .createMock(MessageHandlingTimeRecorder.class);
087: EasyMock.expect(mhtr.isOneWay()).andReturn(true).anyTimes();
088: EasyMock.expect(mhtr.getEndTime()).andReturn((long) 0)
089: .anyTimes();
090: EasyMock.replay(mhtr);
091: cr.increaseCounter(serviceCounter, mhtr);
092: cr.increaseCounter(operationCounter, mhtr);
093: ResponseTimeCounter opCounter = (ResponseTimeCounter) cr
094: .getCounter(operationCounter);
095: ResponseTimeCounter sCounter = (ResponseTimeCounter) cr
096: .getCounter(serviceCounter);
097:
098: assertEquals("The operation counter isn't increased", opCounter
099: .getNumInvocations(), 1);
100: assertEquals("The Service counter isn't increased", sCounter
101: .getNumInvocations(), 1);
102:
103: EasyMock.verify(bus);
104: EasyMock.verify(mhtr);
105: }
106:
107: @Test
108: public void testIncreaseResponseCounter() throws Exception {
109: CounterRepository cr = new CounterRepository();
110: cr.setBus(bus);
111:
112: MessageHandlingTimeRecorder mhtr1 = EasyMock
113: .createMock(MessageHandlingTimeRecorder.class);
114: EasyMock.expect(mhtr1.isOneWay()).andReturn(false).anyTimes();
115: EasyMock.expect(mhtr1.getHandlingTime()).andReturn((long) 1000)
116: .anyTimes();
117: EasyMock.replay(mhtr1);
118: cr.createCounter(operationCounter, mhtr1);
119: cr.increaseCounter(serviceCounter, mhtr1);
120: cr.increaseCounter(operationCounter, mhtr1);
121: ResponseTimeCounter opCounter = (ResponseTimeCounter) cr
122: .getCounter(operationCounter);
123: ResponseTimeCounter sCounter = (ResponseTimeCounter) cr
124: .getCounter(serviceCounter);
125:
126: assertEquals("The operation counter isn't increased", opCounter
127: .getNumInvocations(), 1);
128: assertEquals(
129: "The operation counter's AvgResponseTime is wrong ",
130: opCounter.getAvgResponseTime(), 1000);
131: assertEquals(
132: "The operation counter's MaxResponseTime is wrong ",
133: opCounter.getMaxResponseTime(), (long) 1000);
134: assertEquals(
135: "The operation counter's MinResponseTime is wrong ",
136: opCounter.getMinResponseTime(), (long) 1000);
137: assertEquals("The Service counter isn't increased", sCounter
138: .getNumInvocations(), 1);
139:
140: MessageHandlingTimeRecorder mhtr2 = EasyMock
141: .createMock(MessageHandlingTimeRecorder.class);
142: EasyMock.expect(mhtr2.isOneWay()).andReturn(false).anyTimes();
143: EasyMock.expect(mhtr2.getHandlingTime()).andReturn((long) 2000)
144: .anyTimes();
145: EasyMock.replay(mhtr2);
146: cr.increaseCounter(serviceCounter, mhtr2);
147: cr.increaseCounter(operationCounter, mhtr2);
148: assertEquals("The operation counter isn't increased", opCounter
149: .getNumInvocations(), 2);
150: assertEquals(
151: "The operation counter's AvgResponseTime is wrong ",
152: opCounter.getAvgResponseTime(), 1500);
153: assertEquals(
154: "The operation counter's MaxResponseTime is wrong ",
155: opCounter.getMaxResponseTime(), (long) 2000);
156: assertEquals(
157: "The operation counter's MinResponseTime is wrong ",
158: opCounter.getMinResponseTime(), (long) 1000);
159: assertEquals("The Service counter isn't increased", sCounter
160: .getNumInvocations(), 2);
161:
162: EasyMock.verify(bus);
163: EasyMock.verify(mhtr1);
164: EasyMock.verify(mhtr2);
165: }
166:
167: }
|