|
| 1 | +package com.github.magnusp.libsql.jdbc; |
| 2 | + |
| 3 | +import com.github.magnusp.libsql.hrana.codec.Col; |
| 4 | +import com.github.magnusp.libsql.hrana.codec.Row; |
| 5 | +import com.github.magnusp.libsql.hrana.codec.StmtResult; |
| 6 | +import com.github.magnusp.libsql.hrana.codec.Value; |
| 7 | +import org.junit.jupiter.api.Test; |
| 8 | +import org.junit.jupiter.params.ParameterizedTest; |
| 9 | +import org.junit.jupiter.params.provider.Arguments; |
| 10 | +import org.junit.jupiter.params.provider.MethodSource; |
| 11 | + |
| 12 | +import java.nio.charset.StandardCharsets; |
| 13 | +import java.sql.SQLException; |
| 14 | +import java.sql.SQLFeatureNotSupportedException; |
| 15 | +import java.util.List; |
| 16 | +import java.util.UUID; |
| 17 | +import java.util.stream.Stream; |
| 18 | + |
| 19 | +import static org.assertj.core.api.Assertions.assertThat; |
| 20 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 21 | + |
| 22 | +class LibsqlResultSetGetObjectTest { |
| 23 | + |
| 24 | + static Stream<Arguments> numericTargets() { |
| 25 | + return Stream.of( |
| 26 | + Arguments.of(Integer.class, "Integer", 7, 7), |
| 27 | + Arguments.of(Long.class, "Long", 7L, 7L), |
| 28 | + Arguments.of(Float.class, "Float", 7.0F, 7.5F), |
| 29 | + Arguments.of(Double.class, "Double", 7.0, 7.5)); |
| 30 | + } |
| 31 | + |
| 32 | + static Stream<Arguments> nonIntegerOrFloatStorage() { |
| 33 | + return Stream.of( |
| 34 | + Arguments.of("numeric text", new Value.Text("7")), |
| 35 | + Arguments.of("non-numeric text", new Value.Text("abc")), |
| 36 | + Arguments.of("numeric blob", new Value.Blob("123".getBytes(StandardCharsets.UTF_8))), |
| 37 | + Arguments.of("non-numeric blob", new Value.Blob("abc".getBytes(StandardCharsets.UTF_8))), |
| 38 | + Arguments.of("null", new Value.Null())); |
| 39 | + } |
| 40 | + |
| 41 | + @ParameterizedTest(name = "{1} from INTEGER storage") |
| 42 | + @MethodSource("numericTargets") |
| 43 | + void numericTargetsReadIntegerStorage( |
| 44 | + Class<?> target, String typeName, Object fromInteger, Object fromFloat) throws SQLException { |
| 45 | + LibsqlResultSet rs = resultSet(new Value.Integer(7)); |
| 46 | + assertThat(rs.next()).isTrue(); |
| 47 | + |
| 48 | + assertThat(rs.getObject(1, target)).isEqualTo(fromInteger); |
| 49 | + } |
| 50 | + |
| 51 | + @ParameterizedTest(name = "{1} from FLOAT storage") |
| 52 | + @MethodSource("numericTargets") |
| 53 | + void numericTargetsReadFloatStorage( |
| 54 | + Class<?> target, String typeName, Object fromInteger, Object fromFloat) throws SQLException { |
| 55 | + LibsqlResultSet rs = resultSet(new Value.Float(7.5)); |
| 56 | + assertThat(rs.next()).isTrue(); |
| 57 | + |
| 58 | + assertThat(rs.getObject(1, target)).isEqualTo(fromFloat); |
| 59 | + } |
| 60 | + |
| 61 | + @ParameterizedTest(name = "{1} rejects {0}") |
| 62 | + @MethodSource("numericTargets") |
| 63 | + void numericTargetsRejectTextBlobAndNullStorage(Class<?> target, String typeName) |
| 64 | + throws SQLException { |
| 65 | + for (Arguments arguments : nonIntegerOrFloatStorage().toList()) { |
| 66 | + Object[] parts = arguments.get(); |
| 67 | + Value value = (Value) parts[1]; |
| 68 | + |
| 69 | + LibsqlResultSet rs = resultSet(value); |
| 70 | + assertThat(rs.next()).isTrue(); |
| 71 | + |
| 72 | + assertThatThrownBy(() -> rs.getObject(1, target)) |
| 73 | + .as("%s from %s storage", typeName, parts[0]) |
| 74 | + .isExactlyInstanceOf(SQLException.class) |
| 75 | + .hasMessage("Bad value for type " + typeName); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + static Stream<Arguments> shortAndByteTargets() { |
| 80 | + return Stream.of(Arguments.of(Short.class, "Short"), Arguments.of(Byte.class, "Byte")); |
| 81 | + } |
| 82 | + |
| 83 | + @ParameterizedTest(name = "{1} is unsupported") |
| 84 | + @MethodSource("shortAndByteTargets") |
| 85 | + void shortAndByteAreUnsupportedForEveryStorageClass(Class<?> target, String typeName) |
| 86 | + throws SQLException { |
| 87 | + for (Arguments arguments : nonIntegerOrFloatStorage().toList()) { |
| 88 | + Object[] parts = arguments.get(); |
| 89 | + Value value = (Value) parts[1]; |
| 90 | + |
| 91 | + LibsqlResultSet rs = resultSet(value); |
| 92 | + assertThat(rs.next()).isTrue(); |
| 93 | + |
| 94 | + assertThatThrownBy(() -> rs.getObject(1, target)) |
| 95 | + .as("%s from %s storage", typeName, parts[0]) |
| 96 | + .isExactlyInstanceOf(SQLFeatureNotSupportedException.class) |
| 97 | + .hasMessage("not implemented by SQLite JDBC driver"); |
| 98 | + } |
| 99 | + |
| 100 | + for (Value value : List.of(new Value.Integer(7), new Value.Float(7.5))) { |
| 101 | + LibsqlResultSet rs = resultSet(value); |
| 102 | + assertThat(rs.next()).isTrue(); |
| 103 | + |
| 104 | + assertThatThrownBy(() -> rs.getObject(1, target)) |
| 105 | + .as("%s from %s storage", typeName, value.getClass().getSimpleName()) |
| 106 | + .isExactlyInstanceOf(SQLFeatureNotSupportedException.class) |
| 107 | + .hasMessage("not implemented by SQLite JDBC driver"); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + static Stream<Arguments> unsupportedTargets() { |
| 112 | + return Stream.of( |
| 113 | + Arguments.of(Object.class), |
| 114 | + Arguments.of(Number.class), |
| 115 | + Arguments.of(UUID.class)); |
| 116 | + } |
| 117 | + |
| 118 | + @ParameterizedTest(name = "{0} is unsupported") |
| 119 | + @MethodSource("unsupportedTargets") |
| 120 | + void unsupportedTargetTypeUsesXerialMessage(Class<?> target) throws SQLException { |
| 121 | + LibsqlResultSet rs = resultSet(new Value.Integer(7)); |
| 122 | + assertThat(rs.next()).isTrue(); |
| 123 | + |
| 124 | + assertThatThrownBy(() -> rs.getObject(1, target)) |
| 125 | + .isExactlyInstanceOf(SQLFeatureNotSupportedException.class) |
| 126 | + .hasMessage("not implemented by SQLite JDBC driver"); |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + void nullTargetTypeUsesXerialMessage() throws SQLException { |
| 131 | + LibsqlResultSet rs = resultSet(new Value.Integer(7)); |
| 132 | + assertThat(rs.next()).isTrue(); |
| 133 | + |
| 134 | + assertThatThrownBy(() -> rs.getObject(1, (Class<Object>) null)) |
| 135 | + .isExactlyInstanceOf(SQLException.class) |
| 136 | + .hasMessage("requested type cannot be null"); |
| 137 | + } |
| 138 | + |
| 139 | + @Test |
| 140 | + void nonNumericTargetsKeepTheirUnrestrictedConversions() throws SQLException { |
| 141 | + LibsqlResultSet integers = resultSet(new Value.Integer(7)); |
| 142 | + assertThat(integers.next()).isTrue(); |
| 143 | + assertThat(integers.getObject(1, String.class)).isEqualTo("7"); |
| 144 | + assertThat(integers.getObject(1, byte[].class)) |
| 145 | + .isEqualTo("7".getBytes(StandardCharsets.UTF_8)); |
| 146 | + |
| 147 | + LibsqlResultSet text = resultSet(new Value.Text("abc")); |
| 148 | + assertThat(text.next()).isTrue(); |
| 149 | + assertThat(text.getObject(1, String.class)).isEqualTo("abc"); |
| 150 | + assertThat(text.getObject(1, Boolean.class)).isFalse(); |
| 151 | + |
| 152 | + LibsqlResultSet nullValue = resultSet(new Value.Null()); |
| 153 | + assertThat(nullValue.next()).isTrue(); |
| 154 | + assertThat(nullValue.getObject(1, String.class)).isNull(); |
| 155 | + assertThat(nullValue.getObject(1, Boolean.class)).isFalse(); |
| 156 | + assertThat(nullValue.getObject(1, byte[].class)).isNull(); |
| 157 | + } |
| 158 | + |
| 159 | + private static LibsqlResultSet resultSet(Value value) { |
| 160 | + List<Col> cols = List.of(new Col("c", null)); |
| 161 | + StmtResult result = new StmtResult(cols, List.of(new Row(List.of(value))), 1, null); |
| 162 | + return new LibsqlResultSet(null, result); |
| 163 | + } |
| 164 | +} |
0 commit comments