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: TestPause.java 3810 2007-06-25 13:36:58Z gbevin $
007: */
008: package com.uwyn.rife.continuations;
009:
010: import junit.framework.TestCase;
011:
012: public class TestPause extends TestCase {
013: public TestPause(String name) {
014: super (name);
015: }
016:
017: public void testPauseInWhile() throws Throwable {
018: for (String testclass : new String[] { "$PauseInWhile",
019: "$PauseInWhileInterface" }) {
020: ContinuableRunnerTest runner = new ContinuableRunnerTest();
021:
022: String id1 = runner.start(TestPause.class.getName()
023: + testclass);
024: assertNotNull(id1);
025: ContinuationContext context1 = runner.getManager()
026: .getContext(id1);
027: assertEquals(5, context1.getLocalVars().getInt(1));
028:
029: String id2 = runner.resume(id1);
030: assertNotNull(id2);
031: assertFalse(id1.equals(id2));
032: ContinuationContext context2 = runner.getManager()
033: .getContext(id2);
034: assertEquals(4, context2.getLocalVars().getInt(1));
035:
036: String id3 = runner.resume(id2);
037: assertNotNull(id3);
038: assertFalse(id1.equals(id3));
039: assertFalse(id2.equals(id3));
040: ContinuationContext context3 = runner.getManager()
041: .getContext(id3);
042: assertEquals(3, context3.getLocalVars().getInt(1));
043:
044: String id4 = runner.resume(id3);
045: assertNotNull(id4);
046: assertFalse(id1.equals(id4));
047: assertFalse(id2.equals(id4));
048: assertFalse(id3.equals(id4));
049: ContinuationContext context4 = runner.getManager()
050: .getContext(id4);
051: assertEquals(2, context4.getLocalVars().getInt(1));
052:
053: String id5 = runner.resume(id4);
054: assertNotNull(id5);
055: assertFalse(id1.equals(id5));
056: assertFalse(id2.equals(id5));
057: assertFalse(id3.equals(id5));
058: assertFalse(id4.equals(id5));
059: ContinuationContext context5 = runner.getManager()
060: .getContext(id5);
061: assertEquals(1, context5.getLocalVars().getInt(1));
062:
063: String id6 = runner.resume(id5);
064: assertNull(id6);
065: }
066: }
067:
068: public void testPauseInWhileClones() throws Throwable {
069: for (String testclass : new String[] { "$PauseInWhile",
070: "$PauseInWhileInterface" }) {
071: ContinuableRunnerTest runner = new ContinuableRunnerTest();
072:
073: String id1 = runner.start(TestPause.class.getName()
074: + testclass);
075: assertNotNull(id1);
076: ContinuationContext context1 = runner.getManager()
077: .getContext(id1);
078: assertEquals(5, context1.getLocalVars().getInt(1));
079:
080: String id2a = runner.resume(id1);
081: assertNotNull(id2a);
082: ContinuationContext context2 = runner.getManager()
083: .getContext(id2a);
084: assertEquals(4, context2.getLocalVars().getInt(1));
085:
086: String id3aa = runner.resume(id2a);
087: assertNotNull(id3aa);
088: ContinuationContext context3aa = runner.getManager()
089: .getContext(id3aa);
090: assertEquals(3, context3aa.getLocalVars().getInt(1));
091:
092: String id4aa = runner.resume(id3aa);
093: assertNotNull(id4aa);
094: ContinuationContext context4aa = runner.getManager()
095: .getContext(id4aa);
096: assertEquals(2, context4aa.getLocalVars().getInt(1));
097:
098: String id2b = runner.resume(id1);
099: assertNotNull(id2b);
100: assertFalse(id2a.equals(id2b));
101: ContinuationContext context2b = runner.getManager()
102: .getContext(id2b);
103: assertEquals(4, context2.getLocalVars().getInt(1));
104:
105: String id3ab = runner.resume(id2a);
106: assertNotNull(id3ab);
107: assertFalse(id3aa.equals(id3ab));
108: ContinuationContext context3ab = runner.getManager()
109: .getContext(id3ab);
110: assertEquals(3, context3ab.getLocalVars().getInt(1));
111:
112: String id5aa = runner.resume(id4aa);
113: assertNotNull(id5aa);
114: ContinuationContext context5aa = runner.getManager()
115: .getContext(id5aa);
116: assertEquals(1, context5aa.getLocalVars().getInt(1));
117:
118: String id3b = runner.resume(id2b);
119: assertNotNull(id3b);
120: assertFalse(id3aa.equals(id3b));
121: assertFalse(id3ab.equals(id3b));
122: ContinuationContext context3b = runner.getManager()
123: .getContext(id3b);
124: assertEquals(3, context3b.getLocalVars().getInt(1));
125:
126: String id4ab = runner.resume(id3ab);
127: assertNotNull(id4ab);
128: assertFalse(id4aa.equals(id4ab));
129: ContinuationContext context4ab = runner.getManager()
130: .getContext(id4ab);
131: assertEquals(2, context4ab.getLocalVars().getInt(1));
132:
133: String id4b = runner.resume(id3b);
134: assertNotNull(id4b);
135: assertFalse(id4aa.equals(id4b));
136: assertFalse(id4ab.equals(id4b));
137: ContinuationContext context4b = runner.getManager()
138: .getContext(id4b);
139: assertEquals(2, context4b.getLocalVars().getInt(1));
140:
141: String id6aa = runner.resume(id5aa);
142: assertNull(id6aa);
143:
144: String id5b = runner.resume(id4b);
145: assertNotNull(id5b);
146: assertFalse(id5aa.equals(id5b));
147: ContinuationContext context5b = runner.getManager()
148: .getContext(id5b);
149: assertEquals(1, context5b.getLocalVars().getInt(1));
150:
151: String id5ab = runner.resume(id4ab);
152: assertNotNull(id5ab);
153: assertFalse(id5aa.equals(id5ab));
154: assertFalse(id5b.equals(id5ab));
155: ContinuationContext context5ab = runner.getManager()
156: .getContext(id5ab);
157: assertEquals(1, context5ab.getLocalVars().getInt(1));
158:
159: String id6ab = runner.resume(id5ab);
160: assertNull(id6ab);
161:
162: String id6b = runner.resume(id5b);
163: assertNull(id6b);
164: }
165: }
166:
167: public static class PauseInWhile extends AbstractContinuableObject {
168: public void execute() {
169: int count = 5;
170:
171: while (count > 0) {
172: pause();
173: count--;
174: }
175: }
176: }
177:
178: public static class PauseInWhileInterface implements
179: ContinuableObject, ContinuableSupportAware {
180: private ContinuableSupport mSupport;
181:
182: public void setContinuableSupport(ContinuableSupport support) {
183: mSupport = support;
184: }
185:
186: public Object clone() throws CloneNotSupportedException {
187: return super .clone();
188: }
189:
190: public void execute() {
191: int count = 5;
192:
193: while (count > 0) {
194: mSupport.pause();
195: count--;
196: }
197: }
198: }
199: }
|