KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jmock > builder > InvocationMockerDescriber


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

3 package org.jmock.builder;
4
5 import java.util.Iterator JavaDoc;
6 import java.util.List JavaDoc;
7 import org.jmock.core.InvocationMatcher;
8 import org.jmock.core.InvocationMocker.Describer;
9 import org.jmock.core.Stub;
10 import org.jmock.core.matcher.MethodNameMatcher;
11 import org.jmock.core.matcher.ArgumentsMatcher;
12
13
14 /* This encapsulates knowledge about how the InvocationMockerBuilder class
15  * adds matchers to an InvocationMocker. Therefore it should be considered
16  * to be part of the implementation of the InvocationMockerBuilder class.
17  */

18
19 public class InvocationMockerDescriber implements Describer
20 {
21     private static final String JavaDoc SEP = ", ";
22
23     public boolean hasDescription() {
24         return true;
25     }
26
27     public void describeTo( StringBuffer JavaDoc buffer,
28                             List JavaDoc matchers, Stub stub, String JavaDoc name ) {
29         Iterator JavaDoc i = matchers.iterator();
30         boolean needSeparator = false;
31         boolean lastWasMethodName = false;
32
33         while (i.hasNext()) {
34             InvocationMatcher matcher = (InvocationMatcher)i.next();
35
36             if (!matcher.hasDescription()) continue;
37
38             if (matcher instanceof MethodNameMatcher) {
39                 if (!needSeparator) buffer.append("stub"); // first matcher
40
buffer.append(": ");
41                 lastWasMethodName = true;
42             } else if( matcher instanceof ArgumentsMatcher ) {
43                 if (needSeparator && !lastWasMethodName) buffer.append(SEP);
44                 lastWasMethodName = false;
45
46             } else {
47                 if (needSeparator) buffer.append(SEP);
48                 lastWasMethodName = false;
49             }
50             
51             matcher.describeTo(buffer);
52             needSeparator = true;
53         }
54
55         if (needSeparator) buffer.append(SEP);
56         stub.describeTo(buffer);
57
58         if (name != null) buffer.append(" [").append(name).append("]");
59     }
60 }
61
Popular Tags