KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > jmock > core > matcher > InvokedAfterMatcherTest


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

3 package test.jmock.core.matcher;
4
5 import junit.framework.AssertionFailedError;
6 import junit.framework.TestCase;
7 import org.jmock.core.Invocation;
8 import org.jmock.core.matcher.InvokedAfterMatcher;
9 import org.jmock.core.matcher.InvokedRecorder;
10 import test.jmock.core.testsupport.MethodFactory;
11
12
13 public class InvokedAfterMatcherTest extends TestCase
14 {
15     private static final String JavaDoc PRIOR_CALL_ID = "RECORDER-ID";
16
17     private Invocation invocation1;
18     private Invocation invocation2;
19     private InvokedRecorder recorder;
20     private InvokedAfterMatcher after;
21
22
23     public void setUp() {
24         MethodFactory methodFactory = new MethodFactory();
25
26         invocation1 = new Invocation("INVOKED-OBJECT-1",
27                                      methodFactory.newMethod("invocation1", MethodFactory.NO_ARGUMENTS,
28                                                              Void JavaDoc.class, MethodFactory.NO_EXCEPTIONS),
29                                      new Object JavaDoc[0]);
30
31         invocation2 = new Invocation("INVOKED-OBJECT-2",
32                                      methodFactory.newMethod("invocation2", MethodFactory.NO_ARGUMENTS,
33                                                              Void JavaDoc.class, MethodFactory.NO_EXCEPTIONS),
34                                      new Object JavaDoc[0]);
35
36         recorder = new InvokedRecorder();
37         after = new InvokedAfterMatcher(recorder, PRIOR_CALL_ID);
38     }
39
40     public void testOnlyMatchesWhenInvokedAfterPriorInvocation() {
41         assertFalse("should not match before previous invocation",
42                     after.matches(invocation2));
43         recorder.invoked(invocation1);
44         assertTrue("should match after previous invocation",
45                    after.matches(invocation2));
46     }
47
48     // Note: this will not happen in typical usage because it will not match out of order
49
public void testFailsIfInvokedOutOfOrder() {
50         try {
51             after.invoked(invocation2);
52         }
53         catch (AssertionFailedError ex) {
54             assertTrue("should contain description of prior call",
55                        ex.getMessage().indexOf(PRIOR_CALL_ID) >= 0);
56             return;
57         }
58         fail("AssertionFailedError expected");
59     }
60
61     public void testIdentifiesPriorCallInDescription() {
62         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
63
64         after.describeTo(buf);
65
66         assertTrue("should include 'after <id of prior call>' in description",
67                    buf.toString().indexOf("after " + PRIOR_CALL_ID) >= 0);
68     }
69
70     public void testIncludesWhetherPriorCallHasOccurredInDescription() {
71         String JavaDoc description;
72
73         description = after.describeTo(new StringBuffer JavaDoc()).toString();
74         assertTrue( "should include \"(not invoked)\"",
75                     description.indexOf("(not invoked)") >= 0 );
76
77         recorder.invoked(invocation1);
78         description = after.describeTo(new StringBuffer JavaDoc()).toString();
79         assertTrue( "should include \"(invoked)\"",
80                     description.indexOf("(invoked)") >= 0 );
81
82     }
83 }
84
Popular Tags