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.AssertionFailedError;
019: import junit.framework.TestCase;
020: import static org.unitils.reflectionassert.ReflectionAssert.assertPropertyLenEquals;
021: import static org.unitils.reflectionassert.ReflectionAssert.assertPropertyRefEquals;
022: import static org.unitils.reflectionassert.ReflectionComparatorMode.LENIENT_ORDER;
023:
024: import static java.util.Arrays.asList;
025: import java.util.List;
026:
027: /**
028: * Test class for {@link org.unitils.reflectionassert.ReflectionAssert} tests for with
029: * assertProperty methods with collection arguments.
030: *
031: * @author Tim Ducheyne
032: * @author Filip Neven
033: */
034: public class ReflectionAssertPropertiesCollectionsTest extends TestCase {
035:
036: /* A test collection */
037: private List<TestObject> list;
038:
039: /**
040: * Initializes the test fixture.
041: */
042: protected void setUp() throws Exception {
043: super .setUp();
044:
045: list = asList(new TestObject(1L, "el1"), new TestObject(2L,
046: "el2"));
047: }
048:
049: /**
050: * Test for equal property values.
051: */
052: public void testAssertPropertyRefEquals() {
053: assertPropertyRefEquals("stringProperty", asList("el1", "el2"),
054: list);
055: }
056:
057: /**
058: * Test for equal property values but of different types (int versus long).
059: */
060: public void testAssertPropertyRefEquals_differentTypes() {
061: assertPropertyRefEquals("primitiveProperty", asList(1L, 2L),
062: list);
063: }
064:
065: /**
066: * Test for different property values.
067: */
068: public void testAssertPropertyRefEquals_notEqualsDifferentValues() {
069: try {
070: assertPropertyRefEquals("stringProperty", asList("xxxxx",
071: "xxxxx"), list);
072: fail("Expected AssertionFailedError");
073:
074: } catch (AssertionFailedError e) {
075: // Expected
076: }
077: }
078:
079: /**
080: * Test for property values with different order.
081: */
082: public void testAssertPropertyRefEquals_equalsDifferentOrder() {
083: assertPropertyRefEquals("stringProperty", asList("el1", "el2"),
084: list, LENIENT_ORDER);
085: }
086:
087: /**
088: * Test for property values with different order.
089: */
090: public void testAssertPropertyLenEquals_equalsDifferentOrder() {
091: assertPropertyLenEquals("stringProperty", asList("el1", "el2"),
092: list);
093: }
094:
095: /**
096: * Test for property values with different order.
097: */
098: public void testAssertPropertyRefEquals_notEqualsDifferentOrder() {
099: try {
100: assertPropertyRefEquals("stringProperty", asList("el2",
101: "el1"), list);
102: fail("Expected AssertionFailedError");
103:
104: } catch (AssertionFailedError e) {
105: // Expected
106: }
107: }
108:
109: /**
110: * Test for equal primitive property values. Using ints instead of longs.
111: */
112: public void testAssertPropertyRefEquals_equalsPrimitivesList() {
113: assertPropertyLenEquals("primitiveProperty", asList(2, 1), list);
114: }
115:
116: /**
117: * Test for different primitive property values. Using ints instead of longs.
118: */
119: public void testAssertPropertyRefEquals_notEqualsPrimitivesList() {
120: try {
121: assertPropertyLenEquals("primitiveProperty",
122: asList(999, 1), list);
123: fail("Expected AssertionFailedError");
124: } catch (AssertionFailedError e) {
125: // Expected
126: }
127: }
128:
129: /**
130: * Test case for null as actual object argument.
131: */
132: public void testAssertPropertyRefEquals_actualObjectNull() {
133: try {
134: assertPropertyLenEquals("stringProperty", asList(1, 2),
135: null);
136: fail("Expected AssertionFailedError");
137: } catch (AssertionFailedError a) {
138: // expected
139: }
140: }
141:
142: /**
143: * Test class with failing equals containing test properties.
144: */
145: public class TestObject {
146:
147: private long primitiveProperty;
148:
149: private String stringProperty;
150:
151: public TestObject(long primitiveProperty, String stringProperty) {
152: this .primitiveProperty = primitiveProperty;
153: this .stringProperty = stringProperty;
154: }
155:
156: public long getPrimitiveProperty() {
157: return primitiveProperty;
158: }
159:
160: public void setPrimitiveProperty(long primitiveProperty) {
161: this .primitiveProperty = primitiveProperty;
162: }
163:
164: public String getStringProperty() {
165: return stringProperty;
166: }
167:
168: public void setStringProperty(String stringProperty) {
169: this.stringProperty = stringProperty;
170: }
171: }
172:
173: }
|