|
| 1 | +package com.databricks.jdbc.api.impl.converters; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.*; |
| 4 | + |
| 5 | +import com.databricks.jdbc.exception.DatabricksSQLException; |
| 6 | +import org.junit.jupiter.api.Test; |
| 7 | +import org.junit.jupiter.params.ParameterizedTest; |
| 8 | +import org.junit.jupiter.params.provider.CsvSource; |
| 9 | +import org.junit.jupiter.params.provider.ValueSource; |
| 10 | + |
| 11 | +public class BitConverterTest { |
| 12 | + |
| 13 | + private final BitConverter bitConverter = new BitConverter(); |
| 14 | + |
| 15 | + @ParameterizedTest |
| 16 | + @ValueSource(booleans = {true, false}) |
| 17 | + public void testToStringWithBooleanPrimitives(boolean input) throws DatabricksSQLException { |
| 18 | + String expected = input ? "true" : "false"; |
| 19 | + assertEquals(expected, bitConverter.toString(input)); |
| 20 | + } |
| 21 | + |
| 22 | + @ParameterizedTest |
| 23 | + @CsvSource({"true, true", "false, false"}) |
| 24 | + public void testToStringWithBooleanObjects(String input, String expected) |
| 25 | + throws DatabricksSQLException { |
| 26 | + Boolean booleanInput = Boolean.parseBoolean(input); |
| 27 | + assertEquals(expected, bitConverter.toString(booleanInput)); |
| 28 | + } |
| 29 | + |
| 30 | + @ParameterizedTest |
| 31 | + @CsvSource({ |
| 32 | + "null, null", |
| 33 | + "Object, Object", |
| 34 | + "int[], class [I", |
| 35 | + "String[], class [Ljava.lang.String;" |
| 36 | + }) |
| 37 | + public void testToStringWithUnsupportedTypes(String typeDescription, String expectedInMessage) { |
| 38 | + Object input = getTestObject(typeDescription); |
| 39 | + |
| 40 | + DatabricksSQLException exception = |
| 41 | + assertThrows(DatabricksSQLException.class, () -> bitConverter.toString(input)); |
| 42 | + |
| 43 | + assertTrue(exception.getMessage().contains("Unsupported type for conversion to String")); |
| 44 | + assertTrue(exception.getMessage().contains(expectedInMessage)); |
| 45 | + assertEquals("UNSUPPORTED_OPERATION", exception.getSQLState()); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void testToBooleanWithBooleanTrue() throws DatabricksSQLException { |
| 50 | + assertTrue(bitConverter.toBoolean(true)); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void testToBooleanWithBooleanFalse() throws DatabricksSQLException { |
| 55 | + assertFalse(bitConverter.toBoolean(false)); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void testToBooleanWithBooleanObject() throws DatabricksSQLException { |
| 60 | + Boolean trueObject = Boolean.TRUE; |
| 61 | + Boolean falseObject = Boolean.FALSE; |
| 62 | + |
| 63 | + assertTrue(bitConverter.toBoolean(trueObject)); |
| 64 | + assertFalse(bitConverter.toBoolean(falseObject)); |
| 65 | + } |
| 66 | + |
| 67 | + @ParameterizedTest |
| 68 | + @CsvSource({"0, false", "1, true", "-1, true", "42, true", "0.0, false", "3.14, true"}) |
| 69 | + public void testToBooleanWithNumbers(String numberStr, boolean expected) |
| 70 | + throws DatabricksSQLException { |
| 71 | + Number number = |
| 72 | + numberStr.contains(".") ? Double.parseDouble(numberStr) : Integer.parseInt(numberStr); |
| 73 | + assertEquals(expected, bitConverter.toBoolean(number)); |
| 74 | + } |
| 75 | + |
| 76 | + @ParameterizedTest |
| 77 | + @CsvSource({ |
| 78 | + "true, true", |
| 79 | + "TRUE, true", |
| 80 | + "True, true", |
| 81 | + "false, false", |
| 82 | + "FALSE, false", |
| 83 | + "False, false", |
| 84 | + "anything else, false", |
| 85 | + "'', false", |
| 86 | + "1, false", |
| 87 | + "0, false" |
| 88 | + }) |
| 89 | + public void testToBooleanWithStrings(String input, boolean expected) |
| 90 | + throws DatabricksSQLException { |
| 91 | + assertEquals(expected, bitConverter.toBoolean(input)); |
| 92 | + } |
| 93 | + |
| 94 | + @ParameterizedTest |
| 95 | + @CsvSource({ |
| 96 | + "null, null", |
| 97 | + "Object, Object", |
| 98 | + "int[], class [I", |
| 99 | + "String[], class [Ljava.lang.String;" |
| 100 | + }) |
| 101 | + public void testToBooleanWithUnsupportedTypes(String typeDescription, String expectedInMessage) { |
| 102 | + Object input = getTestObject(typeDescription); |
| 103 | + |
| 104 | + DatabricksSQLException exception = |
| 105 | + assertThrows(DatabricksSQLException.class, () -> bitConverter.toBoolean(input)); |
| 106 | + |
| 107 | + assertTrue(exception.getMessage().contains("Unsupported type for conversion to BIT")); |
| 108 | + assertTrue(exception.getMessage().contains(expectedInMessage)); |
| 109 | + assertEquals("UNSUPPORTED_OPERATION", exception.getSQLState()); |
| 110 | + } |
| 111 | + |
| 112 | + private Object getTestObject(String typeDescription) { |
| 113 | + switch (typeDescription) { |
| 114 | + case "null": |
| 115 | + return null; |
| 116 | + case "Object": |
| 117 | + return new Object(); |
| 118 | + case "int[]": |
| 119 | + return new int[] {1, 2, 3}; |
| 120 | + case "String[]": |
| 121 | + return new String[] {"a", "b"}; |
| 122 | + default: |
| 123 | + throw new IllegalArgumentException("Unknown type: " + typeDescription); |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments