01: package net.sf.mockcreator;
02:
03: import java.util.Iterator;
04: import java.util.List;
05:
06: import net.sf.mockcreator.expectable.ExpectableParameter;
07: import net.sf.mockcreator.utils.CompareByValue;
08:
09: public class ExpectationWithParams extends Expectation {
10:
11: public ExpectationWithParams(String marker, List params,
12: Object returnValue) {
13: this .marker = marker;
14: this .params = params;
15: this .returnValue = returnValue;
16: }
17:
18: public ExpectationWithParams(String marker, List params) {
19: this (marker, params, null);
20: }
21:
22: public boolean equals(Object obj) {
23:
24: if (obj == this ) {
25: return true;
26: }
27:
28: if (!(obj instanceof IExpectation)) {
29: return false;
30: }
31:
32: IExpectation another = (IExpectation) obj;
33:
34: if (!CompareByValue.equals(getMarker(), another.getMarker()))
35: return false;
36: if (!CompareByValue.equals(getParams(), another.getParams()))
37: return false;
38:
39: return true;
40: }
41:
42: // FIXME: more tests for this!
43: public int compareTo(Object o) {
44: if (o instanceof ExpectationWithParams) {
45: ExpectationWithParams ewp = (ExpectationWithParams) o;
46: List ewpp = ewp.getParams();
47:
48: // one having more params is first
49: if (ewpp == null && params != null)
50: return -1;
51: if (ewpp != null && params == null)
52: return 1;
53: if (ewpp == null && params == null)
54: return 0;
55:
56: if (ewpp.size() > params.size())
57: return 1;
58: if (ewpp.size() < params.size())
59: return -1;
60:
61: int ewppExactCount = getExactCount(ewpp);
62: int myExactCount = getExactCount(params);
63:
64: if (ewppExactCount < myExactCount)
65: return -1;
66: if (ewppExactCount > myExactCount)
67: return 1;
68:
69: return 0;
70: }
71:
72: return -1;
73: }
74:
75: private int getExactCount(List lst) {
76: int count = lst.size();
77: for (Iterator iter = lst.iterator(); iter.hasNext();) {
78: if (iter.next() instanceof ExpectableParameter)
79: count--;
80: }
81: return count;
82: }
83: }
|