From a40e60de78dc35986b8aa03806703e6adf1dd1e1 Mon Sep 17 00:00:00 2001 From: CodegassW Date: Thu, 21 Mar 2024 00:59:02 -0400 Subject: [PATCH 1/3] Split testGetLocalDateTimeTypes into two test case to increase the test granularity --- .../jdbc/datatypes/DataTypesTest.java | 33 ++++++++++++------- 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/datatypes/DataTypesTest.java b/src/test/java/com/microsoft/sqlserver/jdbc/datatypes/DataTypesTest.java index 445d23dd0..9b4ad9fd0 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/datatypes/DataTypesTest.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/datatypes/DataTypesTest.java @@ -1874,38 +1874,36 @@ public void testNullValuesWithGetObject() throws Exception { } @Test - public void testGetLocalDateTimeTypes() throws Exception { - // test value needs to be in a time zone other than local - OffsetDateTime value = OffsetDateTime.now().truncatedTo(ChronoUnit.MILLIS); // Linux has more precision than SQL Server - int offsetSeconds = value.getOffset().getTotalSeconds(); - offsetSeconds += offsetSeconds < 0 ? 3600 : -3600; - value = value.withOffsetSameLocal(ZoneOffset.ofTotalSeconds(offsetSeconds)); - LocalDateTime valueWithOffsetConversion = value.atZoneSameInstant(ZoneId.systemDefault()).toLocalDateTime(); + public void testGetLocalDateTimeTypesWithDefaultOffsetDateTimeConversion() throws Exception { + OffsetDateTime value = prepareOffsetDateTime(); + LocalDateTime valueWithOffsetConversion = convertOffsetDateTimeToLocalDateTime(value); try (SQLServerConnection conn = PrepUtil.getConnection(connectionString)) { try (PreparedStatement stmt = conn.prepareStatement("SELECT ?")) { stmt.setObject(1, value); - ResultSet rs = stmt.executeQuery(); rs.next(); - // default behavior is to apply the time zone offset when converting DATETIMEOFFSET to local java.time types assertEquals(value, rs.getObject(1, OffsetDateTime.class)); assertEquals(valueWithOffsetConversion, rs.getObject(1, LocalDateTime.class)); assertEquals(valueWithOffsetConversion.toLocalDate(), rs.getObject(1, LocalDate.class)); assertEquals(valueWithOffsetConversion.toLocalTime(), rs.getObject(1, LocalTime.class)); } + } + } + + @Test + public void testGetLocalDateTimeTypesWithIgnoreOffsetDateTimeConversion() throws Exception { + OffsetDateTime value = prepareOffsetDateTime(); - // change the behavior to be compatible with java.time conversion methods + try (SQLServerConnection conn = PrepUtil.getConnection(connectionString)) { conn.setIgnoreOffsetOnDateTimeOffsetConversion(true); try (PreparedStatement stmt = conn.prepareStatement("SELECT ?")) { stmt.setObject(1, value); - ResultSet rs = stmt.executeQuery(); rs.next(); - // now the offset should be ignored instead of converting to local time zone assertEquals(value, rs.getObject(1, OffsetDateTime.class)); assertEquals(value.toLocalDateTime(), rs.getObject(1, LocalDateTime.class)); assertEquals(value.toLocalDate(), rs.getObject(1, LocalDate.class)); @@ -1946,6 +1944,17 @@ public void testDateTimeOffsetValueOfOffsetDateTime() throws Exception { assertEquals(expected, DateTimeOffset.valueOf(roundDown).getOffsetDateTime()); } + private OffsetDateTime prepareOffsetDateTime() { + OffsetDateTime value = OffsetDateTime.now().truncatedTo(ChronoUnit.MILLIS); + int offsetSeconds = value.getOffset().getTotalSeconds(); + offsetSeconds += offsetSeconds < 0 ? 3600 : -3600; + return value.withOffsetSameLocal(ZoneOffset.ofTotalSeconds(offsetSeconds)); + } + + private LocalDateTime convertOffsetDateTimeToLocalDateTime(OffsetDateTime value) { + return value.atZoneSameInstant(ZoneId.systemDefault()).toLocalDateTime(); + } + static LocalDateTime getUnstorableValue() throws Exception { ZoneId systemTimezone = ZoneId.systemDefault(); Instant now = Instant.now(); From 3f0ad836dc06c67845854e71084712f0e9463812 Mon Sep 17 00:00:00 2001 From: CodegassW Date: Sun, 14 Apr 2024 22:48:28 -0400 Subject: [PATCH 2/3] Put the extracted Arrangement functions back to the tests. --- .../jdbc/datatypes/DataTypesTest.java | 23 ++++++++----------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/datatypes/DataTypesTest.java b/src/test/java/com/microsoft/sqlserver/jdbc/datatypes/DataTypesTest.java index 9b4ad9fd0..e807b8ae8 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/datatypes/DataTypesTest.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/datatypes/DataTypesTest.java @@ -1875,8 +1875,11 @@ public void testNullValuesWithGetObject() throws Exception { @Test public void testGetLocalDateTimeTypesWithDefaultOffsetDateTimeConversion() throws Exception { - OffsetDateTime value = prepareOffsetDateTime(); - LocalDateTime valueWithOffsetConversion = convertOffsetDateTimeToLocalDateTime(value); + OffsetDateTime value = OffsetDateTime.now().truncatedTo(ChronoUnit.MILLIS); // Linux has more precision than SQL Server + int offsetSeconds = value.getOffset().getTotalSeconds(); + offsetSeconds += offsetSeconds < 0 ? 3600 : -3600; + value = value.withOffsetSameLocal(ZoneOffset.ofTotalSeconds(offsetSeconds)); + LocalDateTime valueWithOffsetConversion = value.atZoneSameInstant(ZoneId.systemDefault()).toLocalDateTime(); try (SQLServerConnection conn = PrepUtil.getConnection(connectionString)) { try (PreparedStatement stmt = conn.prepareStatement("SELECT ?")) { @@ -1894,7 +1897,10 @@ public void testGetLocalDateTimeTypesWithDefaultOffsetDateTimeConversion() throw @Test public void testGetLocalDateTimeTypesWithIgnoreOffsetDateTimeConversion() throws Exception { - OffsetDateTime value = prepareOffsetDateTime(); + OffsetDateTime value = OffsetDateTime.now().truncatedTo(ChronoUnit.MILLIS); // Linux has more precision than SQL Server + int offsetSeconds = value.getOffset().getTotalSeconds(); + offsetSeconds += offsetSeconds < 0 ? 3600 : -3600; + value = value.withOffsetSameLocal(ZoneOffset.ofTotalSeconds(offsetSeconds)); try (SQLServerConnection conn = PrepUtil.getConnection(connectionString)) { conn.setIgnoreOffsetOnDateTimeOffsetConversion(true); @@ -1944,17 +1950,6 @@ public void testDateTimeOffsetValueOfOffsetDateTime() throws Exception { assertEquals(expected, DateTimeOffset.valueOf(roundDown).getOffsetDateTime()); } - private OffsetDateTime prepareOffsetDateTime() { - OffsetDateTime value = OffsetDateTime.now().truncatedTo(ChronoUnit.MILLIS); - int offsetSeconds = value.getOffset().getTotalSeconds(); - offsetSeconds += offsetSeconds < 0 ? 3600 : -3600; - return value.withOffsetSameLocal(ZoneOffset.ofTotalSeconds(offsetSeconds)); - } - - private LocalDateTime convertOffsetDateTimeToLocalDateTime(OffsetDateTime value) { - return value.atZoneSameInstant(ZoneId.systemDefault()).toLocalDateTime(); - } - static LocalDateTime getUnstorableValue() throws Exception { ZoneId systemTimezone = ZoneId.systemDefault(); Instant now = Instant.now(); From 93e95d8569a2dfcfda17c16e8ebe8a1f76ed3857 Mon Sep 17 00:00:00 2001 From: CodegassW Date: Sun, 14 Apr 2024 22:51:57 -0400 Subject: [PATCH 3/3] add back the comments --- .../microsoft/sqlserver/jdbc/datatypes/DataTypesTest.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/datatypes/DataTypesTest.java b/src/test/java/com/microsoft/sqlserver/jdbc/datatypes/DataTypesTest.java index e807b8ae8..6caca238c 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/datatypes/DataTypesTest.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/datatypes/DataTypesTest.java @@ -1875,6 +1875,7 @@ public void testNullValuesWithGetObject() throws Exception { @Test public void testGetLocalDateTimeTypesWithDefaultOffsetDateTimeConversion() throws Exception { + // test value needs to be in a time zone other than local OffsetDateTime value = OffsetDateTime.now().truncatedTo(ChronoUnit.MILLIS); // Linux has more precision than SQL Server int offsetSeconds = value.getOffset().getTotalSeconds(); offsetSeconds += offsetSeconds < 0 ? 3600 : -3600; @@ -1887,6 +1888,7 @@ public void testGetLocalDateTimeTypesWithDefaultOffsetDateTimeConversion() throw ResultSet rs = stmt.executeQuery(); rs.next(); + // default behavior is to apply the time zone offset when converting DATETIMEOFFSET to local java.time types assertEquals(value, rs.getObject(1, OffsetDateTime.class)); assertEquals(valueWithOffsetConversion, rs.getObject(1, LocalDateTime.class)); assertEquals(valueWithOffsetConversion.toLocalDate(), rs.getObject(1, LocalDate.class)); @@ -1903,6 +1905,8 @@ public void testGetLocalDateTimeTypesWithIgnoreOffsetDateTimeConversion() throws value = value.withOffsetSameLocal(ZoneOffset.ofTotalSeconds(offsetSeconds)); try (SQLServerConnection conn = PrepUtil.getConnection(connectionString)) { + + // change the behavior to be compatible with java.time conversion methods conn.setIgnoreOffsetOnDateTimeOffsetConversion(true); try (PreparedStatement stmt = conn.prepareStatement("SELECT ?")) { @@ -1910,6 +1914,7 @@ public void testGetLocalDateTimeTypesWithIgnoreOffsetDateTimeConversion() throws ResultSet rs = stmt.executeQuery(); rs.next(); + // now the offset should be ignored instead of converting to local time zone assertEquals(value, rs.getObject(1, OffsetDateTime.class)); assertEquals(value.toLocalDateTime(), rs.getObject(1, LocalDateTime.class)); assertEquals(value.toLocalDate(), rs.getObject(1, LocalDate.class));