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.TestCase;
019: import org.unitils.reflectionassert.ReflectionComparator.Difference;
020:
021: /**
022: * Test class for {@link ReflectionComparator}. Contains tests with primitive types.
023: *
024: * @author Tim Ducheyne
025: * @author Filip Neven
026: */
027: public class ReflectionComparatorPrimitivesTest extends TestCase {
028:
029: /* Test object */
030: private Primitives primitivesA;
031:
032: /* Same as A but different instance */
033: private Primitives primitivesB;
034:
035: /* Same as A and B but different int value for intValue2 */
036: private Primitives primitiveDifferentValue;
037:
038: /* Same as A and B but with 0 value for intValue2 */
039: private Primitives primitives0Value;
040:
041: /* Test object with inner object */
042: private Primitives primitivesInnerA;
043:
044: /* Same as innerA but different instance */
045: private Primitives primitivesInnerB;
046:
047: /* Same as innerA and innerB but different int value for inner intValue2 */
048: private Primitives primitivesInnerDifferentValue;
049:
050: /* Class under test */
051: private ReflectionComparator reflectionComparator;
052:
053: /**
054: * Initializes the test fixture.
055: */
056: protected void setUp() throws Exception {
057: super .setUp();
058:
059: primitivesA = new Primitives(1, 2, null);
060: primitivesB = new Primitives(1, 2, null);
061: primitiveDifferentValue = new Primitives(1, 9999, null);
062: primitives0Value = new Primitives(1, 0, null);
063:
064: primitivesInnerA = new Primitives(0, 0, primitivesA);
065: primitivesInnerB = new Primitives(0, 0, primitivesB);
066: primitivesInnerDifferentValue = new Primitives(0, 0,
067: primitiveDifferentValue);
068:
069: reflectionComparator = ReflectionComparatorChainFactory.STRICT_COMPARATOR;
070: }
071:
072: /**
073: * Test for two equal primitives.
074: */
075: public void testGetDifference_equals() {
076: Difference result = reflectionComparator.getDifference(
077: primitivesA, primitivesB);
078: assertNull(result);
079: }
080:
081: /**
082: * Test for two equal autoboxing. An autoboxed primitive should be considered equals to the object version.
083: */
084: public void testGetDifference_equalsAutoboxing() {
085: Difference result = reflectionComparator.getDifference(5L,
086: new Long(5));
087: assertNull(result);
088: }
089:
090: /**
091: * Test for two equal primitives as an inner field of an object.
092: */
093: public void testGetDifference_equalsInner() {
094: Difference result = reflectionComparator.getDifference(
095: primitivesInnerA, primitivesInnerB);
096: assertNull(result);
097: }
098:
099: /**
100: * Test for two equal primitives but of different type (int vs long).
101: */
102: public void testGetDifference_differentTypes() {
103: Difference result = reflectionComparator.getDifference(5L, 5);
104: assertNull(result);
105: }
106:
107: /**
108: * Test for two primitives that contain different values.
109: */
110: public void testGetDifference_notEqualsDifferentValues() {
111: Difference result = reflectionComparator.getDifference(
112: primitivesA, primitiveDifferentValue);
113:
114: assertNotNull(result);
115: assertEquals("intValue2", result.getFieldStack().get(0));
116: assertEquals(2, result.getLeftValue());
117: assertEquals(9999, result.getRightValue());
118: }
119:
120: /**
121: * Test for two primitives with right value 0.
122: */
123: public void testGetDifference_notEqualsRight0() {
124: Difference result = reflectionComparator.getDifference(
125: primitivesA, primitives0Value);
126:
127: assertNotNull(result);
128: assertEquals("intValue2", result.getFieldStack().get(0));
129: assertEquals(2, result.getLeftValue());
130: assertEquals(0, result.getRightValue());
131: }
132:
133: /**
134: * Test for two primitives with left value 0.
135: */
136: public void testGetDifference_notEqualsLeft0() {
137: Difference result = reflectionComparator.getDifference(
138: primitives0Value, primitivesA);
139:
140: assertNotNull(result);
141: assertEquals("intValue2", result.getFieldStack().get(0));
142: assertEquals(0, result.getLeftValue());
143: assertEquals(2, result.getRightValue());
144: }
145:
146: /**
147: * Test for objects with inner primitives that contain different values.
148: */
149: public void testGetDifference_notEqualsInnerDifferentValues() {
150: Difference result = reflectionComparator.getDifference(
151: primitivesInnerA, primitivesInnerDifferentValue);
152:
153: assertNotNull(result);
154: assertEquals("inner", result.getFieldStack().get(0));
155: assertEquals("intValue2", result.getFieldStack().get(1));
156: assertEquals(2, result.getLeftValue());
157: assertEquals(9999, result.getRightValue());
158: }
159:
160: /**
161: * Tests for equality of two NaN values
162: */
163: public void testNaN() {
164: Difference result = reflectionComparator.getDifference(
165: Double.NaN, Float.NaN);
166: assertNull(result);
167: }
168:
169: /**
170: * Tests for equality of a NaN with a 0 value
171: */
172: public void testNaN_notEqual() {
173: Difference result = reflectionComparator.getDifference(
174: Double.NaN, 0);
175: assertTrue(result.getFieldStack().isEmpty());
176: assertEquals(Double.NaN, result.getLeftValue());
177: assertEquals(0, result.getRightValue());
178: }
179:
180: /**
181: * Tests for equality of two NEGATIVE_INFINITY values
182: */
183: public void testInfinity() {
184: Difference result = reflectionComparator.getDifference(
185: Double.NEGATIVE_INFINITY, Float.NEGATIVE_INFINITY);
186: assertNull(result);
187: }
188:
189: /**
190: * Tests for equality of a NEGATIVE_INFINITY with a POSITIVE_INFINITY value
191: */
192: public void testInfinity_notEqual() {
193: Difference result = reflectionComparator.getDifference(
194: Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY);
195: assertTrue(result.getFieldStack().isEmpty());
196: assertEquals(Double.NEGATIVE_INFINITY, result.getLeftValue());
197: assertEquals(Double.POSITIVE_INFINITY, result.getRightValue());
198: }
199:
200: /**
201: * Test class with failing equals.
202: */
203: @SuppressWarnings("unused")
204: private static class Primitives {
205:
206: /* A fist int value */
207: private int intValue1;
208:
209: /* A second int value */
210: private int intValue2;
211:
212: /* An inner object */
213: private Primitives inner;
214:
215: /**
216: * Creates and initializes the element.
217: *
218: * @param intValue1 the first int value
219: * @param intValue2 the second int value
220: * @param inner the inner collection
221: */
222: public Primitives(int intValue1, int intValue2, Primitives inner) {
223: this .intValue1 = intValue1;
224: this .intValue2 = intValue2;
225: this .inner = inner;
226: }
227:
228: /**
229: * Always returns false
230: *
231: * @param o the object to compare to
232: */
233: @Override
234: public boolean equals(Object o) {
235: return false;
236: }
237: }
238:
239: }
|