KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jmock > core > stub > StubSequence


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

3 package org.jmock.core.stub;
4
5 import java.util.ArrayList JavaDoc;
6 import java.util.Arrays JavaDoc;
7 import java.util.Iterator JavaDoc;
8 import java.util.List JavaDoc;
9 import junit.framework.AssertionFailedError;
10 import org.jmock.core.Invocation;
11 import org.jmock.core.Stub;
12
13
14 public class StubSequence
15         implements Stub
16 {
17     List JavaDoc stubs;
18     Iterator JavaDoc iterator;
19
20
21     public StubSequence( Stub[] stubs ) {
22         this(Arrays.asList(stubs));
23     }
24
25     public StubSequence( List JavaDoc stubs ) {
26         this.stubs = new ArrayList JavaDoc(stubs);
27         this.iterator = this.stubs.iterator();
28     }
29
30     public Object JavaDoc invoke( Invocation invocation ) throws Throwable JavaDoc {
31         if (iterator.hasNext()) {
32             return ((Stub)iterator.next()).invoke(invocation);
33         }
34         throw new AssertionFailedError("no more stubs available");
35     }
36
37     public StringBuffer JavaDoc describeTo( StringBuffer JavaDoc buffer ) {
38         for (int i = 0; i < stubs.size(); i++) {
39             if (i > 0) buffer.append(", and then ");
40             ((Stub)stubs.get(i)).describeTo(buffer);
41         }
42
43         return buffer;
44     }
45 }
46
Popular Tags