View Javadoc

1   /*
2    * Copyright 2005-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.util;
17  
18  import java.math.BigDecimal;
19  
20  import org.junit.Test;
21  import org.kuali.rice.kns.util.KualiDecimal;
22  import org.kuali.rice.kns.util.KualiPercent;
23  import org.kuali.rice.kns.web.format.Formatter;
24  import org.kuali.rice.test.BaseRiceTestCase;
25  
26  /**
27   * This class tests the KualiDecimal methods.
28   */
29  public class KualiPercentTest extends BaseRiceTestCase {
30      private static final int OPERAND_VALUE = 25;
31  
32      @Test public void testToKualiDecimal() throws Exception {
33      	KualiPercent percent1 = new KualiPercent(50);
34      	KualiDecimal percentAsDecimal1 = percent1.toKualiDecimal();
35      	assertEquals(0.50, percentAsDecimal1.doubleValue(), 0);
36      	
37      	KualiPercent percent2 = new KualiPercent(new BigDecimal(25.2));
38      	KualiDecimal percentAsDecimal2 = percent2.toKualiDecimal();
39      	// should round down to 0.25
40      	assertEquals(0.25, percentAsDecimal2.doubleValue(), 0);
41      	
42      	KualiPercent percent3 = new KualiPercent(new BigDecimal(25.7));
43      	KualiDecimal percentAsDecimal3 = percent3.toKualiDecimal();
44      	// should round up to 0.26
45      	assertEquals(0.26, percentAsDecimal3.doubleValue(), 0);
46      }
47  
48      @Test 
49      public void testFormat() throws Exception {
50          Formatter testFormatter = Formatter.getFormatter(KualiPercent.class, null);
51                  
52          KualiDecimal decimal1 = new KualiDecimal(52);
53          KualiDecimal decimal2 = new KualiDecimal(32.3); 
54  
55          String percent1 = (String)testFormatter.format(decimal1);
56          String percent2 = (String)testFormatter.format(decimal2);
57          
58          assertEquals("52 percent", percent1);
59          assertEquals("32.3 percent", percent2);
60      
61      }    
62  
63  }