001/*
002 * Licensed under the Apache License, Version 2.0 (the "License");
003 * you may not use this file except in compliance with the License.
004 * You may obtain a copy of the License at
005 *
006 *     http://www.apache.org/licenses/LICENSE-2.0
007 *
008 * Unless required by applicable law or agreed to in writing, software
009 * distributed under the License is distributed on an "AS IS" BASIS,
010 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011 * See the License for the specific language governing permissions and
012 * limitations under the License.
013 */
014package org.gbif.api.util;
015
016import java.text.ParseException;
017import java.time.LocalDate;
018import java.time.LocalDateTime;
019import java.time.OffsetDateTime;
020import java.time.Year;
021import java.time.YearMonth;
022import java.time.ZoneId;
023import java.time.ZoneOffset;
024import java.time.ZonedDateTime;
025
026import org.junit.jupiter.api.Test;
027
028import static org.junit.jupiter.api.Assertions.assertEquals;
029import static org.junit.jupiter.api.Assertions.assertNotNull;
030import static org.junit.jupiter.api.Assertions.assertNull;
031import static org.junit.jupiter.api.Assertions.assertThrows;
032
033/**
034 * Test case for class IsoDateInterval.
035 */
036public class IsoDateIntervalTest {
037
038  /**
039   * Test case for constructors.
040   */
041  @Test
042  public void constructorTest() {
043    assertNotNull(new IsoDateInterval());
044    assertNotNull(new IsoDateInterval(Year.of(1800)));
045    assertNotNull(new IsoDateInterval(Year.of(1800), Year.of(1900)));
046    assertNotNull(new IsoDateInterval(YearMonth.of(1800, 06)));
047    assertNotNull(new IsoDateInterval(YearMonth.of(1800, 06), YearMonth.of(1900, 06)));
048    assertNotNull(new IsoDateInterval(LocalDate.of(1800, 06, 12)));
049    assertNotNull(new IsoDateInterval(LocalDate.of(1800, 06, 12), LocalDate.of(1900, 06, 12)));
050    assertNotNull(new IsoDateInterval(LocalDateTime.of(1800, 06, 12, 13, 14, 15)));
051    assertNotNull(new IsoDateInterval(LocalDateTime.of(1800, 06, 12, 13, 14, 15), LocalDateTime.of(1900, 06, 12, 13, 14, 15)));
052    assertNotNull(new IsoDateInterval(OffsetDateTime.of(1800, 06, 12, 13, 14, 15, 0, ZoneOffset.ofHours(3))));
053    assertNotNull(new IsoDateInterval(OffsetDateTime.of(1800, 06, 12, 13, 14, 15, 0, ZoneOffset.ofHours(3)), OffsetDateTime.of(1900, 06, 12, 13, 14, 15, 0, ZoneOffset.ofHours(-3))));
054    assertNotNull(new IsoDateInterval(ZonedDateTime.of(1800, 06, 12, 13, 14, 15, 0, ZoneOffset.ofHours(3))));
055    assertNotNull(new IsoDateInterval(ZonedDateTime.of(1800, 06, 12, 13, 14, 15, 0, ZoneOffset.ofHours(3)), ZonedDateTime.of(1900, 06, 12, 13, 14, 15, 0, ZoneOffset.ofHours(-3))));
056
057    // Reversed range
058    assertThrows(IllegalArgumentException.class, () -> new IsoDateInterval(Year.of(1900), Year.of(1800)));
059
060    // Incompatible types
061    assertThrows(IllegalArgumentException.class, () -> new IsoDateInterval(Year.of(1900), YearMonth.of(1901, 06)));
062
063    final IsoDateInterval idi = new IsoDateInterval();
064    idi.setFrom(Year.of(1800));
065    assertThrows(IllegalArgumentException.class, () -> idi.setTo(YearMonth.of(1900, 06)));
066    assertThrows(IllegalArgumentException.class, () -> idi.setTo(Year.of(1750)));
067
068    final IsoDateInterval idi2 = new IsoDateInterval();
069    idi2.setTo(Year.of(1800));
070    assertThrows(IllegalArgumentException.class, () -> idi2.setFrom(YearMonth.of(1900, 06)));
071    assertThrows(IllegalArgumentException.class, () -> idi2.setFrom(Year.of(1950)));
072  }
073
074  /**
075   * Test case for method {@link IsoDateParsingUtils#parseDateRange(String)}.
076   */
077  @Test
078  public void checkParsing() throws ParseException {
079    assertNull(new IsoDateInterval().toString());
080    assertEquals("1800", IsoDateInterval.fromString("1800").toString());
081    assertEquals("1800/1900", IsoDateInterval.fromString("1800/1900").toString());
082    assertEquals("1800-06", IsoDateInterval.fromString("1800-06").toString());
083    assertEquals("1800-06/1900-06", IsoDateInterval.fromString("1800-06/1900-06").toString());
084    assertEquals("1800-06-12", IsoDateInterval.fromString("1800-06-12").toString());
085    assertEquals("1800-06-12/1900-06-12", IsoDateInterval.fromString("1800-06-12/1900-06-12").toString());
086    assertEquals("1800-06-12T13:14:15", IsoDateInterval.fromString("1800-06-12T13:14:15").toString());
087    assertEquals("1800-06-12T13:14:15/1900-06-12T13:14:15", IsoDateInterval.fromString("1800-06-12T13:14:15/1900-06-12T13:14:15").toString());
088    assertEquals("1800-06-12T13:14:15+03:00", IsoDateInterval.fromString("1800-06-12T13:14:15+03:00").toString());
089    assertEquals("1800-06-12T13:14:15+03:00/1900-06-12T13:14:15-03:00", IsoDateInterval.fromString("1800-06-12T13:14:15+03:00/1900-06-12T13:14:15-03:00").toString());
090    assertEquals("1800-06-12T13:14:15Z", IsoDateInterval.fromString("1800-06-12T13:14:15Z").toString());
091    assertEquals("1800-06-12T13:14:15Z/1900-06-12T13:14:15Z", IsoDateInterval.fromString("1800-06-12T13:14:15Z/1900-06-12T13:14:15Z").toString());
092  }
093
094  @Test
095  public void checkStrings() {
096    assertNull(new IsoDateInterval().toString());
097    assertEquals("1800", new IsoDateInterval(Year.of(1800)).toString());
098    assertEquals("1800/1900", new IsoDateInterval(Year.of(1800), Year.of(1900)).toString());
099    assertEquals("1800-06", new IsoDateInterval(YearMonth.of(1800, 06)).toString());
100    assertEquals("1800-06/1900-06", new IsoDateInterval(YearMonth.of(1800, 06), YearMonth.of(1900, 06)).toString());
101    assertEquals("1800-06-12", new IsoDateInterval(LocalDate.of(1800, 06, 12)).toString());
102    assertEquals("1800-06-12/1900-06-12", new IsoDateInterval(LocalDate.of(1800, 06, 12), LocalDate.of(1900, 06, 12)).toString());
103    assertEquals("1800-06-12T13:14:15", new IsoDateInterval(LocalDateTime.of(1800, 06, 12, 13, 14, 15)).toString());
104    assertEquals("1800-06-12T13:14:15/1900-06-12T13:14:15", new IsoDateInterval(LocalDateTime.of(1800, 06, 12, 13, 14, 15), LocalDateTime.of(1900, 06, 12, 13, 14, 15)).toString());
105    assertEquals("1800-06-12T13:14:15+03:00", new IsoDateInterval(OffsetDateTime.of(1800, 06, 12, 13, 14, 15, 0, ZoneOffset.ofHours(3))).toString());
106    assertEquals("1800-06-12T13:14:15+03:00/1900-06-12T13:14:15-03:00", new IsoDateInterval(OffsetDateTime.of(1800, 06, 12, 13, 14, 15, 0, ZoneOffset.ofHours(3)), OffsetDateTime.of(1900, 06, 12, 13, 14, 15, 0, ZoneOffset.ofHours(-3))).toString());
107    assertEquals("1800-06-12T13:14:15Z", new IsoDateInterval(ZonedDateTime.of(1800, 06, 12, 13, 14, 15, 0, ZoneOffset.UTC)).toString());
108    assertEquals("1800-06-12T13:14:15Z/1900-06-12T13:14:15Z", new IsoDateInterval(ZonedDateTime.of(1800, 06, 12, 13, 14, 15, 0, ZoneOffset.UTC), ZonedDateTime.of(1900, 06, 12, 13, 14, 15, 0, ZoneOffset.UTC)).toString());
109
110    // Stripped offset
111    assertEquals("1800-06-12T13:14:15", new IsoDateInterval(LocalDateTime.of(1800, 06, 12, 13, 14, 15)).toString(true));
112    assertEquals("1800-06-12T13:14:15/1900-06-12T13:14:15", new IsoDateInterval(LocalDateTime.of(1800, 06, 12, 13, 14, 15), LocalDateTime.of(1900, 06, 12, 13, 14, 15)).toString(true));
113    assertEquals("1800-06-12T13:14:15", new IsoDateInterval(OffsetDateTime.of(1800, 06, 12, 13, 14, 15, 0, ZoneOffset.ofHours(3))).toString(true));
114    assertEquals("1800-06-12T13:14:15/1900-06-12T13:14:15", new IsoDateInterval(OffsetDateTime.of(1800, 06, 12, 13, 14, 15, 0, ZoneOffset.ofHours(3)), OffsetDateTime.of(1900, 06, 12, 13, 14, 15, 0, ZoneOffset.ofHours(-3))).toString(true));
115    assertEquals("1800-06-12T13:14:15", new IsoDateInterval(ZonedDateTime.of(1800, 06, 12, 13, 14, 15, 0, ZoneId.of("Australia/Perth"))).toString(true));
116    assertEquals("1800-06-12T13:14:15/1900-06-12T13:14:15", new IsoDateInterval(ZonedDateTime.of(1800, 06, 12, 13, 14, 15, 0, ZoneOffset.ofHours(3)), ZonedDateTime.of(1900, 06, 12, 13, 14, 15, 0, ZoneOffset.ofHours(-3))).toString(true));
117
118    // Preserved as offset is zero
119    assertEquals("1800-06-12T13:14:15Z", new IsoDateInterval(OffsetDateTime.of(1800, 06, 12, 13, 14, 15, 0, ZoneOffset.ofHours(0))).toString(true));
120    assertEquals("1800-06-12T13:14:15Z/1900-06-12T13:14:15Z", new IsoDateInterval(OffsetDateTime.of(1800, 06, 12, 13, 14, 15, 0, ZoneOffset.ofHours(0)), OffsetDateTime.of(1900, 06, 12, 13, 14, 15, 0, ZoneOffset.ofHours(0))).toString(true));
121    assertEquals("1800-06-12T13:14:15Z", new IsoDateInterval(ZonedDateTime.of(1800, 06, 12, 13, 14, 15, 0, ZoneId.of("UTC"))).toString(true));
122    assertEquals("1800-06-12T13:14:15Z/1900-06-12T13:14:15Z", new IsoDateInterval(ZonedDateTime.of(1800, 06, 12, 13, 14, 15, 0, ZoneOffset.UTC), ZonedDateTime.of(1900, 06, 12, 13, 14, 15, 0, ZoneOffset.UTC)).toString(true));
123  }
124}