001: /*
002: * Copyright 2005-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.service;
017:
018: import java.util.ArrayList;
019: import java.util.Iterator;
020: import java.util.List;
021: import java.util.Map;
022:
023: import junit.framework.AssertionFailedError;
024:
025: import org.apache.commons.lang.StringUtils;
026: import org.kuali.core.bo.BusinessObjectRelationship;
027: import org.kuali.core.bo.PersistableBusinessObject;
028: import org.kuali.core.document.Document;
029: import org.kuali.core.exceptions.ClassNotPersistableException;
030: import org.kuali.core.exceptions.ReferenceAttributeDoesntExistException;
031: import org.kuali.core.lookup.KualiLookupableImpl;
032: import org.kuali.core.service.impl.PersistenceTestServiceImpl;
033: import org.kuali.core.util.ObjectUtils;
034: import org.kuali.kfs.KFSConstants;
035: import org.kuali.kfs.bo.AccountingLineBase;
036: import org.kuali.kfs.bo.SourceAccountingLine;
037: import org.kuali.kfs.context.KualiTestBase;
038: import org.kuali.kfs.context.SpringContext;
039: import org.kuali.kfs.lookup.LookupableSpringContext;
040: import org.kuali.module.chart.bo.Account;
041: import org.kuali.module.chart.bo.AccountDescription;
042: import org.kuali.module.chart.bo.AccountGuideline;
043: import org.kuali.module.chart.bo.Chart;
044: import org.kuali.module.chart.bo.ObjectCode;
045: import org.kuali.module.chart.bo.Org;
046: import org.kuali.module.chart.bo.SubObjCd;
047: import org.kuali.module.chart.service.SubObjectCodeService;
048: import org.kuali.module.financial.document.InternalBillingDocument;
049: import org.kuali.module.labor.document.LaborJournalVoucherDocument;
050: import org.kuali.test.ConfigureContext;
051: import org.kuali.test.suite.AnnotationTestSuite;
052: import org.kuali.test.suite.LookupRefactoringSuite;
053:
054: /**
055: * This class tests the Persistence service.
056: *
057: * @todo Fix commented out methods to work with migration away from anonymous keys. These were commented out so this doesn't hamper
058: * the unit test refactoring.
059: */
060: @ConfigureContext
061: @AnnotationTestSuite(LookupRefactoringSuite.class)
062: public class PersistenceServiceTest extends KualiTestBase {
063:
064: protected static org.apache.log4j.Logger LOG = org.apache.log4j.Logger
065: .getLogger(PersistenceServiceTest.class);
066:
067: private static final String LOOKUP_PACKAGE = "org.kuali.web.lookup.lookupable.";
068:
069: private PersistenceTestServiceImpl persistenceServiceTest = new PersistenceTestServiceImpl();
070:
071: private SubObjCd globalSubObjCd;
072:
073: @Override
074: protected void setUp() throws Exception {
075: super .setUp();
076: globalSubObjCd = SpringContext.getBean(
077: SubObjectCodeService.class).getByPrimaryId(
078: TestConstants.Data4.POSTING_YEAR,
079: TestConstants.Data4.CHART_CODE,
080: TestConstants.Data4.ACCOUNT,
081: TestConstants.Data4.OBJECT_CODE,
082: TestConstants.Data4.SUBOBJECT_CODE);
083: }
084:
085: public final void testHasPrimaryKeyFieldValues_completeObject()
086: throws Exception {
087: SubObjCd soc = (SubObjCd) ObjectUtils.deepCopy(globalSubObjCd);
088: assertNotNull(soc);
089:
090: assertTrue(SpringContext.getBean(
091: PersistenceStructureService.class)
092: .hasPrimaryKeyFieldValues(soc));
093:
094: SpringContext.getBean(PersistenceStructureService.class)
095: .clearPrimaryKeyFields(soc);
096: assertFalse(SpringContext.getBean(
097: PersistenceStructureService.class)
098: .hasPrimaryKeyFieldValues(soc));
099: }
100:
101: public final void testListPrimaryKeyFields_invalidClass() {
102: boolean failedAsExpected = false;
103:
104: try {
105: SpringContext.getBean(PersistenceStructureService.class)
106: .listPrimaryKeyFieldNames(null);
107: } catch (IllegalArgumentException e) {
108: failedAsExpected = true;
109: }
110:
111: assertTrue(failedAsExpected);
112: }
113:
114: public final void testListPrimaryKeyFields_unpersistableClass() {
115: boolean failedAsExpected = false;
116:
117: try {
118: SpringContext.getBean(PersistenceStructureService.class)
119: .listPrimaryKeyFieldNames(Integer.class);
120: } catch (ClassNotPersistableException e) {
121: failedAsExpected = true;
122: }
123:
124: assertTrue(failedAsExpected);
125: }
126:
127: public final void testListPrimaryKeyFields_lookupableClass() {
128: KualiLookupableImpl i = (KualiLookupableImpl) LookupableSpringContext
129: .getLookupable(KFSConstants.KUALI_LOOKUPABLE_IMPL);
130: i.setBusinessObjectClass(ObjectCode.class);
131:
132: List explicitKeys = i.getReturnKeys();
133: List implicitKeys = SpringContext.getBean(
134: PersistenceStructureService.class)
135: .listPrimaryKeyFieldNames(ObjectCode.class);
136:
137: // Return keys of lookup now returns all fields
138: assertEquals(explicitKeys.size(), implicitKeys.size());
139: assertTrue(explicitKeys.containsAll(implicitKeys));
140: }
141:
142: public final void testListPrimaryKeyFields_documentClasses()
143: throws InstantiationException, IllegalAccessException {
144: List documentClasses = listSuffixedSubclasses(Document.class,
145: "org.kuali.bo", "Document");
146:
147: List failures = new ArrayList();
148: for (Iterator i = documentClasses.iterator(); i.hasNext();) {
149: Class c = (Class) i.next();
150:
151: Document d = (Document) c.newInstance();
152:
153: List explicitKeys = new ArrayList();
154: explicitKeys.add("documentHeaderId");
155:
156: List implicitKeys = SpringContext.getBean(
157: PersistenceStructureService.class)
158: .listPrimaryKeyFieldNames(d.getClass());
159:
160: try {
161: assertEquals(explicitKeys.size(), implicitKeys.size());
162: assertTrue(explicitKeys.containsAll(implicitKeys));
163: } catch (AssertionFailedError e) {
164: failures.add(d.getClass().getName() + "("
165: + e.getMessage() + ")");
166: }
167: }
168:
169: if (failures.size() > 0) {
170: StringBuffer failed = new StringBuffer(
171: "tests failed for document");
172: if (failures.size() > 1) {
173: failed.append("s");
174: }
175: failed.append(": ");
176: for (Iterator i = failures.iterator(); i.hasNext();) {
177: String msg = (String) i.next();
178:
179: failed.append(msg);
180: if (i.hasNext()) {
181: failed.append(", ");
182: }
183: }
184:
185: fail(failed.toString());
186: }
187: }
188:
189: public final void testListPersistableSubclasses_invalidClass() {
190: boolean failedAsExpected = false;
191:
192: try {
193: SpringContext.getBean(PersistenceStructureService.class)
194: .listPersistableSubclasses(null);
195: } catch (IllegalArgumentException e) {
196: failedAsExpected = true;
197: }
198:
199: assertTrue(failedAsExpected);
200: }
201:
202: public final void testListPersistableSubclasses_super class() {
203: List subclasses = SpringContext.getBean(
204: PersistenceStructureService.class)
205: .listPersistableSubclasses(Document.class);
206:
207: assertTrue(subclasses.size() > 0);
208: assertTrue(subclasses.contains(InternalBillingDocument.class));
209: }
210:
211: public final void testListPersistableSubclasses_nonSuperclass() {
212: List subclasses = SpringContext.getBean(
213: PersistenceStructureService.class)
214: .listPersistableSubclasses(
215: LaborJournalVoucherDocument.class);
216:
217: assertEquals(0, subclasses.size());
218: }
219:
220: public final void testGetPrimaryKeyFieldValues_invalidObject() {
221: boolean failedAsExpected = false;
222:
223: try {
224: SpringContext.getBean(PersistenceService.class)
225: .getPrimaryKeyFieldValues(null);
226: } catch (IllegalArgumentException e) {
227: failedAsExpected = true;
228: }
229:
230: assertTrue(failedAsExpected);
231: }
232:
233: public final void testGetPrimaryKeyFieldValues_unpersistableObject() {
234: boolean failedAsExpected = false;
235:
236: try {
237: SpringContext.getBean(PersistenceService.class)
238: .getPrimaryKeyFieldValues("unpersistable object");
239: } catch (ClassNotPersistableException e) {
240: failedAsExpected = true;
241: }
242:
243: assertTrue(failedAsExpected);
244: }
245:
246: public final void testGetPrimaryKeyFieldValues_persistableObject()
247: throws Exception {
248: SubObjCd soc = (SubObjCd) ObjectUtils.deepCopy(globalSubObjCd);
249: assertNotNull(soc);
250:
251: Map keyValues = SpringContext.getBean(PersistenceService.class)
252: .getPrimaryKeyFieldValues(soc);
253: assertEquals(5, keyValues.size());
254:
255: assertTrue(keyValues.containsKey("financialObjectCode"));
256: assertEquals(soc.getFinancialObject().getFinancialObjectCode(),
257: keyValues.get("financialObjectCode"));
258:
259: assertTrue(keyValues.containsKey("accountNumber"));
260: assertEquals(soc.getAccount().getAccountNumber(), keyValues
261: .get("accountNumber"));
262:
263: assertTrue(keyValues.containsKey("chartOfAccountsCode"));
264: assertEquals(soc.getAccount().getChartOfAccountsCode(),
265: keyValues.get("chartOfAccountsCode"));
266:
267: assertTrue(keyValues.containsKey("financialSubObjectCode"));
268: assertEquals(soc.getFinancialSubObjectCode(), keyValues
269: .get("financialSubObjectCode"));
270:
271: assertTrue(keyValues.containsKey("universityFiscalYear"));
272: assertEquals(soc.getUniversityFiscal()
273: .getUniversityFiscalYear(), keyValues
274: .get("universityFiscalYear"));
275: }
276:
277: // public final void testGetPrimaryKeyFieldValues_persistableObject_someNullKeys() throws Exception {
278: // SubObjCd soc = (SubObjCd) ObjectUtils.deepCopy(globalSubObjCd);
279: // assertNotNull(soc);
280: // soc.getAccount().setAccountNumber(null);
281: // soc.getUniversityFiscal().setUniversityFiscalYear(null);
282: //
283: // Map keyValues = persistenceService.getPrimaryKeyFieldValues(soc);
284: // assertEquals(5, keyValues.size());
285: // assertTrue(keyValues.containsKey("financialObjectCode"));
286: // assertEquals(soc.getFinancialObject().getFinancialObjectCode(), keyValues.get("financialObjectCode"));
287: //
288: // assertTrue(keyValues.containsKey("accountNumber"));
289: // assertNull(keyValues.get("accountNumber"));
290: //
291: // assertTrue(keyValues.containsKey("chartOfAccountsCode"));
292: // assertEquals(soc.getAccount().getChartOfAccountsCode(), keyValues.get("chartOfAccountsCode"));
293: //
294: // assertTrue(keyValues.containsKey("financialSubObjectCode"));
295: // assertEquals(soc.getFinancialSubObjectCode(), keyValues.get("financialSubObjectCode"));
296: //
297: // assertTrue(keyValues.containsKey("universityFiscalYear"));
298: // assertNull(keyValues.get("universityFiscalYear"));
299: // }
300:
301: public final void testGetPrimaryKeyFieldValues_sortedKeyFields()
302: throws Exception {
303: SubObjCd soc = (SubObjCd) ObjectUtils.deepCopy(globalSubObjCd);
304: assertNotNull(soc);
305: soc.getAccount().setAccountNumber(null);
306: soc.getUniversityFiscal().setUniversityFiscalYear(null);
307:
308: String[] sortedKeyFields = { "accountNumber",
309: "chartOfAccountsCode", "financialObjectCode",
310: "financialSubObjectCode", "universityFiscalYear" };
311:
312: Map keyValues = SpringContext.getBean(PersistenceService.class)
313: .getPrimaryKeyFieldValues(soc, true);
314: assertEquals(5, keyValues.size());
315:
316: int index = 0;
317: for (Iterator i = keyValues.entrySet().iterator(); i.hasNext();) {
318: Map.Entry e = (Map.Entry) i.next();
319:
320: assertEquals(sortedKeyFields[index++], e.getKey());
321: }
322: }
323:
324: public final void testFlattenPrimaryKeyFieldValues_invalidObject()
325: throws Exception {
326: boolean failedAsExpected = false;
327:
328: try {
329: SpringContext.getBean(PersistenceService.class)
330: .getFlattenedPrimaryKeyFieldValues(null);
331: } catch (IllegalArgumentException e) {
332: failedAsExpected = true;
333: }
334:
335: assertTrue(failedAsExpected);
336: }
337:
338: public final void testFlattenPrimaryKeyFieldValues_unpersistableObject()
339: throws Exception {
340: boolean failedAsExpected = false;
341:
342: try {
343: SpringContext.getBean(PersistenceService.class)
344: .getFlattenedPrimaryKeyFieldValues(
345: "unpersistable object");
346: } catch (ClassNotPersistableException e) {
347: failedAsExpected = true;
348: }
349:
350: assertTrue(failedAsExpected);
351: }
352:
353: public final void testFlattenPrimaryKeyFieldValues_persistableObject()
354: throws Exception {
355: SubObjCd soc = (SubObjCd) ObjectUtils.deepCopy(globalSubObjCd);
356: assertNotNull(soc);
357:
358: String expected = SubObjCd.class.getName() + "(accountNumber="
359: + TestConstants.Data4.ACCOUNT + ",chartOfAccountsCode="
360: + TestConstants.Data4.CHART_CODE
361: + ",financialObjectCode="
362: + TestConstants.Data4.OBJECT_CODE
363: + ",financialSubObjectCode="
364: + TestConstants.Data4.SUBOBJECT_CODE
365: + ",universityFiscalYear="
366: + TestConstants.Data4.POSTING_YEAR + ")";
367: String flattened = SpringContext.getBean(
368: PersistenceService.class)
369: .getFlattenedPrimaryKeyFieldValues(soc);
370: assertEquals(expected, flattened);
371: }
372:
373: // public final void testFlattenPrimaryKeyFieldValues_persistableObject_someNullKeys() throws Exception {
374: // SubObjCd soc = (SubObjCd) ObjectUtils.deepCopy(globalSubObjCd);
375: // assertNotNull(soc);
376: // soc.getAccount().setAccountNumber(null);
377: // soc.getUniversityFiscal().setUniversityFiscalYear(null);
378: //
379: // String expected = SubObjCd.class.getName()+"(accountNumber=null,chartOfAccountsCode="
380: // + TestConstants.Data4.CHART_CODE + ",financialObjectCode=" + TestConstants.Data4.OBJECT_CODE
381: // + ",financialSubObjectCode=" + TestConstants.Data4.SUBOBJECT_CODE + ",universityFiscalYear=null)";
382: // String flattened = persistenceService.getFlattenedPrimaryKeyFieldValues(soc);
383: // assertEquals(expected, flattened);
384: // }
385:
386: public final void testClearPrimaryKeyFields_invalidObject() {
387: boolean failedAsExpected = false;
388:
389: try {
390: SpringContext.getBean(PersistenceStructureService.class)
391: .clearPrimaryKeyFields(null);
392: } catch (IllegalArgumentException e) {
393: failedAsExpected = true;
394: }
395:
396: assertTrue(failedAsExpected);
397: }
398:
399: public final void testClearPrimaryKeyFields_unpersistableObject() {
400: boolean failedAsExpected = false;
401:
402: try {
403: SpringContext.getBean(PersistenceStructureService.class)
404: .clearPrimaryKeyFields("unpersistable object");
405: } catch (ClassNotPersistableException e) {
406: failedAsExpected = true;
407: }
408:
409: assertTrue(failedAsExpected);
410: }
411:
412: public final void testClearPrimaryKeyFieldValues_persistableObject()
413: throws Exception {
414: SubObjCd soc = (SubObjCd) ObjectUtils.deepCopy(globalSubObjCd);
415: assertNotNull(soc);
416:
417: SpringContext.getBean(PersistenceStructureService.class)
418: .clearPrimaryKeyFields(soc);
419:
420: Map keyValues = SpringContext.getBean(PersistenceService.class)
421: .getPrimaryKeyFieldValues(soc);
422: assertEquals(5, keyValues.size());
423:
424: assertTrue(keyValues.containsKey("financialObjectCode"));
425: assertNull(keyValues.get("financialObjectCode"));
426:
427: assertTrue(keyValues.containsKey("accountNumber"));
428: assertNull(keyValues.get("accountNumber"));
429:
430: assertTrue(keyValues.containsKey("chartOfAccountsCode"));
431: assertNull(keyValues.get("chartOfAccountsCode"));
432:
433: assertTrue(keyValues.containsKey("financialSubObjectCode"));
434: assertNull(keyValues.get("financialSubObjectCode"));
435:
436: assertTrue(keyValues.containsKey("universityFiscalYear"));
437: assertNull(keyValues.get("universityFiscalYear"));
438: }
439:
440: // public final void testClearPrimaryKeyFieldValues_persistableObject_someNullKeys() throws Exception {
441: // SubObjCd soc = (SubObjCd) ObjectUtils.deepCopy(globalSubObjCd);
442: // assertNotNull(soc);
443: // soc.getAccount().setAccountNumber(null);
444: // soc.getUniversityFiscal().setUniversityFiscalYear(null);
445: //
446: // persistenceService.clearPrimaryKeyFields(soc);
447: //
448: // Map keyValues = persistenceService.getPrimaryKeyFieldValues(soc);
449: // assertEquals(5, keyValues.size());
450: // assertTrue(keyValues.containsKey("financialObjectCode"));
451: // assertEquals(soc.getFinancialObject().getFinancialObjectCode(), keyValues.get("financialObjectCode"));
452: //
453: // assertTrue(keyValues.containsKey("accountNumber"));
454: // assertEquals(soc.getAccount().getAccountNumber(), keyValues.get("accountNumber"));
455: //
456: // assertTrue(keyValues.containsKey("chartOfAccountsCode"));
457: // assertEquals(soc.getChartOfAccounts().getChartOfAccountsCode(), keyValues.get("chartOfAccountsCode"));
458: //
459: // assertTrue(keyValues.containsKey("financialSubObjectCode"));
460: // assertEquals(soc.getFinancialSubObjectCode(), keyValues.get("financialSubObjectCode"));
461: //
462: // assertTrue(keyValues.containsKey("universityFiscalYear"));
463: // assertEquals(soc.getUniversityFiscal().getUniversityFiscalYear(), keyValues.get("universityFiscalYear"));
464: // }
465:
466: public final void testRetrieveNonKeyFields_validObject()
467: throws Exception {
468: AccountingLineBase accountingLine = createValidAccountingLine();
469: assertNull(accountingLine.getChart()
470: .getFinChartOfAccountDescription());
471:
472: SpringContext.getBean(PersistenceService.class)
473: .retrieveNonKeyFields(accountingLine);
474:
475: assertNotNull(accountingLine.getChart()
476: .getFinChartOfAccountDescription());
477: assertNotNull(accountingLine.getObjectCode()
478: .getFinancialObjectCode());
479: assertEquals(
480: accountingLine.getChart().getChartOfAccountsCode(),
481: TestConstants.Data4.CHART_CODE);
482:
483: }
484:
485: // public final void testRetrieveNonKeyFields_invalidObject() throws Exception {
486: // AccountingLineBase accountingLine = createValidAccountingLine();
487: // accountingLine.getChart().setChartOfAccountsCode("INVALID COA");
488: //
489: // persistenceService.retrieveNonKeyFields(accountingLine);
490: //
491: // assertNull(accountingLine.getChart().getFinChartOfAccountDescription());
492: // }
493:
494: public final void testHasPrimaryKeyFieldValues_invalidObject() {
495: boolean failedAsExpected = false;
496:
497: try {
498: SpringContext.getBean(PersistenceStructureService.class)
499: .hasPrimaryKeyFieldValues(null);
500: } catch (IllegalArgumentException e) {
501: failedAsExpected = true;
502: }
503:
504: assertTrue(failedAsExpected);
505: }
506:
507: public final void testHasPrimaryKeyFieldValues_unpersistableObject() {
508: boolean failedAsExpected = false;
509:
510: try {
511: SpringContext.getBean(PersistenceStructureService.class)
512: .hasPrimaryKeyFieldValues("unpersistable object");
513: } catch (ClassNotPersistableException e) {
514: failedAsExpected = true;
515: }
516:
517: assertTrue(failedAsExpected);
518: }
519:
520: // public final void testHasPrimaryKeyFieldValues_incompleteObject() throws Exception {
521: // SubObjCd soc = (SubObjCd) ObjectUtils.deepCopy(globalSubObjCd);
522: // assertNotNull(soc);
523: //
524: // soc.getAccount().setAccountNumber(null);
525: // soc.getUniversityFiscal().setUniversityFiscalYear(null);
526: //
527: // assertFalse(persistenceService.hasPrimaryKeyFieldValues(soc));
528: // }
529:
530: // public final void testRetrieveAllReferenceKeys() throws Exception {
531: // AccountingLine line = new SourceAccountingLine();
532: // line.setPostingYear(TestConstants.Data4.POSTING_YEAR);
533: // line.getChart().setChartOfAccountsCode(TestConstants.Data4.CHART_CODE);
534: // line.getAccount().setAccountNumber(TestConstants.Data4.ACCOUNT);
535: // line.getObjectCode().setFinancialObjectCode(TestConstants.Data4.OBJECT_CODE);
536: // line.getSubAccount().setSubAccountNumber(TestConstants.Data4.SUBACCOUNT);
537: // line.getSubObjectCode().setFinancialSubObjectCode(TestConstants.Data4.SUBOBJECT_CODE);
538: // line.getProject().setCode(TestConstants.Data4.PROJECT_CODE);
539: //
540: // persistenceService.linkObjects(line);
541: // assertEquals(line.getChart().getChartOfAccountsCode(), TestConstants.Data4.CHART_CODE);
542: //
543: // assertEquals(line.getAccount().getChartOfAccounts().getChartOfAccountsCode(), TestConstants.Data4.CHART_CODE);
544: // assertEquals(line.getAccount().getAccountNumber(), TestConstants.Data4.ACCOUNT);
545: //
546: // assertEquals(line.getSubAccount().getChartOfAccounts().getChartOfAccountsCode(), TestConstants.Data4.CHART_CODE);
547: // assertEquals(line.getSubAccount().getAccount().getAccountNumber(), TestConstants.Data4.ACCOUNT);
548: // assertEquals(line.getSubAccount().getSubAccountNumber(), TestConstants.Data4.SUBACCOUNT);
549: //
550: // assertEquals(line.getObjectCode().getChartOfAccounts().getChartOfAccountsCode(), TestConstants.Data4.CHART_CODE);
551: // assertEquals(line.getObjectCode().getUniversityFiscal().getUniversityFiscalYear(), TestConstants.Data4.POSTING_YEAR);
552: // assertEquals(line.getObjectCode().getFinancialObjectCode(), TestConstants.Data4.OBJECT_CODE);
553: //
554: // assertEquals(line.getSubObjectCode().getChartOfAccounts().getChartOfAccountsCode(), TestConstants.Data4.CHART_CODE);
555: // assertEquals(line.getSubObjectCode().getUniversityFiscal().getUniversityFiscalYear(), TestConstants.Data4.POSTING_YEAR);
556: // assertEquals(line.getSubObjectCode().getAccount().getAccountNumber(), TestConstants.Data4.ACCOUNT);
557: // assertEquals(line.getSubObjectCode().getFinancialObject().getFinancialObjectCode(), TestConstants.Data4.OBJECT_CODE);
558: // assertEquals(line.getSubObjectCode().getFinancialSubObjectCode(), TestConstants.Data4.SUBOBJECT_CODE);
559: //
560: // assertEquals(line.getProject().getCode(), TestConstants.Data4.PROJECT_CODE);
561: // }
562:
563: // public final void testRetrieveAllNonKeyFields() {
564: //
565: // Map pkMap = new HashMap();
566: //
567: // AccountingLine line = new SourceAccountingLine();
568: // line.setPostingYear(TestConstants.Data4.POSTING_YEAR);
569: //
570: // line.getChart().setChartOfAccountsCode(TestConstants.Data4.CHART_CODE);
571: //
572: // line.getAccount().getChartOfAccounts().setChartOfAccountsCode(TestConstants.Data4.CHART_CODE);
573: // line.getAccount().setAccountNumber(TestConstants.Data4.ACCOUNT);
574: //
575: // pkMap.clear();
576: // pkMap.put("chartOfAccountsCode", TestConstants.Data4.CHART_CODE);
577: // line.getSubAccount().setChartOfAccounts((Chart) businessObjectService.findByPrimaryKey(Chart.class, pkMap));
578: // pkMap.clear();
579: // pkMap.put("accountNumber", TestConstants.Data4.ACCOUNT);
580: // line.getSubAccount().setAccount((Account) businessObjectService.findByPrimaryKey(Account.class, pkMap));
581: // line.getSubAccount().setSubAccountNumber(TestConstants.Data4.SUBACCOUNT);
582: //
583: // line.getObjectCode().getUniversityFiscal().setUniversityFiscalYear(TestConstants.Data4.POSTING_YEAR);
584: // line.getObjectCode().getChartOfAccounts().setChartOfAccountsCode(TestConstants.Data4.CHART_CODE);
585: // line.getObjectCode().setFinancialObjectCode(TestConstants.Data4.OBJECT_CODE);
586: //
587: // persistenceService.retrieveNonKeyFields(line);
588: // assertNotNull(line.getAccount());
589: // assertNotNull(line.getSubAccount());
590: // assertNotNull(line.getObjectCode());
591: // }
592:
593: public final void testGetForeignKeyName() {
594: String fkName = SpringContext.getBean(
595: PersistenceStructureService.class)
596: .getForeignKeyFieldName(Account.class,
597: "reportsToAccount", "chartOfAccountsCode");
598: assertEquals("reportsToChartOfAccountsCode", fkName);
599:
600: fkName = SpringContext.getBean(
601: PersistenceStructureService.class)
602: .getForeignKeyFieldName(Account.class,
603: "reportsToAccount", "accountNumber");
604: assertEquals("reportsToAccountNumber", fkName);
605:
606: fkName = SpringContext.getBean(
607: PersistenceStructureService.class)
608: .getForeignKeyFieldName(Account.class,
609: "accountGuideline", "chartOfAccountsCode");
610: assertEquals("chartOfAccountsCode", fkName);
611: try {
612: fkName = SpringContext.getBean(
613: PersistenceStructureService.class)
614: .getForeignKeyFieldName(Account.class,
615: "chartOfAccountsCode",
616: "chartOfAccountsCode");
617: fail("Did not throw exception for invalid attribute reference name");
618: } catch (RuntimeException e) {
619: }
620:
621: }
622:
623: public final void testGetReferencesForForeignKey() {
624: Map referenceClasses = SpringContext.getBean(
625: PersistenceStructureService.class)
626: .getReferencesForForeignKey(Account.class,
627: "chartOfAccountsCode");
628: assertTrue(referenceClasses != null
629: && referenceClasses.size() == 4);
630:
631: assertTrue(referenceClasses.containsKey("chartOfAccounts"));
632: assertTrue(referenceClasses.get("chartOfAccounts").equals(
633: Chart.class));
634: assertTrue(referenceClasses.containsKey("organization"));
635: assertTrue(referenceClasses.get("organization").equals(
636: Org.class));
637: assertTrue(referenceClasses.containsKey("accountGuideline"));
638: assertTrue(referenceClasses.get("accountGuideline").equals(
639: AccountGuideline.class));
640: assertTrue(referenceClasses.containsKey("accountDescription"));
641: assertTrue(referenceClasses.get("accountDescription").equals(
642: AccountDescription.class));
643:
644: referenceClasses = SpringContext.getBean(
645: PersistenceStructureService.class)
646: .getReferencesForForeignKey(Account.class,
647: "indirectCostRcvyFinCoaCode");
648: assertTrue(referenceClasses != null
649: && referenceClasses.size() == 1);
650: assertTrue(referenceClasses.get("indirectCostRecoveryAcct")
651: .equals(Account.class));
652:
653: referenceClasses = SpringContext.getBean(
654: PersistenceStructureService.class)
655: .getReferencesForForeignKey(Account.class,
656: "indirectCostRecoveryAcctNbr");
657: assertTrue(referenceClasses != null
658: && referenceClasses.size() == 1);
659: assertTrue(referenceClasses.get("indirectCostRecoveryAcct")
660: .equals(Account.class));
661:
662: referenceClasses = SpringContext.getBean(
663: PersistenceStructureService.class)
664: .getReferencesForForeignKey(Account.class, "junk");
665: assertTrue(referenceClasses != null
666: && referenceClasses.size() == 0);
667: }
668:
669: public final void testGetForeignKeysForReference_nullClazzArgument() {
670:
671: try {
672: SpringContext.getBean(PersistenceStructureService.class)
673: .getForeignKeysForReference(null, "accountNumber");
674: } catch (IllegalArgumentException e) {
675: assertTrue("An IllegalArgumentException should be thrown.",
676: true);
677: return;
678: } catch (Exception e) {
679: assertEquals(IllegalArgumentException.class.getName(), e
680: .getClass().getName());
681: }
682: assertTrue(
683: "An IllegalArgumentException should have been thrown.",
684: false);
685:
686: }
687:
688: public final void testGetForeignKeysForReference_nullAttributeNameArgument() {
689:
690: try {
691: SpringContext.getBean(PersistenceStructureService.class)
692: .getForeignKeysForReference(Account.class, null);
693: } catch (IllegalArgumentException e) {
694: assertTrue("An IllegalArgumentException should be thrown.",
695: true);
696: return;
697: } catch (Exception e) {
698: assertEquals(IllegalArgumentException.class.getName(), e
699: .getClass().getName());
700: }
701: assertTrue(
702: "An IllegalArgumentException should have been thrown.",
703: false);
704:
705: }
706:
707: public final void testGetForeignKeysForReference_attributeNameDoesntExist() {
708:
709: try {
710: SpringContext.getBean(PersistenceStructureService.class)
711: .getForeignKeysForReference(Account.class,
712: "gobbledyGook");
713: } catch (ReferenceAttributeDoesntExistException e) {
714: assertTrue(
715: "A ReferenceAttributeDoesntExistException should be thrown.",
716: true);
717: return;
718: } catch (Exception e) {
719: assertEquals(ReferenceAttributeDoesntExistException.class
720: .getName(), e.getClass().getName());
721: }
722: assertTrue(
723: "A ReferenceAttributeDoesntExistException should have been thrown.",
724: false);
725:
726: }
727:
728: public final void testGetForeignKeysForReference_attributeNameDoesExist() {
729:
730: Map fkMap = null;
731: try {
732: fkMap = SpringContext.getBean(
733: PersistenceStructureService.class)
734: .getForeignKeysForReference(Account.class,
735: "organization");
736: } catch (ReferenceAttributeDoesntExistException e) {
737: e.printStackTrace();
738: assertTrue(
739: "A ReferenceAttributeDoesntExistException should not be thrown.",
740: false);
741: } catch (Exception e) {
742: e.printStackTrace();
743: assertTrue("An exception should not have been thrown.",
744: false);
745: }
746: assertTrue("A non-null Map should have been returned.",
747: fkMap != null);
748: assertTrue("A non-empty Map should have been returned.", !fkMap
749: .isEmpty());
750:
751: }
752:
753: public final void testGetForeignKeysForReference_attributeHasNoGetter() {
754:
755: try {
756: SpringContext.getBean(PersistenceStructureService.class)
757: .getForeignKeysForReference(Account.class,
758: "serialVersionUID");
759: } catch (ReferenceAttributeDoesntExistException e) {
760: assertTrue(
761: "A ReferenceAttributeDoesntExistException should have been thrown.",
762: true);
763: return;
764: } catch (Exception e) {
765: assertTrue(
766: "A ReferenceAttributeDoesntExistException should have been thrown.",
767: false);
768: }
769: assertTrue(
770: "A ReferenceAttributeDoesntExistException should have been thrown.",
771: false);
772:
773: }
774:
775: public final void testGetForeignKeysForReference_attributeClassNotBusinessObjectDescendent() {
776:
777: try {
778: SpringContext.getBean(PersistenceStructureService.class)
779: .getForeignKeysForReference(Account.class,
780: "accountNumber");
781: fail("A ReferenceAttributeDoesntExistException should have been thrown.");
782: } catch (ReferenceAttributeDoesntExistException expected) {
783: // success: A ReferenceAttributeDoesntExistException was thrown
784: return;
785: } catch (Exception e) {
786: fail("A ReferenceAttributeDoesntExistException should have been thrown. Instead: "
787: + e.getClass().getName() + " : " + e.getMessage());
788: e.printStackTrace();
789: }
790:
791: }
792:
793: /*
794: * public final void testGetForeignKeysForReference_attributeNotReference() { // TODO: I dont know how to test this, I cant
795: * think of any classes that // have contained objects that are BusinessObjects, but are not // declared as
796: * reference-descriptors in the OJB-repository. }
797: */
798: public final void testGetForeignKeysForReference_attributeHasOneForeignKey() {
799:
800: Map fkMap = null;
801: try {
802: fkMap = SpringContext.getBean(
803: PersistenceStructureService.class)
804: .getForeignKeysForReference(Account.class,
805: "chartOfAccounts");
806: } catch (Exception e) {
807: e.printStackTrace();
808: assertTrue("An exception should not have been thrown.",
809: false);
810: }
811: // showFkMap(fkMap);
812: assertTrue("A non-null Map should have been returned.",
813: fkMap != null);
814: assertTrue("A non-empty Map should have been returned.", !fkMap
815: .isEmpty());
816: assertEquals(1, fkMap.size());
817:
818: }
819:
820: public final void testGetForeignKeysForReference_attributeHasTwoForeignKeys() {
821:
822: Map fkMap = null;
823: try {
824: fkMap = SpringContext.getBean(
825: PersistenceStructureService.class)
826: .getForeignKeysForReference(Account.class,
827: "continuationAccount");
828: } catch (Exception e) {
829: e.printStackTrace();
830: assertTrue("An exception should not have been thrown.",
831: false);
832: }
833: // showFkMap(fkMap);
834: assertTrue("A non-null Map should have been returned.",
835: fkMap != null);
836: assertTrue("A non-empty Map should have been returned.", !fkMap
837: .isEmpty());
838: assertEquals(2, fkMap.size());
839:
840: }
841:
842: public final void testGetForeignKeysForReference_attributeHasThreeForeignKeys() {
843:
844: Map fkMap = null;
845: try {
846: fkMap = SpringContext.getBean(
847: PersistenceStructureService.class)
848: .getForeignKeysForReference(ObjectCode.class,
849: "reportsToFinancialObject");
850: } catch (Exception e) {
851: e.printStackTrace();
852: assertTrue("An exception should not have been thrown.",
853: false);
854: }
855: // showFkMap(fkMap);
856: assertTrue("A non-null Map should have been returned.",
857: fkMap != null);
858: assertTrue("A non-empty Map should have been returned.", !fkMap
859: .isEmpty());
860: assertEquals(3, fkMap.size());
861:
862: }
863:
864: /**
865: * This method dumps the contents of the resulting fkMap to the errout & log.info.
866: *
867: * @param fkMap
868: */
869: private void showFkMap(Map fkMap) {
870: for (Iterator iter = fkMap.keySet().iterator(); iter.hasNext();) {
871: String fkFieldName = (String) iter.next();
872: Class clazz = (Class) fkMap.get(fkFieldName);
873: String output = "[" + clazz.getName() + "] " + fkFieldName;
874: System.err.println(output);
875: LOG.info(output);
876: }
877: }
878:
879: // public final void testGetNestedForeignKeyMap() {
880: // Map nestedFkMap = persistenceService.getNestedForeignKeyMap(Account.class);
881: // assertTrue(nestedFkMap.size() == 25);
882: //
883: // assertTrue(nestedFkMap.containsKey("reportsToAccount.chartOfAccountsCode"));
884: // assertTrue(nestedFkMap.get("reportsToAccount.chartOfAccountsCode").equals("reportsToChartOfAccountsCode"));
885: //
886: // assertTrue(nestedFkMap.containsKey("reportsToAccount.accountNumber"));
887: // assertTrue(nestedFkMap.get("reportsToAccount.accountNumber").equals("reportsToAccountNumber"));
888: // }
889:
890: private final List listSuffixedSubclasses(Class uberclass,
891: String prefix, String suffix) {
892: List persistableSubclasses = SpringContext.getBean(
893: PersistenceStructureService.class)
894: .listPersistableSubclasses(
895: PersistableBusinessObject.class);
896:
897: List suffixedSubclasses = new ArrayList();
898: for (Iterator i = persistableSubclasses.iterator(); i.hasNext();) {
899: Class clazz = (Class) i.next();
900: String className = StringUtils.substringAfterLast(clazz
901: .getName(), ".");
902:
903: try {
904: String unterclassName = prefix + className + suffix;
905: Class unterclass = Class.forName(unterclassName);
906: suffixedSubclasses.add(unterclass);
907: } catch (ClassNotFoundException e) {
908: }
909: }
910:
911: return suffixedSubclasses;
912: }
913:
914: private final AccountingLineBase createValidAccountingLine() {
915: SourceAccountingLine accountingLine = new SourceAccountingLine();
916: accountingLine
917: .setChartOfAccountsCode(TestConstants.Data4.CHART_CODE);
918:
919: accountingLine.setPostingYear(TestConstants.Data4.POSTING_YEAR);
920: accountingLine
921: .setFinancialObjectCode(TestConstants.Data4.OBJECT_CODE);
922: return accountingLine;
923: }
924:
925: public void testGetRelationshipMetadata() {
926: Map<String, BusinessObjectRelationship> rels = SpringContext
927: .getBean(PersistenceStructureService.class)
928: .getRelationshipMetadata(Account.class,
929: "chartOfAccountsCode");
930: LOG.info(rels);
931: assertTrue("relationships must be returned", rels.size() > 0);
932:
933: assertTrue("chartOfAccounts attribute must be present", rels
934: .containsKey("chartOfAccounts"));
935: BusinessObjectRelationship rel = rels.get("chartOfAccounts");
936: assertEquals("reference class must be Chart", Chart.class, rel
937: .getRelatedClass());
938: assertEquals("relationship must have 1 reference", 1, rel
939: .getParentToChildReferences().size());
940: assertEquals(
941: "reference class attribute must be chartOfAccountsCode",
942: "chartOfAccountsCode", rel.getParentToChildReferences()
943: .entrySet().iterator().next().getValue());
944:
945: assertTrue("Organization attribute must be present", rels
946: .containsKey("organization"));
947:
948: }
949: }
|