1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kns.test;
17
18 import static junit.framework.Assert.assertEquals;
19 import static junit.framework.Assert.assertFalse;
20 import static junit.framework.Assert.assertNotNull;
21 import static junit.framework.Assert.assertTrue;
22 import static junit.framework.Assert.fail;
23
24 import java.beans.PropertyDescriptor;
25 import java.lang.reflect.InvocationTargetException;
26 import java.util.Arrays;
27 import java.util.Iterator;
28 import java.util.List;
29
30 import org.apache.commons.beanutils.PropertyUtils;
31 import org.kuali.rice.kns.bo.BusinessObject;
32 import org.kuali.rice.kns.util.ErrorMap;
33 import org.kuali.rice.kns.util.ErrorMessage;
34 import org.kuali.rice.kns.util.GlobalVariables;
35 import org.kuali.rice.kns.util.MessageMap;
36 import org.kuali.rice.kns.util.ObjectUtils;
37
38
39
40
41
42
43 public class KualiTestAssertionUtils {
44
45
46 public static void assertEquality(Object a, Object b) {
47 assertNotNull(a);
48 assertNotNull(b);
49
50 assertTrue(a.equals(b));
51 }
52
53 public static void assertInequality(Object a, Object b) {
54 assertNotNull(a);
55 assertNotNull(b);
56
57 assertFalse(a.equals(b));
58 }
59
60
61
62
63 public static void assertSparselyEqualBean(Object expectedBean, Object actualBean) throws InvocationTargetException, NoSuchMethodException {
64 assertSparselyEqualBean(null, expectedBean, actualBean);
65 }
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85 public static void assertSparselyEqualBean(String message, Object expectedBean, Object actualBean) throws InvocationTargetException, NoSuchMethodException {
86 if (message == null) {
87 message = "";
88 }
89 else {
90 message = message + " ";
91 }
92 assertNotNull(message + "actual bean is null", actualBean);
93 PropertyDescriptor[] descriptors = PropertyUtils.getPropertyDescriptors(expectedBean);
94 for (int i = 0; i < descriptors.length; i++) {
95 PropertyDescriptor descriptor = descriptors[i];
96 if (PropertyUtils.getReadMethod(descriptor) != null) {
97 try {
98 Object expectedValue = PropertyUtils.getSimpleProperty(expectedBean, descriptor.getName());
99 if (expectedValue != null && !(expectedValue instanceof BusinessObject)) {
100 assertEquals(message + descriptor.getName(), expectedValue, PropertyUtils.getSimpleProperty(actualBean, descriptor.getName()));
101 }
102 }
103 catch (IllegalAccessException e) {
104 throw new AssertionError(e);
105 }
106 }
107 }
108 }
109
110 public static void assertGlobalErrorMapContains(String expectedFieldName, String expectedErrorKey) {
111 assertGlobalErrorMapContains("", expectedFieldName, expectedErrorKey, null, true);
112 }
113
114 public static void assertGlobalErrorMapContains(String message, String expectedFieldName, String expectedErrorKey) {
115 assertGlobalErrorMapContains(message, expectedFieldName, expectedErrorKey, null, true);
116 }
117
118 public static void assertGlobalErrorMapContains(String expectedFieldName, String expectedErrorKey, String[] expectedErrorParameters) {
119 assertGlobalErrorMapContains("", expectedFieldName, expectedErrorKey, expectedErrorParameters, true);
120 }
121
122 public static void assertGlobalErrorMapContains(String message, String expectedFieldName, String expectedErrorKey, String[] expectedErrorParameters) {
123 assertGlobalErrorMapContains(message, expectedFieldName, expectedErrorKey, expectedErrorParameters, true);
124 }
125
126 public static void assertGlobalErrorMapNotContains(String expectedFieldName, String expectedErrorKey) {
127 assertGlobalErrorMapContains("", expectedFieldName, expectedErrorKey, null, false);
128 }
129
130 public static void assertGlobalErrorMapNotContains(String message, String expectedFieldName, String expectedErrorKey) {
131 assertGlobalErrorMapContains(message, expectedFieldName, expectedErrorKey, null, false);
132 }
133
134 private static void assertGlobalErrorMapContains(String message, String expectedFieldName, String expectedErrorKey, String[] expectedErrorParameters, boolean contains) {
135 String header = message.length() == 0 ? "" : message + ": ";
136 MessageMap map = GlobalVariables.getMessageMap();
137 assertEquals(header + "no error path (global error map path size)", 0, map.getErrorPath().size());
138 assertEquals(header + "error property '" + expectedFieldName + "' has message key " + expectedErrorKey + ": " + map, contains, map.fieldHasMessage(expectedFieldName, expectedErrorKey));
139
140 if (contains && expectedErrorParameters != null) {
141 List expectedParameterList = Arrays.asList(expectedErrorParameters);
142 List fieldMessages = map.getMessages(expectedFieldName);
143 if (fieldMessages != null) {
144 for (Iterator i = fieldMessages.iterator(); i.hasNext();) {
145 ErrorMessage errorMessage = (ErrorMessage) i.next();
146 if (sparselyEqualLists(expectedParameterList, Arrays.asList(errorMessage.getMessageParameters()))) {
147 return;
148 }
149 }
150 }
151 fail(header + "error property '" + expectedFieldName + "' message key " + expectedErrorKey + " does not contain expected parameters " + expectedParameterList + ": " + map);
152 }
153 }
154
155 private static boolean sparselyEqualLists(List expected, List actual) {
156 if (expected.size() != actual.size()) {
157 return false;
158 }
159 Iterator actualIterator = actual.iterator();
160 for (Iterator expectedIterator = expected.iterator(); expectedIterator.hasNext();) {
161 Object expectedItem = expectedIterator.next();
162 Object actualItem = actualIterator.next();
163 if (expectedItem != null && !expectedItem.equals(actualItem)) {
164 return false;
165 }
166 }
167 return true;
168 }
169
170 public static void assertGlobalErrorMapEmpty() {
171 assertGlobalErrorMapSize("", 0);
172 }
173
174 public static void assertGlobalErrorMapEmpty(String message) {
175 assertGlobalErrorMapSize(message, 0);
176 }
177
178 public static void assertGlobalErrorMapSize(int expectedSize) {
179 assertGlobalErrorMapSize("", expectedSize);
180 }
181
182 public static void assertGlobalErrorMapSize(String message, int expectedSize) {
183 String header = message.length() == 0 ? "" : message + ": ";
184 MessageMap map = GlobalVariables.getMessageMap();
185 assertEquals(header + "ThreadLocal ErrorMap size: " + map, expectedSize, map.size());
186 }
187
188
189
190
191
192
193
194
195 public static void assertNullHandlingOjbProxies(Object expectedNull) {
196 assertNullHandlingOjbProxies(null, expectedNull);
197 }
198
199
200
201
202
203
204
205
206
207 public static void assertNullHandlingOjbProxies(String message, Object expectedNull) {
208 assertTrue(message, ObjectUtils.isNull(expectedNull));
209 }
210
211
212
213
214
215
216
217
218 public static void assertNotNullHandlingOjbProxies(Object expectedNotNull) {
219 assertNotNullHandlingOjbProxies(null, expectedNotNull);
220 }
221
222
223
224
225
226
227
228
229
230 public static void assertNotNullHandlingOjbProxies(String message, Object expectedNotNull) {
231 assertFalse(message, ObjectUtils.isNull(expectedNotNull));
232 }
233 }