View Javadoc

1   /*
2    * Copyright 2006-2008 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Contains assertion related convenience methods for testing (not for production use).
40   * 
41   * @see org.kuali.rice.kns.util.AssertionUtils
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       * @see #assertSparselyEqualBean(String, Object, Object)
62       */
63      public static void assertSparselyEqualBean(Object expectedBean, Object actualBean) throws InvocationTargetException, NoSuchMethodException {
64          assertSparselyEqualBean(null, expectedBean, actualBean);
65      }
66  
67      /**
68       * Asserts that the non-null non-BO properties of the expected bean are equal to those of the actual bean. Any null or
69       * BusinessObject expected properties are ignored. Attributes are reflected bean-style via any public no-argument methods
70       * starting with "get" (or "is" for booleans), including inherited methods. The expected and actual beans do not need to be of
71       * the same class.
72       * <p>
73       * Reflection wraps primitives, so differences in primitiveness are ignored. Properties that are BusinessObjects (generally
74       * relations based on foreign key properties) are also ignored, because this method is not testing OJB foreign key resolution
75       * (e.g., via the <code>refresh</code> method), we do not want to have to put all the related BOs into the test fixture
76       * (redundant with the foreign keys), and many (all?) of our BOs implement the <code>equals</code> method in terms of identity
77       * so would fail this assertion anyway. This is a data-oriented assertion, for our data-oriented tests and persistence layer.
78       * 
79       * @param message a description of this test assertion
80       * @param expectedBean a java bean containing expected properties
81       * @param actualBean a java bean containing actual properties
82       * @throws InvocationTargetException if a getter method throws an exception (the cause)
83       * @throws NoSuchMethodException if an expected property does not exist in the actualBean
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); // can't happen because getReadMethod() returns only public methods
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; // success;
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      * Asserts that the given reference is either null or an OJB proxy that references to a nonexistant object. This is different
190      * from assertNull() because ObjectUtils.isNull() checks for OJB proxies and goes to the database to see if real data is
191      * available.
192      * 
193      * @param expectedNull the reference to check
194      */
195     public static void assertNullHandlingOjbProxies(Object expectedNull) {
196         assertNullHandlingOjbProxies(null, expectedNull);
197     }
198 
199     /**
200      * Asserts that the given reference is either null or an OJB proxy that references a nonexistant object. This is different from
201      * assertNotNull() because ObjectUtils.isNull() checks for OJB proxies and goes to the database to see if real data is
202      * available.
203      * 
204      * @param message the context of this assertion, if it fails
205      * @param expectedNull the reference to check
206      */
207     public static void assertNullHandlingOjbProxies(String message, Object expectedNull) {
208         assertTrue(message, ObjectUtils.isNull(expectedNull));
209     }
210 
211     /**
212      * Asserts that the given reference is neither null nor an OJB proxy that references a nonexistant object. This is different
213      * from assertNotNull() because ObjectUtils.isNull() checks for OJB proxies and goes to the database to see if real data is
214      * available.
215      * 
216      * @param expectedNotNull the reference to check
217      */
218     public static void assertNotNullHandlingOjbProxies(Object expectedNotNull) {
219         assertNotNullHandlingOjbProxies(null, expectedNotNull);
220     }
221 
222     /**
223      * Asserts that the given reference is neither null nor an OJB proxy that references a nonexistant object. This is different
224      * from assertNotNull() because ObjectUtils.isNull() checks for OJB proxies and goes to the database to see if real data is
225      * available.
226      * 
227      * @param message the context of this assertion, if it fails
228      * @param expectedNotNull the reference to check
229      */
230     public static void assertNotNullHandlingOjbProxies(String message, Object expectedNotNull) {
231         assertFalse(message, ObjectUtils.isNull(expectedNotNull));
232     }
233 }