KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > jmock > examples > timedcache > StubsSequence


1 /* Copyright (c) 2000-2004 jMock.org
2  */

3 package test.jmock.examples.timedcache;
4
5 import java.util.ArrayList JavaDoc;
6 import java.util.Iterator JavaDoc;
7 import java.util.List JavaDoc;
8 import junit.framework.AssertionFailedError;
9 import org.jmock.core.Invocation;
10 import org.jmock.core.Stub;
11
12
13 public class StubsSequence
14         implements Stub
15 {
16     List JavaDoc stubs;
17     Iterator JavaDoc iterator;
18
19     static public List JavaDoc asList( Stub stub1, Stub stub2 ) {
20         ArrayList JavaDoc list = new ArrayList JavaDoc();
21         list.add(stub1);
22         list.add(stub2);
23         return list;
24     }
25
26     static public List JavaDoc asList( Stub stub1, Stub stub2, Stub stub3 ) {
27         ArrayList JavaDoc list = new ArrayList JavaDoc();
28         list.add(stub1);
29         list.add(stub2);
30         list.add(stub3);
31         return list;
32     }
33
34     public StubsSequence( List JavaDoc stubs ) {
35         this.stubs = stubs;
36         iterator = this.stubs.iterator();
37     }
38
39     public Object JavaDoc invoke( Invocation invocation ) throws Throwable JavaDoc {
40         if (iterator.hasNext()) {
41             return ((Stub)iterator.next()).invoke(invocation);
42         }
43         throw new AssertionFailedError("No more stubs available.");
44     }
45
46     public StringBuffer JavaDoc describeTo( StringBuffer JavaDoc buffer ) {
47         Iterator JavaDoc sequence = stubs.iterator();
48         String JavaDoc filler = "";
49         while (sequence.hasNext()) {
50             buffer.append(filler);
51             ((Stub)sequence.next()).describeTo(buffer);
52             filler = ", then ";
53         }
54         return buffer;
55     }
56
57 }
58
Popular Tags