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 org.unitils.reflectionassert.ReflectionComparator.Difference;
019:
020: import java.util.Arrays;
021: import java.util.Collection;
022:
023: import junit.framework.TestCase;
024:
025: /**
026: * Test class for {@link ReflectionComparator}.
027: * Contains tests with non-primitive array types.
028: *
029: * @author Tim Ducheyne
030: * @author Filip Neven
031: */
032: public class ReflectionComparatorArrayTest extends TestCase {
033:
034: /* Test array */
035: private Element[] arrayA;
036:
037: /* Same as A but different instance */
038: private Element[] arrayB;
039:
040: /* Same as A and B but different string value for element 2 */
041: private Element[] arrayDifferentValue;
042:
043: /* Same as A and B but no 3rd element */
044: private Element[] arrayDifferentSize;
045:
046: /* Same as A and B but different order of elements */
047: private Element[] arrayDifferentOrder;
048:
049: /* Same as A and B but different order of elements and different string value */
050: private Element[] arrayDifferentOrderDifferentValue;
051:
052: /* Test array with inner array for element 2 */
053: private Element[] arrayInnerA;
054:
055: /* Same as innerA but different instance */
056: private Element[] arrayInnerB;
057:
058: /* Same as innerA and innerB but different string value for inner element 2 */
059: private Element[] arrayInnerDifferentValue;
060:
061: /* Same as innerA and innerB but no 3rd inner element */
062: private Element[] arrayInnerDifferentSize;
063:
064: /* Class under test */
065: private ReflectionComparator reflectionComparator,
066: lenientOrderComparator;
067:
068: /**
069: * Initializes the test fixture.
070: */
071: protected void setUp() throws Exception {
072: super .setUp();
073:
074: arrayA = createArray("test 2", null, true);
075: arrayB = createArray("test 2", null, true);
076: arrayDifferentValue = createArray("XXXXXX", null, true);
077: arrayDifferentSize = createArray("test 2", null, false);
078: arrayDifferentOrder = createReverseArray("test 2");
079: arrayDifferentOrderDifferentValue = createReverseArray("XXXXXX");
080:
081: arrayInnerA = createArray(null, arrayA, true);
082: arrayInnerB = createArray(null, arrayB, true);
083: arrayInnerDifferentValue = createArray(null,
084: arrayDifferentValue, true);
085: arrayInnerDifferentSize = createArray(null, arrayDifferentSize,
086: true);
087:
088: reflectionComparator = ReflectionComparatorChainFactory.STRICT_COMPARATOR;
089: lenientOrderComparator = ReflectionComparatorChainFactory.LENIENTORDER_COMPARATOR;
090: }
091:
092: /**
093: * Test for two equal arrays.
094: */
095: public void testGetDifference_equals() {
096: Difference result = reflectionComparator.getDifference(arrayA,
097: arrayB);
098: assertNull(result);
099: }
100:
101: /**
102: * Test for two equal arrays as an inner field of an object.
103: */
104: public void testGetDifference_equalsInner() {
105: Difference result = reflectionComparator.getDifference(
106: arrayInnerA, arrayInnerB);
107: assertNull(result);
108: }
109:
110: /**
111: * Test for two arrays that contain different values.
112: */
113: public void testGetDifference_notEqualsDifferentValues() {
114: Difference result = reflectionComparator.getDifference(arrayA,
115: arrayDifferentValue);
116:
117: assertNotNull(result);
118: assertEquals("1", result.getFieldStack().get(0));
119: assertEquals("test 2", result.getLeftValue());
120: assertEquals("XXXXXX", result.getRightValue());
121: }
122:
123: /**
124: * Test for two arrays that have a different size.
125: */
126: public void testGetDifference_notEqualsDifferentSize() {
127: Difference result = reflectionComparator.getDifference(arrayA,
128: arrayDifferentSize);
129:
130: assertNotNull(result);
131: assertTrue(result.getFieldStack().isEmpty());
132: assertSame(arrayA, result.getLeftValue());
133: assertSame(arrayDifferentSize, result.getRightValue());
134: }
135:
136: /**
137: * Test for objects with inner arrays that contain different values.
138: */
139: public void testGetDifference_notEqualsInnerDifferentValues() {
140: Difference result = reflectionComparator.getDifference(
141: arrayInnerA, arrayInnerDifferentValue);
142:
143: assertNotNull(result);
144: assertEquals("1", result.getFieldStack().get(0));
145: assertEquals("inner", result.getFieldStack().get(1));
146: assertEquals("test 2", result.getLeftValue());
147: assertEquals("XXXXXX", result.getRightValue());
148: }
149:
150: /**
151: * Tests for objects with inner arrays that have a different size.
152: */
153: public void testGetDifference_notEqualsInnerDifferentSize() {
154: Difference result = reflectionComparator.getDifference(
155: arrayInnerA, arrayInnerDifferentSize);
156:
157: assertNotNull(result);
158: assertEquals("1", result.getFieldStack().get(0));
159: assertSame(arrayA, result.getLeftValue());
160: assertSame(arrayDifferentSize, result.getRightValue());
161: }
162:
163: /**
164: * Tests for objects with inner arrays that have a element order.
165: */
166: public void testGetDifference_notEqualsDifferentOrderNotLenient() {
167: Difference result = reflectionComparator.getDifference(arrayA,
168: arrayDifferentOrder);
169:
170: assertNotNull(result);
171: assertEquals("0", result.getFieldStack().get(0));
172: assertEquals("test 1", result.getLeftValue());
173: assertEquals("test 3", result.getRightValue());
174: }
175:
176: /**
177: * Tests for objects with inner arrays that have a element order but with lenient order checking.
178: */
179: public void testGetDifference_equalsDifferentOrderLenient() {
180: Difference result = lenientOrderComparator.getDifference(
181: arrayA, arrayDifferentOrder);
182: assertNull(result);
183: }
184:
185: /**
186: * Tests for objects with inner arrays that have a element order but with lenient order checking.
187: */
188: public void testGetDifference_notEqualsDifferentOrderLenientDifferentValues() {
189: Difference result = lenientOrderComparator.getDifference(
190: arrayA, arrayDifferentOrderDifferentValue);
191:
192: assertNotNull(result);
193: assertTrue(result.getFieldStack().isEmpty());
194: assertSame(arrayA, result.getLeftValue());
195: assertSame(arrayDifferentOrderDifferentValue, result
196: .getRightValue());
197: }
198:
199: /**
200: * Tests for arrays but right value is not an array.
201: */
202: public void testGetDifference_notEqualsRightNotArray() {
203: Difference result = reflectionComparator.getDifference(arrayA,
204: "Test string");
205:
206: assertNotNull(result);
207: assertTrue(result.getFieldStack().empty());
208: assertSame(arrayA, result.getLeftValue());
209: assertEquals("Test string", result.getRightValue());
210: }
211:
212: /**
213: * Test for an array and a collection containing equal values (array == collection).
214: */
215: public void testGetDifference_equalsLeftCollection() {
216: Difference result = reflectionComparator.getDifference(arrayA,
217: Arrays.asList(arrayA));
218: assertNull(result);
219: }
220:
221: /**
222: * Test for an array and a collection containing equal values (array == collection).
223: */
224: public void testGetDifference_equalsRightCollection() {
225: Difference result = reflectionComparator.getDifference(Arrays
226: .asList(arrayA), arrayA);
227: assertNull(result);
228: }
229:
230: /**
231: * Test for an array and a collection containing different values (array != collection).
232: */
233: public void testGetDifference_notEqualsCollectionDifferentValues() {
234: Difference result = reflectionComparator.getDifference(arrayA,
235: Arrays.asList(arrayDifferentValue));
236:
237: assertNotNull(result);
238: assertEquals("1", result.getFieldStack().get(0));
239: assertEquals("test 2", result.getLeftValue());
240: assertEquals("XXXXXX", result.getRightValue());
241: }
242:
243: /**
244: * Test for an array and a collection having a different size (array != collection).
245: */
246: public void testGetDifference_notEqualsCollectionDifferentSize() {
247: Collection<?> collectionDifferentSize = Arrays
248: .asList(arrayDifferentSize);
249: Difference result = reflectionComparator.getDifference(arrayA,
250: collectionDifferentSize);
251:
252: assertNotNull(result);
253: assertTrue(result.getFieldStack().isEmpty());
254: assertSame(arrayA, result.getLeftValue());
255: assertSame(collectionDifferentSize, result.getRightValue());
256: }
257:
258: /**
259: * Creates an array.
260: *
261: * @param stringValueElement2 the value for the 2nd element in the array
262: * @param innerElement2 the value for the inner array of the 2nd element in the array
263: * @param addElement3 true for an array of 3 elements, false for 2 elements
264: * @return the test array
265: */
266: private Element[] createArray(String stringValueElement2,
267: Element[] innerElement2, boolean addElement3) {
268: Element[] array = new Element[addElement3 ? 3 : 2];
269: array[0] = new Element("test 1", null);
270: array[1] = new Element(stringValueElement2, innerElement2);
271: if (addElement3) {
272: array[2] = new Element("test 3", null);
273: }
274: return array;
275: }
276:
277: /**
278: * Creates an array and reverses the elements.
279: *
280: * @param stringValueElement2 the value for the 2nd element in the array
281: * @return the test array
282: */
283: private Element[] createReverseArray(String stringValueElement2) {
284: Element[] array = createArray(stringValueElement2, null, true);
285: Element temp = array[2];
286: array[2] = array[0];
287: array[0] = temp;
288: return array;
289: }
290:
291: /**
292: * Test class with failing equals.
293: */
294: private class Element {
295:
296: /* A string value */
297: private String string;
298:
299: /* An inner array */
300: private Element[] inner;
301:
302: /**
303: * Creates and initializes the element.
304: *
305: * @param string the string value
306: * @param inner the inner array
307: */
308: public Element(String string, Element[] inner) {
309: this .string = string;
310: this .inner = inner;
311: }
312:
313: /**
314: * Gets the string value
315: *
316: * @return the value
317: */
318: public String getString() {
319: return string;
320: }
321:
322: /**
323: * Gets the inner array
324: *
325: * @return the array
326: */
327: public Element[] getInner() {
328: return inner;
329: }
330:
331: /**
332: * Always returns false
333: *
334: * @param o the object to compare to
335: */
336: @Override
337: public boolean equals(Object o) {
338: return false;
339: }
340: }
341:
342: }
|