001: /*
002: * Copyright 2006-2007, Unitils.org
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.unitils.easymock.util;
017:
018: import junit.framework.TestCase;
019: import static org.easymock.EasyMock.*;
020: import static org.unitils.easymock.EasyMockUnitils.lenEq;
021: import static org.unitils.easymock.EasyMockUnitils.refEq;
022: import static org.unitils.reflectionassert.ReflectionComparatorMode.IGNORE_DEFAULTS;
023: import static org.unitils.reflectionassert.ReflectionComparatorMode.LENIENT_ORDER;
024:
025: import java.util.Arrays;
026: import java.util.List;
027:
028: /**
029: * Test for {@link org.unitils.easymock.util.ReflectionArgumentMatcher}.
030: */
031: public class ReflectionArgumentMatcherTest extends TestCase {
032:
033: /* A test mock instance */
034: private TestMock testMock;
035:
036: /**
037: * Initializes the test fixture.
038: */
039: protected void setUp() throws Exception {
040: super .setUp();
041:
042: testMock = createMock(TestMock.class);
043: }
044:
045: /**
046: * Tests the refEq argument matcher with strict mode and equal values.
047: */
048: public void testRefEq() {
049:
050: testMock.method(refEq("stringValue"), refEq(3),
051: refEq("objectValue1"), refEq("objectValue2"));
052: replay(testMock);
053:
054: testMock.method("stringValue", 3, "objectValue1",
055: "objectValue2");
056: verify(testMock);
057: }
058:
059: /**
060: * Tests the refEq argument matcher with strict mode and different values.
061: */
062: public void testRefEq_notEquals() {
063:
064: testMock.method(refEq("stringValue"), refEq(3),
065: refEq("objectValue1"), refEq("objectValue2"));
066: replay(testMock);
067:
068: try {
069: testMock.method("xxxx", 3, "objectValue1", "objectValue2");
070: fail("Expected AssertionError");
071:
072: } catch (AssertionError e) {
073: //expected
074: }
075: }
076:
077: /**
078: * Tests the refEq argument matcher with strict mode and different vararg values.
079: */
080: public void testRefEq_notEqualsVarArgs() {
081:
082: testMock.method(refEq("stringValue"), refEq(3),
083: refEq("objectValue1"), refEq("objectValue2"));
084: replay(testMock);
085:
086: try {
087: testMock.method("stringValue", 3, "objectValue1");
088: fail("Expected AssertionError");
089:
090: } catch (AssertionError e) {
091: //expected
092: }
093: }
094:
095: /**
096: * Tests the refEq argument matcher with lenient order mode and lists having a different order.
097: */
098: public void testRefEq_equalsLenientOrder() {
099:
100: testMock.method(refEq(Arrays.asList("element1", "element2",
101: "element3"), LENIENT_ORDER));
102: replay(testMock);
103:
104: testMock.method(Arrays.asList("element3", "element1",
105: "element2"));
106: verify(testMock);
107: }
108:
109: /**
110: * Tests the lenEq argument matcher with lists having a different order.
111: * This should be the same as refEq with lenient order and ignore defaults.
112: */
113: public void testLenEq() {
114:
115: testMock.method(lenEq(Arrays.asList("element1", "element2",
116: "element3")));
117: replay(testMock);
118:
119: testMock.method(Arrays.asList("element3", "element1",
120: "element2"));
121: verify(testMock);
122: }
123:
124: /**
125: * Tests the refEq argument matcher with strict order mode and lists having a different order.
126: */
127: public void testRefEq_notEqualsStrictOrder() {
128:
129: testMock.method(refEq(Arrays.asList("element1", "element2",
130: "element3")));
131: replay(testMock);
132:
133: try {
134: testMock.method(Arrays.asList("element3", "element1",
135: "element2"));
136: fail("Expected AssertionError");
137:
138: } catch (AssertionError e) {
139: //expected
140: }
141: }
142:
143: /**
144: * Tests the refEq argument matcher with ignore defaults mode and null default value.
145: */
146: public void testRefEq_equalsIgnoreDefaults() {
147:
148: testMock.method(refEq((List<String>) null, IGNORE_DEFAULTS));
149: replay(testMock);
150:
151: testMock.method(Arrays.asList("element3", "element1",
152: "element2"));
153: verify(testMock);
154: }
155:
156: /**
157: * Tests the refEq argument matcher without ignore defaults mode and null default value.
158: */
159: public void testRefEq_notEqualsNoIgnoreDefaults() {
160:
161: testMock.method(refEq((List<String>) null));
162: replay(testMock);
163:
164: try {
165: testMock.method(Arrays.asList("element3", "element1",
166: "element2"));
167: fail("Expected AssertionError");
168:
169: } catch (AssertionError e) {
170: //expected
171: }
172: }
173:
174: /**
175: * Test interface that is going to be mocked.
176: */
177: private interface TestMock {
178:
179: public void method(String arg1, int arg2, Object... arg3);
180:
181: public void method(List<String> arg1);
182:
183: }
184:
185: }
|