KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > jmock > core > stub > StubSequenceTest


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

3 package test.jmock.core.stub;
4
5 import junit.framework.AssertionFailedError;
6 import junit.framework.TestCase;
7 import org.jmock.core.Invocation;
8 import org.jmock.core.stub.StubSequence;
9 import org.jmock.expectation.AssertMo;
10 import test.jmock.core.testsupport.MethodFactory;
11 import test.jmock.core.testsupport.MockStub;
12
13
14 public class StubSequenceTest extends TestCase
15 {
16     private Object JavaDoc invokedObject = new Object JavaDoc()
17     {
18         public String JavaDoc toString() {
19             return "INVOKED_OBJECT";
20         }
21     };
22     private MethodFactory methodFactory = new MethodFactory();
23     private Invocation invocation =
24             new Invocation(invokedObject, methodFactory.newMethodReturning(String JavaDoc.class), null);
25
26     static final String JavaDoc RESULT1 = "RESULT1";
27     static final String JavaDoc RESULT2 = "RESULT2";
28     static final String JavaDoc RESULT3 = "RESULT3";
29
30     public void testInvokesStubsInOrder() throws Throwable JavaDoc {
31         MockStub[] stubs = new MockStub[4];
32
33         for (int i = 0; i < stubs.length; i++) {
34             stubs[i] = new MockStub();
35             stubs[i].invokeResult = "RESULT-" + i;
36         }
37
38         StubSequence sequence = new StubSequence(stubs);
39
40         for (int current = 0; current < stubs.length; current++) {
41             reset(stubs);
42
43             stubs[current].invokeInvocation.setExpected(invocation);
44
45             assertEquals("should be result of stubs[" + current + "]",
46                          stubs[current].invokeResult, sequence.invoke(invocation));
47
48             stubs[current].verify();
49         }
50     }
51
52     public void testThrowsAssertionFailedErrorIfInvokedMoreTimesThanThereAreStubs() throws Throwable JavaDoc {
53         MockStub[] stubs = new MockStub[]{new MockStub(), new MockStub()};
54         StubSequence sequence = new StubSequence(stubs);
55
56         for (int i = 0; i < stubs.length; i++) sequence.invoke(invocation);
57
58         try {
59             sequence.invoke(invocation);
60         }
61         catch (AssertionFailedError ex) {
62             AssertMo.assertIncludes("should describe error",
63                                     "no more stubs", ex.getMessage());
64         }
65     }
66
67     public void testDescribesItselfAsSequenceOfStubs() throws Throwable JavaDoc {
68         MockStub[] stubs = new MockStub[]{new MockStub(), new MockStub()};
69
70         stubs[0].describeToOutput = "STUB-0";
71         stubs[1].describeToOutput = "STUB-1";
72
73         StubSequence sequence = new StubSequence(stubs);
74
75         String JavaDoc sequenceDescription = sequence.describeTo(new StringBuffer JavaDoc()).toString();
76
77         for (int i = 0; i < stubs.length; i++) {
78             AssertMo.assertIncludes("should include stub " + i,
79                                     stubs[i].describeToOutput, sequenceDescription);
80
81             if (i > 0) {
82                 int h = i - 1;
83
84                 assertTrue("description of stub " + h + " should be before that of stub " + i,
85                            sequenceDescription.indexOf(stubs[h].describeToOutput) <
86                            sequenceDescription.indexOf(stubs[i].describeToOutput));
87             }
88         }
89     }
90
91     private void reset( MockStub[] stubs ) {
92         for (int i = 0; i < stubs.length; i++) {
93             stubs[i].invokeInvocation.setExpectNothing();
94         }
95     }
96 }
97
Popular Tags