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.core.maintenance;
017:
018: import java.util.Iterator;
019: import java.util.Map;
020:
021: import org.kuali.core.bo.PersistableBusinessObject;
022: import org.kuali.core.document.MaintenanceDocument;
023: import org.kuali.core.maintenance.rules.MaintenanceDocumentRule;
024: import org.kuali.core.maintenance.rules.MaintenanceDocumentRuleBase;
025: import org.kuali.core.service.DictionaryValidationService;
026: import org.kuali.core.service.DocumentService;
027: import org.kuali.core.service.MaintenanceDocumentDictionaryService;
028: import org.kuali.core.util.ErrorMessage;
029: import org.kuali.core.util.GlobalVariables;
030: import org.kuali.core.util.TypedArrayList;
031: import org.kuali.kfs.KFSConstants;
032: import org.kuali.kfs.KFSKeyConstants;
033: import org.kuali.kfs.context.KualiTestBase;
034: import org.kuali.kfs.context.SpringContext;
035: import org.kuali.test.ConfigureContext;
036:
037: import edu.iu.uis.eden.exception.WorkflowException;
038:
039: @ConfigureContext
040: public abstract class MaintenanceRuleTestBase extends KualiTestBase {
041: /**
042: * This method creates a minimal MaintenanceDocument instance, and populates it with the provided businessObject for the
043: * newMaintainable, and null for the oldMaintainable.
044: *
045: * @param newSubAccount - populated subAccount for the newMaintainable
046: * @return a populated MaintenanceDocument instance
047: */
048: protected MaintenanceDocument newMaintDoc(
049: PersistableBusinessObject newBo) {
050: return newMaintDoc(null, newBo);
051: }
052:
053: /**
054: * This method creates a minimal MaintenanceDocument instance, and populates it with the provided businessObjects for the
055: * newMaintainable and oldMaintainable.
056: *
057: * @param oldSubAccount - populated subAccount for the oldMaintainable
058: * @param newSubAccount - populated subAccount for the newMaintainable
059: * @return a populated MaintenanceDocument instance
060: */
061: protected MaintenanceDocument newMaintDoc(
062: PersistableBusinessObject oldBo,
063: PersistableBusinessObject newBo) {
064:
065: // disallow null value for newBo
066: if (null == newBo) {
067: throw new IllegalArgumentException(
068: "Invalid value (null) for newBo. "
069: + "This must always be a valid, populated BusinessObject instance.");
070: }
071:
072: // get a new MaintenanceDocument from Spring
073: MaintenanceDocument document = null;
074: try {
075: document = (MaintenanceDocument) SpringContext.getBean(
076: DocumentService.class).getNewDocument(
077: SpringContext.getBean(
078: MaintenanceDocumentDictionaryService.class)
079: .getDocumentTypeName(newBo.getClass()));
080: } catch (WorkflowException e) {
081: throw new RuntimeException(e);
082: }
083:
084: // add all the pieces
085: document.getDocumentHeader().setFinancialDocumentDescription(
086: "test");
087: if (null == oldBo) {
088: document
089: .setOldMaintainableObject(new KualiMaintainableImpl());
090: } else {
091: document
092: .setOldMaintainableObject(new KualiMaintainableImpl(
093: oldBo));
094: document.getOldMaintainableObject().setBoClass(
095: oldBo.getClass());
096: }
097: document.setNewMaintainableObject(new KualiMaintainableImpl(
098: newBo));
099: document.getNewMaintainableObject()
100: .setBoClass(newBo.getClass());
101: return document;
102: }
103:
104: /**
105: * This method creates a new instance of the specified ruleClass, injects the businessObject(s). With this method, the
106: * oldMaintainable will be set to null.
107: *
108: * @param newBo - the populated businessObject for the newMaintainble
109: * @param ruleClass - the class of rule to instantiate
110: * @return a populated and ready-to-test rule, of the specified class
111: */
112: protected MaintenanceDocumentRule setupMaintDocRule(
113: PersistableBusinessObject newBo, Class ruleClass) {
114: MaintenanceDocument maintDoc = newMaintDoc(newBo);
115: return setupMaintDocRule(maintDoc, ruleClass);
116: }
117:
118: /**
119: * This method first creates a new MaintenanceDocument with the BusinessObject(s) passed in. Note that the maintDoc is created
120: * and destroyed internally, and is never returned. This method then creates a new instance of the specified ruleClass, injects
121: * the businessObject(s).
122: *
123: * @param oldBo - the populated businessObject for the oldMaintainable
124: * @param newBo - the populated businessObject for the newMaintainable
125: * @param ruleClass - the class of rule to instantiate
126: * @return a populated and ready-to-test rule, of the specified class
127: */
128: protected MaintenanceDocumentRule setupMaintDocRule(
129: PersistableBusinessObject oldBo,
130: PersistableBusinessObject newBo, Class ruleClass) {
131:
132: MaintenanceDocument maintDoc = newMaintDoc(oldBo, newBo);
133:
134: return setupMaintDocRule(maintDoc, ruleClass);
135: }
136:
137: /**
138: * This method creates a new instance of the specified ruleClass, and then injects the maintenanceDocument and associated
139: * business objects.
140: *
141: * @param maintDoc - the populated MaintenanceDocument instance
142: * @param ruleClass - the class of rule to instantiate
143: * @return a populated and ready-to-test rule, of the specified class
144: */
145: protected MaintenanceDocumentRule setupMaintDocRule(
146: MaintenanceDocument maintDoc, Class ruleClass) {
147:
148: MaintenanceDocumentRule rule;
149: try {
150: rule = (MaintenanceDocumentRule) ruleClass.newInstance();
151: } catch (InstantiationException e) {
152: throw new RuntimeException(e);
153: } catch (IllegalAccessException e) {
154: throw new RuntimeException(e);
155: }
156:
157: rule.setupBaseConvenienceObjects(maintDoc);
158:
159: // confirm that we're starting with no errors
160: assertEquals(0, GlobalVariables.getErrorMap().size());
161:
162: return rule;
163: }
164:
165: protected void testDefaultExistenceCheck(
166: PersistableBusinessObject bo, String fieldName,
167: boolean shouldFail) {
168:
169: // init the error path
170: GlobalVariables.getErrorMap().addToErrorPath(
171: "document.newMaintainableObject");
172:
173: // run the dataDictionary validation
174: SpringContext.getBean(DictionaryValidationService.class)
175: .validateDefaultExistenceChecks(bo);
176:
177: // clear the error path
178: GlobalVariables.getErrorMap().removeFromErrorPath(
179: "document.newMaintainableObject");
180:
181: // assert that the existence of the error is what is expected
182: assertFieldErrorExistence(fieldName,
183: KFSKeyConstants.ERROR_EXISTENCE, shouldFail);
184:
185: }
186:
187: /**
188: * This method tests whether the expected number of errors exists in the errorMap. The assert will fail if this expected number
189: * isnt what is returned.
190: *
191: * @param expectedErrorCount - the number of errors expected
192: */
193: protected void assertErrorCount(int expectedErrorCount) {
194: assertEquals(expectedErrorCount, GlobalVariables.getErrorMap()
195: .getErrorCount());
196: }
197:
198: /**
199: * This method tests whether the field error exists and returns the result of this test.
200: *
201: * @param fieldName
202: * @param errorKey
203: * @return True if the error exists in the GlobalErrors, false if not.
204: */
205: protected boolean doesFieldErrorExist(String fieldName,
206: String errorKey) {
207: return GlobalVariables.getErrorMap().fieldHasMessage(
208: MaintenanceDocumentRuleBase.MAINTAINABLE_ERROR_PREFIX
209: + fieldName, errorKey);
210: }
211:
212: /**
213: * This method tests whether the existence check on the error matches what is expected by what is passed into expectedResult.
214: * This method will fail the assertion if the presence of the error is not what is expected.
215: *
216: * @param fieldName
217: * @param errorKey
218: * @param expectedResult - True if the error is expected, False if it is not.
219: */
220: protected void assertFieldErrorExistence(String fieldName,
221: String errorKey, boolean expectedResult) {
222: boolean result = doesFieldErrorExist(fieldName, errorKey);
223: assertEquals(
224: "Existence check for Error on fieldName/errorKey: "
225: + fieldName + "/" + errorKey + ". "
226: + GlobalVariables.getErrorMap(),
227: expectedResult, result);
228: }
229:
230: /**
231: * This method tests whether a given combination of fieldName and errorKey does NOT exist in the GlobalVariables.getErrorMap().
232: * The assert will fail if the fieldName & errorKey combination DOES exist. NOTE that fieldName should NOT include the prefix
233: * errorPath.
234: *
235: * @param fieldName - fieldName as it would be provided when adding the error
236: * @param errorKey - errorKey as it would be provided when adding the error
237: */
238: protected void assertFieldErrorDoesNotExist(String fieldName,
239: String errorKey) {
240: boolean result = doesFieldErrorExist(fieldName, errorKey);
241: assertTrue("FieldName (" + fieldName
242: + ") should NOT contain errorKey: " + errorKey, !result);
243: }
244:
245: /**
246: * This method tests whether a given combination of fieldName and errorKey exists in the GlobalVariables.getErrorMap(). The
247: * assert will fail if the fieldName & errorKey combination doesnt exist. NOTE that fieldName should NOT include the prefix
248: * errorPath.
249: *
250: * @param fieldName - fieldName as it would be provided when adding the error
251: * @param errorKey - errorKey as it would be provided when adding the error
252: */
253: protected void assertFieldErrorExists(String fieldName,
254: String errorKey) {
255: boolean result = GlobalVariables.getErrorMap().fieldHasMessage(
256: MaintenanceDocumentRuleBase.MAINTAINABLE_ERROR_PREFIX
257: + fieldName, errorKey);
258: assertTrue("FieldName (" + fieldName
259: + ") should contain errorKey: " + errorKey, result);
260: }
261:
262: /**
263: * This method tests whether a given errorKey exists on the document itself (ie, not tied to a specific field). The assert will
264: * fail if the errorKey already exists on the document.
265: *
266: * @param errorKey - errorKey as it would be provided when adding the error
267: */
268: protected void assertGlobalErrorExists(String errorKey) {
269: boolean result = GlobalVariables.getErrorMap().fieldHasMessage(
270: KFSConstants.DOCUMENT_ERRORS, errorKey);
271: assertTrue("Document should contain errorKey: " + errorKey,
272: result);
273: }
274:
275: /**
276: * This method is used during debugging to dump the contents of the error map, including the key names. It is not used by the
277: * application in normal circumstances at all.
278: */
279: protected void showErrorMap() {
280:
281: if (GlobalVariables.getErrorMap().isEmpty()) {
282: return;
283: }
284:
285: for (Iterator i = GlobalVariables.getErrorMap().entrySet()
286: .iterator(); i.hasNext();) {
287: Map.Entry e = (Map.Entry) i.next();
288:
289: TypedArrayList errorList = (TypedArrayList) e.getValue();
290: for (Iterator j = errorList.iterator(); j.hasNext();) {
291: ErrorMessage em = (ErrorMessage) j.next();
292:
293: if (em.getMessageParameters() == null) {
294: System.err.println(e.getKey().toString() + " = "
295: + em.getErrorKey());
296: } else {
297: StringBuffer messageParams = new StringBuffer();
298: String delim = "";
299: for (int k = 0; k < em.getMessageParameters().length; k++) {
300: messageParams.append(delim + "'"
301: + em.getMessageParameters()[k] + "'");
302: if ("".equals(delim)) {
303: delim = ", ";
304: }
305: }
306: System.err.println(e.getKey().toString() + " = "
307: + em.getErrorKey() + " : "
308: + messageParams.toString());
309: }
310: }
311: }
312:
313: }
314: }
|