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: TestStepBack.java 3810 2007-06-25 13:36:58Z gbevin $
007: */
008: package com.uwyn.rife.continuations;
009:
010: import java.lang.reflect.Method;
011: import junit.framework.TestCase;
012:
013: public class TestStepBack extends TestCase {
014: public TestStepBack(String name) {
015: super (name);
016: }
017:
018: public void testStepBackInWhile() throws Throwable {
019: for (String testclass : new String[] { "$StepBackCounter",
020: "$StepBackCounterInterface" }) {
021: ContinuableRunnerTest runner = new ContinuableRunnerTest();
022:
023: String id1 = runner.start(TestStepBack.class.getName()
024: + testclass);
025: assertNotNull(id1);
026: ContinuationContext context1 = runner.getManager()
027: .getContext(id1);
028:
029: ContinuableObject continuable1 = context1.getContinuable();
030: Method start_method = continuable1.getClass().getMethod(
031: "setStart", new Class[] { boolean.class });
032: Method total_method = continuable1.getClass().getMethod(
033: "getTotal", new Class[0]);
034: Method answer_method = continuable1.getClass().getMethod(
035: "setAnswer", new Class[] { int.class });
036:
037: start_method.invoke(continuable1, new Object[] { true });
038: String id2 = runner.resume(id1);
039: assertNotNull(id2);
040: assertFalse(id1.equals(id2));
041: ContinuationContext context2 = runner.getManager()
042: .getContext(id2);
043: ContinuableObject continuable2 = context2.getContinuable();
044: int total2 = ((Integer) total_method.invoke(continuable2,
045: new Object[0])).intValue();
046: assertEquals(0, total2);
047:
048: answer_method.invoke(continuable2, new Object[] { 12 });
049: String id3 = runner.resume(id2);
050: assertNotNull(id3);
051: assertFalse(id1.equals(id3));
052: assertFalse(id2.equals(id3));
053: ContinuationContext context3 = runner.getManager()
054: .getContext(id3);
055: ContinuableObject continuable3 = context3.getContinuable();
056: int total3 = ((Integer) total_method.invoke(continuable3,
057: new Object[0])).intValue();
058: assertEquals(12, total3);
059:
060: answer_method.invoke(continuable3, new Object[] { 32 });
061: String id4 = runner.resume(id3);
062: assertNotNull(id4);
063: assertFalse(id1.equals(id4));
064: assertFalse(id2.equals(id4));
065: assertFalse(id3.equals(id4));
066: ContinuationContext context4 = runner.getManager()
067: .getContext(id4);
068: ContinuableObject continuable4 = context4.getContinuable();
069: int total4 = ((Integer) total_method.invoke(continuable4,
070: new Object[0])).intValue();
071: assertEquals(44, total4);
072:
073: answer_method.invoke(continuable4, new Object[] { 41 });
074: String id5 = runner.resume(id4);
075: assertNull(id5);
076: }
077: }
078:
079: public static class StepBackCounter extends
080: AbstractContinuableObject {
081: private int mTotal = -5;
082:
083: public int getTotal() {
084: return mTotal;
085: }
086:
087: private boolean mStart = false;
088:
089: public void setStart(boolean start) {
090: mStart = start;
091: }
092:
093: private int mAnswer;
094:
095: public void setAnswer(int answer) {
096: mAnswer = answer;
097: }
098:
099: public void execute() {
100: int number_of_resumes = 0;
101:
102: if (mTotal < 0) {
103: mTotal++;
104: stepback();
105: }
106:
107: pause();
108: number_of_resumes++;
109:
110: if (mStart) {
111: pause();
112: number_of_resumes++;
113:
114: mTotal += mAnswer;
115:
116: if (mTotal < 50) {
117: stepback();
118: }
119: }
120: }
121: }
122:
123: public static class StepBackCounterInterface implements
124: ContinuableObject, ContinuableSupportAware {
125: private ContinuableSupport mSupport;
126:
127: public void setContinuableSupport(ContinuableSupport support) {
128: mSupport = support;
129: }
130:
131: public Object clone() throws CloneNotSupportedException {
132: return super .clone();
133: }
134:
135: private int mTotal = -5;
136:
137: public int getTotal() {
138: return mTotal;
139: }
140:
141: private boolean mStart = false;
142:
143: public void setStart(boolean start) {
144: mStart = start;
145: }
146:
147: private int mAnswer;
148:
149: public void setAnswer(int answer) {
150: mAnswer = answer;
151: }
152:
153: public void execute() {
154: int number_of_resumes = 0;
155:
156: if (mTotal < 0) {
157: mTotal++;
158: mSupport.stepback();
159: }
160:
161: mSupport.pause();
162: number_of_resumes++;
163:
164: if (mStart) {
165: mSupport.pause();
166: number_of_resumes++;
167:
168: mTotal += mAnswer;
169:
170: if (mTotal < 50) {
171: mSupport.stepback();
172: }
173: }
174: }
175: }
176: }
|