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.core.web.format;
017:
018: import java.math.BigDecimal;
019:
020: import org.kuali.kfs.context.KualiTestBase;
021:
022: public class BigDecimalFormatterTest extends KualiTestBase {
023:
024: BigDecimalFormatter f;
025:
026: @Override
027: protected void setUp() throws Exception {
028: super .setUp();
029:
030: f = new BigDecimalFormatter();
031:
032: }
033:
034: /**
035: * This method tests for null if object is null
036: */
037: public final void testConvertToObject_null() {
038: String s = (String) f.convertToObject(null);
039:
040: assertNull(s);
041: }
042:
043: /**
044: * This method tests that blank is not allowed
045: */
046: public final void testConvertToObject_blank() {
047: boolean failedAsExpected = false;
048:
049: try {
050: f.convertToObject(" ");
051: } catch (FormatException e) {
052: failedAsExpected = true;
053: }
054:
055: assertTrue(failedAsExpected);
056: }
057:
058: /**
059: * This method tests that invalid characters at the begining fail
060: */
061: public final void testNonAllowedCharacterEnd() {
062: boolean failedAsExpected = false;
063:
064: try {
065: f.convertToObject("9a");
066: f.convertToObject("1$");
067: f.convertToObject("1%");
068: } catch (FormatException e) {
069: failedAsExpected = true;
070: }
071:
072: assertTrue(failedAsExpected);
073: }
074:
075: /**
076: * This method tests that invalid characters at the end fail
077: */
078: public final void testNonAllowedCharacterBeginning() {
079: boolean failedAsExpected = false;
080:
081: try {
082: f.convertToObject("a9");
083: f.convertToObject("$1");
084: f.convertToObject("%1");
085: } catch (FormatException e) {
086: failedAsExpected = true;
087: }
088:
089: assertTrue(failedAsExpected);
090: }
091:
092: /**
093: * This method tests that invalid characters in the middle fail
094: */
095: public final void testNonAllowedCharacterMiddle() {
096: boolean failedAsExpected = false;
097:
098: try {
099: f.convertToObject("9a9");
100: f.convertToObject("1$1");
101: f.convertToObject("1%1");
102: } catch (FormatException e) {
103: failedAsExpected = true;
104: }
105:
106: assertTrue(failedAsExpected);
107: }
108:
109: /**
110: * This method tests that invalid characters in the middle fail
111: */
112: public final void testMultipleofSingleChars() {
113: boolean failedAsExpected = false;
114:
115: try {
116: f.convertToObject("9..9");
117: f.convertToObject("-92-");
118: f.convertToObject(".9.");
119: } catch (FormatException e) {
120: failedAsExpected = true;
121: }
122:
123: assertTrue(failedAsExpected);
124: }
125:
126: /**
127: * This method tests big number with precision
128: */
129: public final void testBigNumberWithBigScale() {
130: String number = "39,483,098,405,908,498,608,560,954,654.546098540698546809456098450968";
131: BigDecimal num = null;
132: // should throw exception if problem here
133: num = (BigDecimal) f.convertToObject(number);
134:
135: String bdNumber = (String) f.format(num);
136: assertEquals(number, bdNumber);
137: }
138:
139: /**
140: * This method tests big number with precision
141: */
142: public final void testBigScale() {
143: String number = "0.5460985406985468094560984509683423432546547854484575464565453454234234523";
144: BigDecimal num = null;
145: // should throw exception if problem here
146: num = (BigDecimal) f.convertToObject(number);
147:
148: String bdNumber = (String) f.format(num);
149: assertEquals(number, bdNumber);
150: }
151:
152: /**
153: * This method tests big number with precision
154: */
155: public final void testBigNoScale() {
156: String number = "5,460,985,406,985,468,094,560,984,509,683,423,432,546,547,854,484,575,464,565,453,454,234,234,523";
157: BigDecimal num = null;
158: // should throw exception if problem here
159: num = (BigDecimal) f.convertToObject(number);
160:
161: String bdNumber = (String) f.format(num);
162: assertEquals(number, bdNumber);
163: }
164:
165: /**
166: * This method tests negative numbers
167: */
168: public final void testNegScale() {
169: String number = "-5,460,985,406,985,468,094,560,984,509,683,423,432,546,547,854,484,575,464,565,453,454,234,234,523";
170: BigDecimal num = null;
171: // should throw exception if problem here
172: num = (BigDecimal) f.convertToObject(number);
173:
174: String bdNumber = (String) f.format(num);
175: assertEquals(number, bdNumber);
176: }
177:
178: /**
179: * This method tests trailing zeroes
180: */
181: public final void testTrailingZeroesOut() {
182: String numberResult = "1.00";
183: String inputNumber = "1";
184: BigDecimal num = null;
185: // should throw exception if problem here
186: num = new BigDecimal(inputNumber).setScale(2,
187: BigDecimal.ROUND_HALF_EVEN);
188:
189: String bdNumber = (String) f.format(num);
190: assertEquals(numberResult, bdNumber);
191: }
192: }
|