KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > jmock > builder > InvocationMockerDescriberTest


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

3 package test.jmock.builder;
4
5 import java.util.ArrayList JavaDoc;
6 import java.util.List JavaDoc;
7
8 import junit.framework.TestCase;
9
10 import org.jmock.builder.InvocationMockerDescriber;
11 import org.jmock.core.Constraint;
12 import org.jmock.core.matcher.ArgumentsMatcher;
13 import org.jmock.core.matcher.MethodNameMatcher;
14 import org.jmock.util.Verifier;
15
16 import test.jmock.core.testsupport.MockConstraint;
17 import test.jmock.core.testsupport.MockInvocationMatcher;
18 import test.jmock.core.testsupport.MockStub;
19
20
21 public class InvocationMockerDescriberTest extends TestCase
22 {
23     static final String JavaDoc METHOD_NAME = "METHOD-NAME";
24
25     InvocationMockerDescriber describer;
26     List JavaDoc matchers;
27     MethodNameMatcher methodNameMatcher;
28     Constraint arg1;
29     Constraint arg2;
30     ArgumentsMatcher argumentsMatcher;
31     MockInvocationMatcher expectation;
32     MockStub stub;
33     String JavaDoc name;
34     StringBuffer JavaDoc buffer;
35
36
37     public void setUp() {
38         describer = new InvocationMockerDescriber();
39
40         methodNameMatcher = new MethodNameMatcher(METHOD_NAME);
41         arg1 = new MockConstraint("ARG1");
42         arg2 = new MockConstraint("ARG2");
43         argumentsMatcher = new ArgumentsMatcher(new Constraint[]{arg1, arg2});
44         matchers = new ArrayList JavaDoc();
45         expectation = new MockInvocationMatcher();
46         expectation.describeToOutput = "EXPECTATION";
47         stub = new MockStub();
48         stub.describeToOutput = "STUB";
49         name = null;
50         buffer = new StringBuffer JavaDoc();
51     }
52
53     public void testDescribesLeadingExpectation() {
54         matchers.add(expectation);
55         matchers.add(methodNameMatcher);
56         matchers.add(argumentsMatcher);
57
58         describer.describeTo(buffer, matchers, stub, name);
59
60         assertEquals("EXPECTATION: METHOD-NAME( ARG1, ARG2 ), STUB",
61                      buffer.toString());
62
63         Verifier.verifyObject(this);
64     }
65
66     public void testDescribesLeadingStub() {
67         matchers.add(methodNameMatcher);
68         matchers.add(argumentsMatcher);
69
70         describer.describeTo(buffer, matchers, stub, name);
71
72         assertEquals("stub: METHOD-NAME( ARG1, ARG2 ), STUB",
73                      buffer.toString());
74
75         Verifier.verifyObject(this);
76     }
77
78     public void testAppendsEmphasisedName() {
79         matchers.add(expectation);
80         matchers.add(methodNameMatcher);
81         matchers.add(argumentsMatcher);
82
83         name = "NAME";
84
85         describer.describeTo(buffer, matchers, stub, name);
86
87         assertEquals("EXPECTATION: METHOD-NAME( ARG1, ARG2 ), STUB [NAME]",
88                      buffer.toString());
89
90         Verifier.verifyObject(this);
91     }
92
93     public void testDescribesAdditionalMatchersWithAppropriatePunctuation() {
94         MockInvocationMatcher otherMatcher1 = new MockInvocationMatcher();
95         otherMatcher1.describeToOutput = "OTHER-MATCHER#1";
96         MockInvocationMatcher otherMatcher2 = new MockInvocationMatcher();
97         otherMatcher2.describeToOutput = "OTHER-MATCHER#2";
98
99         matchers.add(expectation);
100         matchers.add(methodNameMatcher);
101         matchers.add(argumentsMatcher);
102         matchers.add(otherMatcher1);
103         matchers.add(otherMatcher2);
104
105         describer.describeTo(buffer, matchers, stub, name);
106
107         assertEquals("EXPECTATION: METHOD-NAME( ARG1, ARG2 ), OTHER-MATCHER#1, OTHER-MATCHER#2, STUB",
108                      buffer.toString());
109
110         Verifier.verifyObject(this);
111     }
112
113     public void testIgnoresHiddenMatchers() {
114         MockInvocationMatcher hidden1 = new MockInvocationMatcher();
115         hidden1.describeToOutput = "";
116         MockInvocationMatcher hidden2 = new MockInvocationMatcher();
117         hidden2.describeToOutput = "";
118
119         matchers.add(expectation);
120         matchers.add(methodNameMatcher);
121         matchers.add(argumentsMatcher);
122         matchers.add(hidden1);
123         matchers.add(hidden2);
124
125         describer.describeTo(buffer, matchers, stub, name);
126
127         assertEquals("EXPECTATION: METHOD-NAME( ARG1, ARG2 ), STUB",
128                      buffer.toString());
129
130         Verifier.verifyObject(this);
131     }
132
133     public void testAddsSeparatorAfterMethodNameIfNoArgumentsMatcher() {
134         MockInvocationMatcher otherMatcher = new MockInvocationMatcher();
135         otherMatcher.describeToOutput = "OTHER-MATCHER";
136
137         matchers.add(expectation);
138         matchers.add(methodNameMatcher);
139         matchers.add(otherMatcher);
140
141         describer.describeTo(buffer, matchers, stub, name);
142
143         assertEquals("EXPECTATION: METHOD-NAME, OTHER-MATCHER, STUB",
144                      buffer.toString());
145         
146         Verifier.verifyObject(this);
147     }
148 }
149
Popular Tags