001: /*
002: * Copyright 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.module.financial.bo;
017:
018: import org.kuali.core.util.KualiDecimal;
019: import org.kuali.kfs.context.KualiTestBase;
020:
021: public class CoinDetailTest extends KualiTestBase {
022: public enum CoinDetailAmountFixture {
023: GOOD_COIN_AMOUNT(5.0, 5.5, 0.75, 0.60, 0.45, 0.07), BAD_COIN_AMOUNT(
024: 5.5, 5.25, 0.80, 0.65, 0.63, 0.07), ALL_FIVES_COIN_AMOUNT(
025: 5.0, 5.0, 5.0, 5.0, 5.0, 5.0), ZERO_COIN_AMOUNT(0.0,
026: 0.0, 0.0, 0.0, 0.0, 0.0), NEGATIVE_COIN_AMOUNT(-5.0,
027: -5.0, -5.0, -5.0, -5.0, -5.0), NULL_COIN_AMOUNT(), ALL_TENS_COIN_AMOUNT(
028: 10.0, 10.0, 10.0, 10.0, 10.0, 10.0);
029:
030: private KualiDecimal hundredCentAmount;
031: private KualiDecimal fiftyCentAmount;
032: private KualiDecimal twentyFiveCentAmount;
033: private KualiDecimal tenCentAmount;
034: private KualiDecimal fiveCentAmount;
035: private KualiDecimal oneCentAmount;
036:
037: private CoinDetailAmountFixture() {
038: }
039:
040: private CoinDetailAmountFixture(double hundredCentAmount,
041: double fiftyCentAmount, double twentyFiveCentAmount,
042: double tenCentAmount, double fiveCentAmount,
043: double oneCentAmount) {
044: this .hundredCentAmount = new KualiDecimal(hundredCentAmount);
045: this .fiftyCentAmount = new KualiDecimal(fiftyCentAmount);
046: this .twentyFiveCentAmount = new KualiDecimal(
047: twentyFiveCentAmount);
048: this .tenCentAmount = new KualiDecimal(tenCentAmount);
049: this .fiveCentAmount = new KualiDecimal(fiveCentAmount);
050: this .oneCentAmount = new KualiDecimal(oneCentAmount);
051: }
052:
053: public CoinDetail convertToCoinDetail() {
054: CoinDetail coinDetail = new CoinDetail();
055: coinDetail
056: .setFinancialDocumentHundredCentAmount(this .hundredCentAmount);
057: coinDetail
058: .setFinancialDocumentFiftyCentAmount(this .fiftyCentAmount);
059: coinDetail
060: .setFinancialDocumentTwentyFiveCentAmount(this .twentyFiveCentAmount);
061: coinDetail
062: .setFinancialDocumentTenCentAmount(this .tenCentAmount);
063: coinDetail
064: .setFinancialDocumentFiveCentAmount(this .fiveCentAmount);
065: coinDetail
066: .setFinancialDocumentOneCentAmount(this .oneCentAmount);
067:
068: return coinDetail;
069: }
070: }
071:
072: public enum CoinDetailCountFixture {
073: GOOD_COIN_AMOUNT(new Integer(5), new Integer(7),
074: new Integer(3), new Integer(6), new Integer(9),
075: new Integer(7)), ALL_FIVES_COIN_AMOUNT(new Integer(5),
076: new Integer(5), new Integer(5), new Integer(5),
077: new Integer(5), new Integer(5)), ZERO_COIN_AMOUNT(
078: new Integer(0), new Integer(0), new Integer(0),
079: new Integer(0), new Integer(0), new Integer(0)), NULL_COIN_AMOUNT(
080: null, null, null, null, null, null), NEGATIVE_COIN_AMOUNT(
081: new Integer(-5), new Integer(-7), new Integer(-3),
082: new Integer(-6), new Integer(-9), new Integer(-7));
083:
084: private Integer hundredCentCount;
085: private Integer fiftyCentCount;
086: private Integer twentyFiveCentCount;
087: private Integer tenCentCount;
088: private Integer fiveCentCount;
089: private Integer oneCentCount;
090:
091: private CoinDetailCountFixture(Integer hundredCentCount,
092: Integer fiftyCentCount, Integer twentyFiveCentCount,
093: Integer tenCentCount, Integer fiveCentCount,
094: Integer oneCentCount) {
095: this .hundredCentCount = hundredCentCount;
096: this .fiftyCentCount = fiftyCentCount;
097: this .twentyFiveCentCount = twentyFiveCentCount;
098: this .tenCentCount = tenCentCount;
099: this .fiveCentCount = fiveCentCount;
100: this .oneCentCount = oneCentCount;
101: }
102:
103: public CoinDetail convertToCoinDetail() {
104: CoinDetail coinDetail = new CoinDetail();
105: coinDetail.setHundredCentCount(this .hundredCentCount);
106: coinDetail.setFiftyCentCount(this .fiftyCentCount);
107: coinDetail.setTwentyFiveCentCount(this .twentyFiveCentCount);
108: coinDetail.setTenCentCount(this .tenCentCount);
109: coinDetail.setFiveCentCount(this .fiveCentCount);
110: coinDetail.setOneCentCount(this .oneCentCount);
111: return coinDetail;
112: }
113: }
114:
115: public void testAmountToCountConversion() {
116: CoinDetail goodDetail = CoinDetailAmountFixture.GOOD_COIN_AMOUNT
117: .convertToCoinDetail();
118: assertEquals(goodDetail.getHundredCentCount(), new Integer(5));
119: assertEquals(goodDetail.getFiftyCentCount(), new Integer(11));
120: assertEquals(goodDetail.getTwentyFiveCentCount(),
121: new Integer(3));
122: assertEquals(goodDetail.getTenCentCount(), new Integer(6));
123: assertEquals(goodDetail.getFiveCentCount(), new Integer(9));
124: assertEquals(goodDetail.getOneCentCount(), new Integer(7));
125: }
126:
127: public void testCountToAmountConversion() {
128: CoinDetail goodDetail = CoinDetailCountFixture.GOOD_COIN_AMOUNT
129: .convertToCoinDetail();
130: assertEquals(
131: goodDetail.getFinancialDocumentHundredCentAmount(),
132: new KualiDecimal(5.0));
133: assertEquals(goodDetail.getFinancialDocumentFiftyCentAmount(),
134: new KualiDecimal(3.5));
135: assertEquals(goodDetail
136: .getFinancialDocumentTwentyFiveCentAmount(),
137: new KualiDecimal(0.75));
138: assertEquals(goodDetail.getFinancialDocumentTenCentAmount(),
139: new KualiDecimal(0.6));
140: assertEquals(goodDetail.getFinancialDocumentFiveCentAmount(),
141: new KualiDecimal(0.45));
142: assertEquals(goodDetail.getFinancialDocumentOneCentAmount(),
143: new KualiDecimal(0.07));
144: }
145:
146: public void testZeroOutAmounts() {
147: CoinDetail zeroAmount = CoinDetailAmountFixture.ZERO_COIN_AMOUNT
148: .convertToCoinDetail();
149:
150: CoinDetail goodAmount = CoinDetailAmountFixture.GOOD_COIN_AMOUNT
151: .convertToCoinDetail();
152: CoinDetail goodZeroAmount = CoinDetailAmountFixture.ZERO_COIN_AMOUNT
153: .convertToCoinDetail();
154: CoinDetail nullAmount = CoinDetailAmountFixture.NULL_COIN_AMOUNT
155: .convertToCoinDetail();
156:
157: goodZeroAmount.zeroOutAmounts();
158: assertDetailAmountsEqual(goodZeroAmount, zeroAmount);
159:
160: goodAmount.zeroOutAmounts();
161: assertDetailAmountsEqual(goodAmount, zeroAmount);
162:
163: nullAmount.zeroOutAmounts();
164: assertDetailAmountsEqual(nullAmount, zeroAmount);
165:
166: CoinDetail goodCount = CoinDetailCountFixture.GOOD_COIN_AMOUNT
167: .convertToCoinDetail();
168: CoinDetail goodZeroCount = CoinDetailCountFixture.ZERO_COIN_AMOUNT
169: .convertToCoinDetail();
170: CoinDetail nullCount = CoinDetailCountFixture.NULL_COIN_AMOUNT
171: .convertToCoinDetail();
172:
173: goodZeroCount.zeroOutAmounts();
174: assertDetailAmountsEqual(goodZeroCount, zeroAmount);
175: goodCount.zeroOutAmounts();
176: assertDetailAmountsEqual(goodCount, zeroAmount);
177: nullCount.zeroOutAmounts();
178: assertDetailAmountsEqual(nullCount, zeroAmount);
179: }
180:
181: public void testZeroOutUnpopulatedAmounts() {
182: CoinDetail zeroAmount = CoinDetailAmountFixture.ZERO_COIN_AMOUNT
183: .convertToCoinDetail();
184:
185: CoinDetail goodAmount = CoinDetailAmountFixture.ALL_FIVES_COIN_AMOUNT
186: .convertToCoinDetail();
187: CoinDetail nullAmount = CoinDetailAmountFixture.NULL_COIN_AMOUNT
188: .convertToCoinDetail();
189:
190: nullAmount.zeroOutUnpopulatedAmounts();
191: assertDetailAmountsEqual(nullAmount, zeroAmount);
192:
193: goodAmount.zeroOutUnpopulatedAmounts();
194: assertDetailAmountsNotEqual(goodAmount, nullAmount);
195:
196: goodAmount.setFinancialDocumentHundredCentAmount(null);
197: goodAmount.setFinancialDocumentTenCentAmount(null);
198: goodAmount.setFinancialDocumentOneCentAmount(null);
199: CoinDetail semiPopulatedAmount = CoinDetailAmountFixture.ALL_FIVES_COIN_AMOUNT
200: .convertToCoinDetail();
201: semiPopulatedAmount
202: .setFinancialDocumentHundredCentAmount(new KualiDecimal(
203: 0));
204: semiPopulatedAmount
205: .setFinancialDocumentTenCentAmount(new KualiDecimal(0));
206: semiPopulatedAmount
207: .setFinancialDocumentOneCentAmount(new KualiDecimal(0));
208: goodAmount.zeroOutUnpopulatedAmounts();
209: assertDetailAmountsEqual(goodAmount, semiPopulatedAmount);
210:
211: CoinDetail goodCount = CoinDetailCountFixture.ALL_FIVES_COIN_AMOUNT
212: .convertToCoinDetail();
213: CoinDetail nullCount = CoinDetailCountFixture.NULL_COIN_AMOUNT
214: .convertToCoinDetail();
215:
216: nullCount.zeroOutUnpopulatedAmounts();
217: assertDetailAmountsEqual(nullCount, zeroAmount);
218: goodCount.zeroOutUnpopulatedAmounts();
219: assertDetailAmountsNotEqual(goodCount, zeroAmount);
220: }
221:
222: public void testAdd() {
223: CoinDetail zeroAmount = CoinDetailAmountFixture.ZERO_COIN_AMOUNT
224: .convertToCoinDetail();
225: CoinDetail allFivesAmountControl = CoinDetailAmountFixture.ALL_FIVES_COIN_AMOUNT
226: .convertToCoinDetail();
227: CoinDetail allFivesAmount = CoinDetailAmountFixture.ALL_FIVES_COIN_AMOUNT
228: .convertToCoinDetail();
229: CoinDetail negativeAmountControl = CoinDetailAmountFixture.NEGATIVE_COIN_AMOUNT
230: .convertToCoinDetail();
231: CoinDetail negativeAmount = CoinDetailAmountFixture.NEGATIVE_COIN_AMOUNT
232: .convertToCoinDetail();
233: CoinDetail nullAmount = CoinDetailAmountFixture.NULL_COIN_AMOUNT
234: .convertToCoinDetail();
235:
236: allFivesAmount.add(zeroAmount);
237: assertDetailAmountsEqual(allFivesAmount, allFivesAmountControl);
238: allFivesAmount.add(nullAmount);
239: assertDetailAmountsEqual(allFivesAmount, allFivesAmountControl);
240: allFivesAmount.add(negativeAmount);
241: assertDetailAmountsEqual(allFivesAmount, zeroAmount);
242: negativeAmount.add(zeroAmount);
243: assertDetailAmountsEqual(negativeAmount, negativeAmountControl);
244: negativeAmount.add(nullAmount);
245: assertDetailAmountsEqual(negativeAmount, negativeAmountControl);
246: negativeAmount.add(allFivesAmountControl);
247: assertDetailAmountsEqual(negativeAmount, zeroAmount);
248: }
249:
250: public void testSubtract() {
251: CoinDetail zeroAmount = CoinDetailAmountFixture.ZERO_COIN_AMOUNT
252: .convertToCoinDetail();
253: CoinDetail allFivesAmountControl = CoinDetailAmountFixture.ALL_FIVES_COIN_AMOUNT
254: .convertToCoinDetail();
255: CoinDetail allFivesAmount = CoinDetailAmountFixture.ALL_FIVES_COIN_AMOUNT
256: .convertToCoinDetail();
257: CoinDetail negativeAmountControl = CoinDetailAmountFixture.NEGATIVE_COIN_AMOUNT
258: .convertToCoinDetail();
259: CoinDetail negativeAmount = CoinDetailAmountFixture.NEGATIVE_COIN_AMOUNT
260: .convertToCoinDetail();
261: CoinDetail nullAmount = CoinDetailAmountFixture.NULL_COIN_AMOUNT
262: .convertToCoinDetail();
263:
264: allFivesAmount.subtract(zeroAmount);
265: assertDetailAmountsEqual(allFivesAmount, allFivesAmountControl);
266: allFivesAmount.subtract(nullAmount);
267: assertDetailAmountsEqual(allFivesAmount, allFivesAmountControl);
268:
269: allFivesAmount.subtract(negativeAmount);
270: CoinDetail doublePositives = new CoinDetail();
271: doublePositives
272: .setFinancialDocumentHundredCentAmount(new KualiDecimal(
273: 2).multiply(allFivesAmountControl
274: .getFinancialDocumentHundredCentAmount()));
275: doublePositives
276: .setFinancialDocumentFiftyCentAmount(new KualiDecimal(2)
277: .multiply(allFivesAmountControl
278: .getFinancialDocumentFiftyCentAmount()));
279: doublePositives
280: .setFinancialDocumentTwentyFiveCentAmount(new KualiDecimal(
281: 2).multiply(allFivesAmountControl
282: .getFinancialDocumentTwentyFiveCentAmount()));
283: doublePositives
284: .setFinancialDocumentTenCentAmount(new KualiDecimal(2)
285: .multiply(allFivesAmountControl
286: .getFinancialDocumentTenCentAmount()));
287: doublePositives
288: .setFinancialDocumentFiveCentAmount(new KualiDecimal(2)
289: .multiply(allFivesAmountControl
290: .getFinancialDocumentFiveCentAmount()));
291: doublePositives
292: .setFinancialDocumentOneCentAmount(new KualiDecimal(2)
293: .multiply(allFivesAmountControl
294: .getFinancialDocumentOneCentAmount()));
295: assertDetailAmountsEqual(allFivesAmount, doublePositives);
296:
297: negativeAmount.subtract(zeroAmount);
298: assertDetailAmountsEqual(negativeAmount, negativeAmountControl);
299: negativeAmount.subtract(nullAmount);
300: assertDetailAmountsEqual(negativeAmount, negativeAmountControl);
301:
302: negativeAmount.subtract(allFivesAmountControl);
303: CoinDetail doubleNegatives = new CoinDetail();
304: doubleNegatives
305: .setFinancialDocumentHundredCentAmount(new KualiDecimal(
306: 2).multiply(negativeAmountControl
307: .getFinancialDocumentHundredCentAmount()));
308: doubleNegatives
309: .setFinancialDocumentFiftyCentAmount(new KualiDecimal(2)
310: .multiply(negativeAmountControl
311: .getFinancialDocumentFiftyCentAmount()));
312: doubleNegatives
313: .setFinancialDocumentTwentyFiveCentAmount(new KualiDecimal(
314: 2).multiply(negativeAmountControl
315: .getFinancialDocumentTwentyFiveCentAmount()));
316: doubleNegatives
317: .setFinancialDocumentTenCentAmount(new KualiDecimal(2)
318: .multiply(negativeAmountControl
319: .getFinancialDocumentTenCentAmount()));
320: doubleNegatives
321: .setFinancialDocumentFiveCentAmount(new KualiDecimal(2)
322: .multiply(negativeAmountControl
323: .getFinancialDocumentFiveCentAmount()));
324: doubleNegatives
325: .setFinancialDocumentOneCentAmount(new KualiDecimal(2)
326: .multiply(negativeAmountControl
327: .getFinancialDocumentOneCentAmount()));
328: assertDetailAmountsEqual(negativeAmount, doubleNegatives);
329: }
330:
331: public void testTotal() {
332: CoinDetail zeroAmount = CoinDetailAmountFixture.ZERO_COIN_AMOUNT
333: .convertToCoinDetail();
334: assertEquals(zeroAmount.getTotalAmount(), new KualiDecimal(0));
335: CoinDetail nullAmount = CoinDetailAmountFixture.NULL_COIN_AMOUNT
336: .convertToCoinDetail();
337: assertEquals(nullAmount.getTotalAmount(), new KualiDecimal(0));
338: CoinDetail allFivesAmount = CoinDetailAmountFixture.ALL_FIVES_COIN_AMOUNT
339: .convertToCoinDetail();
340: assertEquals(allFivesAmount.getTotalAmount(), new KualiDecimal(
341: 30));
342: CoinDetail negativeAmount = CoinDetailAmountFixture.NEGATIVE_COIN_AMOUNT
343: .convertToCoinDetail();
344: assertEquals(negativeAmount.getTotalAmount(), new KualiDecimal(
345: -30));
346: }
347:
348: public void testIsEmpty() {
349: CoinDetail zeroAmount = CoinDetailAmountFixture.ZERO_COIN_AMOUNT
350: .convertToCoinDetail();
351: assertTrue(zeroAmount.isEmpty());
352: CoinDetail nullAmount = CoinDetailAmountFixture.NULL_COIN_AMOUNT
353: .convertToCoinDetail();
354: assertTrue(nullAmount.isEmpty());
355: CoinDetail allFivesAmount = CoinDetailAmountFixture.ALL_FIVES_COIN_AMOUNT
356: .convertToCoinDetail();
357: assertFalse(allFivesAmount.isEmpty());
358: CoinDetail negativeAmount = CoinDetailAmountFixture.NEGATIVE_COIN_AMOUNT
359: .convertToCoinDetail();
360: assertFalse(allFivesAmount.isEmpty());
361: }
362:
363: private void assertDetailAmountsEqual(CoinDetail tweedleDee,
364: CoinDetail tweedleDum) {
365: assertEquals(
366: tweedleDee.getFinancialDocumentHundredCentAmount(),
367: tweedleDum.getFinancialDocumentHundredCentAmount());
368: assertEquals(tweedleDee.getFinancialDocumentFiftyCentAmount(),
369: tweedleDum.getFinancialDocumentFiftyCentAmount());
370: assertEquals(tweedleDee
371: .getFinancialDocumentTwentyFiveCentAmount(), tweedleDum
372: .getFinancialDocumentTwentyFiveCentAmount());
373: assertEquals(tweedleDee.getFinancialDocumentTenCentAmount(),
374: tweedleDum.getFinancialDocumentTenCentAmount());
375: assertEquals(tweedleDee.getFinancialDocumentFiveCentAmount(),
376: tweedleDum.getFinancialDocumentFiveCentAmount());
377: assertEquals(tweedleDee.getFinancialDocumentOneCentAmount(),
378: tweedleDum.getFinancialDocumentOneCentAmount());
379: }
380:
381: private void assertDetailAmountsNotEqual(CoinDetail tweedleDee,
382: CoinDetail tweedleDum) {
383: assertFalse(tweedleDee
384: .getFinancialDocumentHundredCentAmount()
385: .equals(
386: tweedleDum
387: .getFinancialDocumentHundredCentAmount()));
388: assertFalse(tweedleDee.getFinancialDocumentFiftyCentAmount()
389: .equals(
390: tweedleDum
391: .getFinancialDocumentFiftyCentAmount()));
392: assertFalse(tweedleDee
393: .getFinancialDocumentTwentyFiveCentAmount()
394: .equals(
395: tweedleDum
396: .getFinancialDocumentTwentyFiveCentAmount()));
397: assertFalse(tweedleDee.getFinancialDocumentTenCentAmount()
398: .equals(tweedleDum.getFinancialDocumentTenCentAmount()));
399: assertFalse(tweedleDee
400: .getFinancialDocumentFiveCentAmount()
401: .equals(tweedleDum.getFinancialDocumentFiveCentAmount()));
402: assertFalse(tweedleDee.getFinancialDocumentOneCentAmount()
403: .equals(tweedleDum.getFinancialDocumentOneCentAmount()));
404: }
405: }
|