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 junit.framework.TestCase;
021:
022: /**
023: * Test class for {@link ReflectionComparator}.
024: * Contains tests with primitive array types.
025: *
026: * @author Tim Ducheyne
027: * @author Filip Neven
028: */
029: public class ReflectionComparatorPrimitivesArrayTest extends TestCase {
030:
031: /* Test array */
032: private int[] arrayA;
033:
034: /* Same as A but different instance */
035: private int[] arrayB;
036:
037: /* Same as A but different order of values*/
038: private int[] arrayDifferentOrder;
039:
040: /* Same as A and B but different int value for element 2 */
041: private int[] arrayDifferentValue;
042:
043: /* Same as A and B but no 3rd element */
044: private int[] arrayDifferentSize;
045:
046: /* Same as A and B but of type long */
047: private long[] arrayDifferentType;
048:
049: /* Test element with inner array for element 2 */
050: private Element arrayInnerA;
051:
052: /* Same as innerA but different instance */
053: private Element arrayInnerB;
054:
055: /* Same as innerA and innerB but different int value for inner element 2 */
056: private Element arrayInnerDifferentValue;
057:
058: /* Same as innerA and innerB but no 3rd inner element */
059: private Element arrayInnerDifferentSize;
060:
061: /* Class under test */
062: private ReflectionComparator reflectionComparator;
063:
064: /* Class under test lenient order version */
065: private ReflectionComparator reflectionComparatorLenientOrder;
066:
067: /**
068: * Initializes the test fixture.
069: */
070: protected void setUp() throws Exception {
071: super .setUp();
072:
073: arrayA = new int[] { 1, 2, 3 };
074: arrayB = new int[] { 1, 2, 3 };
075: arrayDifferentOrder = new int[] { 3, 1, 2 };
076: arrayDifferentValue = new int[] { 1, 9999, 3 };
077: arrayDifferentSize = new int[] { 1, 2 };
078: arrayDifferentType = new long[] { 1, 2, 3 };
079:
080: arrayInnerA = new Element(arrayA);
081: arrayInnerB = new Element(arrayB);
082: arrayInnerDifferentValue = new Element(arrayDifferentValue);
083: arrayInnerDifferentSize = new Element(arrayDifferentSize);
084:
085: reflectionComparator = ReflectionComparatorChainFactory.STRICT_COMPARATOR;
086: reflectionComparatorLenientOrder = ReflectionComparatorChainFactory.LENIENTORDER_COMPARATOR;
087: }
088:
089: /**
090: * Test for two equal arrays.
091: */
092: public void testGetDifference_equals() {
093: Difference result = reflectionComparator.getDifference(arrayA,
094: arrayB);
095: assertNull(result);
096: }
097:
098: /**
099: * Test for two equal arrays as an inner field of an object.
100: */
101: public void testGetDifference_equalsInner() {
102: Difference result = reflectionComparator.getDifference(
103: arrayInnerA, arrayInnerB);
104: assertNull(result);
105: }
106:
107: /**
108: * Test for two equal arrays with different order and no lenient order.
109: */
110: public void testGetDifference_notEqualsDifferentOrder() {
111: Difference result = reflectionComparator.getDifference(arrayA,
112: arrayDifferentOrder);
113:
114: assertNotNull(result);
115: assertEquals("0", result.getFieldStack().get(0));
116: assertEquals(1, result.getLeftValue());
117: assertEquals(3, result.getRightValue());
118: }
119:
120: /**
121: * Test for two equal arrays with different order but with lenient order.
122: */
123: public void testGetDifference_equalsLenientOrder() {
124: Difference result = reflectionComparatorLenientOrder
125: .getDifference(arrayA, arrayDifferentOrder);
126: assertNull(result);
127: }
128:
129: /**
130: * Test for two equal primitives arrays but of different type (int vs long).
131: */
132: public void testGetDifference_differentTypes() {
133: Difference result = reflectionComparator.getDifference(arrayA,
134: arrayDifferentType);
135: assertNull(result);
136: }
137:
138: /**
139: * Test for two arrays that contain different values.
140: */
141: public void testGetDifference_notEqualsDifferentValues() {
142: Difference result = reflectionComparator.getDifference(arrayA,
143: arrayDifferentValue);
144:
145: assertNotNull(result);
146: assertEquals("1", result.getFieldStack().get(0));
147: assertEquals(2, result.getLeftValue());
148: assertEquals(9999, result.getRightValue());
149: }
150:
151: /**
152: * Test for two arrays that have a different size.
153: */
154: public void testGetDifference_notEqualsDifferentSize() {
155: Difference result = reflectionComparator.getDifference(arrayA,
156: arrayDifferentSize);
157:
158: assertNotNull(result);
159: assertTrue(result.getFieldStack().isEmpty());
160: assertSame(arrayA, result.getLeftValue());
161: assertSame(arrayDifferentSize, result.getRightValue());
162: }
163:
164: /**
165: * Test for objects with inner arrays that contain different values.
166: */
167: public void testGetDifference_notEqualsInnerDifferentValues() {
168: Difference result = reflectionComparator.getDifference(
169: arrayInnerA, arrayInnerDifferentValue);
170:
171: assertNotNull(result);
172: assertEquals("inner", result.getFieldStack().get(0));
173: assertEquals("1", result.getFieldStack().get(1));
174: assertEquals(2, result.getLeftValue());
175: assertEquals(9999, result.getRightValue());
176: }
177:
178: /**
179: * Tests for objects with inner arrays that have a different size.
180: */
181: public void testGetDifference_notEqualsInnerDifferentSize() {
182: Difference result = reflectionComparator.getDifference(
183: arrayInnerA, arrayInnerDifferentSize);
184:
185: assertNotNull(result);
186: assertEquals("inner", result.getFieldStack().get(0));
187: assertSame(arrayA, result.getLeftValue());
188: assertSame(arrayDifferentSize, result.getRightValue());
189: }
190:
191: /**
192: * Test class with failing equals.
193: */
194: private class Element {
195:
196: /* An inner array */
197: private int[] inner;
198:
199: /**
200: * Creates and initializes the element.
201: *
202: * @param inner the inner array
203: */
204: public Element(int[] inner) {
205: this .inner = inner;
206: }
207:
208: /**
209: * Gets the inner array
210: *
211: * @return the array
212: */
213: public int[] getInner() {
214: return inner;
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: }
|