001: package net.sf.mockcreator.utils;
002:
003: import java.lang.reflect.Array;
004: import java.util.Collection;
005: import java.util.Date;
006: import java.util.Iterator;
007: import java.util.Map;
008: import java.util.Set;
009: import java.util.Map.Entry;
010:
011: import net.sf.mockcreator.MockBase;
012: import net.sf.mockcreator.expectable.ExpectableParameter;
013:
014: public class CompareByValue {
015: private static long timeTolerance = 0l;
016:
017: public static boolean equals(Object expected, Object actual) {
018:
019: if (expected == actual)
020: return true;
021:
022: if (expected == null && actual == null) {
023: return true;
024: }
025:
026: if ((expected != null) && (actual != null)
027: && expected.getClass().isArray()
028: && actual.getClass().isArray()) {
029: return compareArrays(expected, actual);
030: } else if (expected instanceof Set && actual instanceof Set) {
031: Set ec = (Set) expected;
032: Set ac = (Set) actual;
033: return compareSets(ec, ac);
034: } else if (expected instanceof Map && actual instanceof Map) {
035: Map ec = (Map) expected;
036: Map ac = (Map) actual;
037: return compareMaps(ec, ac);
038: } else if (expected instanceof Collection
039: && actual instanceof Collection) {
040: Collection ec = (Collection) expected;
041: Collection ac = (Collection) actual;
042: return compareCollections(ec, ac);
043: } else if (expected instanceof ExpectableParameter) {
044: ExpectableParameter ev = (ExpectableParameter) expected;
045: return ev.isExpected(actual);
046: } else if (actual instanceof ExpectableParameter) {
047: ExpectableParameter av = (ExpectableParameter) actual;
048: return av.isExpected(expected);
049: } else if (actual instanceof MockBase
050: && expected instanceof MockBase) {
051: return compareMockBase(expected, actual);
052: } else if (timeTolerance != 0 && actual instanceof Date
053: && expected instanceof Date) {
054: long te = ((Date) expected).getTime();
055: long ta = ((Date) actual).getTime();
056: if (Math.abs(te - ta) <= timeTolerance)
057: return true;
058: } else if ((expected != null) && expected.equals(actual)) {
059: return true;
060: } else if ((actual != null) && actual.equals(expected)) {
061: return true;
062: }
063:
064: return false;
065: }
066:
067: private static boolean compareMockBase(Object expected,
068: Object actual) {
069: MockBase av = (MockBase) actual;
070: MockBase ev = (MockBase) expected;
071: if (av.__id() != null && ev.__id() != null
072: && av.__id().equals(ev.__id()))
073: return true;
074: return false;
075: }
076:
077: static boolean compareArrays(Object expected, Object actual) {
078: Class ec = expected.getClass();
079: Class ac = actual.getClass();
080:
081: if (!ec.equals(ac)) {
082: return false;
083: }
084:
085: int len = Array.getLength(expected);
086: if (Array.getLength(actual) != len) {
087: return false;
088: }
089:
090: for (int n = 0; n < len; ++n) {
091: Object el1 = Array.get(expected, n);
092: Object el2 = Array.get(actual, n);
093: if (!equals(el1, el2)) {
094: return false;
095: }
096: }
097:
098: return true;
099: }
100:
101: static boolean compareCollections(Collection ec, Collection ac) {
102: if (ec.size() != ac.size()) {
103: return false;
104: }
105:
106: Iterator ei = ec.iterator();
107: Iterator ai = ac.iterator();
108:
109: while (ei.hasNext()) {
110: if (!equals(ei.next(), ai.next())) {
111: return false;
112: }
113: }
114:
115: return true;
116: }
117:
118: static boolean compareSets(Set ec, Set ac) {
119: if (ec.size() != ac.size()) {
120: return false;
121: }
122:
123: return containsAll(ec, ac) && containsAll(ac, ec);
124: }
125:
126: static boolean compareMaps(Map ec, Map ac) {
127: if (ec.size() != ac.size()) {
128: return false;
129: }
130:
131: return containsAllEntries(ec, ac) && containsAllEntries(ac, ec);
132: }
133:
134: private static boolean containsAll(Set one, Set another) {
135: Iterator oi = one.iterator();
136: while (oi.hasNext()) {
137: Object key = oi.next();
138:
139: // we cannot use Set.contains due to possibility of unfitting hash
140: // functions.
141: if (!containsKey(another, key))
142: return false;
143: }
144: return true;
145: }
146:
147: static boolean containsAllEntries(Map one, Map another) {
148: Iterator oi = one.entrySet().iterator();
149: while (oi.hasNext()) {
150: Map.Entry oo = (Map.Entry) oi.next();
151:
152: if (!containsEntry(another, oo))
153: return false;
154: }
155:
156: return true;
157: }
158:
159: private static boolean containsEntry(Map another, Map.Entry oo) {
160: Iterator ai = another.entrySet().iterator();
161: while (ai.hasNext()) {
162: Map.Entry ao = (Entry) ai.next();
163:
164: if (equals(oo.getKey(), ao.getKey())
165: && equals(oo.getValue(), ao.getValue())) {
166: return true;
167: }
168: }
169: return false;
170: }
171:
172: private static boolean containsKey(Set another, Object key) {
173: Iterator ai = another.iterator();
174: while (ai.hasNext()) {
175: Object key2 = ai.next();
176: if (equals(key, key2)) {
177: return true;
178: }
179: }
180: return false;
181: }
182:
183: public static void setTimeTolerance(long milliseconds) {
184: CompareByValue.timeTolerance = milliseconds;
185: }
186: }
|