001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)TestMessagingStatistics.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.messaging;
030:
031: /**
032: * Tests for MessagingStatistics.
033: *
034: * @author Sun Microsystems, Inc.
035: */
036: public class TestMessagingStatistics extends junit.framework.TestCase {
037: /**
038: * Instance of MessagingStatistics.
039: */
040: private MessagingStatistics mStats;
041:
042: /**
043: * Constant for tests.
044: */
045: private static final long ONE_HUNDRED = 100;
046:
047: /**
048: * Constant for tests.
049: */
050: private static final long ONE_THOUSAND = 1000;
051:
052: /**
053: * The constructor for this testcase, forwards the test name to
054: * the jUnit TestCase base class.
055: * @param aTestName String with the name of this test.
056: */
057: public TestMessagingStatistics(String aTestName) {
058: super (aTestName);
059: }
060:
061: /**
062: * Setup for the test. This creates the MessagingStatistics instance
063: * and other objects needed for the tests.
064: * @throws Exception when set up fails for any reason.
065: */
066: public void setUp() throws Exception {
067: super .setUp();
068:
069: mStats = new MessagingStatistics();
070: mStats.setLastRestartTime(new java.util.Date());
071: }
072:
073: /**
074: * Cleanup for the test.
075: * @throws Exception when tearDown fails for any reason.
076: */
077: public void tearDown() throws Exception {
078: super .tearDown();
079: }
080:
081: // ============================= test methods ================================
082:
083: /**
084: * Tests get/setLastRestartTime.
085: * @throws Exception if an unexpected error occurs.
086: */
087: public void testLastRestartTime() {
088: java.util.Date d = new java.util.Date();
089: mStats.setLastRestartTime(d);
090: assertEquals("Failure on set/getLastRestartTime: ", d, mStats
091: .getLastRestartTime());
092: }
093:
094: /**
095: * Tests get/increment/decrementActiveExchanges.
096: * @throws Exception if an unexpected error occurs.
097: */
098: public void testActiveExchanges() throws Exception {
099: int n = 0;
100: assertEquals("Failure on getActiveExchanges: ", new Integer(n),
101: mStats.getActiveExchanges());
102: mStats.incrementActiveExchanges();
103: ++n;
104: assertEquals("Failure on incrementActiveExchanges: ",
105: new Integer(n), mStats.getActiveExchanges());
106: mStats.decrementActiveExchanges();
107: --n;
108: assertEquals("Failure on decrementActiveExchanges: ",
109: new Integer(n), mStats.getActiveExchanges());
110: }
111:
112: /**
113: * Tests getActiveExchangeRate.
114: * @throws Exception if an unexpected error occurs.
115: */
116: public void testActiveExchangeRate() throws Exception {
117: // TODO: Set a fixed value here?
118: java.util.Date d = new java.util.Date();
119: mStats.setLastRestartTime(d);
120: double n = 0;
121: assertEquals("Failure on getActiveExchangeRate: ",
122: new Double(n), mStats.getActiveExchangeRate());
123: }
124:
125: /**
126: * Tests get/incrementInOnlyExchanges.
127: * @throws Exception if an unexpected error occurs.
128: */
129: public void testInOnlyExchanges() throws Exception {
130: long n = 0;
131: assertEquals("Failure on getInOnlyExchanges: ", new Long(n),
132: mStats.getInOnlyExchanges());
133: mStats.incrementInOnlyExchanges();
134: ++n;
135: assertEquals("Failure on incrementInOnlyExchanges: ", new Long(
136: n), mStats.getInOnlyExchanges());
137: }
138:
139: /**
140: * Tests get/incrementRobustInOnlyExchanges.
141: * @throws Exception if an unexpected error occurs.
142: */
143: public void testRobustInOnlyExchanges() throws Exception {
144: long n = 0;
145: assertEquals("Failure on getRobustInOnlyExchanges: ", new Long(
146: n), mStats.getRobustInOnlyExchanges());
147: mStats.incrementRobustInOnlyExchanges();
148: ++n;
149: assertEquals("Failure on incrementRobustInOnlyExchanges: ",
150: new Long(n), mStats.getRobustInOnlyExchanges());
151: }
152:
153: /**
154: * Tests get/incrementInOptionalOutExchanges.
155: * @throws Exception if an unexpected error occurs.
156: */
157: public void testInOptionalOutExchanges() throws Exception {
158: long n = 0;
159: assertEquals("Failure on getInOptionalOutExchanges: ",
160: new Long(n), mStats.getInOptionalOutExchanges());
161: mStats.incrementInOptionalOutExchanges();
162: ++n;
163: assertEquals("Failure on incrementInOptionalOutExchanges: ",
164: new Long(n), mStats.getInOptionalOutExchanges());
165: }
166:
167: /**
168: * Tests get/incrementInOutExchanges.
169: * @throws Exception if an unexpected error occurs.
170: */
171: public void testInOutExchanges() throws Exception {
172: long n = 0;
173: assertEquals("Failure on getInOutExchanges: ", new Long(n),
174: mStats.getInOnlyExchanges());
175: mStats.incrementInOutExchanges();
176: ++n;
177: assertEquals("Failure on incrementInOutExchanges: ",
178: new Long(n), mStats.getInOutExchanges());
179: }
180:
181: /**
182: * Tests getTotalExchanges.
183: * @throws Exception if an unexpected error occurs.
184: */
185: public void testTotalExchanges() throws Exception {
186: long n = 0;
187: assertEquals("Failure on getTotalExchanges: ", new Long(n),
188: mStats.getTotalExchanges());
189: mStats.incrementInOnlyExchanges();
190: ++n;
191: assertEquals("Failure on getTotalExchanges: ", new Long(n),
192: mStats.getTotalExchanges());
193: mStats.incrementInOutExchanges();
194: ++n;
195: assertEquals("Failure on getTotalExchanges: ", new Long(n),
196: mStats.getTotalExchanges());
197: mStats.incrementInOptionalOutExchanges();
198: ++n;
199: assertEquals("Failure on getTotalExchanges: ", new Long(n),
200: mStats.getTotalExchanges());
201: mStats.incrementRobustInOnlyExchanges();
202: ++n;
203: assertEquals("Failure on getTotalExchanges: ", new Long(n),
204: mStats.getTotalExchanges());
205: }
206:
207: /**
208: * Tests get/incrementFailedExchanges.
209: * @throws Exception if an unexpected error occurs.
210: */
211: public void testFailedExchanges() throws Exception {
212: long n = 0;
213: assertEquals("Failure on getFailedExchanges: ", new Long(n),
214: mStats.getFailedExchanges());
215: mStats.incrementFailedExchanges();
216: ++n;
217: assertEquals("Failure on incrementFailedExchanges: ", new Long(
218: n), mStats.getFailedExchanges());
219: }
220:
221: /**
222: * Tests get/incrementTransactedExchanges.
223: * @throws Exception if an unexpected error occurs.
224: */
225: public void testTransactedExchanges() throws Exception {
226: long n = 0;
227: assertEquals("Failure on getTransactedExchanges: ",
228: new Long(n), mStats.getTransactedExchanges());
229: mStats.incrementTransactedExchanges();
230: ++n;
231: assertEquals("Failure on incrementTransactedExchanges: ",
232: new Long(n), mStats.getTransactedExchanges());
233: }
234:
235: /**
236: * Tests addTotalExchangeTimeToClose/getTotalExchangeTimeToClose/
237: * getExchangeMeanTimeToClose.
238: * @throws Exception if an unexpected error occurs.
239: */
240: public void testExchangeTimeToClose() throws Exception {
241: long t = 0;
242: assertEquals("Failure on getTotalExchangeTimeToClose: ",
243: new Long(t), new Long(mStats
244: .getTotalExchangeTimeToClose()));
245: t = ONE_THOUSAND;
246: mStats.addTotalExchangeTimeToClose(t);
247: assertEquals("Failure on addTotalExchangeTimeToClose: ",
248: new Long(t), new Long(mStats
249: .getTotalExchangeTimeToClose()));
250: assertEquals("Failure on getExchangeMeanTimeToClose: ",
251: new Long(0), mStats.getExchangeMeanTimeToClose());
252: int e = 0;
253: mStats.incrementInOnlyExchanges();
254: ++e;
255: assertEquals("Failure on getExchangeMeanTimeToClose: ",
256: new Long(t / e), mStats.getExchangeMeanTimeToClose());
257: mStats.incrementInOptionalOutExchanges();
258: ++e;
259: assertEquals("Failure on getExchangeMeanTimeToClose: ",
260: new Long(t / e), mStats.getExchangeMeanTimeToClose());
261: mStats.incrementInOutExchanges();
262: ++e;
263: assertEquals("Failure on getExchangeMeanTimeToClose: ",
264: new Long(t / e), mStats.getExchangeMeanTimeToClose());
265: mStats.incrementRobustInOnlyExchanges();
266: ++e;
267: assertEquals("Failure on getExchangeMeanTimeToClose: ",
268: new Long(t / e), mStats.getExchangeMeanTimeToClose());
269: }
270:
271: /**
272: * Tests getExchangeSuccessRate.
273: * @throws Exception if an unexpected error occurs.
274: */
275: public void testExchangeSuccessRate() throws Exception {
276: float percent = ONE_HUNDRED;
277: assertEquals("Failure on getExchangeSuccessRate: ", new Float(
278: percent), mStats.getExchangeSuccessRate());
279: mStats.incrementInOnlyExchanges();
280: assertEquals("Failure on getExchangeSuccessRate: ", new Float(
281: percent), mStats.getExchangeSuccessRate());
282: percent = 0;
283: mStats.incrementFailedExchanges();
284: assertEquals("Failure on getExchangeSuccessRate: ", new Float(
285: percent), mStats.getExchangeSuccessRate());
286: }
287:
288: /**
289: * Tests toCompositeData.
290: * @throws Exception if an unexpected error occurs.
291: */
292: public void testToCompositeData() throws Exception {
293: assertTrue(
294: "Failure on toCompositeData: ",
295: mStats.toCompositeData() instanceof javax.management.openmbean.CompositeData);
296: }
297:
298: /**
299: * Test resetStatistics.
300: * @throws Exception if an unexpected error occurs.
301: */
302: public void testResetStatistics() {
303: // First, populate all the fields with values. These methods are all
304: // tested in other junit tests so they are assumed to work here.
305:
306: mStats.incrementActiveExchanges();
307: mStats.incrementInOnlyExchanges();
308: mStats.incrementInOptionalOutExchanges();
309: mStats.incrementInOutExchanges();
310: mStats.incrementRobustInOnlyExchanges();
311: mStats.incrementFailedExchanges();
312: mStats.incrementTransactedExchanges();
313: mStats.addTotalExchangeTimeToClose(ONE_THOUSAND);
314:
315: // Now reset all the fields, then check to see if they all got reset.
316:
317: mStats.resetStatistics();
318: assertEquals("ActiveExchanges not reset", new Integer(0),
319: mStats.getActiveExchanges());
320: assertEquals("InOnlyExchanges not reset", new Long(0), mStats
321: .getInOnlyExchanges());
322: assertEquals("InOptionalOutExchanges not reset", new Long(0),
323: mStats.getInOptionalOutExchanges());
324: assertEquals("InOutExchanges not reset", new Long(0), mStats
325: .getInOutExchanges());
326: assertEquals("RobustInOnlyExchanges not reset", new Long(0),
327: mStats.getRobustInOnlyExchanges());
328: assertEquals("FailedExchanges not reset", new Long(0), mStats
329: .getFailedExchanges());
330: assertEquals("TransactedExchanges not reset", new Long(0),
331: mStats.getTransactedExchanges());
332: assertEquals("TotalExchangeTimeToClose not reset", new Long(0),
333: new Long(mStats.getTotalExchangeTimeToClose()));
334: }
335: }
|