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}.
023: * Contains tests with enums.
024: *
025: * @author Tim Ducheyne
026: * @author Filip Neven
027: */
028: public class ReflectionComparatorEnumsTest extends TestCase {
029:
030: /* Test object */
031: private Enums enumsA;
032:
033: /* Same as A but different instance */
034: private Enums enumsB;
035:
036: /* Same as A and B but different value */
037: private Enums enumsDifferentValue;
038:
039: /* Class under test */
040: private ReflectionComparator reflectionComparator;
041:
042: /**
043: * Initializes the test fixture.
044: */
045: protected void setUp() throws Exception {
046: super .setUp();
047:
048: reflectionComparator = ReflectionComparatorChainFactory.STRICT_COMPARATOR;
049:
050: enumsA = new Enums(Enums.TestEnum.TEST1);
051: enumsB = new Enums(Enums.TestEnum.TEST1);
052: enumsDifferentValue = new Enums(Enums.TestEnum.TEST2);
053: }
054:
055: /**
056: * Test for two equal enum values.
057: */
058: public void testGetDifference_equals() {
059: Difference result = reflectionComparator.getDifference(enumsA,
060: enumsB);
061: assertNull(result);
062: }
063:
064: /**
065: * Test for two different enum values
066: */
067: public void testGetDifference_notEqualsDifferentValues() {
068: Difference result = reflectionComparator.getDifference(enumsA,
069: enumsDifferentValue);
070:
071: assertNotNull(result);
072: assertEquals("testEnumValue", result.getFieldStack().get(0));
073: assertEquals(Enums.TestEnum.TEST1, result.getLeftValue());
074: assertEquals(Enums.TestEnum.TEST2, result.getRightValue());
075: }
076:
077: /**
078: * Test class with enum field and failing equals.
079: */
080: private static class Enums {
081:
082: public enum TestEnum {
083: TEST1, TEST2
084: }
085:
086: @SuppressWarnings("unused")
087: private TestEnum testEnumValue;
088:
089: public Enums(TestEnum testEnumValue) {
090: this .testEnumValue = testEnumValue;
091: }
092:
093: /**
094: * Always returns false
095: *
096: * @param o the object to compare to
097: */
098: @Override
099: public boolean equals(Object o) {
100: return false;
101: }
102: }
103: }
|