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: * @(#)TestOperation.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 Operation class.
035: *
036: * @author Sun Microsystems, Inc.
037: */
038: public class TestOperation extends junit.framework.TestCase {
039: /**
040: * OperationCounter.
041: */
042: private OperationCounter mCounter;
043:
044: /**
045: * MyOperation (extends Operation).
046: */
047: private MyOperation mOperation;
048:
049: /**
050: * Arguments to operation.
051: */
052: private Object mArguments[];
053:
054: /**
055: * Constant for 1 second interval in milliseconds.
056: */
057: private static final long ONE_SECOND = 1000;
058:
059: /**
060: * Constant for 5 second interval in milliseconds.
061: */
062: private static final long FIVE_SECONDS = 5000;
063:
064: /**
065: * Constant for integer 3.
066: */
067: private static final int THREE = 3;
068:
069: /**
070: * The constructor for this testcase, forwards the test name to
071: * the jUnit TestCase base class.
072: * @param aTestName String with the name of this test.
073: */
074: public TestOperation(String aTestName) {
075: super (aTestName);
076: }
077:
078: /**
079: * Setup for the test. This creates the OperationCounter instance.
080: * @throws Exception when set up fails for any reason.
081: */
082: public void setUp() throws Exception {
083: super .setUp();
084: mArguments = new Object[THREE];
085: mArguments[0] = "MyOperation";
086: mArguments[1] = "FirstParameter";
087: mArguments[2] = "LastParameter";
088: mCounter = new OperationCounter();
089: mOperation = new MyOperation(mCounter, mArguments);
090: }
091:
092: /**
093: * Cleanup for the test.
094: * @throws Exception when tearDown fails for any reason.
095: */
096: public void tearDown() throws Exception {
097: super .tearDown();
098: }
099:
100: // ============================= test methods ================================
101:
102: /**
103: * Test the run() method.
104: * @throws Exception if an unexpected error occurs.
105: */
106: public void testRun() throws Exception {
107: Date start;
108: Date stop;
109: new Thread(mOperation, "testRun").start();
110: start = new Date();
111: synchronized (mCounter) {
112: if (0 < mCounter.getValue()) {
113: mCounter.wait(FIVE_SECONDS);
114: }
115: }
116: stop = new Date();
117: long interval = stop.getTime() - start.getTime();
118: assertTrue("Run() failed, wait() timed out",
119: FIVE_SECONDS > interval);
120: }
121:
122: /**
123: * Test the completed() method.
124: * @throws Exception if an unexpected error occurs.
125: */
126: public void testCompleted() throws Exception {
127: assertFalse("completed() returned wrong value", mOperation
128: .completed());
129: synchronized (mCounter) {
130: new Thread(mOperation, "testCompleted").start();
131: if (0 < mCounter.getValue()) {
132: mCounter.wait(ONE_SECOND);
133: }
134: }
135: assertTrue("completed() returned wrong value", mOperation
136: .completed());
137: }
138:
139: /**
140: * Test the getArgument() method.
141: * @throws Exception if an unexpected error occurs.
142: */
143: public void testGetArgument() throws Exception {
144: assertSame("getArgument(0) returned wrong object",
145: mArguments[0], mOperation.getArgument(0));
146: assertSame("getArgument(1) returned wrong object",
147: mArguments[1], mOperation.getArgument(1));
148: assertSame("getArgument(2) returned wrong object",
149: mArguments[2], mOperation.getArgument(2));
150: }
151:
152: /**
153: * Test the getArguments() method.
154: * @throws Exception if an unexpected error occurs.
155: */
156: public void testGetArguments() throws Exception {
157: assertSame("getArguments() returned wrong array", mArguments,
158: mOperation.getArguments());
159: }
160:
161: /**
162: * Test the getException() method.
163: * @throws Exception if an unexpected error occurs.
164: */
165: public void testGetException() throws Exception {
166: mArguments[2] = new Exception("TestException");
167: assertNull("getException() returned wrong value", mOperation
168: .getException());
169: synchronized (mCounter) {
170: new Thread(mOperation, "testGetException").start();
171: if (0 < mCounter.getValue()) {
172: mCounter.wait(ONE_SECOND);
173: }
174: }
175: assertEquals("getException() returned wrong value",
176: mArguments[2], mOperation.getException());
177: }
178:
179: /**
180: * Test the getReturnValue() method.
181: * @throws Exception if an unexpected error occurs.
182: */
183: public void testGetReturnValue() throws Exception {
184: assertNull("getReturnValue() returned wrong value", mOperation
185: .getReturnValue());
186: synchronized (mCounter) {
187: new Thread(mOperation, "testGetReturnValue").start();
188: if (0 < mCounter.getValue()) {
189: mCounter.wait(ONE_SECOND);
190: }
191: }
192: assertSame("getReturnValue() returned wrong value",
193: mArguments[0], mOperation.getReturnValue());
194: }
195:
196: /**
197: * Test the getThread() method.
198: * @throws Exception if an unexpected error occurs.
199: */
200: public void testGetThread() throws Exception {
201: assertNull(
202: "getThread() returned non-null value when null expected",
203: mOperation.getThread());
204: Thread t = new Thread(mOperation, "testGetThread");
205: synchronized (mCounter) {
206: t.start();
207: if (0 < mCounter.getValue()) {
208: mCounter.wait(ONE_SECOND);
209: }
210: }
211: assertSame("getThread() returned wrong value: ", t, mOperation
212: .getThread());
213: }
214:
215: /**
216: * Class to represent an Operation.
217: */
218: class MyOperation extends Operation {
219: /**
220: * Constructor.
221: *
222: * @param counter the OperationCounter instance associated with this
223: * operation.
224: * @param args an array of arguments containing one String argument.
225: */
226: MyOperation(OperationCounter counter, Object args[]) {
227: super (counter, args);
228: }
229:
230: /**
231: * Separate thread to do some work.
232: * @param argumentList an array of arguments for the process.
233: * @return the first argument from the argumentList.
234: * @throws Throwable if any error occurs.
235: */
236: public Object process(Object argumentList[]) throws Throwable {
237: if (argumentList[2] instanceof Exception) {
238: System.out.println(argumentList[0] + " on thread "
239: + Thread.currentThread().getName()
240: + " threw Exception at " + new Date());
241: throw (Exception) argumentList[2];
242: } else {
243: System.out.println(argumentList[0] + " on thread "
244: + Thread.currentThread().getName()
245: + " completed at " + new Date());
246: }
247: return argumentList[0];
248: }
249: }
250:
251: }
|