001 /**
002 * Copyright 2005-2014 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.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/ecl2.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.rice.krad.util;
017
018 import org.junit.Test;
019 import org.kuali.rice.core.api.util.type.KualiDecimal;
020 import org.kuali.rice.core.api.util.type.KualiPercent;
021 import org.kuali.rice.core.web.format.Formatter;
022
023 import java.math.BigDecimal;
024
025 import static org.junit.Assert.assertEquals;
026
027 /**
028 * This class tests the KualiDecimal methods.
029 */
030 public class KualiPercentTest {
031 private static final int OPERAND_VALUE = 25;
032
033 @Test public void testToKualiDecimal() throws Exception {
034 KualiPercent percent1 = new KualiPercent(50);
035 KualiDecimal percentAsDecimal1 = percent1.toKualiDecimal();
036 assertEquals(0.50, percentAsDecimal1.doubleValue(), 0);
037
038 KualiPercent percent2 = new KualiPercent(new BigDecimal(25.2));
039 KualiDecimal percentAsDecimal2 = percent2.toKualiDecimal();
040 // should round down to 0.25
041 assertEquals(0.25, percentAsDecimal2.doubleValue(), 0);
042
043 KualiPercent percent3 = new KualiPercent(new BigDecimal(25.7));
044 KualiDecimal percentAsDecimal3 = percent3.toKualiDecimal();
045 // should round up to 0.26
046 assertEquals(0.26, percentAsDecimal3.doubleValue(), 0);
047 }
048
049 @Test
050 public void testFormat() throws Exception {
051 Formatter testFormatter = Formatter.getFormatter(KualiPercent.class, null);
052
053 KualiDecimal decimal1 = new KualiDecimal(52);
054 KualiDecimal decimal2 = new KualiDecimal(32.3);
055
056 String percent1 = (String)testFormatter.format(decimal1);
057 String percent2 = (String)testFormatter.format(decimal2);
058
059 assertEquals("52 percent", percent1);
060 assertEquals("32.3 percent", percent2);
061
062 }
063
064 }