|
| 1 | +/* |
| 2 | + * The MIT License |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | + * of this software and associated documentation files (the "Software"), to deal |
| 6 | + * in the Software without restriction, including without limitation the rights |
| 7 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | + * copies of the Software, and to permit persons to whom the Software is |
| 9 | + * furnished to do so, subject to the following conditions: |
| 10 | + * |
| 11 | + * The above copyright notice and this permission notice shall be included in |
| 12 | + * all copies or substantial portions of the Software. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 20 | + * THE SOFTWARE. |
| 21 | + */ |
| 22 | +package com.influxdb.client; |
| 23 | + |
| 24 | +import java.io.IOException; |
| 25 | +import java.io.StringWriter; |
| 26 | +import java.time.LocalDateTime; |
| 27 | +import java.time.OffsetDateTime; |
| 28 | +import java.time.ZoneOffset; |
| 29 | + |
| 30 | +import com.google.gson.JsonIOException; |
| 31 | +import com.google.gson.stream.JsonWriter; |
| 32 | +import org.assertj.core.api.Assertions; |
| 33 | +import org.junit.jupiter.api.BeforeEach; |
| 34 | +import org.junit.jupiter.api.Test; |
| 35 | + |
| 36 | +/** |
| 37 | + * @author Jakub Bednar (25/06/2020 13:47) |
| 38 | + */ |
| 39 | +public class OffsetDateTimeTypeAdapterTest { |
| 40 | + |
| 41 | + private JSON.OffsetDateTimeTypeAdapter adapter; |
| 42 | + private StringWriter writer; |
| 43 | + |
| 44 | + @BeforeEach |
| 45 | + void beforeEach() { |
| 46 | + adapter = new JSON.OffsetDateTimeTypeAdapter(); |
| 47 | + writer = new StringWriter(); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void max() throws IOException { |
| 52 | + OffsetDateTime time = LocalDateTime.of(9999, 12, 31, 23, 59) |
| 53 | + .atOffset(ZoneOffset.UTC); |
| 54 | + adapter.write(new JsonWriter(writer), time); |
| 55 | + |
| 56 | + Assertions.assertThat(writer.toString()).isEqualTo("\"9999-12-31T23:59:00Z\""); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void max_over() { |
| 61 | + |
| 62 | + OffsetDateTime time = LocalDateTime.of(10_000, 12, 31, 23, 59) |
| 63 | + .atOffset(ZoneOffset.UTC); |
| 64 | + |
| 65 | + Assertions.assertThatThrownBy(() -> adapter.write(new JsonWriter(writer), time)) |
| 66 | + .isInstanceOf(JsonIOException.class) |
| 67 | + .hasMessage("OffsetDateTime is out of range. All dates and times are assumed to be in the " |
| 68 | + + "\"current era\", somewhere between 0000AD and 9999AD."); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void max_offset() { |
| 73 | + |
| 74 | + Assertions.assertThatThrownBy(() -> adapter.write(new JsonWriter(writer), OffsetDateTime.MAX)) |
| 75 | + .isInstanceOf(JsonIOException.class) |
| 76 | + .hasMessage("OffsetDateTime is out of range. All dates and times are assumed to be in the " |
| 77 | + + "\"current era\", somewhere between 0000AD and 9999AD."); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void min() throws IOException { |
| 82 | + OffsetDateTime time = LocalDateTime.of(0, 1, 1, 0, 0) |
| 83 | + .atOffset(ZoneOffset.UTC); |
| 84 | + adapter.write(new JsonWriter(writer), time); |
| 85 | + |
| 86 | + Assertions.assertThat(writer.toString()).isEqualTo("\"0000-01-01T00:00:00Z\""); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + public void min_over() { |
| 91 | + OffsetDateTime time = LocalDateTime.of(-1, 1, 1, 0, 0) |
| 92 | + .atOffset(ZoneOffset.UTC); |
| 93 | + Assertions.assertThatThrownBy(() -> adapter.write(new JsonWriter(writer), time)) |
| 94 | + .isInstanceOf(JsonIOException.class) |
| 95 | + .hasMessage("OffsetDateTime is out of range. All dates and times are assumed to be in the " |
| 96 | + + "\"current era\", somewhere between 0000AD and 9999AD."); |
| 97 | + |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + public void min_offset() { |
| 102 | + Assertions.assertThatThrownBy(() -> adapter.write(new JsonWriter(writer), OffsetDateTime.MIN)) |
| 103 | + .isInstanceOf(JsonIOException.class) |
| 104 | + .hasMessage("OffsetDateTime is out of range. All dates and times are assumed to be in the " |
| 105 | + + "\"current era\", somewhere between 0000AD and 9999AD."); |
| 106 | + |
| 107 | + } |
| 108 | +} |
0 commit comments