View Javadoc

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