001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: TestCall.java 3810 2007-06-25 13:36:58Z gbevin $
007: */
008: package com.uwyn.rife.continuations;
009:
010: import com.uwyn.rife.tools.ExceptionUtils;
011: import java.lang.reflect.Method;
012: import junit.framework.TestCase;
013:
014: public class TestCall extends TestCase {
015: public TestCall(String name) {
016: super (name);
017: }
018:
019: public void testSimpleCall() throws Throwable {
020: for (String testclass : new String[] { "$SimpleCallSource",
021: "$SimpleCallSourceInterface" }) {
022: ContinuableRunnerTest runner = new ContinuableRunnerTest();
023:
024: String id1 = runner.start(TestCall.class.getName()
025: + testclass);
026: assertNull(id1);
027: ContinuableObject continuable = runner
028: .getCurrentContinuable();
029: assertNotNull(continuable);
030: Method method_getresult = continuable.getClass().getMethod(
031: "getResult", new Class[0]);
032: assertEquals(
033: "before call\nduring call target 1\nduring call target 2\nafter call",
034: method_getresult.invoke(continuable, new Object[0]));
035: }
036: }
037:
038: public void testAnswerInOtherThread() throws Throwable {
039: for (final String testclass : new String[] {
040: "$AnswerInOtherThreadCallSource",
041: "$AnswerInOtherThreadCallSourceInterface" }) {
042: final ContinuableRunnerTest runner = new ContinuableRunnerTest();
043: final ContinuableObject[] continuables = new ContinuableObject[2];
044: final String[] ids = new String[2];
045:
046: Thread thread1 = new Thread() {
047: public void run() {
048: try {
049: String id = runner.start(TestCall.class
050: .getName()
051: + testclass);
052: assertNull(id);
053: ids[0] = ContinuationContext.getLastContext()
054: .getId();
055: continuables[0] = ContinuationContext
056: .getLastContext().getContinuable();
057: assertNotNull(continuables[0]);
058: Method method_setdoanswer = continuables[0]
059: .getClass().getMethod("setDoAnswer",
060: new Class[] { boolean.class });
061: method_setdoanswer.invoke(continuables[0],
062: new Object[] { true });
063: } catch (Throwable e) {
064: fail(ExceptionUtils.getExceptionStackTrace(e));
065: } finally {
066: synchronized (this ) {
067: this .notifyAll();
068: }
069: }
070: }
071: };
072:
073: Thread thread2 = new Thread() {
074: public void run() {
075: try {
076: String id = runner.run(ids[0]);
077: assertNull(id);
078: continuables[1] = runner
079: .getCurrentContinuable();
080: assertNotNull(continuables[1]);
081: Method method_getresult = continuables[1]
082: .getClass().getMethod("getResult",
083: new Class[0]);
084: assertEquals("before call\ntrue\nafter call",
085: method_getresult.invoke(
086: continuables[1], new Object[0]));
087: } catch (Throwable e) {
088: fail(ExceptionUtils.getExceptionStackTrace(e));
089: } finally {
090: synchronized (this ) {
091: this .notifyAll();
092: }
093: }
094: }
095: };
096:
097: thread1.start();
098: while (thread1.isAlive()) {
099: synchronized (thread1) {
100: thread1.wait();
101: }
102: }
103:
104: thread2.start();
105: while (thread2.isAlive()) {
106: synchronized (thread2) {
107: thread2.wait();
108: }
109: }
110: }
111: }
112:
113: // classes for testSimpleCall
114: public static class SimpleCallSource extends
115: AbstractContinuableObject {
116: private StringBuilder mResult;
117:
118: public void execute() {
119: mResult = new StringBuilder("before call\n");
120: String answer = (String) call(SimpleCallTarget1.class);
121: mResult.append(answer);
122: mResult.append("after call");
123: }
124:
125: public String getResult() {
126: return mResult.toString();
127: }
128: }
129:
130: public static class SimpleCallTarget1 extends
131: AbstractContinuableObject {
132: public void execute() {
133: String answer = "during call target 1\n"
134: + call(SimpleCallTarget2.class);
135: answer(answer);
136: }
137: }
138:
139: public static class SimpleCallTarget2 extends
140: AbstractContinuableObject {
141: public void execute() {
142: String answer = "during call target 2\n";
143: answer(answer);
144: }
145: }
146:
147: public static class SimpleCallSourceInterface implements
148: ContinuableObject, ContinuableSupportAware {
149: private ContinuableSupport mSupport;
150:
151: public void setContinuableSupport(ContinuableSupport support) {
152: mSupport = support;
153: }
154:
155: public Object clone() throws CloneNotSupportedException {
156: return super .clone();
157: }
158:
159: private StringBuilder mResult;
160:
161: public void execute() {
162: mResult = new StringBuilder("before call\n");
163: String answer = (String) mSupport
164: .call(SimpleCallTarget1Interface.class);
165: mResult.append(answer);
166: mResult.append("after call");
167: }
168:
169: public String getResult() {
170: return mResult.toString();
171: }
172: }
173:
174: public static class SimpleCallTarget1Interface implements
175: ContinuableObject, ContinuableSupportAware {
176: private ContinuableSupport mSupport;
177:
178: public void setContinuableSupport(ContinuableSupport support) {
179: mSupport = support;
180: }
181:
182: public Object clone() throws CloneNotSupportedException {
183: return super .clone();
184: }
185:
186: public void execute() {
187: String answer = "during call target 1\n"
188: + mSupport.call(SimpleCallTarget2Interface.class);
189: mSupport.answer(answer);
190: }
191: }
192:
193: public static class SimpleCallTarget2Interface implements
194: ContinuableObject, ContinuableSupportAware {
195: private ContinuableSupport mSupport;
196:
197: public void setContinuableSupport(ContinuableSupport support) {
198: mSupport = support;
199: }
200:
201: public Object clone() throws CloneNotSupportedException {
202: return super .clone();
203: }
204:
205: public void execute() {
206: String answer = "during call target 2\n";
207: mSupport.answer(answer);
208: }
209: }
210:
211: // classes for testAnswerInOtherThread
212: public static class AnswerInOtherThreadCallSource extends
213: AbstractContinuableObject {
214: private StringBuilder mResult;
215:
216: public void execute() {
217: mResult = new StringBuilder("before call\n");
218: Boolean answer = (Boolean) call(AnswerInOtherThreadCallTarget.class);
219: mResult.append(answer);
220: mResult.append("\nafter call");
221: }
222:
223: public String getResult() {
224: return mResult.toString();
225: }
226: }
227:
228: public static class AnswerInOtherThreadCallTarget extends
229: AbstractContinuableObject {
230: private boolean mDoAnswer = false;
231:
232: public void setDoAnswer(boolean doAnswer) {
233: mDoAnswer = doAnswer;
234: }
235:
236: public void execute() {
237: if (mDoAnswer) {
238: answer(true);
239: }
240: }
241: }
242:
243: public static class AnswerInOtherThreadCallSourceInterface
244: implements ContinuableObject, ContinuableSupportAware {
245: private ContinuableSupport mSupport;
246:
247: public void setContinuableSupport(ContinuableSupport support) {
248: mSupport = support;
249: }
250:
251: public Object clone() throws CloneNotSupportedException {
252: return super .clone();
253: }
254:
255: private StringBuilder mResult;
256:
257: public void execute() {
258: mResult = new StringBuilder("before call\n");
259: Boolean answer = (Boolean) mSupport
260: .call(AnswerInOtherThreadCallTargetInterface.class);
261: mResult.append(answer);
262: mResult.append("\nafter call");
263: }
264:
265: public String getResult() {
266: return mResult.toString();
267: }
268: }
269:
270: public static class AnswerInOtherThreadCallTargetInterface
271: implements ContinuableObject, ContinuableSupportAware {
272: private ContinuableSupport mSupport;
273:
274: public void setContinuableSupport(ContinuableSupport support) {
275: mSupport = support;
276: }
277:
278: public Object clone() throws CloneNotSupportedException {
279: return super .clone();
280: }
281:
282: private boolean mDoAnswer = false;
283:
284: public void setDoAnswer(boolean doAnswer) {
285: mDoAnswer = doAnswer;
286: }
287:
288: public void execute() {
289: if (mDoAnswer) {
290: mSupport.answer(true);
291: }
292: }
293: }
294: }
|