001: /*
002: * Copyright 2006-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.web.format;
017:
018: import java.text.NumberFormat;
019:
020: import org.kuali.core.util.KualiDecimal;
021: import org.kuali.kfs.KFSKeyConstants;
022: import org.kuali.kfs.context.KualiTestBase;
023:
024: public class CurrencyFormatterTest extends KualiTestBase {
025:
026: CurrencyFormatter f;
027:
028: @Override
029: protected void setUp() throws Exception {
030: super .setUp();
031:
032: f = new CurrencyFormatter();
033:
034: }
035:
036: public final void testConvertToObject_null() {
037: String s = (String) f.convertToObject(null);
038:
039: assertNull(s);
040: }
041:
042: public final void testConvertToObject_blank() {
043: boolean failedAsExpected = false;
044:
045: try {
046: f.convertToObject(" ");
047: } catch (FormatException e) {
048: failedAsExpected = true;
049: }
050:
051: assertTrue(failedAsExpected);
052: }
053:
054: // sorry, the names are just going to have to be vague
055: public final void testConvertToObject01() {
056: boolean failedAsExpected = false;
057:
058: try {
059: f.convertToObject("9aaaaaaaaaaaa");
060: } catch (FormatException e) {
061: failedAsExpected = true;
062: }
063:
064: assertTrue(failedAsExpected);
065: }
066:
067: public final void testConvertToObject02() {
068: boolean failedAsExpected = false;
069:
070: try {
071: f.convertToObject("aaaaaaaaaaaa9");
072: } catch (FormatException e) {
073: failedAsExpected = true;
074: }
075:
076: assertTrue(failedAsExpected);
077: }
078:
079: public final void testConvertToObject03() {
080: boolean failedAsExpected = false;
081:
082: try {
083: f.convertToObject("9aaaaaaaaaaaa9");
084: } catch (FormatException e) {
085: failedAsExpected = true;
086: }
087:
088: assertTrue(failedAsExpected);
089: }
090:
091: /**
092: * Demonstrates that trying to format a string with excessive decimal places will fail.
093: */
094: public final void testConvertToObject04() {
095: String stringValue = "1.9999999";
096:
097: boolean failedAsExpected = false;
098: try {
099: f.convertToObject(stringValue);
100: } catch (FormatException e) {
101: failedAsExpected = true;
102: assertEquals(KFSKeyConstants.ERROR_CURRENCY_DECIMAL, e
103: .getErrorKey());
104: }
105: assertTrue(failedAsExpected);
106: }
107:
108: /**
109: * Demonstrates that trying to convert a KualiDecimal which has 0, 1, or 2 digits after the decimal place succeeds.
110: */
111: public final void testConvertToObject05() {
112: String zeroDigitString = "0.";
113: String oneDigitString = "0.0";
114: String twoDigitString = "0.00";
115:
116: KualiDecimal zeroDigits = (KualiDecimal) f
117: .convertToObject(zeroDigitString);
118: assertEquals(KualiDecimal.ZERO, zeroDigits);
119:
120: KualiDecimal oneDigit = (KualiDecimal) f
121: .convertToObject(oneDigitString);
122: assertEquals(KualiDecimal.ZERO, oneDigit);
123:
124: KualiDecimal twoDigit = (KualiDecimal) f
125: .convertToObject(twoDigitString);
126: assertEquals(KualiDecimal.ZERO, twoDigit);
127:
128: }
129:
130: /**
131: * this test is related to [KULEDOCS-742] test that the correct value is returned for the NumberFormat instance used by
132: * <code>CurrencyFormatter</code> when parsing large numbers
133: *
134: * @throws Exception
135: */
136: public final void testRealBigNumberFormat_parse() throws Exception {
137: String number = "12345678901234567899.00";
138: NumberFormat formatter = CurrencyFormatter
139: .getCurrencyInstanceUsingParseBigDecimal();
140: Number parsedNumber = formatter.parse("$" + number);
141: KualiDecimal newNumber = new KualiDecimal(parsedNumber
142: .toString());
143:
144: assertEquals(number, newNumber.toString());
145: }
146:
147: /**
148: * this relates to [KULEDOCS-742] test that the correct value is returned for <code>CurrencyFormatter</code> format method
149: * using large numbers
150: *
151: * @see CurrencyFormatter#format(java.lang.Object)
152: * @throws Exception
153: */
154: public final void testRealBigNumberFormat_format() throws Exception {
155: String stringNumber = "12345678901234567899";
156: String expected = "12,345,678,901,234,567,899.00";
157:
158: KualiDecimal number = new KualiDecimal(stringNumber);
159: Object parsedNumber = f.format(number);
160:
161: assertEquals(expected, parsedNumber);
162: }
163:
164: /**
165: * this relates to [KULEDOCS-742] test that the correct value is returned for <code>CurrencyFormatter</code> format method
166: * using large numbers
167: *
168: * @see CurrencyFormatter#format(java.lang.Object)
169: * @throws Exception
170: */
171: public final void testRealBigDecimalNumberFormat_format()
172: throws Exception {
173: String stringNumber = "12345678901234567899.12";
174: String expected = "12,345,678,901,234,567,899.12";
175:
176: KualiDecimal number = new KualiDecimal(stringNumber);
177: Object parsedNumber = f.format(number);
178:
179: assertEquals(expected, parsedNumber);
180: }
181: }
|