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: * @(#)TestOperationCounter.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.framework;
030:
031: import java.util.Date;
032:
033: /**
034: * Tests for the OperationCounter class.
035: *
036: * @author Sun Microsystems, Inc.
037: */
038: public class TestOperationCounter extends junit.framework.TestCase {
039: /**
040: * OperationCounter
041: */
042: private OperationCounter mCounter;
043:
044: /**
045: * Constant for 5 second interval in milliseconds.
046: */
047: private static final long FIVE_SECONDS = 5000;
048:
049: /**
050: * Constant for count of 5.
051: */
052: private static final int FIVE = 5;
053:
054: /**
055: * Constant for count of 4.
056: */
057: private static final int FOUR = 4;
058:
059: /**
060: * The constructor for this testcase, forwards the test name to
061: * the jUnit TestCase base class.
062: * @param aTestName String with the name of this test.
063: */
064: public TestOperationCounter(String aTestName) {
065: super (aTestName);
066: }
067:
068: /**
069: * Setup for the test. This creates the OperationCounter instance.
070: * @throws Exception when set up fails for any reason.
071: */
072: public void setUp() throws Exception {
073: super .setUp();
074: mCounter = new OperationCounter();
075: }
076:
077: /**
078: * Cleanup for the test.
079: * @throws Exception when tearDown fails for any reason.
080: */
081: public void tearDown() throws Exception {
082: super .tearDown();
083: }
084:
085: // ============================= test methods ================================
086:
087: /**
088: * Test the increment and decrement methods for the counter.
089: * @throws Exception if an unexpected error occurs.
090: */
091: public void testIncrementDecrement() throws Exception {
092: for (int i = 1; i <= FIVE; i++) {
093: assertEquals("Failure incrementing counter: ", i, mCounter
094: .increment());
095: }
096: for (int i = FOUR; i >= 0; i--) {
097: assertEquals("Failure decrementing counter: ", i, mCounter
098: .decrement());
099: }
100: }
101:
102: /**
103: * Test the notification capability of the decrement() method.
104: * @throws Exception if an unexpected error occurs.
105: */
106: public void testDecrementNotify() throws Exception {
107: for (int i = 0; i < FIVE; i++) {
108: mCounter.increment();
109: }
110: CounterThread ct = new CounterThread();
111: Date start;
112: Date stop;
113: synchronized (mCounter) {
114: new Thread(ct).start();
115: start = new Date();
116: mCounter.wait(FIVE_SECONDS);
117: stop = new Date();
118: }
119: long startTime = start.getTime();
120: long stopTime = stop.getTime();
121: assertTrue("Notify() failed, wait() timed out",
122: FIVE_SECONDS > (stopTime - startTime));
123: }
124:
125: /**
126: * Class to run on a separate thread to decrement counters.
127: */
128: class CounterThread implements Runnable {
129: /**
130: * Separate thread to do the counter decrements to test the notify()
131: * capability.
132: */
133: public final void run() {
134: while (0 < mCounter.decrement()) {
135: ;
136: }
137: }
138: }
139:
140: }
|