|
6 | 6 | import org.testng.Assert; |
7 | 7 | import org.testng.annotations.*; |
8 | 8 |
|
| 9 | +import java.io.InputStream; |
9 | 10 | import java.math.BigDecimal; |
10 | 11 | import java.sql.SQLException; |
11 | 12 | import java.time.Instant; |
12 | 13 | import java.time.ZoneId; |
13 | 14 | import java.util.Arrays; |
14 | 15 | import java.util.List; |
| 16 | +import java.util.zip.CRC32; |
15 | 17 |
|
16 | 18 | import static org.testng.Assert.assertEquals; |
17 | 19 | import static org.testng.Assert.assertNull; |
@@ -109,4 +111,35 @@ public void testAllDataTypesMapped() { |
109 | 111 | Assert.assertNotNull(JdbcUtils.convertToJavaClass(dt), "Data type " + dt + " has no mapping to java class"); |
110 | 112 | } |
111 | 113 | } |
| 114 | + |
| 115 | + @Test(groups = {"unit"}) |
| 116 | + public void testConvertToUnhexExpression() throws Exception { |
| 117 | + // Load binary file from test resources |
| 118 | + byte[] originalBytes; |
| 119 | + try (InputStream is = getClass().getResourceAsStream("/ch_logo.png")) { |
| 120 | + Assert.assertNotNull(is, "ch_logo.png not found in test resources"); |
| 121 | + originalBytes = is.readAllBytes(); |
| 122 | + } |
| 123 | + |
| 124 | + // Calculate checksum of original bytes |
| 125 | + CRC32 originalChecksum = new CRC32(); |
| 126 | + originalChecksum.update(originalBytes); |
| 127 | + long expectedChecksum = originalChecksum.getValue(); |
| 128 | + |
| 129 | + // Convert to unhex expression |
| 130 | + String unhexExpr = JdbcUtils.convertToUnhexExpression(originalBytes); |
| 131 | + Assert.assertTrue(unhexExpr.startsWith("unhex('"), "Expression should start with unhex('"); |
| 132 | + Assert.assertTrue(unhexExpr.endsWith("')"), "Expression should end with ')"); |
| 133 | + |
| 134 | + // Extract hex string and decode back to bytes |
| 135 | + String hexString = unhexExpr.substring("unhex('".length(), unhexExpr.length() - "')".length()); |
| 136 | + assertEquals(hexString.length(), originalBytes.length * 2, "Hex string length should be twice the byte array length"); |
| 137 | + |
| 138 | + byte[] decodedBytes = JdbcUtils.decodeHexString(hexString); |
| 139 | + |
| 140 | + // Verify checksum of decoded bytes matches original |
| 141 | + CRC32 decodedChecksum = new CRC32(); |
| 142 | + decodedChecksum.update(decodedBytes); |
| 143 | + assertEquals(decodedChecksum.getValue(), expectedChecksum, "Checksum of decoded bytes should match original"); |
| 144 | + } |
112 | 145 | } |
0 commit comments