View Javadoc

1   /*
2    * Copyright 2007-2008 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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       * This method tests for null if object is null
29       */
30      @Test public void testConvertToObject_null() {
31          String s = (String) f.convertToObject(null);
32  
33          assertNull(s);
34      }
35  
36      /**
37       * This method tests that blank is not allowed
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       * This method tests that invalid characters at the beginning fail
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       * This method tests that invalid characters at the end fail
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       * This method tests that invalid characters in the middle fail
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      * This method tests that invalid characters in the middle fail
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      * This method tests big number with precision
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         // should throw exception if problem here
131         num = (BigDecimal) f.convertToObject(number);
132 
133         String bdNumber = (String) f.format(num);
134         assertEquals(number, bdNumber);
135     }
136 
137     /**
138      * This method tests big number with precision
139      */
140     @Test public void testBigScale() {
141         String number = "0.5460985406985468094560984509683423432546547854484575464565453454234234523";
142         BigDecimal num = null;
143         // should throw exception if problem here
144         num = (BigDecimal) f.convertToObject(number);
145 
146         String bdNumber = (String) f.format(num);
147         assertEquals(number, bdNumber);
148     }
149 
150     /**
151      * This method tests big number with precision
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         // should throw exception if problem here
157         num = (BigDecimal) f.convertToObject(number);
158 
159         String bdNumber = (String) f.format(num);
160         assertEquals(number, bdNumber);
161     }
162 
163     /**
164      * This method tests negative numbers
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         // should throw exception if problem here
170         num = (BigDecimal) f.convertToObject(number);
171 
172         String bdNumber = (String) f.format(num);
173         assertEquals(number, bdNumber);
174     }
175 
176     /**
177      * This method tests trailing zeroes
178      */
179     @Test public void testTrailingZeroesOut() {
180         String numberResult = "1.00";
181         String inputNumber = "1";
182         BigDecimal num = null;
183         // should throw exception if problem here
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 }