001: /*
002: * Copyright 2006-2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.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.opensource.org/licenses/ecl1.php
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.kuali.test.util;
017:
018: import static junit.framework.Assert.assertEquals;
019: import static junit.framework.Assert.assertFalse;
020: import static junit.framework.Assert.assertNotNull;
021: import static junit.framework.Assert.assertTrue;
022: import static junit.framework.Assert.fail;
023:
024: import java.beans.PropertyDescriptor;
025: import java.lang.reflect.InvocationTargetException;
026: import java.util.Arrays;
027: import java.util.Iterator;
028: import java.util.List;
029:
030: import org.apache.commons.beanutils.PropertyUtils;
031: import org.kuali.core.bo.BusinessObject;
032: import org.kuali.core.util.ErrorMap;
033: import org.kuali.core.util.ErrorMessage;
034: import org.kuali.core.util.GlobalVariables;
035: import org.kuali.core.util.ObjectUtils;
036:
037: /**
038: * Contains assertion related convenience methods for testing (not for production use).
039: *
040: * @see org.kuali.core.util.AssertionUtils
041: */
042: public class KualiTestAssertionUtils {
043:
044: public static void assertEquality(Object a, Object b) {
045: assertNotNull(a);
046: assertNotNull(b);
047:
048: assertTrue(a.equals(b));
049: }
050:
051: public static void assertInequality(Object a, Object b) {
052: assertNotNull(a);
053: assertNotNull(b);
054:
055: assertFalse(a.equals(b));
056: }
057:
058: /**
059: * @see #assertSparselyEqualBean(String, Object, Object)
060: */
061: public static void assertSparselyEqualBean(Object expectedBean,
062: Object actualBean) throws InvocationTargetException,
063: NoSuchMethodException {
064: assertSparselyEqualBean(null, expectedBean, actualBean);
065: }
066:
067: /**
068: * Asserts that the non-null non-BO properties of the expected bean are equal to those of the actual bean. Any null or
069: * BusinessObject expected properties are ignored. Attributes are reflected bean-style via any public no-argument methods
070: * starting with "get" (or "is" for booleans), including inherited methods. The expected and actual beans do not need to be of
071: * the same class.
072: * <p>
073: * Reflection wraps primitives, so differences in primitiveness are ignored. Properties that are BusinessObjects (generally
074: * relations based on foreign key properties) are also ignored, because this method is not testing OJB foreign key resolution
075: * (e.g., via the <code>refresh</code> method), we do not want to have to put all the related BOs into the test fixture
076: * (redundant with the foreign keys), and many (all?) of our BOs implement the <code>equals</code> method in terms of identity
077: * so would fail this assertion anyway. This is a data-oriented assertion, for our data-oriented tests and persistence layer.
078: *
079: * @param message a description of this test assertion
080: * @param expectedBean a java bean containing expected properties
081: * @param actualBean a java bean containing actual properties
082: * @throws InvocationTargetException if a getter method throws an exception (the cause)
083: * @throws NoSuchMethodException if an expected property does not exist in the actualBean
084: */
085: public static void assertSparselyEqualBean(String message,
086: Object expectedBean, Object actualBean)
087: throws InvocationTargetException, NoSuchMethodException {
088: if (message == null) {
089: message = "";
090: } else {
091: message = message + " ";
092: }
093: assertNotNull(message + "actual bean is null", actualBean);
094: PropertyDescriptor[] descriptors = PropertyUtils
095: .getPropertyDescriptors(expectedBean);
096: for (int i = 0; i < descriptors.length; i++) {
097: PropertyDescriptor descriptor = descriptors[i];
098: if (PropertyUtils.getReadMethod(descriptor) != null) {
099: try {
100: Object expectedValue = PropertyUtils
101: .getSimpleProperty(expectedBean, descriptor
102: .getName());
103: if (expectedValue != null
104: && !(expectedValue instanceof BusinessObject)) {
105: assertEquals(message + descriptor.getName(),
106: expectedValue, PropertyUtils
107: .getSimpleProperty(actualBean,
108: descriptor.getName()));
109: }
110: } catch (IllegalAccessException e) {
111: throw new AssertionError(e); // can't happen because getReadMethod() returns only public methods
112: }
113: }
114: }
115: }
116:
117: public static void assertGlobalErrorMapContains(
118: String expectedFieldName, String expectedErrorKey) {
119: assertGlobalErrorMapContains("", expectedFieldName,
120: expectedErrorKey, null, true);
121: }
122:
123: public static void assertGlobalErrorMapContains(String message,
124: String expectedFieldName, String expectedErrorKey) {
125: assertGlobalErrorMapContains(message, expectedFieldName,
126: expectedErrorKey, null, true);
127: }
128:
129: public static void assertGlobalErrorMapContains(
130: String expectedFieldName, String expectedErrorKey,
131: String[] expectedErrorParameters) {
132: assertGlobalErrorMapContains("", expectedFieldName,
133: expectedErrorKey, expectedErrorParameters, true);
134: }
135:
136: public static void assertGlobalErrorMapContains(String message,
137: String expectedFieldName, String expectedErrorKey,
138: String[] expectedErrorParameters) {
139: assertGlobalErrorMapContains(message, expectedFieldName,
140: expectedErrorKey, expectedErrorParameters, true);
141: }
142:
143: public static void assertGlobalErrorMapNotContains(
144: String expectedFieldName, String expectedErrorKey) {
145: assertGlobalErrorMapContains("", expectedFieldName,
146: expectedErrorKey, null, false);
147: }
148:
149: public static void assertGlobalErrorMapNotContains(String message,
150: String expectedFieldName, String expectedErrorKey) {
151: assertGlobalErrorMapContains(message, expectedFieldName,
152: expectedErrorKey, null, false);
153: }
154:
155: private static void assertGlobalErrorMapContains(String message,
156: String expectedFieldName, String expectedErrorKey,
157: String[] expectedErrorParameters, boolean contains) {
158: String header = message.length() == 0 ? "" : message + ": ";
159: ErrorMap map = GlobalVariables.getErrorMap();
160: assertEquals(header
161: + "no error path (global error map path size)", 0, map
162: .getErrorPath().size());
163: assertEquals(header + "error property '" + expectedFieldName
164: + "' has message key " + expectedErrorKey + ": " + map,
165: contains, map.fieldHasMessage(expectedFieldName,
166: expectedErrorKey));
167:
168: if (contains && expectedErrorParameters != null) {
169: List expectedParameterList = Arrays
170: .asList(expectedErrorParameters);
171: List fieldMessages = map.getMessages(expectedFieldName);
172: if (fieldMessages != null) {
173: for (Iterator i = fieldMessages.iterator(); i.hasNext();) {
174: ErrorMessage errorMessage = (ErrorMessage) i.next();
175: if (sparselyEqualLists(expectedParameterList,
176: Arrays.asList(errorMessage
177: .getMessageParameters()))) {
178: return; // success;
179: }
180: }
181: }
182: fail(header + "error property '" + expectedFieldName
183: + "' message key " + expectedErrorKey
184: + " does not contain expected parameters "
185: + expectedParameterList + ": " + map);
186: }
187: }
188:
189: private static boolean sparselyEqualLists(List expected, List actual) {
190: if (expected.size() != actual.size()) {
191: return false;
192: }
193: Iterator actualIterator = actual.iterator();
194: for (Iterator expectedIterator = expected.iterator(); expectedIterator
195: .hasNext();) {
196: Object expectedItem = expectedIterator.next();
197: Object actualItem = actualIterator.next();
198: if (expectedItem != null
199: && !expectedItem.equals(actualItem)) {
200: return false;
201: }
202: }
203: return true;
204: }
205:
206: public static void assertGlobalErrorMapEmpty() {
207: assertGlobalErrorMapSize("", 0);
208: }
209:
210: public static void assertGlobalErrorMapEmpty(String message) {
211: assertGlobalErrorMapSize(message, 0);
212: }
213:
214: public static void assertGlobalErrorMapSize(int expectedSize) {
215: assertGlobalErrorMapSize("", expectedSize);
216: }
217:
218: public static void assertGlobalErrorMapSize(String message,
219: int expectedSize) {
220: String header = message.length() == 0 ? "" : message + ": ";
221: ErrorMap map = GlobalVariables.getErrorMap();
222: assertEquals(header + "ThreadLocal ErrorMap size: " + map,
223: expectedSize, map.size());
224: }
225:
226: /**
227: * Asserts that the given reference is either null or an OJB proxy that references to a nonexistant object. This is different
228: * from assertNull() because ObjectUtils.isNull() checks for OJB proxies and goes to the database to see if real data is
229: * available.
230: *
231: * @param expectedNull the reference to check
232: */
233: public static void assertNullHandlingOjbProxies(Object expectedNull) {
234: assertNullHandlingOjbProxies(null, expectedNull);
235: }
236:
237: /**
238: * Asserts that the given reference is either null or an OJB proxy that references a nonexistant object. This is different from
239: * assertNotNull() because ObjectUtils.isNull() checks for OJB proxies and goes to the database to see if real data is
240: * available.
241: *
242: * @param message the context of this assertion, if it fails
243: * @param expectedNull the reference to check
244: */
245: public static void assertNullHandlingOjbProxies(String message,
246: Object expectedNull) {
247: assertTrue(message, ObjectUtils.isNull(expectedNull));
248: }
249:
250: /**
251: * Asserts that the given reference is neither null nor an OJB proxy that references a nonexistant object. This is different
252: * from assertNotNull() because ObjectUtils.isNull() checks for OJB proxies and goes to the database to see if real data is
253: * available.
254: *
255: * @param expectedNotNull the reference to check
256: */
257: public static void assertNotNullHandlingOjbProxies(
258: Object expectedNotNull) {
259: assertNotNullHandlingOjbProxies(null, expectedNotNull);
260: }
261:
262: /**
263: * Asserts that the given reference is neither null nor an OJB proxy that references a nonexistant object. This is different
264: * from assertNotNull() because ObjectUtils.isNull() checks for OJB proxies and goes to the database to see if real data is
265: * available.
266: *
267: * @param message the context of this assertion, if it fails
268: * @param expectedNotNull the reference to check
269: */
270: public static void assertNotNullHandlingOjbProxies(String message,
271: Object expectedNotNull) {
272: assertFalse(message, ObjectUtils.isNull(expectedNotNull));
273: }
274: }
|