01: /*
02: * Copyright 2006-2007, Unitils.org
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package org.unitils.easymock.util;
17:
18: import org.easymock.internal.matchers.Equals;
19: import org.unitils.reflectionassert.ReflectionComparator;
20: import org.unitils.reflectionassert.ReflectionComparatorMode;
21: import org.unitils.reflectionassert.ReflectionComparatorChainFactory;
22:
23: /**
24: * An easy mock argument matcher to check whether 2 objects are equal by comparing all fields of the objects using
25: * reflection.
26: * <p/>
27: * The (combination of) comparator modes specify how strict the comparison must be:<ul>
28: * <li>ignore defaults: compare only arguments (and inner values) that have a non default value (eg null) as exepected value</li>
29: * <li>lenient dates: do not compare actual date values, just that they both have a value or not</li>
30: * <li>lenient order: order is not important when comparing collections or arrays</li>
31: * </ul>
32: *
33: * @author Tim Ducheyne
34: * @author Filip Neven
35: * @see ReflectionComparator
36: * @see ReflectionComparatorMode
37: */
38: public class ReflectionArgumentMatcher<T> extends Equals {
39:
40: /* The comparator for lenient comparing expected and actual argument values */
41: private ReflectionComparator reflectionComparator;
42:
43: /**
44: * Creates a matcher for the expected argument value.
45: * The modes specify how to compare the expected value with the actual value.
46: *
47: * @param expected the argument value, not null
48: * @param modes the comparator modes
49: */
50: public ReflectionArgumentMatcher(T expected,
51: ReflectionComparatorMode... modes) {
52: super (expected);
53: this .reflectionComparator = ReflectionComparatorChainFactory
54: .getComparatorChainForModes(modes);
55: }
56:
57: /**
58: * Checks whether the given actual value is equal to the expected value.
59: * A reflection comparator is used to compare both values. The modes determine how
60: * strict this comparison will be.
61: * <p/>
62: * An assertion error is thrown if expected and actual did not match. This way, a more
63: * detailed message can be provided.
64: *
65: * @param actual the actual argument value
66: * @return true
67: * @throws AssertionError in case expected and actual did not match
68: */
69: @Override
70: public boolean matches(Object actual) {
71: return reflectionComparator.isEqual(getExpected(), actual);
72: }
73:
74: }
|