Skip to content

Commit 20ac44a

Browse files
Devi Pathakdevi-pathak2263
authored andcommitted
FINERACT-2539: Add unit tests for MathUtil utility class
Add comprehensive unit tests for MathUtil covering core behaviors including null handling, arithmetic operations, comparison helpers, BigDecimal operations, percentage calculations, and formatting helpers. The tests verify correct handling of: - null and default value behavior - negative and zero values - arithmetic operations (add, subtract, abs) - BigDecimal utilities - percentage calculations - trailing zero normalization These tests improve reliability and increase test coverage for shared mathematical utilities used across the platform.
1 parent c8a58b9 commit 20ac44a

1 file changed

Lines changed: 145 additions & 0 deletions

File tree

  • fineract-core/src/test/java/org/apache/fineract/infrastructure/core/service
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.apache.fineract.infrastructure.core.service;
21+
22+
import java.math.BigDecimal;
23+
import java.math.MathContext;
24+
import static org.junit.jupiter.api.Assertions.assertEquals;
25+
import static org.junit.jupiter.api.Assertions.assertTrue;
26+
import static org.junit.jupiter.api.Assertions.assertNull;
27+
import org.junit.jupiter.api.Test;
28+
29+
class MathUtilTest {
30+
31+
@Test
32+
void nullToDefaultReturnsDefaultWhenValueNull() {
33+
Long result = MathUtil.nullToDefault(null, 5L);
34+
assertEquals(5L, result);
35+
}
36+
37+
@Test
38+
void nullToDefaultReturnsValueWhenNotNull() {
39+
Long result = MathUtil.nullToDefault(10L, 5L);
40+
assertEquals(10L, result);
41+
}
42+
43+
@Test
44+
void nullToZeroLongReturnsZeroWhenNull() {
45+
Long result = MathUtil.nullToZero((Long) null);
46+
assertEquals(0L, result);
47+
}
48+
49+
@Test
50+
void zeroToNullReturnsNullWhenZero() {
51+
Long result = MathUtil.zeroToNull(0L);
52+
assertNull(result);
53+
}
54+
55+
@Test
56+
void negativeToZeroReturnsZeroForNegativeValue() {
57+
Long result = MathUtil.negativeToZero(-10L);
58+
assertEquals(0L, result);
59+
}
60+
61+
@Test
62+
void negativeToZeroReturnsSameValueForPositive() {
63+
Long result = MathUtil.negativeToZero(20L);
64+
assertEquals(20L, result);
65+
}
66+
67+
@Test
68+
void isGreaterThanZeroReturnsTrueForPositive() {
69+
assertTrue(MathUtil.isGreaterThanZero(5L));
70+
}
71+
72+
@Test
73+
void isLessThanZeroReturnsTrueForNegative() {
74+
assertTrue(MathUtil.isLessThanZero(-5L));
75+
}
76+
77+
@Test
78+
void addHandlesNullValues() {
79+
Long result = MathUtil.add(null, 10L);
80+
assertEquals(10L, result);
81+
}
82+
83+
@Test
84+
void addReturnsSumOfValues() {
85+
Long result = MathUtil.add(10L, 20L);
86+
assertEquals(30L, result);
87+
}
88+
89+
@Test
90+
void subtractReturnsCorrectDifference() {
91+
Long result = MathUtil.subtract(20L, 5L);
92+
assertEquals(15L, result);
93+
}
94+
95+
@Test
96+
void absReturnsPositiveValue() {
97+
Long result = MathUtil.abs(-25L);
98+
assertEquals(25L, result);
99+
}
100+
101+
@Test
102+
void bigDecimalAddReturnsCorrectSum() {
103+
BigDecimal result = MathUtil.add(new BigDecimal("10.5"), new BigDecimal("5.5"), new MathContext(10));
104+
105+
assertEquals(new BigDecimal("16.0"), result);
106+
}
107+
108+
@Test
109+
void bigDecimalSubtractReturnsCorrectDifference() {
110+
BigDecimal result = MathUtil.subtract(new BigDecimal("20"), new BigDecimal("5"), new MathContext(10));
111+
112+
assertEquals(new BigDecimal("15"), result);
113+
}
114+
115+
@Test
116+
void percentageOfCalculatesCorrectValue() {
117+
BigDecimal value = new BigDecimal("200");
118+
BigDecimal percentage = new BigDecimal("10");
119+
120+
BigDecimal result = MathUtil.percentageOf(value, percentage, new MathContext(10));
121+
122+
assertEquals(0, result.compareTo(new BigDecimal("20")));
123+
}
124+
125+
@Test
126+
void percentageOfReturnsZeroWhenValueZero() {
127+
BigDecimal result = MathUtil.percentageOf(BigDecimal.ZERO, new BigDecimal("10"), new MathContext(10));
128+
assertEquals(BigDecimal.ZERO, result);
129+
}
130+
131+
@Test
132+
void stripTrailingZerosRemovesExtraZeros() {
133+
BigDecimal value = new BigDecimal("10.5000");
134+
135+
BigDecimal result = MathUtil.stripTrailingZeros(value);
136+
137+
assertEquals(new BigDecimal("10.5"), result);
138+
}
139+
140+
@Test
141+
void stripTrailingZerosReturnsNullWhenInputNull() {
142+
assertNull(MathUtil.stripTrailingZeros(null));
143+
}
144+
145+
}

0 commit comments

Comments
 (0)