Download data/plugin/src/test/java/copilotj/util/FlexibleLocalDateTimeDeserializerTest.java from introvoyz045/CopilotJ: direct link, hf CLI and curl.
- Browser
- Download file 3.19 kB
-
https://huggingface.co/datasets/introvoyz045/CopilotJ/resolve/main/data/plugin/src/test/java/copilotj/util/FlexibleLocalDateTimeDeserializerTest.java
- Command line
-
hf download hf://datasets/introvoyz045/CopilotJ/data/plugin/src/test/java/copilotj/util/FlexibleLocalDateTimeDeserializerTest.java
-
curl -L -o FlexibleLocalDateTimeDeserializerTest.java https://huggingface.co/datasets/introvoyz045/CopilotJ/resolve/main/data/plugin/src/test/java/copilotj/util/FlexibleLocalDateTimeDeserializerTest.java
3.19 kB
| /** | |
| * SPDX-FileCopyrightText: Copyright contributors to the CopilotJ project. | |
| * | |
| * SPDX-License-Identifier: Apache-2.0 | |
| */ | |
| package copilotj.util; | |
| import static org.junit.jupiter.api.Assertions.assertEquals; | |
| import static org.junit.jupiter.api.Assertions.assertNull; | |
| import static org.junit.jupiter.api.Assertions.assertThrows; | |
| import java.time.LocalDateTime; | |
| import java.time.OffsetDateTime; | |
| import java.time.ZoneId; | |
| import org.junit.jupiter.api.Test; | |
| import com.fasterxml.jackson.annotation.JsonProperty; | |
| import com.fasterxml.jackson.databind.ObjectMapper; | |
| import com.fasterxml.jackson.databind.module.SimpleModule; | |
| /** | |
| * Unit tests for {@link FlexibleLocalDateTimeDeserializer}. Exercised through a | |
| * real {@link ObjectMapper} (no mock) so the production {@code JsonParser} path | |
| * runs. Expected offset-aware values are computed via the same chain the | |
| * implementation uses, so the assertions are zone-agnostic. | |
| */ | |
| public class FlexibleLocalDateTimeDeserializerTest { | |
| /** Plain static holder (NOT a record — tests compile under Java 8). */ | |
| public static final class Holder { | |
| public LocalDateTime t; | |
| } | |
| private static ObjectMapper mapper() { | |
| final ObjectMapper m = new ObjectMapper(); | |
| final SimpleModule module = new SimpleModule(); | |
| module.addDeserializer(LocalDateTime.class, new FlexibleLocalDateTimeDeserializer()); | |
| m.registerModule(module); | |
| return m; | |
| } | |
| private static LocalDateTime parse(final String jsonValue) throws Exception { | |
| return mapper().readValue("{\"t\":\"" + jsonValue + "\"}", Holder.class).t; | |
| } | |
| private static LocalDateTime localAtSystemZone(final String offsetDateTime) { | |
| return OffsetDateTime.parse(offsetDateTime).toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime(); | |
| } | |
| public void parsesNaiveLocalDateTime() throws Exception { | |
| assertEquals(LocalDateTime.of(2026, 6, 18, 10, 0, 0), parse("2026-06-18T10:00:00")); | |
| } | |
| public void parsesUtcZ() throws Exception { | |
| final String s = "2026-06-18T10:00:00Z"; | |
| assertEquals(localAtSystemZone(s), parse(s)); | |
| } | |
| public void parsesExplicitPositiveOffset() throws Exception { | |
| final String s = "2026-06-18T18:00:00+08:00"; | |
| assertEquals(localAtSystemZone(s), parse(s)); | |
| } | |
| public void parsesNegativeOffset() throws Exception { | |
| final String s = "2026-06-18T05:00:00-05:00"; | |
| assertEquals(localAtSystemZone(s), parse(s)); | |
| } | |
| public void trimsWhitespace() throws Exception { | |
| assertEquals(LocalDateTime.of(2026, 6, 18, 10, 0, 0), parse(" 2026-06-18T10:00:00 ")); | |
| } | |
| public void returnsNullForJsonNull() throws Exception { | |
| final Holder h = mapper().readValue("{\"t\":null}", Holder.class); | |
| assertNull(h.t); | |
| } | |
| public void returnsNullForEmptyAfterTrim() throws Exception { | |
| assertNull(parse(" ")); | |
| } | |
| public void rejectsDateOnly() { | |
| // No time component -> LocalDateTime.parse throws, propagating out of readValue. | |
| assertThrows(Exception.class, () -> parse("2026-06-18")); | |
| } | |
| public void rejectsGarbage() { | |
| assertThrows(Exception.class, () -> parse("not-a-date")); | |
| } | |
| } | |