|
| 1 | +/* |
| 2 | + * Copyright © 2025 Cask Data, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | + * use this file except in compliance with the License. You may obtain a copy of |
| 6 | + * the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 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, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations under |
| 14 | + * the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.cdap.plugin.db.source; |
| 18 | + |
| 19 | +import org.apache.hadoop.conf.Configuration; |
| 20 | +import org.apache.hadoop.mapreduce.InputSplit; |
| 21 | +import org.apache.hadoop.mapreduce.lib.db.BigDecimalSplitter; |
| 22 | +import org.junit.Test; |
| 23 | + |
| 24 | +import java.math.BigDecimal; |
| 25 | +import java.sql.ResultSet; |
| 26 | +import java.sql.SQLException; |
| 27 | +import java.util.List; |
| 28 | + |
| 29 | +import static org.junit.Assert.assertEquals; |
| 30 | +import static org.junit.Assert.assertThrows; |
| 31 | +import static org.junit.Assert.assertTrue; |
| 32 | +import static org.mockito.Mockito.mock; |
| 33 | +import static org.mockito.Mockito.when; |
| 34 | + |
| 35 | +/** |
| 36 | + * Test class for {@link SafeBigDecimalSplitter} |
| 37 | + */ |
| 38 | +public class SafeBigDecimalSplitterTest { |
| 39 | + private final SafeBigDecimalSplitter splitter = new SafeBigDecimalSplitter(); |
| 40 | + |
| 41 | + @Test |
| 42 | + public void testSmallRangeDivision() { |
| 43 | + BigDecimal result = splitter.tryDivide(BigDecimal.ONE, new BigDecimal("4")); |
| 44 | + assertEquals(new BigDecimal("0.25000"), result); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void testLargePrecision() { |
| 49 | + BigDecimal numerator = new BigDecimal("1.0000000000000000001"); |
| 50 | + BigDecimal denominator = new BigDecimal("3"); |
| 51 | + BigDecimal result = splitter.tryDivide(numerator, denominator); |
| 52 | + assertTrue(result.compareTo(BigDecimal.ZERO) > 0); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void testDivisionByZero() { |
| 57 | + assertThrows(ArithmeticException.class, () -> |
| 58 | + splitter.tryDivide(BigDecimal.ONE, BigDecimal.ZERO)); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void testDivisionWithZeroNumerator() { |
| 63 | + // when minVal == maxVal |
| 64 | + BigDecimal result = splitter.tryDivide(BigDecimal.ZERO, BigDecimal.ONE); |
| 65 | + assertEquals(0, result.compareTo(BigDecimal.ZERO)); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void testSplits() throws SQLException { |
| 70 | + BigDecimal minVal = BigDecimal.valueOf(1); |
| 71 | + BigDecimal maxVal = BigDecimal.valueOf(2); |
| 72 | + int numSplits = 4; |
| 73 | + ResultSet resultSet = mock(ResultSet.class); |
| 74 | + Configuration conf = mock(Configuration.class); |
| 75 | + when(conf.getInt("mapreduce.job.maps", 1)).thenReturn(numSplits); |
| 76 | + when(resultSet.getBigDecimal(1)).thenReturn(minVal); |
| 77 | + when(resultSet.getBigDecimal(2)).thenReturn(maxVal); |
| 78 | + BigDecimalSplitter bigDecimalSplitter = new SafeBigDecimalSplitter(); |
| 79 | + List<InputSplit> actualSplits = bigDecimalSplitter.split(conf, resultSet, "id"); |
| 80 | + assertEquals(numSplits, actualSplits.size()); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void testSplitsWithMinValueEqualToMaxValue() throws SQLException { |
| 85 | + // when minVal == maxVal |
| 86 | + BigDecimal minVal = BigDecimal.valueOf(1); |
| 87 | + BigDecimal maxVal = BigDecimal.valueOf(1); |
| 88 | + int numSplits = 1; |
| 89 | + ResultSet resultSet = mock(ResultSet.class); |
| 90 | + Configuration conf = mock(Configuration.class); |
| 91 | + when(conf.getInt("mapreduce.job.maps", 1)).thenReturn(numSplits); |
| 92 | + when(resultSet.getBigDecimal(1)).thenReturn(minVal); |
| 93 | + when(resultSet.getBigDecimal(2)).thenReturn(maxVal); |
| 94 | + BigDecimalSplitter bigDecimalSplitter = new SafeBigDecimalSplitter(); |
| 95 | + List<InputSplit> actualSplits = bigDecimalSplitter.split(conf, resultSet, "id"); |
| 96 | + assertEquals(numSplits, actualSplits.size()); |
| 97 | + } |
| 98 | +} |
0 commit comments