KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > easymock > internal > ResultListMap


1 /*
2  * Copyright (c) 2001-2005 OFFIS. This program is made available under the terms of
3  * the MIT License.
4  */

5 package org.easymock.internal;
6
7 import java.lang.reflect.Method JavaDoc;
8 import java.util.ArrayList JavaDoc;
9 import java.util.List JavaDoc;
10
11 import junit.framework.AssertionFailedError;
12
13 import org.easymock.ArgumentsMatcher;
14
15 public class ResultListMap {
16
17     private final List JavaDoc<ArgumentsEntry> results = new ArrayList JavaDoc<ArgumentsEntry>();
18
19     private final ArgumentsMatcher matcher;
20
21     private final Method JavaDoc method;
22
23     private static class ArgumentsEntry {
24         ExpectedMethodCall matchableArguments;
25
26         ResultList resultList;
27
28         ArgumentsEntry(ExpectedMethodCall key, ResultList value) {
29             this.matchableArguments = key;
30             this.resultList = value;
31         }
32
33         public ExpectedMethodCall getMatchableArguments() {
34             return matchableArguments;
35         }
36
37         public ResultList getResultList() {
38             return resultList;
39         }
40     }
41
42     public ResultListMap(Method JavaDoc method, ArgumentsMatcher matcher) {
43         this.method = method;
44         this.matcher = matcher;
45     }
46
47     public void addExpected(Object JavaDoc[] expected, Result result, Range count) {
48         ExpectedMethodCall matchableArguments = new ExpectedMethodCall(method,
49                 expected, matcher);
50
51         for (ArgumentsEntry entry : results) {
52             if (entry.getMatchableArguments().equals(matchableArguments)) {
53                 entry.getResultList().add(result, count);
54                 return;
55             }
56         }
57         ResultList list = new ResultList();
58         list.add(result, count);
59         results.add(new ArgumentsEntry(matchableArguments, list));
60     }
61
62     public Result addActual(Object JavaDoc[] arguments) {
63         boolean matched = false;
64
65         for (ArgumentsEntry entry : results) {
66             ExpectedMethodCall matchableArguments = entry
67                     .getMatchableArguments();
68
69             if (matchableArguments.matches(method, arguments)) {
70                 matched = true;
71                 Result result = entry.getResultList().next();
72                 if (result != null) {
73                     return result;
74                 }
75             }
76         }
77
78         throw new AssertionFailedErrorWrapper(new AssertionFailedError(
79                 createFailureMessage(arguments, matched)));
80     }
81
82     private String JavaDoc createFailureMessage(Object JavaDoc[] actual, boolean matched) {
83         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
84
85         if (!matched) {
86             result.append("\n ");
87             result.append(new MethodCall(method, actual).toString(matcher));
88             result.append(": ");
89             result.append(new Range(7).expectedAndActual(1).replace('7', '0'));
90         }
91
92         for (ArgumentsEntry entry : results) {
93             ExpectedMethodCall matchableArguments = entry
94                     .getMatchableArguments();
95             ResultList list = (ResultList) entry.getResultList();
96
97             if (list.hasValidCallCount()
98                     && !matchableArguments.matches(method, actual)) {
99                 continue;
100             }
101
102             int count = list.getCallCount();
103
104             if (matched && matchableArguments.matches(method, actual)) {
105                 count += 1;
106                 matched = false;
107             }
108             result.append("\n ");
109             result.append(matchableArguments.toString());
110             result.append(": ");
111             result.append(list.getMessage(count));
112         }
113
114         return result.toString();
115     }
116
117     public void verify() {
118         String JavaDoc failureMessage = "";
119         boolean verifyFailed = false;
120
121         for (ArgumentsEntry entry : results) {
122             ExpectedMethodCall matchableArguments = entry
123                     .getMatchableArguments();
124             ResultList list = (ResultList) entry.getResultList();
125
126             if (list.hasValidCallCount()) {
127                 continue;
128             }
129             verifyFailed = true;
130             failureMessage += "\n " + matchableArguments.toString() + ": "
131                     + list.getMessage();
132         }
133         if (verifyFailed) {
134             throw new AssertionFailedError(failureMessage);
135         }
136     }
137 }
Popular Tags