Skip to content
Permalink
Browse files

test: refactor SetObject310Test to use proper assertion messages and …

…use less statements (make it faster)
  • Loading branch information
vlsi committed Jan 8, 2018
1 parent e8c43f3 commit be06946f8b908536f4d659aaf6fc660bed339f67
@@ -25,6 +25,7 @@
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.chrono.IsoEra;
import java.time.format.DateTimeParseException;
import java.time.temporal.ChronoField;
@@ -761,22 +762,20 @@ public synchronized String toString(OffsetDateTime offsetDateTime) {
return sbuf.toString();
}

/**
* Formats {@link LocalDateTime} to be sent to the backend, thus it adds time zone.
* Do not use this method in {@link java.sql.ResultSet#getString(int)}
*/
public synchronized String toString(LocalDateTime localDateTime) {
if (LocalDateTime.MAX.equals(localDateTime)) {
return "infinity";
} else if (LocalDateTime.MIN.equals(localDateTime)) {
return "-infinity";
}

sbuf.setLength(0);

LocalDate localDate = localDateTime.toLocalDate();
appendDate(sbuf, localDate);
sbuf.append(' ');
appendTime(sbuf, localDateTime.toLocalTime());
appendEra(sbuf, localDate);

return sbuf.toString();
// LocalDateTime is always passed with time zone so backend can decide between timestamp and timestamptz
ZonedDateTime zonedDateTime = localDateTime.atZone(getDefaultTz().toZoneId());
return toString(zonedDateTime.toOffsetDateTime());
}

private static void appendDate(StringBuilder sb, LocalDate localDate) {
@@ -30,6 +30,7 @@
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.chrono.IsoEra;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoField;
import java.util.ArrayList;
import java.util.Arrays;
@@ -103,10 +104,6 @@ private void insertWithoutType(Object data, String columnName) throws SQLExcepti
insert(data, columnName, null);
}

private void insertWithType(OffsetDateTime data, String columnName) throws SQLException {
insert(data, columnName, Types.TIMESTAMP_WITH_TIMEZONE);
}

private <T> T insertThenReadWithoutType(Object data, String columnName, Class<T> expectedType) throws SQLException {
PreparedStatement ps = con.prepareStatement(TestUtil.insertSQL("table1", columnName, "?"));
try {
@@ -178,7 +175,9 @@ public void testSetLocalDateTime() throws SQLException {
ZoneId zone = ZoneId.of(zoneId);
for (String date : datesToTest) {
LocalDateTime localDateTime = LocalDateTime.parse(date);
String expected = date.replace('T', ' ');
String expected = localDateTime.atZone(zone)
.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
.replace('T', ' ');
localTimestamps(zone, localDateTime, expected);
}
}
@@ -241,37 +240,42 @@ public void testSetOffsetDateTime() throws SQLException {
private void localTimestamps(ZoneId zoneId, LocalDateTime localDateTime, String expected) throws SQLException {
TimeZone.setDefault(TimeZone.getTimeZone(zoneId));
String readBack = insertThenReadStringWithoutType(localDateTime, "timestamp_without_time_zone_column");
assertEquals(expected, readBack);
assertEquals(
"LocalDateTime=" + localDateTime + ", with TimeZone.default=" + zoneId + ", setObject(int, Object)",
expected, readBack);
deleteRows();

readBack = insertThenReadStringWithType(localDateTime, "timestamp_without_time_zone_column");
assertEquals(expected, readBack);
assertEquals(
"LocalDateTime=" + localDateTime + ", with TimeZone.default=" + zoneId + ", setObject(int, Object, TIMESTAMP)",
expected, readBack);
deleteRows();
}

private void offsetTimestamps(ZoneId dataZone, LocalDateTime localDateTime, String expected, List<TimeZone> storeZones) throws SQLException {
OffsetDateTime data = localDateTime.atZone(dataZone).toOffsetDateTime();
for (TimeZone storeZone : storeZones) {
TimeZone.setDefault(storeZone);
insertWithoutType(data, "timestamp_with_time_zone_column");

String readBack = readString("timestamp_with_time_zone_column");
OffsetDateTime o = OffsetDateTime.parse(readBack.replace(' ', 'T') + ":00");
assertEquals(data.toInstant(), o.toInstant());

deleteRows();
}

for (TimeZone storeZone : storeZones) {
TimeZone.setDefault(storeZone);
insertWithType(data, "timestamp_with_time_zone_column");

String readBack = readString("timestamp_with_time_zone_column");
OffsetDateTime o = OffsetDateTime.parse(readBack.replace(' ', 'T') + ":00");

assertEquals(data.toInstant(), o.toInstant());

deleteRows();
try (PreparedStatement ps = con.prepareStatement(
"select ?::timestamp with time zone, ?::timestamp with time zone")) {
for (TimeZone storeZone : storeZones) {
TimeZone.setDefault(storeZone);
ps.setObject(1, data);
ps.setObject(2, data, Types.TIMESTAMP_WITH_TIMEZONE);
try (ResultSet rs = ps.executeQuery()) {
rs.next();
String noType = rs.getString(1);
OffsetDateTime noTypeRes = OffsetDateTime.parse(noType.replace(' ', 'T') + ":00");
assertEquals(
"OffsetDateTime=" + data + " (with ZoneId=" + dataZone + "), with TimeZone.default="
+ storeZone + ", setObject(int, Object)", data.toInstant(),
noTypeRes.toInstant());
String withType = rs.getString(1);
OffsetDateTime withTypeRes = OffsetDateTime.parse(withType.replace(' ', 'T') + ":00");
assertEquals(
"OffsetDateTime=" + data + " (with ZoneId=" + dataZone + "), with TimeZone.default="
+ storeZone + ", setObject(int, Object, TIMESTAMP_WITH_TIMEZONE)",
data.toInstant(), withTypeRes.toInstant());
}
}
}
}

0 comments on commit be06946

Please sign in to comment.
You can’t perform that action at this time.