1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.kuali.kfs.fp.businessobject;
20
21 import org.kuali.kfs.sys.context.KualiTestBase;
22 import org.kuali.rice.core.api.util.type.KualiDecimal;
23
24 public class CurrencyDetailTest extends KualiTestBase {
25 public enum CurrencyDetailAmountFixture {
26 GOOD_POSITIVE_AMOUNT(500.0, 250.0, 100.0, 50.0, 25.0, 10.0, 5.0), BAD_POSITIVE_AMOUNT(367.0, 367.0, 367.0, 367.0, 367.0, 367.0, 367.5), ALL_FIVES_AMOUNT(500.0, 500.0, 500.0, 500.0, 500.0, 500.0, 500.0), NULL_AMOUNT, ZERO_AMOUNT(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0), NEGATIVE_AMOUNT(-500.0, -500.0, -500.0, -500.0, -500.0, -500.0, -500.0), ALL_TENS_AMOUNT(1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0);
27
28 private KualiDecimal hundredDollarAmount;
29 private KualiDecimal fiftyDollarAmount;
30 private KualiDecimal twentyDollarAmount;
31 private KualiDecimal tenDollarAmount;
32 private KualiDecimal fiveDollarAmount;
33 private KualiDecimal twoDollarAmount;
34 private KualiDecimal oneDollarAmount;
35
36 private CurrencyDetailAmountFixture() {
37 }
38
39 private CurrencyDetailAmountFixture(double hundredDollarAmount, double fiftyDollarAmount, double twentyDollarAmount, double tenDollarAmount, double fiveDollarAmount, double twoDollarAmount, double oneDollarAmount) {
40 this.hundredDollarAmount = new KualiDecimal(hundredDollarAmount);
41 this.fiftyDollarAmount = new KualiDecimal(fiftyDollarAmount);
42 this.twentyDollarAmount = new KualiDecimal(twentyDollarAmount);
43 this.tenDollarAmount = new KualiDecimal(tenDollarAmount);
44 this.fiveDollarAmount = new KualiDecimal(fiveDollarAmount);
45 this.twoDollarAmount = new KualiDecimal(twoDollarAmount);
46 this.oneDollarAmount = new KualiDecimal(oneDollarAmount);
47 }
48
49 public CurrencyDetail convertToCurrencyDetail() {
50 CurrencyDetail detail = new CurrencyDetail();
51 detail.setFinancialDocumentHundredDollarAmount(this.hundredDollarAmount);
52 detail.setFinancialDocumentFiftyDollarAmount(this.fiftyDollarAmount);
53 detail.setFinancialDocumentTwentyDollarAmount(this.twentyDollarAmount);
54 detail.setFinancialDocumentTenDollarAmount(this.tenDollarAmount);
55 detail.setFinancialDocumentFiveDollarAmount(this.fiveDollarAmount);
56 detail.setFinancialDocumentTwoDollarAmount(this.twoDollarAmount);
57 detail.setFinancialDocumentOneDollarAmount(this.oneDollarAmount);
58
59 return detail;
60 }
61 }
62
63 public enum CurrencyDetailCountFixture {
64 GOOD_POSITIVE_COUNT(new Integer(5), new Integer(10), new Integer(25), new Integer(50), new Integer(100), new Integer(250), new Integer(500)), ALL_FIVE_HUNDREDS_COUNT(new Integer(5), new Integer(5), new Integer(5), new Integer(5), new Integer(5), new Integer(5), new Integer(5)), NULL_COUNT, ZERO_COUNT(new Integer(0), new Integer(0), new Integer(0), new Integer(0), new Integer(0), new Integer(0), new Integer(0)), NEGATIVE_COUNT(new Integer(-5), new Integer(-5), new Integer(-5), new Integer(-5), new Integer(-5), new Integer(-5), new Integer(-5));
65
66 private Integer hundredDollarCount;
67 private Integer fiftyDollarCount;
68 private Integer twentyDollarCount;
69 private Integer tenDollarCount;
70 private Integer fiveDollarCount;
71 private Integer twoDollarCount;
72 private Integer oneDollarCount;
73
74 private CurrencyDetailCountFixture() {
75 }
76
77 private CurrencyDetailCountFixture(Integer hundredDollarCount, Integer fiftyDollarCount, Integer twentyDollarCount, Integer tenDollarCount, Integer fiveDollarCount, Integer twoDollarCount, Integer oneDollarCount) {
78 this.hundredDollarCount = hundredDollarCount;
79 this.fiftyDollarCount = fiftyDollarCount;
80 this.twentyDollarCount = twentyDollarCount;
81 this.tenDollarCount = tenDollarCount;
82 this.fiveDollarCount = fiveDollarCount;
83 this.twoDollarCount = twoDollarCount;
84 this.oneDollarCount = oneDollarCount;
85 }
86
87 public CurrencyDetail convertToCurrencyDetail() {
88 CurrencyDetail detail = new CurrencyDetail();
89 detail.setHundredDollarCount(this.hundredDollarCount);
90 detail.setFiftyDollarCount(this.fiftyDollarCount);
91 detail.setTwentyDollarCount(this.twentyDollarCount);
92 detail.setTenDollarCount(this.tenDollarCount);
93 detail.setFiveDollarCount(this.fiveDollarCount);
94 detail.setTwoDollarCount(this.twoDollarCount);
95 detail.setOneDollarCount(this.oneDollarCount);
96
97 return detail;
98 }
99 }
100
101 public void testAmountToCountConversion() {
102 CurrencyDetail goodAmount = CurrencyDetailAmountFixture.GOOD_POSITIVE_AMOUNT.convertToCurrencyDetail();
103 assertEquals(goodAmount.getHundredDollarCount(), new Integer(5));
104 assertEquals(goodAmount.getFiftyDollarCount(), new Integer(5));
105 assertEquals(goodAmount.getTwentyDollarCount(), new Integer(5));
106 assertEquals(goodAmount.getTenDollarCount(), new Integer(5));
107 assertEquals(goodAmount.getFiveDollarCount(), new Integer(5));
108 assertEquals(goodAmount.getTwoDollarCount(), new Integer(5));
109 assertEquals(goodAmount.getOneDollarCount(), new Integer(5));
110 }
111
112 public void testCountToAmountConversion() {
113 CurrencyDetail goodCount = CurrencyDetailCountFixture.GOOD_POSITIVE_COUNT.convertToCurrencyDetail();
114 assertEquals(goodCount.getFinancialDocumentHundredDollarAmount(), new KualiDecimal(500));
115 assertEquals(goodCount.getFinancialDocumentFiftyDollarAmount(), new KualiDecimal(500));
116 assertEquals(goodCount.getFinancialDocumentTwentyDollarAmount(), new KualiDecimal(500));
117 assertEquals(goodCount.getFinancialDocumentTenDollarAmount(), new KualiDecimal(500));
118 assertEquals(goodCount.getFinancialDocumentFiveDollarAmount(), new KualiDecimal(500));
119 assertEquals(goodCount.getFinancialDocumentTwoDollarAmount(), new KualiDecimal(500));
120 assertEquals(goodCount.getFinancialDocumentOneDollarAmount(), new KualiDecimal(500));
121 }
122
123 public void testZeroOutAmounts() {
124 CurrencyDetail zeroAmountControl = CurrencyDetailAmountFixture.ZERO_AMOUNT.convertToCurrencyDetail();
125 CurrencyDetail zeroAmount = CurrencyDetailAmountFixture.ZERO_AMOUNT.convertToCurrencyDetail();
126 CurrencyDetail nullAmount = CurrencyDetailAmountFixture.NULL_AMOUNT.convertToCurrencyDetail();
127 CurrencyDetail goodAmount = CurrencyDetailAmountFixture.GOOD_POSITIVE_AMOUNT.convertToCurrencyDetail();
128 CurrencyDetail negativeAmount = CurrencyDetailAmountFixture.NEGATIVE_AMOUNT.convertToCurrencyDetail();
129
130 zeroAmount.zeroOutAmounts();
131 assertDetailAmountsEqual(zeroAmount, zeroAmountControl);
132 nullAmount.zeroOutAmounts();
133 assertDetailAmountsEqual(nullAmount, zeroAmountControl);
134 goodAmount.zeroOutAmounts();
135 assertDetailAmountsEqual(goodAmount, zeroAmountControl);
136 negativeAmount.zeroOutAmounts();
137 assertDetailAmountsEqual(negativeAmount, zeroAmountControl);
138
139 CurrencyDetail goodCount = CurrencyDetailCountFixture.GOOD_POSITIVE_COUNT.convertToCurrencyDetail();
140 CurrencyDetail zeroCount = CurrencyDetailCountFixture.ZERO_COUNT.convertToCurrencyDetail();
141 CurrencyDetail negativeCount = CurrencyDetailCountFixture.NEGATIVE_COUNT.convertToCurrencyDetail();
142 CurrencyDetail nullCount = CurrencyDetailCountFixture.NULL_COUNT.convertToCurrencyDetail();
143
144 zeroCount.zeroOutAmounts();
145 assertDetailAmountsEqual(zeroCount, zeroAmountControl);
146 nullCount.zeroOutAmounts();
147 assertDetailAmountsEqual(nullCount, zeroAmountControl);
148 goodCount.zeroOutAmounts();
149 assertDetailAmountsEqual(goodCount, zeroAmountControl);
150 negativeCount.zeroOutAmounts();
151 assertDetailAmountsEqual(negativeCount, zeroAmountControl);
152 }
153
154 public void testZeroOutUnpopulatedAmounts() {
155 CurrencyDetail zeroAmountControl = CurrencyDetailAmountFixture.ZERO_AMOUNT.convertToCurrencyDetail();
156 CurrencyDetail zeroAmount = CurrencyDetailAmountFixture.ZERO_AMOUNT.convertToCurrencyDetail();
157 CurrencyDetail nullAmount = CurrencyDetailAmountFixture.NULL_AMOUNT.convertToCurrencyDetail();
158 CurrencyDetail goodAmount = CurrencyDetailAmountFixture.GOOD_POSITIVE_AMOUNT.convertToCurrencyDetail();
159
160 zeroAmount.zeroOutUnpopulatedAmounts();
161 assertDetailAmountsEqual(zeroAmount, zeroAmountControl);
162 nullAmount.zeroOutUnpopulatedAmounts();
163 assertDetailAmountsEqual(nullAmount, zeroAmountControl);
164 goodAmount.zeroOutUnpopulatedAmounts();
165 assertDetailAmountsNotEqual(goodAmount, zeroAmountControl);
166
167 goodAmount.setFinancialDocumentHundredDollarAmount(null);
168 goodAmount.setFinancialDocumentTwentyDollarAmount(null);
169 goodAmount.setFinancialDocumentFiveDollarAmount(null);
170 goodAmount.setFinancialDocumentTwoDollarAmount(null);
171 CurrencyDetail semiPopulatedAmount = CurrencyDetailAmountFixture.GOOD_POSITIVE_AMOUNT.convertToCurrencyDetail();
172 semiPopulatedAmount.setFinancialDocumentHundredDollarAmount(KualiDecimal.ZERO);
173 semiPopulatedAmount.setFinancialDocumentTwentyDollarAmount(KualiDecimal.ZERO);
174 semiPopulatedAmount.setFinancialDocumentFiveDollarAmount(KualiDecimal.ZERO);
175 semiPopulatedAmount.setFinancialDocumentTwoDollarAmount(KualiDecimal.ZERO);
176 goodAmount.zeroOutUnpopulatedAmounts();
177 assertDetailAmountsEqual(goodAmount, semiPopulatedAmount);
178
179 CurrencyDetail goodCount = CurrencyDetailCountFixture.GOOD_POSITIVE_COUNT.convertToCurrencyDetail();
180 CurrencyDetail zeroCount = CurrencyDetailCountFixture.ZERO_COUNT.convertToCurrencyDetail();
181 CurrencyDetail nullCount = CurrencyDetailCountFixture.NULL_COUNT.convertToCurrencyDetail();
182
183 zeroCount.zeroOutUnpopulatedAmounts();
184 assertDetailAmountsEqual(zeroCount, zeroAmountControl);
185 nullCount.zeroOutUnpopulatedAmounts();
186 assertDetailAmountsEqual(nullCount, zeroAmountControl);
187 goodCount.zeroOutUnpopulatedAmounts();
188 assertDetailAmountsNotEqual(goodCount, zeroAmountControl);
189 }
190
191 public void testAdd() {
192 CurrencyDetail zeroAmountControl = CurrencyDetailAmountFixture.ZERO_AMOUNT.convertToCurrencyDetail();
193 CurrencyDetail zeroAmount = CurrencyDetailAmountFixture.ZERO_AMOUNT.convertToCurrencyDetail();
194 CurrencyDetail nullAmount = CurrencyDetailAmountFixture.NULL_AMOUNT.convertToCurrencyDetail();
195 CurrencyDetail goodAmountControl = CurrencyDetailAmountFixture.ALL_FIVES_AMOUNT.convertToCurrencyDetail();
196 CurrencyDetail goodAmount = CurrencyDetailAmountFixture.ALL_FIVES_AMOUNT.convertToCurrencyDetail();
197 CurrencyDetail negativeAmountControl = CurrencyDetailAmountFixture.NEGATIVE_AMOUNT.convertToCurrencyDetail();
198 CurrencyDetail negativeAmount = CurrencyDetailAmountFixture.NEGATIVE_AMOUNT.convertToCurrencyDetail();
199
200 zeroAmount.add(zeroAmountControl);
201 assertDetailAmountsEqual(zeroAmount, zeroAmountControl);
202 nullAmount.add(zeroAmountControl);
203 assertDetailAmountsEqual(nullAmount, zeroAmountControl);
204 goodAmount.add(zeroAmountControl);
205 assertDetailAmountsEqual(goodAmount, goodAmountControl);
206 negativeAmount.add(zeroAmountControl);
207 assertDetailAmountsEqual(negativeAmount, negativeAmountControl);
208 goodAmount.add(negativeAmount);
209 assertDetailAmountsEqual(goodAmount, zeroAmountControl);
210 }
211
212 public void testSubtract() {
213 CurrencyDetail zeroAmountControl = CurrencyDetailAmountFixture.ZERO_AMOUNT.convertToCurrencyDetail();
214 CurrencyDetail zeroAmount = CurrencyDetailAmountFixture.ZERO_AMOUNT.convertToCurrencyDetail();
215 CurrencyDetail nullAmount = CurrencyDetailAmountFixture.NULL_AMOUNT.convertToCurrencyDetail();
216 CurrencyDetail goodAmountControl = CurrencyDetailAmountFixture.ALL_FIVES_AMOUNT.convertToCurrencyDetail();
217 CurrencyDetail goodAmount = CurrencyDetailAmountFixture.ALL_FIVES_AMOUNT.convertToCurrencyDetail();
218 CurrencyDetail negativeAmountControl = CurrencyDetailAmountFixture.NEGATIVE_AMOUNT.convertToCurrencyDetail();
219 CurrencyDetail negativeAmount = CurrencyDetailAmountFixture.NEGATIVE_AMOUNT.convertToCurrencyDetail();
220
221 zeroAmount.subtract(zeroAmountControl);
222 assertDetailAmountsEqual(zeroAmount, zeroAmountControl);
223 nullAmount.subtract(zeroAmountControl);
224 assertDetailAmountsEqual(nullAmount, zeroAmountControl);
225 goodAmount.subtract(zeroAmountControl);
226 assertDetailAmountsEqual(goodAmount, goodAmountControl);
227 negativeAmount.subtract(zeroAmountControl);
228 assertDetailAmountsEqual(negativeAmount, negativeAmountControl);
229
230 CurrencyDetail doublePositives = new CurrencyDetail();
231 doublePositives.setFinancialDocumentHundredDollarAmount(goodAmount.getFinancialDocumentHundredDollarAmount().multiply(new KualiDecimal(2.0)));
232 doublePositives.setFinancialDocumentFiftyDollarAmount(goodAmount.getFinancialDocumentFiftyDollarAmount().multiply(new KualiDecimal(2.0)));
233 doublePositives.setFinancialDocumentTwentyDollarAmount(goodAmount.getFinancialDocumentTwentyDollarAmount().multiply(new KualiDecimal(2.0)));
234 doublePositives.setFinancialDocumentTenDollarAmount(goodAmount.getFinancialDocumentTenDollarAmount().multiply(new KualiDecimal(2.0)));
235 doublePositives.setFinancialDocumentFiveDollarAmount(goodAmount.getFinancialDocumentFiveDollarAmount().multiply(new KualiDecimal(2.0)));
236 doublePositives.setFinancialDocumentTwoDollarAmount(goodAmount.getFinancialDocumentTwoDollarAmount().multiply(new KualiDecimal(2.0)));
237 doublePositives.setFinancialDocumentOneDollarAmount(goodAmount.getFinancialDocumentOneDollarAmount().multiply(new KualiDecimal(2.0)));
238 goodAmount.subtract(negativeAmount);
239 assertDetailAmountsEqual(goodAmount, doublePositives);
240
241 negativeAmount = CurrencyDetailAmountFixture.NEGATIVE_AMOUNT.convertToCurrencyDetail();
242 goodAmount = CurrencyDetailAmountFixture.ALL_FIVES_AMOUNT.convertToCurrencyDetail();
243 CurrencyDetail doubleNegatives = new CurrencyDetail();
244 doubleNegatives.setFinancialDocumentHundredDollarAmount(negativeAmount.getFinancialDocumentHundredDollarAmount().multiply(new KualiDecimal(2.0)));
245 doubleNegatives.setFinancialDocumentFiftyDollarAmount(negativeAmount.getFinancialDocumentFiftyDollarAmount().multiply(new KualiDecimal(2.0)));
246 doubleNegatives.setFinancialDocumentTwentyDollarAmount(negativeAmount.getFinancialDocumentTwentyDollarAmount().multiply(new KualiDecimal(2.0)));
247 doubleNegatives.setFinancialDocumentTenDollarAmount(negativeAmount.getFinancialDocumentTenDollarAmount().multiply(new KualiDecimal(2.0)));
248 doubleNegatives.setFinancialDocumentFiveDollarAmount(negativeAmount.getFinancialDocumentFiveDollarAmount().multiply(new KualiDecimal(2.0)));
249 doubleNegatives.setFinancialDocumentTwoDollarAmount(negativeAmount.getFinancialDocumentTwoDollarAmount().multiply(new KualiDecimal(2.0)));
250 doubleNegatives.setFinancialDocumentOneDollarAmount(negativeAmount.getFinancialDocumentOneDollarAmount().multiply(new KualiDecimal(2.0)));
251 negativeAmount.subtract(goodAmount);
252 assertDetailAmountsEqual(negativeAmount, doubleNegatives);
253 }
254
255 public void testTotal() {
256 CurrencyDetail zeroAmount = CurrencyDetailAmountFixture.ZERO_AMOUNT.convertToCurrencyDetail();
257 assertEquals(zeroAmount.getTotalAmount(), new KualiDecimal(0.0));
258 CurrencyDetail nullAmount = CurrencyDetailAmountFixture.NULL_AMOUNT.convertToCurrencyDetail();
259 assertEquals(nullAmount.getTotalAmount(), new KualiDecimal(0.0));
260
261 CurrencyDetail goodAmount = CurrencyDetailAmountFixture.GOOD_POSITIVE_AMOUNT.convertToCurrencyDetail();
262 assertEquals(goodAmount.getTotalAmount(), new KualiDecimal(940.0));
263 CurrencyDetail negativeAmount = CurrencyDetailAmountFixture.NEGATIVE_AMOUNT.convertToCurrencyDetail();
264 assertEquals(negativeAmount.getTotalAmount(), new KualiDecimal(-3500.0));
265 }
266
267 public void testIsEmpty() {
268 CurrencyDetail zeroAmount = CurrencyDetailAmountFixture.ZERO_AMOUNT.convertToCurrencyDetail();
269 assertTrue(zeroAmount.isEmpty());
270 CurrencyDetail nullAmount = CurrencyDetailAmountFixture.NULL_AMOUNT.convertToCurrencyDetail();
271 assertTrue(nullAmount.isEmpty());
272
273 CurrencyDetail goodAmount = CurrencyDetailAmountFixture.GOOD_POSITIVE_AMOUNT.convertToCurrencyDetail();
274 assertFalse(goodAmount.isEmpty());
275 CurrencyDetail negativeAmount = CurrencyDetailAmountFixture.NEGATIVE_AMOUNT.convertToCurrencyDetail();
276 assertFalse(negativeAmount.isEmpty());
277 }
278
279 private void assertDetailAmountsEqual(CurrencyDetail tweedleDee, CurrencyDetail tweedleDum) {
280 assertEquals(tweedleDee.getFinancialDocumentHundredDollarAmount(), tweedleDum.getFinancialDocumentHundredDollarAmount());
281 assertEquals(tweedleDee.getFinancialDocumentFiftyDollarAmount(), tweedleDum.getFinancialDocumentFiftyDollarAmount());
282 assertEquals(tweedleDee.getFinancialDocumentTwentyDollarAmount(), tweedleDum.getFinancialDocumentTwentyDollarAmount());
283 assertEquals(tweedleDee.getFinancialDocumentTenDollarAmount(), tweedleDum.getFinancialDocumentTenDollarAmount());
284 assertEquals(tweedleDee.getFinancialDocumentFiveDollarAmount(), tweedleDum.getFinancialDocumentFiveDollarAmount());
285 assertEquals(tweedleDee.getFinancialDocumentTwoDollarAmount(), tweedleDum.getFinancialDocumentTwoDollarAmount());
286 assertEquals(tweedleDee.getFinancialDocumentOneDollarAmount(), tweedleDum.getFinancialDocumentOneDollarAmount());
287 }
288
289 public void assertDetailAmountsNotEqual(CurrencyDetail tweedleDee, CurrencyDetail tweedleDum) {
290 assertFalse(tweedleDee.getFinancialDocumentHundredDollarAmount().equals(tweedleDum.getFinancialDocumentHundredDollarAmount()));
291 assertFalse(tweedleDee.getFinancialDocumentFiftyDollarAmount().equals(tweedleDum.getFinancialDocumentFiftyDollarAmount()));
292 assertFalse(tweedleDee.getFinancialDocumentTwentyDollarAmount().equals(tweedleDum.getFinancialDocumentTwentyDollarAmount()));
293 assertFalse(tweedleDee.getFinancialDocumentTenDollarAmount().equals(tweedleDum.getFinancialDocumentTenDollarAmount()));
294 assertFalse(tweedleDee.getFinancialDocumentFiveDollarAmount().equals(tweedleDum.getFinancialDocumentFiveDollarAmount()));
295 assertFalse(tweedleDee.getFinancialDocumentTwoDollarAmount().equals(tweedleDum.getFinancialDocumentTwoDollarAmount()));
296 assertFalse(tweedleDee.getFinancialDocumentOneDollarAmount().equals(tweedleDum.getFinancialDocumentOneDollarAmount()));
297 }
298 }