Skip to content

Commit

Permalink
Removed usage of Junit4 asserts
Browse files Browse the repository at this point in the history
  • Loading branch information
alex268 committed Sep 3, 2024
1 parent 913e773 commit d4c9c69
Showing 1 changed file with 91 additions and 94 deletions.
185 changes: 91 additions & 94 deletions jdbc/src/test/java/tech/ydb/jdbc/impl/YdbPreparedStatementTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;

import org.junit.Assert;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
Expand All @@ -37,8 +36,6 @@
import tech.ydb.table.values.PrimitiveType;
import tech.ydb.table.values.PrimitiveValue;
import tech.ydb.table.values.StructValue;
import static tech.ydb.table.values.Type.Kind.OPTIONAL;
import static tech.ydb.table.values.Type.Kind.PRIMITIVE;
import tech.ydb.table.values.Value;
import tech.ydb.test.junit5.YdbHelperExtension;

Expand Down Expand Up @@ -261,44 +258,44 @@ private void fillRowValues(PreparedStatement statement, int id) throws SQLExcept
}

private void assertRowValues(ResultSet rs, int id) throws SQLException {
Assert.assertTrue(rs.next());
Assertions.assertTrue(rs.next());

Assert.assertEquals(id, rs.getInt("key"));
Assertions.assertEquals(id, rs.getInt("key"));

Assert.assertEquals(id % 2 == 0, rs.getBoolean("c_Bool"));
Assertions.assertEquals(id % 2 == 0, rs.getBoolean("c_Bool"));

Assert.assertEquals(id + 1, rs.getByte("c_Int8"));
Assert.assertEquals(id + 2, rs.getShort("c_Int16"));
Assert.assertEquals(id + 3, rs.getInt("c_Int32"));
Assert.assertEquals(id + 4, rs.getLong("c_Int64"));
Assertions.assertEquals(id + 1, rs.getByte("c_Int8"));
Assertions.assertEquals(id + 2, rs.getShort("c_Int16"));
Assertions.assertEquals(id + 3, rs.getInt("c_Int32"));
Assertions.assertEquals(id + 4, rs.getLong("c_Int64"));

Assert.assertEquals(id + 5, rs.getByte("c_Uint8"));
Assert.assertEquals(id + 6, rs.getShort("c_Uint16"));
Assert.assertEquals(id + 7, rs.getInt("c_Uint32"));
Assert.assertEquals(id + 8, rs.getLong("c_Uint64"));
Assertions.assertEquals(id + 5, rs.getByte("c_Uint8"));
Assertions.assertEquals(id + 6, rs.getShort("c_Uint16"));
Assertions.assertEquals(id + 7, rs.getInt("c_Uint32"));
Assertions.assertEquals(id + 8, rs.getLong("c_Uint64"));

Assert.assertEquals(1.5f * id, rs.getFloat("c_Float"), 0.001f);
Assert.assertEquals(2.5d * id, rs.getDouble("c_Double"), 0.001d);
Assertions.assertEquals(1.5f * id, rs.getFloat("c_Float"), 0.001f);
Assertions.assertEquals(2.5d * id, rs.getDouble("c_Double"), 0.001d);

Assert.assertArrayEquals(new byte[] { (byte)id }, rs.getBytes("c_Bytes"));
Assert.assertEquals("Text_" + id, rs.getString("c_Text"));
Assert.assertEquals("{\"json\": " + id + "}", rs.getString("c_Json"));
Assert.assertEquals("{\"jsonDoc\":" + id + "}", rs.getString("c_JsonDocument"));
Assert.assertEquals("{yson=" + id + "}", rs.getString("c_Yson"));
Assertions.assertArrayEquals(new byte[] { (byte)id }, rs.getBytes("c_Bytes"));
Assertions.assertEquals("Text_" + id, rs.getString("c_Text"));
Assertions.assertEquals("{\"json\": " + id + "}", rs.getString("c_Json"));
Assertions.assertEquals("{\"jsonDoc\":" + id + "}", rs.getString("c_JsonDocument"));
Assertions.assertEquals("{yson=" + id + "}", rs.getString("c_Yson"));


Date sqlDate = new Date(TEST_TS.toEpochMilli());
LocalDateTime dateTime = LocalDateTime.ofInstant(TEST_TS, ZoneOffset.UTC).plusMinutes(id)
.truncatedTo(ChronoUnit.SECONDS);
Timestamp timestamp = Timestamp.from(truncToMicros(TEST_TS.plusSeconds(id)));

Assert.assertEquals(sqlDate.toLocalDate(), rs.getDate("c_Date").toLocalDate());
Assert.assertEquals(dateTime, rs.getObject("c_Datetime"));
Assert.assertEquals(timestamp, rs.getTimestamp("c_Timestamp"));
Assert.assertEquals(Duration.ofMinutes(id), rs.getObject("c_Interval"));
Assertions.assertEquals(sqlDate.toLocalDate(), rs.getDate("c_Date").toLocalDate());
Assertions.assertEquals(dateTime, rs.getObject("c_Datetime"));
Assertions.assertEquals(timestamp, rs.getTimestamp("c_Timestamp"));
Assertions.assertEquals(Duration.ofMinutes(id), rs.getObject("c_Interval"));

Assert.assertNull(rs.getString("c_Decimal"));
Assert.assertTrue(rs.wasNull());
Assertions.assertNull(rs.getString("c_Decimal"));
Assertions.assertTrue(rs.wasNull());
}

private void assertStructMember(PrimitiveValue value, StructValue sv, String name) {
Expand All @@ -323,7 +320,7 @@ private void assertStructMember(PrimitiveValue value, StructValue sv, String nam
}

private void assertTableRow(ResultSet rs, int id) throws SQLException {
Assert.assertTrue(rs.next());
Assertions.assertTrue(rs.next());

Object obj = rs.getObject(1);
Assertions.assertNotNull(obj);
Expand Down Expand Up @@ -407,7 +404,7 @@ public void batchUpsertAllTest(SqlQueries.JdbcQuery query) throws SQLException {
assertRowValues(rs, 3);
assertRowValues(rs, 6);

Assert.assertFalse(rs.next());
Assertions.assertFalse(rs.next());
}
}
};
Expand All @@ -429,7 +426,7 @@ public void tableRowTest() throws SQLException {
try (ResultSet rs = statement.executeQuery(selectTableRow)) {
assertTableRow(rs, 1);
assertTableRow(rs, 2);
Assert.assertFalse(rs.next());
Assertions.assertFalse(rs.next());
}
}
};
Expand Down Expand Up @@ -510,45 +507,45 @@ public void int32Test(SqlQueries.JdbcQuery query) throws SQLException {
assertNextInt32(rs, 8, (int) TEST_TS.atZone(ZoneId.systemDefault()).toLocalDate().toEpochDay());
assertNextInt32(rs, 9, (int) TEST_TS.toEpochMilli());

Assert.assertFalse(rs.next());
Assertions.assertFalse(rs.next());
}
}
};

private void assertNextInt32(ResultSet rs, int key, Integer value) throws SQLException {
Assert.assertTrue(rs.next());
Assert.assertEquals(key, rs.getInt("key"));
Assertions.assertTrue(rs.next());
Assertions.assertEquals(key, rs.getInt("key"));

Object obj = rs.getObject("c_Int32");
Assert.assertTrue(obj instanceof Integer);
Assert.assertEquals(value, obj);
Assertions.assertTrue(obj instanceof Integer);
Assertions.assertEquals(value, obj);

if (value.byteValue() == value.intValue()) {
Assert.assertEquals(value.intValue(), rs.getByte("c_Int32"));
Assertions.assertEquals(value.intValue(), rs.getByte("c_Int32"));
} else {
String msg = String.format("Cannot cast [Int32] with value [%s] to [byte]", value);
ExceptionAssert.sqlException(msg, () -> rs.getByte("c_Int32"));
}

if (value.shortValue() == value.intValue()) {
Assert.assertEquals(value.intValue(), rs.getShort("c_Int32"));
Assertions.assertEquals(value.intValue(), rs.getShort("c_Int32"));
} else {
String msg = String.format("Cannot cast [Int32] with value [%s] to [short]", value);
ExceptionAssert.sqlException(msg, () -> rs.getShort("c_Int32"));
}

Assert.assertEquals(value.intValue(), rs.getInt("c_Int32"));
Assert.assertEquals(value.longValue(), rs.getLong("c_Int32"));
Assertions.assertEquals(value.intValue(), rs.getInt("c_Int32"));
Assertions.assertEquals(value.longValue(), rs.getLong("c_Int32"));

if (value >= 0 && value < 24 * 3600) {
Assert.assertEquals(Time.valueOf(LocalTime.ofSecondOfDay(value)), rs.getTime("c_Int32"));
Assertions.assertEquals(Time.valueOf(LocalTime.ofSecondOfDay(value)), rs.getTime("c_Int32"));
} else {
String msg = String.format("Cannot cast [Int32] with value [%s] to [class java.sql.Time]", value);
ExceptionAssert.sqlException(msg, () -> rs.getTime("c_Int32"));
}

Assert.assertEquals(Date.valueOf(LocalDate.ofEpochDay(value)), rs.getDate("c_Int32"));
Assert.assertEquals(new Timestamp(value), rs.getTimestamp("c_Int32"));
Assertions.assertEquals(Date.valueOf(LocalDate.ofEpochDay(value)), rs.getDate("c_Int32"));
Assertions.assertEquals(new Timestamp(value), rs.getTimestamp("c_Int32"));
}

@ParameterizedTest(name = "with {0}")
Expand Down Expand Up @@ -632,57 +629,57 @@ public void int64Test(SqlQueries.JdbcQuery query) throws SQLException {
assertNextInt64(rs, 9, TEST_TS.atZone(ZoneId.systemDefault()).toLocalDate().toEpochDay());
assertNextInt64(rs, 10, TEST_TS.toEpochMilli());

Assert.assertFalse(rs.next());
Assertions.assertFalse(rs.next());
}
}
};

private void assertNextInt64(ResultSet rs, int key, Long value) throws SQLException {
Assert.assertTrue(rs.next());
Assert.assertEquals(key, rs.getInt("key"));
Assertions.assertTrue(rs.next());
Assertions.assertEquals(key, rs.getInt("key"));

Object obj = rs.getObject("c_Int64");
Assert.assertTrue(obj instanceof Long);
Assert.assertEquals(value, obj);
Assertions.assertTrue(obj instanceof Long);
Assertions.assertEquals(value, obj);

if (value.byteValue() == value.intValue()) {
Assert.assertEquals(value.intValue(), rs.getByte("c_Int64"));
Assertions.assertEquals(value.intValue(), rs.getByte("c_Int64"));
} else {
String msg = String.format("Cannot cast [Int64] with value [%s] to [byte]", value);
ExceptionAssert.sqlException(msg, () -> rs.getByte("c_Int64"));
}

if (value.shortValue() == value.intValue()) {
Assert.assertEquals(value.intValue(), rs.getShort("c_Int64"));
Assertions.assertEquals(value.intValue(), rs.getShort("c_Int64"));
} else {
String msg = String.format("Cannot cast [Int64] with value [%s] to [short]", value);
ExceptionAssert.sqlException(msg, () -> rs.getShort("c_Int64"));
}

if (value.intValue() == value.longValue()) {
Assert.assertEquals(value.intValue(), rs.getInt("c_Int64"));
Assertions.assertEquals(value.intValue(), rs.getInt("c_Int64"));
} else {
String msg = String.format("Cannot cast [Int64] with value [%s] to [int]", value);
ExceptionAssert.sqlException(msg, () -> rs.getInt("c_Int64"));
}

Assert.assertEquals(value.longValue(), rs.getLong("c_Int64"));
Assertions.assertEquals(value.longValue(), rs.getLong("c_Int64"));

if (value >= 0 && value < 24 * 3600) {
Assert.assertEquals(Time.valueOf(LocalTime.ofSecondOfDay(value)), rs.getTime("c_Int64"));
Assertions.assertEquals(Time.valueOf(LocalTime.ofSecondOfDay(value)), rs.getTime("c_Int64"));
} else {
String msg = String.format("Cannot cast [Int64] with value [%s] to [class java.sql.Time]", value);
ExceptionAssert.sqlException(msg, () -> rs.getTime("c_Int64"));
}

if (ChronoField.EPOCH_DAY.range().isValidValue(value)) {
Assert.assertEquals(Date.valueOf(LocalDate.ofEpochDay(value)), rs.getDate("c_Int64"));
Assertions.assertEquals(Date.valueOf(LocalDate.ofEpochDay(value)), rs.getDate("c_Int64"));
} else {
String msg = String.format("Cannot cast [Int64] with value [%s] to [class java.sql.Date]", value);
ExceptionAssert.sqlException(msg, () -> rs.getDate("c_Int64"));
}

Assert.assertEquals(new Timestamp(value), rs.getTimestamp("c_Int64"));
Assertions.assertEquals(new Timestamp(value), rs.getTimestamp("c_Int64"));
}

@ParameterizedTest(name = "with {0}")
Expand Down Expand Up @@ -742,30 +739,30 @@ public void timestampTest(SqlQueries.JdbcQuery query) throws SQLException {
assertNextTimestamp(rs, 6, Instant.ofEpochMilli(1585932011123l));
assertNextTimestamp(rs, 7, Instant.parse("2011-12-03T10:15:30.456789000Z"));

Assert.assertFalse(rs.next());
Assertions.assertFalse(rs.next());
}
}
};

private void assertNextTimestamp(ResultSet rs, int key, Instant ts) throws SQLException {
Assert.assertTrue(rs.next());
Assert.assertEquals(key, rs.getInt("key"));
Assertions.assertTrue(rs.next());
Assertions.assertEquals(key, rs.getInt("key"));

Object obj = rs.getObject("c_Timestamp");
Assert.assertTrue(obj instanceof Instant);
Assert.assertEquals(ts, obj);
Assertions.assertTrue(obj instanceof Instant);
Assertions.assertEquals(ts, obj);

Assert.assertEquals(ts.toEpochMilli(), rs.getLong("c_Timestamp"));
Assertions.assertEquals(ts.toEpochMilli(), rs.getLong("c_Timestamp"));

Assert.assertEquals(new Date(ts.toEpochMilli()), rs.getDate("c_Timestamp"));
Assert.assertEquals(Timestamp.from(ts), rs.getTimestamp("c_Timestamp"));
Assert.assertEquals(ts.toString(), rs.getString("c_Timestamp"));
Assertions.assertEquals(new Date(ts.toEpochMilli()), rs.getDate("c_Timestamp"));
Assertions.assertEquals(Timestamp.from(ts), rs.getTimestamp("c_Timestamp"));
Assertions.assertEquals(ts.toString(), rs.getString("c_Timestamp"));

LocalDateTime ldt = LocalDateTime.ofInstant(ts, ZoneId.systemDefault());
Assert.assertEquals(Long.valueOf(ts.toEpochMilli()), rs.getObject("c_Timestamp", Long.class));
Assert.assertEquals(ldt.toLocalDate(), rs.getObject("c_Timestamp", LocalDate.class));
Assert.assertEquals(ldt, rs.getObject("c_Timestamp", LocalDateTime.class));
Assert.assertEquals(ts, rs.getObject("c_Timestamp", Instant.class));
Assertions.assertEquals(Long.valueOf(ts.toEpochMilli()), rs.getObject("c_Timestamp", Long.class));
Assertions.assertEquals(ldt.toLocalDate(), rs.getObject("c_Timestamp", LocalDate.class));
Assertions.assertEquals(ldt, rs.getObject("c_Timestamp", LocalDateTime.class));
Assertions.assertEquals(ts, rs.getObject("c_Timestamp", Instant.class));
}

@ParameterizedTest(name = "with {0}")
Expand Down Expand Up @@ -837,29 +834,29 @@ public void datetimeTest(SqlQueries.JdbcQuery query) throws SQLException {
assertNextDatetime(rs, 5, LocalDateTime.of(2021, Month.JULY, 21, 0, 0, 0));
assertNextDatetime(rs, 6, LocalDateTime.of(2011, Month.DECEMBER, 3, 10, 15, 30));
assertNextDatetime(rs, 7, ts);
Assert.assertFalse(rs.next());
Assertions.assertFalse(rs.next());
}
}
};

private void assertNextDatetime(ResultSet rs, int key, LocalDateTime ldt) throws SQLException {
Assert.assertTrue(rs.next());
Assert.assertEquals(key, rs.getInt("key"));
Assertions.assertTrue(rs.next());
Assertions.assertEquals(key, rs.getInt("key"));

Object obj = rs.getObject("c_Datetime");
Assert.assertTrue(obj instanceof LocalDateTime);
Assert.assertEquals(ldt, obj);
Assertions.assertTrue(obj instanceof LocalDateTime);
Assertions.assertEquals(ldt, obj);

Assert.assertEquals(ldt.toEpochSecond(ZoneOffset.UTC), rs.getLong("c_Datetime"));
Assertions.assertEquals(ldt.toEpochSecond(ZoneOffset.UTC), rs.getLong("c_Datetime"));

Assert.assertEquals(Date.valueOf(ldt.toLocalDate()), rs.getDate("c_Datetime"));
Assert.assertEquals(Timestamp.valueOf(ldt), rs.getTimestamp("c_Datetime"));
Assert.assertEquals(ldt.toString(), rs.getString("c_Datetime"));
Assertions.assertEquals(Date.valueOf(ldt.toLocalDate()), rs.getDate("c_Datetime"));
Assertions.assertEquals(Timestamp.valueOf(ldt), rs.getTimestamp("c_Datetime"));
Assertions.assertEquals(ldt.toString(), rs.getString("c_Datetime"));

Assert.assertEquals(Long.valueOf(ldt.toEpochSecond(ZoneOffset.UTC)), rs.getObject("c_Datetime", Long.class));
Assert.assertEquals(ldt.toLocalDate(), rs.getObject("c_Datetime", LocalDate.class));
Assert.assertEquals(ldt, rs.getObject("c_Datetime", LocalDateTime.class));
Assert.assertEquals(ldt.atZone(ZoneId.systemDefault()).toInstant(), rs.getObject("c_Datetime", Instant.class));
Assertions.assertEquals(Long.valueOf(ldt.toEpochSecond(ZoneOffset.UTC)), rs.getObject("c_Datetime", Long.class));
Assertions.assertEquals(ldt.toLocalDate(), rs.getObject("c_Datetime", LocalDate.class));
Assertions.assertEquals(ldt, rs.getObject("c_Datetime", LocalDateTime.class));
Assertions.assertEquals(ldt.atZone(ZoneId.systemDefault()).toInstant(), rs.getObject("c_Datetime", Instant.class));
}

@ParameterizedTest(name = "with {0}")
Expand Down Expand Up @@ -940,29 +937,29 @@ public void dateTest(SqlQueries.JdbcQuery query) throws SQLException {
assertNextDate(rs, 7, LocalDate.of(2011, Month.DECEMBER, 3));
assertNextDate(rs, 8, LocalDate.of(2020, Month.MARCH, 3));

Assert.assertFalse(rs.next());
Assertions.assertFalse(rs.next());
}
}
};

private void assertNextDate(ResultSet rs, int key, LocalDate ld) throws SQLException {
Assert.assertTrue(rs.next());
Assert.assertEquals(key, rs.getInt("key"));
Assertions.assertTrue(rs.next());
Assertions.assertEquals(key, rs.getInt("key"));

Object obj = rs.getObject("c_Date");
Assert.assertTrue(obj instanceof LocalDate);
Assert.assertEquals(ld, obj);
Assertions.assertTrue(obj instanceof LocalDate);
Assertions.assertEquals(ld, obj);

Assert.assertEquals(ld.toEpochDay(), rs.getInt("c_Date"));
Assert.assertEquals(ld.toEpochDay(), rs.getLong("c_Date"));
Assertions.assertEquals(ld.toEpochDay(), rs.getInt("c_Date"));
Assertions.assertEquals(ld.toEpochDay(), rs.getLong("c_Date"));

Assert.assertEquals(Date.valueOf(ld), rs.getDate("c_Date"));
Assert.assertEquals(Timestamp.valueOf(ld.atStartOfDay()), rs.getTimestamp("c_Date"));
Assert.assertEquals(ld.toString(), rs.getString("c_Date"));
Assertions.assertEquals(Date.valueOf(ld), rs.getDate("c_Date"));
Assertions.assertEquals(Timestamp.valueOf(ld.atStartOfDay()), rs.getTimestamp("c_Date"));
Assertions.assertEquals(ld.toString(), rs.getString("c_Date"));

Assert.assertEquals(Long.valueOf(ld.toEpochDay()), rs.getObject("c_Date", Long.class));
Assert.assertEquals(ld, rs.getObject("c_Date", LocalDate.class));
Assert.assertEquals(ld.atStartOfDay(), rs.getObject("c_Date", LocalDateTime.class));
Assert.assertEquals(ld.atStartOfDay(ZoneId.systemDefault()).toInstant(), rs.getObject("c_Date", Instant.class));
Assertions.assertEquals(Long.valueOf(ld.toEpochDay()), rs.getObject("c_Date", Long.class));
Assertions.assertEquals(ld, rs.getObject("c_Date", LocalDate.class));
Assertions.assertEquals(ld.atStartOfDay(), rs.getObject("c_Date", LocalDateTime.class));
Assertions.assertEquals(ld.atStartOfDay(ZoneId.systemDefault()).toInstant(), rs.getObject("c_Date", Instant.class));
}
}

0 comments on commit d4c9c69

Please sign in to comment.