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.LinkedHashMap;
019:
020: import org.kuali.core.bo.DocumentType;
021: import org.kuali.core.document.DocumentBase;
022: import org.kuali.core.document.MaintenanceDocument;
023: import org.kuali.core.document.MaintenanceDocumentBase;
024: import org.kuali.core.exceptions.UnknownDocumentTypeException;
025: import org.kuali.kfs.context.KualiTestBase;
026: import org.kuali.kfs.context.SpringContext;
027: import org.kuali.module.financial.document.InternalBillingDocument;
028: import org.kuali.module.financial.document.JournalVoucherDocument;
029: import org.kuali.test.ConfigureContext;
030: import org.kuali.test.suite.AnnotationTestSuite;
031: import org.kuali.test.suite.PreCommitSuite;
032:
033: /**
034: * This class tests the DocumentType service.
035: */
036: @AnnotationTestSuite(PreCommitSuite.class)
037: @ConfigureContext
038: public class DocumentTypeServiceTest extends KualiTestBase {
039:
040: public final void testGetClassByName_nullName() {
041: boolean failedAsExpected = false;
042:
043: try {
044: SpringContext.getBean(DocumentTypeService.class)
045: .getClassByName(null);
046: } catch (IllegalArgumentException e) {
047: failedAsExpected = true;
048: }
049:
050: assertTrue(
051: "getClassByName failed to fail correctly for null name",
052: failedAsExpected);
053: }
054:
055: public final void testGetClassByName_blankName() {
056: boolean failedAsExpected = false;
057:
058: try {
059: SpringContext.getBean(DocumentTypeService.class)
060: .getClassByName(" ");
061: } catch (IllegalArgumentException e) {
062: failedAsExpected = true;
063: }
064:
065: assertTrue(
066: "getClassByName failed to fail correctly for blank name",
067: failedAsExpected);
068: }
069:
070: public final void testGetClassByName_unknownName() {
071: boolean failedAsExpected = false;
072:
073: try {
074: SpringContext.getBean(DocumentTypeService.class)
075: .getClassByName("foo");
076: } catch (UnknownDocumentTypeException e) {
077: failedAsExpected = true;
078: }
079:
080: assertTrue(
081: "getClassByName failed to fail correctly for unknown name",
082: failedAsExpected);
083: }
084:
085: public final void testGetClassByName_knownNameTransactionalDocument() {
086: Class clazz = SpringContext.getBean(DocumentTypeService.class)
087: .getClassByName("InternalBillingDocument");
088:
089: assertEquals(
090: "getClassByName failed for known transactionalDocument type",
091: InternalBillingDocument.class, clazz);
092: }
093:
094: public final void testGetClassByName_knownNameMaintenanceDocument() {
095: boolean passedAsExpected = false;
096:
097: Class clazz = SpringContext.getBean(DocumentTypeService.class)
098: .getClassByName("AccountMaintenanceDocument");
099:
100: try {
101: MaintenanceDocument clazzInstance = (MaintenanceDocument) clazz
102: .newInstance();
103: passedAsExpected = (clazzInstance instanceof MaintenanceDocumentBase);
104: } catch (Exception e) {
105: passedAsExpected = false;
106: }
107:
108: assertTrue(
109: "getClassByName failed for known maintenanceDocument type",
110: passedAsExpected);
111: }
112:
113: public final void testGetNameByClass_nullClass() {
114: boolean failedAsExpected = false;
115:
116: try {
117: SpringContext.getBean(DocumentTypeService.class)
118: .getDocumentTypeNameByClass(null);
119: } catch (IllegalArgumentException e) {
120: failedAsExpected = true;
121: }
122:
123: assertTrue(failedAsExpected);
124: }
125:
126: public final void testGetNameByClass_nonDocClass() {
127: boolean failedAsExpected = false;
128:
129: try {
130: SpringContext.getBean(DocumentTypeService.class)
131: .getDocumentTypeNameByClass(Integer.class);
132: } catch (IllegalArgumentException e) {
133: failedAsExpected = true;
134: }
135:
136: assertTrue(failedAsExpected);
137: }
138:
139: public final void testGetNameByClass_unknownDocClass() {
140: boolean failedAsExpected = false;
141:
142: try {
143: SpringContext.getBean(DocumentTypeService.class)
144: .getDocumentTypeNameByClass(MockDocument.class);
145: } catch (UnknownDocumentTypeException e) {
146: failedAsExpected = true;
147: }
148:
149: assertTrue(failedAsExpected);
150: }
151:
152: public final void testGetNameByClass_knownTransactionalDocument() {
153: String documentTypeName = SpringContext.getBean(
154: DocumentTypeService.class).getDocumentTypeNameByClass(
155: InternalBillingDocument.class);
156:
157: assertEquals("InternalBillingDocument", documentTypeName);
158: }
159:
160: public final void testGetTypeCodeByClass_nullClass() {
161: boolean failedAsExpected = false;
162:
163: try {
164: SpringContext.getBean(DocumentTypeService.class)
165: .getDocumentTypeCodeByClass(null);
166: } catch (IllegalArgumentException e) {
167: failedAsExpected = true;
168: }
169:
170: assertTrue(failedAsExpected);
171: }
172:
173: public final void testGetTypeCodeByClass_nonDocClass() {
174: boolean failedAsExpected = false;
175:
176: try {
177: SpringContext.getBean(DocumentTypeService.class)
178: .getDocumentTypeCodeByClass(Integer.class);
179: } catch (IllegalArgumentException e) {
180: failedAsExpected = true;
181: }
182:
183: assertTrue(failedAsExpected);
184: }
185:
186: public final void testGetTypeCodeByClass_unknownDocClass() {
187: boolean failedAsExpected = false;
188:
189: try {
190: SpringContext.getBean(DocumentTypeService.class)
191: .getDocumentTypeCodeByClass(MockDocument.class);
192: } catch (UnknownDocumentTypeException e) {
193: failedAsExpected = true;
194: }
195:
196: assertTrue(failedAsExpected);
197: }
198:
199: public final void testGetTypeCodeByClass_knownTransactionalDocument() {
200: String documentTypeName = SpringContext.getBean(
201: DocumentTypeService.class).getDocumentTypeCodeByClass(
202: JournalVoucherDocument.class);
203:
204: assertEquals("JV", documentTypeName);
205: }
206:
207: public final void testGetDocumentTypeByCode_inactive() {
208: assertEquals(false, SpringContext.getBean(
209: DocumentTypeService.class)
210: .getDocumentTypeByCode("ACLO")
211: .isFinDocumentTypeActiveIndicator());
212: }
213:
214: public final void testGetDocumentTypeByCode_active() {
215: assertEquals(true, SpringContext.getBean(
216: DocumentTypeService.class).getDocumentTypeByCode("JV")
217: .isFinDocumentTypeActiveIndicator());
218: }
219:
220: public final void testGetDocumentTypeByCode_unknown() {
221: try {
222: SpringContext.getBean(DocumentTypeService.class)
223: .getDocumentTypeByCode("non-existent");
224: fail("got non-existent document type");
225: } catch (UnknownDocumentTypeException e) {
226: // good
227: }
228: }
229:
230: public final void testGetDocumentTypeByName_active() {
231: assertEquals(true, SpringContext.getBean(
232: DocumentTypeService.class).getDocumentTypeByName(
233: "JournalVoucherDocument")
234: .isFinDocumentTypeActiveIndicator());
235: }
236:
237: public final void testGetDocumentTypeByName_unknownName() {
238: try {
239: String documentTypeName = "some-name-not-in-DD";
240: DocumentType type = SpringContext.getBean(
241: DocumentTypeService.class).getDocumentTypeByName(
242: documentTypeName);
243: fail("from unknown name '" + documentTypeName
244: + "' got DocumentType " + type);
245: } catch (UnknownDocumentTypeException e) {
246: // good
247: }
248: }
249:
250: // todo: delete this test after all document types are in FP_DOC_TYPE_T
251: public final void testGetDocumentTypeByName_unknownType() {
252: try {
253: DocumentType type = SpringContext.getBean(
254: DocumentTypeService.class).getDocumentTypeByName(
255: "KualiAccountGuidelineMaintenanceDocument");
256: fail("got non-existent DocumentType " + type);
257: } catch (UnknownDocumentTypeException e) {
258: // good
259: }
260: }
261:
262: private static class MockDocument extends DocumentBase {
263: @Override
264: public final void populateDocumentForRouting() {
265: throw new UnsupportedOperationException();
266: }
267:
268: @Override
269: protected LinkedHashMap toStringMapper() {
270: return new LinkedHashMap();
271: }
272:
273: public boolean getAllowsCopy() {
274: return false;
275: }
276: }
277: }
|