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.reflectionassert;
017:
018: import junit.framework.AssertionFailedError;
019: import junit.framework.TestCase;
020: import junitx.framework.StringAssert;
021: import static org.unitils.reflectionassert.ReflectionAssert.assertLenEquals;
022: import static org.unitils.reflectionassert.ReflectionAssert.assertRefEquals;
023: import static org.unitils.reflectionassert.ReflectionComparatorMode.IGNORE_DEFAULTS;
024: import static org.unitils.reflectionassert.ReflectionComparatorMode.LENIENT_ORDER;
025:
026: import static java.util.Arrays.asList;
027:
028: /**
029: * Test class for {@link ReflectionAssert}.
030: *
031: * @author Tim Ducheyne
032: * @author Filip Neven
033: */
034: public class ReflectionAssertTest extends TestCase {
035:
036: /* Test object */
037: private TestObject testObjectA;
038:
039: /* Same as A but different instance */
040: private TestObject testObjectB;
041:
042: /* Same as A and B but different string value for stringValue2 */
043: private TestObject testObjectDifferentValue;
044:
045: /**
046: * Initializes the test fixture.
047: */
048: protected void setUp() throws Exception {
049: super .setUp();
050:
051: testObjectA = new TestObject("test 1", "test 2");
052: testObjectB = new TestObject("test 1", "test 2");
053: testObjectDifferentValue = new TestObject("test 1", "XXXXXX");
054: }
055:
056: /**
057: * Test for two equal objects.
058: */
059: public void testAssertRefEquals_equals() {
060: assertRefEquals(testObjectA, testObjectB);
061: }
062:
063: /**
064: * Test for two equal objects (message version).
065: */
066: public void testAssertRefEquals_equalsMessage() {
067: assertRefEquals("a message", testObjectA, testObjectB);
068: }
069:
070: /**
071: * Test for two equal objects.
072: */
073: public void testAssertLenEquals_equals() {
074: assertLenEquals(testObjectA, testObjectB);
075: }
076:
077: /**
078: * Test for two equal objects (message version).
079: */
080: public void testAssertLenEquals_equalsMessage() {
081: assertLenEquals("a message", testObjectA, testObjectB);
082: }
083:
084: /**
085: * Test for two objects that contain different values.
086: */
087: public void testAssertRefEquals_notEqualsDifferentValues() {
088: String message = null;
089: try {
090: assertRefEquals(testObjectA, testObjectDifferentValue);
091:
092: } catch (AssertionFailedError a) {
093: message = a.getMessage();
094: }
095:
096: assertNotNull("An assertion exception should have been thrown",
097: message);
098: StringAssert.assertContains("string2", message);
099: StringAssert.assertContains("XXXXXX", message);
100: StringAssert.assertContains("test 2", message);
101: }
102:
103: /**
104: * Test case for a null left-argument.
105: */
106: public void testAssertRefEquals_leftNull() {
107: try {
108: assertRefEquals(null, testObjectA);
109: fail("Expected AssertionFailedError");
110:
111: } catch (AssertionFailedError a) {
112: // expected
113: }
114: }
115:
116: /**
117: * Test case for a null right-argument.
118: */
119: public void testAssertRefEquals_rightNull() {
120: try {
121: assertRefEquals(testObjectA, null);
122: fail("Expected AssertionFailedError");
123:
124: } catch (AssertionFailedError a) {
125: // expected
126: }
127: }
128:
129: /**
130: * Test case for both null arguments.
131: */
132: public void testAssertRefEquals_null() {
133: assertRefEquals(null, null);
134: }
135:
136: /**
137: * Test for two equal collections but with different order.
138: */
139: public void testAssertRefEquals_equalsLenientOrder() {
140: assertRefEquals(asList("element1", "element2", "element3"),
141: asList("element3", "element1", "element2"),
142: LENIENT_ORDER);
143: }
144:
145: /**
146: * Test for two equal collections but with different order.
147: */
148: public void testAssertLenEquals_equalsLenientOrder() {
149: assertLenEquals(asList("element1", "element2", "element3"),
150: asList("element3", "element1", "element2"));
151: }
152:
153: /**
154: * Test for ignored default left value.
155: */
156: public void testAssertRefEquals_equalsIgnoredDefault() {
157: testObjectA.setString1(null);
158: testObjectB.setString1("xxxxxx");
159:
160: assertRefEquals(testObjectA, testObjectB, IGNORE_DEFAULTS);
161: }
162:
163: /**
164: * Test for ignored default left value.
165: */
166: public void testAssertLenEquals_equalsIgnoredDefault() {
167: testObjectA.setString1(null);
168: testObjectB.setString1("xxxxxx");
169:
170: assertLenEquals(testObjectA, testObjectB);
171: }
172:
173: /**
174: * Test for message of 2 not equal arrays. Should return return actual content instead of something like String[234
175: */
176: public void testAssertLenEquals_formatArraysMessage() {
177: try {
178: assertLenEquals(new String[] { "test1", "test2" },
179: new Integer[] { 1, 2 });
180: } catch (AssertionFailedError a) {
181: // expected
182: assertTrue(a.getMessage().contains("[test1, test2]"));
183: assertTrue(a.getMessage().contains("[1, 2]"));
184: }
185: }
186:
187: /**
188: * Test class with failing equals.
189: */
190: private class TestObject {
191:
192: private String string1;
193:
194: private String string2;
195:
196: public TestObject(String stringValue1, String stringValue2) {
197: this .string1 = stringValue1;
198: this .string2 = stringValue2;
199: }
200:
201: public String getString1() {
202: return string1;
203: }
204:
205: public void setString1(String string1) {
206: this .string1 = string1;
207: }
208:
209: public String getString2() {
210: return string2;
211: }
212:
213: public void setString2(String string2) {
214: this .string2 = string2;
215: }
216:
217: /**
218: * Always returns false
219: *
220: * @param o the object to compare to
221: */
222: @Override
223: public boolean equals(Object o) {
224: return false;
225: }
226: }
227:
228: }
|