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.Assert;
019: import junit.framework.AssertionFailedError;
020: import junit.framework.TestCase;
021: import static org.unitils.reflectionassert.ReflectionAssert.assertLenEquals;
022: import static org.unitils.reflectionassert.ReflectionAssert.assertRefEquals;
023: import static org.unitils.reflectionassert.ReflectionComparatorMode.LENIENT_ORDER;
024:
025: import java.util.Arrays;
026: import java.util.List;
027:
028: /**
029: * Test class for {@link ReflectionAssert} tests for with assertProperty methods with collection arguments.
030: *
031: * @author Tim Ducheyne
032: * @author Filip Neven
033: */
034: public class ReflectionAssertCollectionsTest extends TestCase {
035:
036: /* A test collection */
037: private List<String> listA;
038:
039: /* Same as listA but different instance */
040: private List<String> listB;
041:
042: /* Same as listA but with a different order */
043: private List<String> listDifferentOrder;
044:
045: /* A list having same size as listA but containing different values */
046: private List<String> listDifferentValues;
047:
048: /* A list containing 1 extra element as listA, a double of another element */
049: private List<String> listDuplicateElement;
050:
051: /* A list with one element less than listA */
052: private List<String> listOneElementLess;
053:
054: /* A list with one element more than listA */
055: private List<String> listOneElementMore;
056:
057: /**
058: * Initializes the test fixture.
059: */
060: protected void setUp() throws Exception {
061: super .setUp();
062:
063: listA = Arrays.asList("el1", "el2");
064: listB = Arrays.asList("el1", "el2");
065: listDifferentOrder = Arrays.asList("el2", "el1");
066: listDifferentValues = Arrays.asList("el2", "el3");
067: listDuplicateElement = Arrays.asList("el2", "el2", "el1");
068: listOneElementLess = Arrays.asList("el1");
069: listOneElementMore = Arrays.asList("el1", "el2", "el3");
070: }
071:
072: /**
073: * Test for two equal collections.
074: */
075: public void testAssertRefEquals() {
076: assertRefEquals(listA, listB);
077: }
078:
079: /**
080: * Test for two equal collections but with different order.
081: */
082: public void testAssertRefEquals_notEqualsDifferentOrder() {
083: try {
084: assertRefEquals(listA, listDifferentOrder);
085: Assert.fail("Expected AssertionFailedError");
086:
087: } catch (AssertionFailedError e) {
088: // Expected
089: }
090: }
091:
092: /**
093: * Test for two equal collections but with different order.
094: */
095: public void testAssertRefEquals_equalsDifferentOrder() {
096: assertRefEquals(listA, listDifferentOrder, LENIENT_ORDER);
097: }
098:
099: /**
100: * Test for two equal collections but with different order.
101: */
102: public void testAssertLenEquals_equalsDifferentOrder() {
103: assertLenEquals(listA, listDifferentOrder);
104: }
105:
106: /**
107: * Test for two collections with different elements.
108: */
109: public void testAssertEquals_differentListSameSize() {
110: try {
111: assertRefEquals(listA, listDifferentValues);
112: Assert.fail("Expected AssertionFailedError");
113:
114: } catch (AssertionFailedError e) {
115: // Expected
116: }
117: }
118:
119: /**
120: * Test for a collection with a duplicate element.
121: */
122: public void testAssertEquals_duplicateElement() {
123: try {
124: assertRefEquals(listA, listDuplicateElement);
125: Assert.fail("Expected AssertionFailedError");
126:
127: } catch (AssertionFailedError e) {
128: // Expected
129: }
130: }
131:
132: /**
133: * Test for with a collection that has one element less.
134: */
135: public void testAssertEquals_oneElementLess() {
136: try {
137: assertRefEquals(listA, listOneElementLess);
138: Assert.fail("Expected AssertionFailedError");
139:
140: } catch (AssertionFailedError e) {
141: // Expected
142: }
143: }
144:
145: /**
146: * Test for with a collection that has one element more.
147: */
148: public void testAssertEquals_oneElementMore() {
149: try {
150: assertRefEquals(listA, listOneElementMore);
151: Assert.fail("Expected AssertionFailedError");
152:
153: } catch (AssertionFailedError e) {
154: // Expected
155: }
156: }
157:
158: }
|