-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Closed
Description
When inserting a null into a JSON column, H2 stores a literal 'null' instead. This test therefore fails for the wrong reason. It should fail
assertThat(resultSet.getString(2), is("null"));
because this assert should be
assertThat(resultSet.getString(2), nullValue());
But instead it fails on
assertThat(resultSet.next(), is(true));
import org.h2.jdbcx.JdbcDataSource;
import org.junit.jupiter.api.Test;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
public class H2Bug {
void execute(Connection connection, String sql, Object... args) {
try (PreparedStatement statement = connection.prepareStatement(sql)) {
for (int i = 0; i < args.length; ++i) {
statement.setObject(i + 1, args[i]);
}
statement.execute();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Test
void canInsertNullAndSelectNull() throws SQLException {
JdbcDataSource ds = new JdbcDataSource();
ds.setURL("jdbc:h2:mem:test");
ds.setUser("sa");
ds.setPassword("sa");
try (Connection conn = ds.getConnection()) {
execute(conn, "create table json_test(id integer not null primary key, json_data json)");
execute(conn, "insert into json_test(id, json_data) values (?, ? format json)", 1, null);
try (PreparedStatement statement = conn.prepareStatement("select * from json_test where id = 1");
ResultSet resultSet = statement.executeQuery()) {
assertThat(resultSet.next(), is(true));
assertThat(resultSet.getString(2), is("null"));
// Should be assertThat(resultSet.getString(2), nullValue()); but this fails
}
try (PreparedStatement statement = conn.prepareStatement("select * from json_test where id = 1 and json_data is null");
ResultSet resultSet = statement.executeQuery()) {
assertThat(resultSet.next(), is(true));
}
}
}
}
Metadata
Metadata
Assignees
Labels
No labels