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.util;
017:
018: import org.kuali.kfs.context.KualiTestBase;
019:
020: /**
021: * This class tests the KualiDecimal methods.
022: */
023: public class KualiDecimalTest extends KualiTestBase {
024: private static final int OPERAND_VALUE = 25;
025:
026: private KualiDecimal operand;
027:
028: @Override
029: protected void setUp() throws Exception {
030: super .setUp();
031:
032: operand = new KualiDecimal(OPERAND_VALUE);
033: }
034:
035: final public void testIntConstructor() throws Exception {
036: final int INTVALUE = -1;
037:
038: KualiDecimal k = new KualiDecimal(INTVALUE);
039: assertEquals(INTVALUE, k.intValue());
040: }
041:
042: final public void testStringConstructor_nullString()
043: throws Exception {
044: boolean failedAsExpected = false;
045:
046: try {
047: String s = null;
048: new KualiDecimal(s);
049: } catch (IllegalArgumentException e) {
050: failedAsExpected = true;
051: }
052:
053: assertTrue(failedAsExpected);
054: }
055:
056: final public void testStringConstructor_nonnumericString()
057: throws Exception {
058: boolean failedAsExpected = false;
059:
060: try {
061: new KualiDecimal("n0nnum3r1c");
062: } catch (IllegalArgumentException e) {
063: failedAsExpected = true;
064: }
065:
066: assertTrue(failedAsExpected);
067: }
068:
069: final public void testStringConstructor_integerString()
070: throws Exception {
071: final String INTSTRING = "123";
072:
073: KualiDecimal k = new KualiDecimal(INTSTRING);
074: assertEquals(Integer.valueOf(INTSTRING).intValue(), k
075: .intValue());
076: }
077:
078: final public void testStringConstructor_floatingPointString()
079: throws Exception {
080: final String DOUBLESTRING = "0.4567";
081:
082: KualiDecimal k = new KualiDecimal(DOUBLESTRING);
083: assertEquals("0.46", k.toString());
084: }
085:
086: final public void testDoubleConstructor_noSig() throws Exception {
087: double value = 21;
088:
089: KualiDecimal k = new KualiDecimal(value);
090: assertEquals(value, k.doubleValue());
091: }
092:
093: final public void testDoubleConstructor_oneSig() throws Exception {
094: double value = 21.1;
095:
096: KualiDecimal k = new KualiDecimal(value);
097: assertEquals(value, k.doubleValue());
098: }
099:
100: final public void testDoubleConstructor_twoSig() throws Exception {
101: double value = 21.12;
102:
103: KualiDecimal k = new KualiDecimal(value);
104: assertEquals(value, k.doubleValue());
105: }
106:
107: final public void testDoubleConstructor_threeSig() throws Exception {
108: double value = 21.123;
109: double roundedValue = 21.12;
110:
111: KualiDecimal k = new KualiDecimal(value);
112: assertEquals(roundedValue, k.doubleValue());
113: }
114:
115: final public void testAdd_nullAddend() throws Exception {
116: boolean failedAsExpected = false;
117:
118: try {
119: this .operand.add(null);
120: } catch (IllegalArgumentException e) {
121: failedAsExpected = true;
122: }
123:
124: assertTrue(failedAsExpected);
125: }
126:
127: final public void testAdd_validAddend() throws Exception {
128: KualiDecimal addend = new KualiDecimal(2);
129: KualiDecimal sum = this .operand.add(addend);
130:
131: assertEquals(new KualiDecimal(OPERAND_VALUE + 2), sum);
132: }
133:
134: final public void testSubtract_nullSubtrahend() throws Exception {
135: boolean failedAsExpected = false;
136:
137: try {
138: this .operand.subtract(null);
139: } catch (IllegalArgumentException e) {
140: failedAsExpected = true;
141: }
142:
143: assertTrue(failedAsExpected);
144: }
145:
146: final public void testSubtract_validSubtrahend() throws Exception {
147: KualiDecimal subtrahend = new KualiDecimal(2);
148: KualiDecimal difference = this .operand.subtract(subtrahend);
149:
150: assertEquals(new KualiDecimal(OPERAND_VALUE - 2), difference);
151: }
152:
153: final public void testMultiply_nullMultiplier() throws Exception {
154: boolean failedAsExpected = false;
155:
156: try {
157: this .operand.multiply(null);
158: } catch (IllegalArgumentException e) {
159: failedAsExpected = true;
160: }
161:
162: assertTrue(failedAsExpected);
163: }
164:
165: final public void testMultiply_validMultiplier() throws Exception {
166: KualiDecimal multiplier = new KualiDecimal(2);
167: KualiDecimal product = this .operand.multiply(multiplier);
168:
169: assertEquals(new KualiDecimal(OPERAND_VALUE * 2), product);
170: }
171:
172: final public void testDivide_nullDivisor() throws Exception {
173: boolean failedAsExpected = false;
174:
175: try {
176: this .operand.divide(null);
177: } catch (IllegalArgumentException e) {
178: failedAsExpected = true;
179: }
180:
181: assertTrue(failedAsExpected);
182: }
183:
184: final public void testDivide_validDivisor() throws Exception {
185: KualiDecimal divisor = new KualiDecimal(2).setScale();
186: KualiDecimal quotient = this .operand.divide(divisor).setScale();
187:
188: double expectedQuotient = OPERAND_VALUE / 2.0;
189:
190: assertEquals(expectedQuotient, quotient.doubleValue());
191: }
192:
193: final public void testMod_nullModulus() throws Exception {
194: boolean failedAsExcpected = false;
195:
196: try {
197: this .operand.mod(null);
198: } catch (IllegalArgumentException e) {
199: failedAsExcpected = true;
200: }
201:
202: assertTrue(failedAsExcpected);
203: }
204:
205: final public void testMod_validZeroModulus() throws Exception {
206: // divide by some number to make sure that the two aren't the same
207: KualiDecimal zeroModulus = this .operand
208: .divide(new KualiDecimal(5));
209: KualiDecimal expectedZero = this .operand.mod(zeroModulus);
210:
211: assertEquals(new KualiDecimal(0), expectedZero);
212: }
213:
214: final public void testMod_validNonZeroModulus() throws Exception {
215: KualiDecimal nonZeroModulus = this .operand.divide(
216: new KualiDecimal(5)).add(new KualiDecimal(1));
217: KualiDecimal expectedNonZero = this .operand.mod(nonZeroModulus);
218:
219: assertNotSame(new KualiDecimal(0), expectedNonZero);
220: }
221:
222: final public void testNegative_zeroValue() {
223: KualiDecimal zeroValue = new KualiDecimal(0);
224: KualiDecimal negativeZeroValue = zeroValue.negated();
225:
226: assertEquals(zeroValue, negativeZeroValue);
227: }
228:
229: final public void testNegative_negativeValue() {
230: KualiDecimal negativeValue = new KualiDecimal(-4000);
231: KualiDecimal negativeNegativeValue = negativeValue.negated();
232:
233: assertEquals(negativeValue.intValue() * -1,
234: negativeNegativeValue.intValue());
235: }
236:
237: final public void testNegative_positiveValue() {
238: KualiDecimal positiveValue = new KualiDecimal(2112);
239: KualiDecimal negativePositiveValue = positiveValue.negated();
240:
241: assertEquals(positiveValue.intValue() * -1,
242: negativePositiveValue.intValue());
243: }
244:
245: final public void testNegative_negativeDoubleValue() {
246: KualiDecimal negativeValue = new KualiDecimal("-23.12");
247: KualiDecimal negativeNegativeValue = negativeValue.negated();
248:
249: assertEquals(new Double(negativeValue.doubleValue() * -1.0),
250: new Double(negativeNegativeValue.doubleValue()));
251: }
252:
253: final public void testNegative_positiveDoubleValue() {
254: KualiDecimal positiveValue = new KualiDecimal("12.34");
255: KualiDecimal negativePositiveValue = positiveValue.negated();
256:
257: assertEquals(new Double(positiveValue.doubleValue() * -1.0),
258: new Double(negativePositiveValue.doubleValue()));
259: }
260:
261: final public void testEquals_inequalIntegerValues() {
262: KualiDecimal v1 = new KualiDecimal(1);
263: KualiDecimal v2 = new KualiDecimal(2);
264:
265: assertFalse(v1.equals(v2));
266: assertFalse(v2.equals(v1));
267: }
268:
269: final public void testEquals_equalIntegerValues() {
270: KualiDecimal v1 = new KualiDecimal(3);
271: KualiDecimal v2 = new KualiDecimal(3);
272:
273: assertTrue(v1.equals(v2));
274: assertTrue(v2.equals(v1));
275: }
276:
277: final public void testEquals_inequalDoubleValues() {
278: KualiDecimal v1 = new KualiDecimal(1.0d);
279: KualiDecimal v2 = new KualiDecimal(2.0d);
280:
281: assertFalse(v1.equals(v2));
282: assertFalse(v2.equals(v1));
283: }
284:
285: final public void testEquals_roughlyEqualDoubleValues() {
286: KualiDecimal v1 = new KualiDecimal(6.03d);
287: KualiDecimal v2 = new KualiDecimal(6.029997d);
288:
289: assertTrue(v1.equals(v2));
290: assertTrue(v2.equals(v1));
291: }
292:
293: final public void testEquals_equalDoubleValues() {
294: KualiDecimal v1 = new KualiDecimal(6.03d);
295: KualiDecimal v2 = new KualiDecimal(6.03d);
296:
297: assertTrue(v1.equals(v2));
298: assertTrue(v2.equals(v1));
299: }
300:
301: final public void testEquals_inequalStringValue() {
302: KualiDecimal s1 = new KualiDecimal("100.00");
303: KualiDecimal s2 = new KualiDecimal("100.01");
304:
305: assertFalse(s1.equals(s2));
306: assertFalse(s2.equals(s1));
307: }
308:
309: final public void testEquals_equalStringValues() {
310: KualiDecimal s1 = new KualiDecimal("100.00");
311: KualiDecimal s2 = new KualiDecimal("100.00");
312:
313: assertTrue(s1.equals(s2));
314: assertTrue(s2.equals(s1));
315: }
316:
317: final public void testEquals_equivalentValues() {
318: KualiDecimal d1 = new KualiDecimal(100);
319: KualiDecimal s1 = new KualiDecimal("100");
320:
321: assertTrue(d1.equals(s1));
322: assertTrue(s1.equals(d1));
323: }
324:
325: /**
326: * The specific values used in this test case were copied from the code which was breaking because it was converting the
327: * KualiDecimals into floats, adding the floats, and comparing them. The float addition is here to illustrate one of the
328: * problems that requires us to do KualiDecimal math instead of converting back to a primitive format.
329: */
330: final public void testEquals_summedFloatValues() {
331:
332: // sum them as floats
333: float c1 = 1.01f;
334: float c2 = 3.00f;
335: float c3 = 2.02f;
336:
337: float d1 = 4.02f;
338: float d2 = 2.01f;
339:
340: float c = c1 + c2 + c3;
341: float d = d1 + d2;
342:
343: assertFalse(c == d);
344:
345: // sum them as KualiDecimals built from Strings, as the JSP was doing
346: KualiDecimal a1 = new KualiDecimal("1.01");
347: KualiDecimal a2 = new KualiDecimal("3.00");
348: KualiDecimal a3 = new KualiDecimal("2.02");
349:
350: KualiDecimal b1 = new KualiDecimal("4.02");
351: KualiDecimal b2 = new KualiDecimal("2.01");
352:
353: KualiDecimal a = a1.add(a2).add(a3);
354: KualiDecimal b = b1.add(b2);
355:
356: assertTrue(a.equals(b));
357: assertTrue(b.equals(a));
358: }
359:
360: public final void testIsNegative_negativeValue() {
361: KualiDecimal v1 = new KualiDecimal(-231);
362:
363: assertTrue(v1.isNegative());
364: }
365:
366: public final void testIsNegative_positiveValue() {
367: KualiDecimal v1 = new KualiDecimal(987);
368:
369: assertFalse(v1.isNegative());
370: }
371:
372: public final void testIsNegative_zeroValue() {
373: KualiDecimal v1 = new KualiDecimal(0);
374:
375: assertFalse(v1.isNegative());
376: }
377:
378: public final void testIsPositive_negativeValue() {
379: KualiDecimal v1 = new KualiDecimal(-231);
380:
381: assertFalse(v1.isPositive());
382: }
383:
384: public final void testIsPositive_positiveValue() {
385: KualiDecimal v1 = new KualiDecimal(987);
386:
387: assertTrue(v1.isPositive());
388: }
389:
390: public final void testIsPositive_zeroValue() {
391: KualiDecimal v1 = new KualiDecimal(0);
392:
393: assertFalse(v1.isPositive());
394: }
395:
396: public final void testIsZero_negativeValue() {
397: KualiDecimal v1 = new KualiDecimal(-231);
398:
399: assertFalse(v1.isZero());
400: }
401:
402: public final void testIsZero_positiveValue() {
403: KualiDecimal v1 = new KualiDecimal(987);
404:
405: assertFalse(v1.isZero());
406: }
407:
408: public final void testIsZero_zeroValue() {
409: KualiDecimal v1 = new KualiDecimal(0);
410:
411: assertTrue(v1.isZero());
412: }
413:
414: public final void testIsNonZero_negativeValue() {
415: KualiDecimal v1 = new KualiDecimal(-231);
416:
417: assertTrue(v1.isNonZero());
418: }
419:
420: public final void testIsNonZero_positiveValue() {
421: KualiDecimal v1 = new KualiDecimal(987);
422:
423: assertTrue(v1.isNonZero());
424: }
425:
426: public final void testIsNonZero_zeroValue() {
427: KualiDecimal v1 = new KualiDecimal(0);
428:
429: assertFalse(v1.isNonZero());
430: }
431:
432: public final void testIsLessThan_nullOperand() {
433: boolean failedAsExpected = false;
434:
435: KualiDecimal v1 = new KualiDecimal(123);
436: try {
437: v1.isLessThan(null);
438: } catch (IllegalArgumentException e) {
439: failedAsExpected = true;
440: }
441:
442: assertTrue(failedAsExpected);
443: }
444:
445: public final void testIsLessThan_greaterOperand() {
446: KualiDecimal v1 = new KualiDecimal(123);
447: KualiDecimal v2 = new KualiDecimal(456);
448:
449: assertTrue(v1.isLessThan(v2));
450: }
451:
452: public final void testIsLessThan_equalOperand() {
453: KualiDecimal v1 = new KualiDecimal(456);
454: KualiDecimal v2 = new KualiDecimal(456);
455:
456: assertFalse(v1.isLessThan(v2));
457: }
458:
459: public final void testIsLessThan_lesserOperand() {
460: KualiDecimal v1 = new KualiDecimal(789);
461: KualiDecimal v2 = new KualiDecimal(345);
462:
463: assertFalse(v1.isLessThan(v2));
464: }
465:
466: public final void testIsGreaterThan_nullOperand() {
467: boolean failedAsExpected = false;
468:
469: KualiDecimal v1 = new KualiDecimal(123);
470: try {
471: v1.isGreaterThan(null);
472: } catch (IllegalArgumentException e) {
473: failedAsExpected = true;
474: }
475:
476: assertTrue(failedAsExpected);
477: }
478:
479: public final void testIsGreaterThan_greaterOperand() {
480: KualiDecimal v1 = new KualiDecimal(123);
481: KualiDecimal v2 = new KualiDecimal(456);
482:
483: assertTrue(v2.isGreaterThan(v1));
484: }
485:
486: public final void testIsGreaterThan_equalOperand() {
487: KualiDecimal v1 = new KualiDecimal(456);
488: KualiDecimal v2 = new KualiDecimal(456);
489:
490: assertFalse(v1.isGreaterThan(v2));
491: }
492:
493: public final void testIsGreaterThan_lesserOperand() {
494: KualiDecimal v1 = new KualiDecimal(789);
495: KualiDecimal v2 = new KualiDecimal(345);
496:
497: assertFalse(v2.isGreaterThan(v1));
498: }
499:
500: public final void testIsLessEqual_nullOperand() {
501: boolean failedAsExpected = false;
502:
503: KualiDecimal v1 = new KualiDecimal(123);
504: try {
505: v1.isLessEqual(null);
506: } catch (IllegalArgumentException e) {
507: failedAsExpected = true;
508: }
509:
510: assertTrue(failedAsExpected);
511: }
512:
513: public final void testIsLessEqual_greaterOperand() {
514: KualiDecimal v1 = new KualiDecimal(123);
515: KualiDecimal v2 = new KualiDecimal(456);
516:
517: assertTrue(v1.isLessEqual(v2));
518: }
519:
520: public final void testIsLessEqual_equalOperand() {
521: KualiDecimal v1 = new KualiDecimal(456);
522: KualiDecimal v2 = new KualiDecimal(456);
523:
524: assertTrue(v1.isLessEqual(v2));
525: }
526:
527: public final void testIsLessEqual_lesserOperand() {
528: KualiDecimal v1 = new KualiDecimal(789);
529: KualiDecimal v2 = new KualiDecimal(345);
530:
531: assertFalse(v1.isLessEqual(v2));
532: }
533:
534: public final void testIsGreaterEqual_nullOperand() {
535: boolean failedAsExpected = false;
536:
537: KualiDecimal v1 = new KualiDecimal(123);
538: try {
539: v1.isGreaterEqual(null);
540: } catch (IllegalArgumentException e) {
541: failedAsExpected = true;
542: }
543:
544: assertTrue(failedAsExpected);
545: }
546:
547: public final void testIsGreaterEqual_greaterOperand() {
548: KualiDecimal v1 = new KualiDecimal(123);
549: KualiDecimal v2 = new KualiDecimal(456);
550:
551: assertTrue(v2.isGreaterEqual(v1));
552: }
553:
554: public final void testIsGreaterEqual_equalOperand() {
555: KualiDecimal v1 = new KualiDecimal(456);
556: KualiDecimal v2 = new KualiDecimal(456);
557:
558: assertTrue(v1.isGreaterEqual(v2));
559: }
560:
561: public final void testIsGreaterEqual_lesserOperand() {
562: KualiDecimal v1 = new KualiDecimal(789);
563: KualiDecimal v2 = new KualiDecimal(345);
564:
565: assertFalse(v2.isGreaterEqual(v1));
566: }
567:
568: public final void testIsNumeric_null() {
569: assertFalse(KualiDecimal.isNumeric(null));
570: }
571:
572: public final void testIsNumeric_blank() {
573: assertFalse(KualiDecimal.isNumeric(" "));
574: }
575:
576: public final void testIsNumeric_alphanumeric() {
577: assertFalse(KualiDecimal.isNumeric("12YYZ23"));
578: }
579:
580: public final void testIsNumeric_integral() {
581: assertTrue(KualiDecimal.isNumeric("1234"));
582: }
583:
584: public final void testIsNumeric_decimal() {
585: assertTrue(KualiDecimal.isNumeric("1234.56"));
586: }
587:
588: public final void testIsNumeric_moreSignificantDecimal() {
589: assertTrue(KualiDecimal.isNumeric("1234.56789"));
590: }
591:
592: public final void testIsNumeric_negativeIntegral() {
593: assertTrue(KualiDecimal.isNumeric("-1234"));
594: }
595:
596: public final void testIsNumeric_negativeDecimal() {
597: assertTrue(KualiDecimal.isNumeric("-1234.56"));
598: }
599:
600: public final void testIsNumeric_zero() {
601: assertTrue(KualiDecimal.isNumeric("0"));
602: }
603:
604: public final void testIsNumeric_multiZero() {
605: assertTrue(KualiDecimal.isNumeric("0000"));
606: }
607:
608: public final void testIsNumeric_negativeZero() {
609: assertTrue(KualiDecimal.isNumeric("-0"));
610: }
611:
612: public final void testIsNumeric_decimalZero() {
613: assertTrue(KualiDecimal.isNumeric("0.0"));
614: }
615:
616: public final void testIsNumeric_negativeDecimalZero() {
617: assertTrue(KualiDecimal.isNumeric("-0.00"));
618: }
619: }
|