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 static org.kuali.test.fixtures.AccountingLineFixture.LINE2_TOF;
019: import static org.kuali.test.fixtures.UserNameFixture.KHUNTLEY;
020:
021: import java.util.Iterator;
022: import java.util.List;
023:
024: import org.kuali.kfs.bo.AccountingLine;
025: import org.kuali.kfs.bo.SourceAccountingLine;
026: import org.kuali.kfs.bo.TargetAccountingLine;
027: import org.kuali.kfs.context.KualiTestBase;
028: import org.kuali.kfs.context.SpringContext;
029: import org.kuali.kfs.document.AccountingDocument;
030: import org.kuali.kfs.service.AccountingLineService;
031: import org.kuali.module.financial.document.TransferOfFundsDocument;
032: import org.kuali.test.ConfigureContext;
033: import org.kuali.test.DocumentTestUtils;
034:
035: /**
036: * This class tests the AccountingLine service.
037: */
038: @ConfigureContext(session=KHUNTLEY,shouldCommitTransactions=true)
039: public class AccountingLineServiceTest extends KualiTestBase {
040:
041: private SourceAccountingLine sline;
042: private TargetAccountingLine tline;
043: private AccountingDocument document;
044:
045: @Override
046: protected void setUp() throws Exception {
047: super .setUp();
048: document = DocumentTestUtils.createDocument(SpringContext
049: .getBean(DocumentService.class),
050: TransferOfFundsDocument.class);
051: SpringContext.getBean(DocumentService.class).saveDocument(
052: document);
053: LINE2_TOF.addAsSourceTo(document);
054: LINE2_TOF.addAsTargetTo(document);
055:
056: sline = (SourceAccountingLine) document
057: .getSourceAccountingLine(0);
058: tline = (TargetAccountingLine) document
059: .getTargetAccountingLine(0);
060: }
061:
062: /**
063: * Tests an accounting line is correctly persisted when the primitives of the line are set.
064: *
065: * @throws Exception
066: */
067: public void testPersistence() throws Exception {
068:
069: SpringContext.getBean(AccountingLineService.class).save(sline);
070:
071: List<? extends SourceAccountingLine> sourceLines = SpringContext
072: .getBean(AccountingLineService.class)
073: .getByDocumentHeaderId(SourceAccountingLine.class,
074: document.getDocumentNumber());
075: assertTrue(sourceLines.size() > 0);
076:
077: AccountingLine line = sourceLines.get(0);
078:
079: assertEquals(LINE2_TOF.chartOfAccountsCode, line
080: .getChartOfAccountsCode());
081: assertEquals(LINE2_TOF.accountNumber, line.getAccountNumber());
082: assertEquals(LINE2_TOF.subAccountNumber, line
083: .getSubAccountNumber());
084: assertEquals(LINE2_TOF.financialObjectCode, line
085: .getFinancialObjectCode());
086: assertEquals(LINE2_TOF.financialSubObjectCode, line
087: .getFinancialSubObjectCode());
088:
089: SpringContext.getBean(AccountingLineService.class)
090: .deleteAccountingLine((AccountingLine) line);
091:
092: }
093:
094: /**
095: * Tests reference objects are being corrected refreshed from changed pritive values.
096: */
097: public void testRefresh() {
098: assertEquals(LINE2_TOF.chartOfAccountsCode, sline.getAccount()
099: .getChartOfAccountsCode());
100: assertEquals(LINE2_TOF.accountNumber, sline.getAccount()
101: .getAccountNumber());
102:
103: sline.setAccountNumber(TestConstants.Data4.ACCOUNT2);
104: sline.refresh();
105:
106: assertEquals(LINE2_TOF.chartOfAccountsCode, sline.getAccount()
107: .getChartOfAccountsCode());
108: assertEquals(TestConstants.Data4.ACCOUNT2, sline.getAccount()
109: .getAccountNumber());
110:
111: sline.setChartOfAccountsCode(TestConstants.Data4.CHART_CODE_BA);
112: sline.setFinancialObjectCode(TestConstants.Data4.OBJECT_CODE2);
113: sline.refresh();
114:
115: assertEquals(TestConstants.Data4.CHART_CODE_BA, sline
116: .getObjectCode().getChartOfAccounts()
117: .getChartOfAccountsCode());
118: assertEquals(TestConstants.Data4.OBJECT_CODE2, sline
119: .getObjectCode().getFinancialObjectCode());
120:
121: }
122:
123: // no obvious way to test these separately, since we need to create to test save, need to save to (really) test get, and need
124: // to delete so future test-runs can recreate
125: public void testLifecycle() throws Exception {
126: String docNumber = document.getDocumentNumber();
127: // make sure they dont' exist
128: assertEquals(0, SpringContext.getBean(
129: AccountingLineService.class).getByDocumentHeaderId(
130: SourceAccountingLine.class, docNumber).size());
131: assertEquals(0, SpringContext.getBean(
132: AccountingLineService.class).getByDocumentHeaderId(
133: TargetAccountingLine.class, docNumber).size());
134: List sourceLines = null;
135: List targetLines = null;
136:
137: // save 'em
138: SpringContext.getBean(AccountingLineService.class).save(sline);
139: SpringContext.getBean(AccountingLineService.class).save(tline);
140:
141: sourceLines = SpringContext
142: .getBean(AccountingLineService.class)
143: .getByDocumentHeaderId(SourceAccountingLine.class,
144: docNumber);
145: targetLines = SpringContext
146: .getBean(AccountingLineService.class)
147: .getByDocumentHeaderId(TargetAccountingLine.class,
148: docNumber);
149:
150: // make sure they got saved
151: assertTrue(sourceLines.size() > 0);
152: assertTrue(targetLines.size() > 0);
153: // delete 'em
154: if (sourceLines != null) {
155: for (Iterator i = sourceLines.iterator(); i.hasNext();) {
156: SpringContext
157: .getBean(AccountingLineService.class)
158: .deleteAccountingLine((AccountingLine) i.next());
159: }
160: }
161: if (targetLines != null) {
162: for (Iterator i = targetLines.iterator(); i.hasNext();) {
163: SpringContext
164: .getBean(AccountingLineService.class)
165: .deleteAccountingLine((AccountingLine) i.next());
166: }
167: }
168:
169: // make sure they got deleted
170: assertEquals(0, SpringContext.getBean(
171: AccountingLineService.class).getByDocumentHeaderId(
172: SourceAccountingLine.class, docNumber).size());
173: assertEquals(0, SpringContext.getBean(
174: AccountingLineService.class).getByDocumentHeaderId(
175: TargetAccountingLine.class, docNumber).size());
176: }
177: }
|