1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kns.web.format;
17
18 import java.math.BigDecimal;
19
20 import org.junit.Test;
21 import org.kuali.test.KNSTestCase;
22
23 public class BigDecimalFormatterTest extends KNSTestCase {
24
25 BigDecimalFormatter f = new BigDecimalFormatter();
26
27
28
29
30 @Test public void testConvertToObject_null() {
31 String s = (String) f.convertToObject(null);
32
33 assertNull(s);
34 }
35
36
37
38
39 @Test public void testConvertToObject_blank() {
40 boolean failedAsExpected = false;
41
42 try {
43 f.convertToObject(" ");
44 }
45 catch (FormatException e) {
46 failedAsExpected = true;
47 }
48
49 assertTrue(failedAsExpected);
50 }
51
52
53
54
55 @Test public void testNonAllowedCharacterEnd() {
56 boolean failedAsExpected = false;
57
58 try {
59 f.convertToObject("9a");
60 f.convertToObject("1$");
61 f.convertToObject("1%");
62 }
63 catch (FormatException e) {
64 failedAsExpected = true;
65 }
66
67 assertTrue(failedAsExpected);
68 }
69
70
71
72
73 @Test public void testNonAllowedCharacterBeginning() {
74 boolean failedAsExpected = false;
75
76 try {
77 f.convertToObject("a9");
78 f.convertToObject("$1");
79 f.convertToObject("%1");
80 }
81 catch (FormatException e) {
82 failedAsExpected = true;
83 }
84
85 assertTrue(failedAsExpected);
86 }
87
88
89
90
91 @Test public void testNonAllowedCharacterMiddle() {
92 boolean failedAsExpected = false;
93
94 try {
95 f.convertToObject("9a9");
96 f.convertToObject("1$1");
97 f.convertToObject("1%1");
98 }
99 catch (FormatException e) {
100 failedAsExpected = true;
101 }
102
103 assertTrue(failedAsExpected);
104 }
105
106
107
108
109 @Test public void testMultipleofSingleChars() {
110 boolean failedAsExpected = false;
111
112 try {
113 f.convertToObject("9..9");
114 f.convertToObject("-92-");
115 f.convertToObject(".9.");
116 }
117 catch (FormatException e) {
118 failedAsExpected = true;
119 }
120
121 assertTrue(failedAsExpected);
122 }
123
124
125
126
127 @Test public void testBigNumberWithBigScale() {
128 String number = "39,483,098,405,908,498,608,560,954,654.546098540698546809456098450968";
129 BigDecimal num = null;
130
131 num = (BigDecimal) f.convertToObject(number);
132
133 String bdNumber = (String) f.format(num);
134 assertEquals(number, bdNumber);
135 }
136
137
138
139
140 @Test public void testBigScale() {
141 String number = "0.5460985406985468094560984509683423432546547854484575464565453454234234523";
142 BigDecimal num = null;
143
144 num = (BigDecimal) f.convertToObject(number);
145
146 String bdNumber = (String) f.format(num);
147 assertEquals(number, bdNumber);
148 }
149
150
151
152
153 @Test public void testBigNoScale() {
154 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";
155 BigDecimal num = null;
156
157 num = (BigDecimal) f.convertToObject(number);
158
159 String bdNumber = (String) f.format(num);
160 assertEquals(number, bdNumber);
161 }
162
163
164
165
166 @Test public void testNegScale() {
167 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";
168 BigDecimal num = null;
169
170 num = (BigDecimal) f.convertToObject(number);
171
172 String bdNumber = (String) f.format(num);
173 assertEquals(number, bdNumber);
174 }
175
176
177
178
179 @Test public void testTrailingZeroesOut() {
180 String numberResult = "1.00";
181 String inputNumber = "1";
182 BigDecimal num = null;
183
184 num = new BigDecimal(inputNumber).setScale(2, BigDecimal.ROUND_HALF_EVEN);
185
186 String bdNumber = (String) f.format(num);
187 assertEquals(numberResult, bdNumber);
188 }
189 }