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.IGNORE_DEFAULTS;
023:
024: /**
025: * Test class for {@link ReflectionAssert} tests for with assertProperty methods.
026: *
027: * @author Tim Ducheyne
028: * @author Filip Neven
029: */
030: public class ReflectionAssertPropertiesTest extends TestCase {
031:
032: /* Test object */
033: private TestObject testObject;
034:
035: /**
036: * Initializes the test fixture.
037: */
038: protected void setUp() throws Exception {
039: super .setUp();
040:
041: testObject = new TestObject(1, "stringValue");
042: }
043:
044: /**
045: * Test for equal property value.
046: */
047: public void testAssertPropertyRefEquals_equals() {
048: assertPropertyRefEquals("stringProperty", "stringValue",
049: testObject);
050: }
051:
052: /**
053: * Test for equal property value (message version).
054: */
055: public void testAssertPropertyRefEquals_equalsMessage() {
056: assertPropertyRefEquals("a message", "stringProperty",
057: "stringValue", testObject);
058: }
059:
060: /**
061: * Test for equal property value.
062: */
063: public void testAssertPropertyLenEquals_equals() {
064: assertPropertyLenEquals("stringProperty", "stringValue",
065: testObject);
066: }
067:
068: /**
069: * Test for equal property value (message version).
070: */
071: public void testAssertPropertyLenEquals_equalsMessage() {
072: assertPropertyLenEquals("a message", "stringProperty",
073: "stringValue", testObject);
074: }
075:
076: /**
077: * Test for equal primitive property value.
078: */
079: public void testAssertPropertyRefEquals_equalsPrimitive() {
080: assertPropertyRefEquals("primitiveProperty", 1L, testObject);
081: }
082:
083: /**
084: * Test for different property value.
085: */
086: public void testAssertPropertyRefEquals_notEqualsDifferentValues() {
087: try {
088: assertPropertyRefEquals("stringProperty", "xxxxxx",
089: testObject);
090: fail("Expected AssertionFailedError");
091: } catch (AssertionFailedError a) {
092: // expected
093: }
094: }
095:
096: /**
097: * Test case for a null left-argument.
098: */
099: public void testAssertPropertyRefEquals_leftNull() {
100: try {
101: assertPropertyRefEquals("stringProperty", null, testObject);
102: fail("Expected AssertionFailedError");
103: } catch (AssertionFailedError a) {
104: // expected
105: }
106: }
107:
108: /**
109: * Test case for a null right-argument.
110: */
111: public void testAssertPropertyRefEquals_rightNull() {
112: testObject.setStringProperty(null);
113: try {
114: assertPropertyRefEquals("stringProperty", "stringValue",
115: testObject);
116: fail("Expected AssertionFailedError");
117: } catch (AssertionFailedError a) {
118: // expected
119: }
120: }
121:
122: /**
123: * Test case for null as actual object argument.
124: */
125: public void testAssertPropertyRefEquals_actualObjectNull() {
126: try {
127: assertPropertyRefEquals("aProperty", "aValue", null);
128: fail("Expected AssertionFailedError");
129: } catch (AssertionFailedError a) {
130: // expected
131: }
132: }
133:
134: /**
135: * Test case for both null arguments.
136: */
137: public void testAssertPropertyRefEquals_null() {
138: testObject.setStringProperty(null);
139: assertPropertyRefEquals("stringProperty", null, testObject);
140: }
141:
142: /**
143: * Test for ignored default left value.
144: */
145: public void testAssertPropertyRefEquals_equalsIgnoredDefault() {
146: assertPropertyRefEquals("a message", "stringProperty", null,
147: testObject, IGNORE_DEFAULTS);
148: }
149:
150: /**
151: * Test for ignored default left value.
152: */
153: public void testAssertPropertyLenEquals_equalsIgnoredDefault() {
154: assertPropertyLenEquals("stringProperty", null, testObject);
155: }
156:
157: /**
158: * Test class with failing equals containing test properties.
159: */
160: public class TestObject {
161:
162: private long primitiveProperty;
163:
164: private String stringProperty;
165:
166: public TestObject(long primitiveProperty, String stringProperty) {
167: this .primitiveProperty = primitiveProperty;
168: this .stringProperty = stringProperty;
169: }
170:
171: public long getPrimitiveProperty() {
172: return primitiveProperty;
173: }
174:
175: public void setPrimitiveProperty(long primitiveProperty) {
176: this .primitiveProperty = primitiveProperty;
177: }
178:
179: public String getStringProperty() {
180: return stringProperty;
181: }
182:
183: public void setStringProperty(String stringProperty) {
184: this.stringProperty = stringProperty;
185: }
186: }
187:
188: }
|