|
| 1 | +package com.microsoft.sqlserver.jdbc.unit.statement; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | + |
| 5 | +import java.math.BigDecimal; |
| 6 | +import java.sql.CallableStatement; |
| 7 | +import java.sql.Connection; |
| 8 | +import java.sql.SQLException; |
| 9 | +import java.sql.Statement; |
| 10 | +import java.sql.Types; |
| 11 | + |
| 12 | +import org.junit.jupiter.api.AfterEach; |
| 13 | +import org.junit.jupiter.api.BeforeAll; |
| 14 | +import org.junit.jupiter.api.BeforeEach; |
| 15 | +import org.junit.jupiter.api.Tag; |
| 16 | +import org.junit.jupiter.api.Test; |
| 17 | + |
| 18 | +import com.microsoft.sqlserver.jdbc.RandomUtil; |
| 19 | +import com.microsoft.sqlserver.testframework.AbstractSQLGenerator; |
| 20 | +import com.microsoft.sqlserver.testframework.AbstractTest; |
| 21 | + |
| 22 | +public class BigDecimalPrecisionTest extends AbstractTest { |
| 23 | + |
| 24 | + String procName1 = AbstractSQLGenerator.escapeIdentifier(RandomUtil.getIdentifier("test_bigdecimal_3")); |
| 25 | + String procName2 = AbstractSQLGenerator.escapeIdentifier(RandomUtil.getIdentifier("test_bigdecimal_5")); |
| 26 | + |
| 27 | + @BeforeEach |
| 28 | + public void init() throws SQLException { |
| 29 | + try (Connection connection = getConnection()) { |
| 30 | + String createProcedureSQL1 = "CREATE PROCEDURE " + procName1 + "\n" + |
| 31 | + " @big_decimal_type decimal(15, 3),\n" + |
| 32 | + " @big_decimal_type_o decimal(15, 3) OUTPUT\n" + |
| 33 | + "AS\n" + |
| 34 | + "BEGIN\n" + |
| 35 | + " SET @big_decimal_type_o = @big_decimal_type;\n" + |
| 36 | + "END;"; |
| 37 | + String createProcedureSQL2 = "CREATE PROCEDURE " + procName2 + "\n" + |
| 38 | + " @big_decimal_type decimal(15, 5),\n" + |
| 39 | + " @big_decimal_type_o decimal(15, 5) OUTPUT\n" + |
| 40 | + "AS\n" + |
| 41 | + "BEGIN\n" + |
| 42 | + " SET @big_decimal_type_o = @big_decimal_type;\n" + |
| 43 | + "END;"; |
| 44 | + |
| 45 | + try (Statement stmt = connection.createStatement()) { |
| 46 | + stmt.execute(createProcedureSQL1); |
| 47 | + stmt.execute(createProcedureSQL2); |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + @AfterEach |
| 53 | + public void terminate() throws SQLException { |
| 54 | + try (Connection connection = getConnection()) { |
| 55 | + try (Statement stmt = connection.createStatement()) { |
| 56 | + String dropProcedureSQL = "DROP PROCEDURE IF EXISTS " + procName1 + ", " + procName2; |
| 57 | + stmt.execute(dropProcedureSQL); |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + @Tag("BigDecimal") |
| 64 | + public void testBigDecimalPrecision() throws SQLException { |
| 65 | + try (Connection connection = getConnection()) { |
| 66 | + // Test for DECIMAL(15, 3) |
| 67 | + String callSQL1 = "{call " + procName1 + "(100.241, ?)}"; |
| 68 | + try (CallableStatement call = connection.prepareCall(callSQL1)) { |
| 69 | + call.registerOutParameter(1, Types.DECIMAL); |
| 70 | + call.execute(); |
| 71 | + BigDecimal actual1 = call.getBigDecimal(1); |
| 72 | + assertEquals(new BigDecimal("100.241"), actual1); |
| 73 | + } |
| 74 | + |
| 75 | + // Test for DECIMAL(15, 5) |
| 76 | + String callSQL2 = "{call " + procName2 + "(100.24112, ?)}"; |
| 77 | + try (CallableStatement call = connection.prepareCall(callSQL2)) { |
| 78 | + call.registerOutParameter(1, Types.DECIMAL); |
| 79 | + call.execute(); |
| 80 | + BigDecimal actual2 = call.getBigDecimal(1); |
| 81 | + assertEquals(new BigDecimal("100.24112"), actual2); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + @BeforeAll |
| 87 | + public static void setupTests() throws Exception { |
| 88 | + setConnection(); |
| 89 | + } |
| 90 | +} |
0 commit comments