| 1 |
package ezvcard.util; |
| 2 |
|
| 3 |
import static org.junit.Assert.assertEquals; |
| 4 |
import static org.junit.Assert.assertFalse; |
| 5 |
import static org.junit.Assert.assertTrue; |
| 6 |
import static org.junit.Assert.fail; |
| 7 |
|
| 8 |
import java.time.Instant; |
| 9 |
import java.time.LocalDate; |
| 10 |
import java.time.LocalDateTime; |
| 11 |
import java.time.OffsetDateTime; |
| 12 |
import java.time.ZoneId; |
| 13 |
import java.time.ZoneOffset; |
| 14 |
import java.time.ZonedDateTime; |
| 15 |
import java.time.temporal.ChronoUnit; |
| 16 |
import java.util.Locale; |
| 17 |
|
| 18 |
import org.junit.Test; |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public class VCardDateFormatTest { |
| 49 |
@Test |
| 50 |
public void format_date() { |
| 51 |
LocalDate date = LocalDate.of(2006, 1, 2); |
| 52 |
assertEquals("20060102", VCardDateFormat.BASIC.format(date)); |
| 53 |
assertEquals("2006-01-02", VCardDateFormat.EXTENDED.format(date)); |
| 54 |
} |
| 55 |
|
| 56 |
@Test |
| 57 |
public void format_datetime() { |
| 58 |
LocalDateTime dateTime = LocalDateTime.of(2006, 1, 2, 10, 20, 30); |
| 59 |
assertEquals("20060102T102030", VCardDateFormat.BASIC.format(dateTime)); |
| 60 |
assertEquals("2006-01-02T10:20:30", VCardDateFormat.EXTENDED.format(dateTime)); |
| 61 |
} |
| 62 |
|
| 63 |
@Test |
| 64 |
public void format_offset_positive() { |
| 65 |
OffsetDateTime offsetPositive = OffsetDateTime.of(2006, 1, 2, 10, 20, 30, 0, ZoneOffset.ofHours(1)); |
| 66 |
assertEquals("20060102T102030+0100", VCardDateFormat.BASIC.format(offsetPositive)); |
| 67 |
assertEquals("2006-01-02T10:20:30+01:00", VCardDateFormat.EXTENDED.format(offsetPositive)); |
| 68 |
} |
| 69 |
|
| 70 |
@Test |
| 71 |
public void format_offset_positive_minutes() { |
| 72 |
OffsetDateTime offsetPositiveMinutes = OffsetDateTime.of(2006, 1, 2, 10, 20, 30, 0, ZoneOffset.ofHoursMinutes(1, 30)); |
| 73 |
assertEquals("20060102T102030+0130", VCardDateFormat.BASIC.format(offsetPositiveMinutes)); |
| 74 |
assertEquals("2006-01-02T10:20:30+01:30", VCardDateFormat.EXTENDED.format(offsetPositiveMinutes)); |
| 75 |
} |
| 76 |
|
| 77 |
@Test |
| 78 |
public void format_offset_negative() { |
| 79 |
OffsetDateTime offsetNegative = OffsetDateTime.of(2006, 1, 2, 10, 20, 30, 0, ZoneOffset.ofHours(-1)); |
| 80 |
assertEquals("20060102T102030-0100", VCardDateFormat.BASIC.format(offsetNegative)); |
| 81 |
assertEquals("2006-01-02T10:20:30-01:00", VCardDateFormat.EXTENDED.format(offsetNegative)); |
| 82 |
} |
| 83 |
|
| 84 |
@Test |
| 85 |
public void format_offset_negative_minutes() { |
| 86 |
OffsetDateTime offsetNegativeMinutes = OffsetDateTime.of(2006, 1, 2, 10, 20, 30, 0, ZoneOffset.ofHoursMinutes(-1, -30)); |
| 87 |
assertEquals("20060102T102030-0130", VCardDateFormat.BASIC.format(offsetNegativeMinutes)); |
| 88 |
assertEquals("2006-01-02T10:20:30-01:30", VCardDateFormat.EXTENDED.format(offsetNegativeMinutes)); |
| 89 |
} |
| 90 |
|
| 91 |
@Test |
| 92 |
public void format_instant() { |
| 93 |
Instant instant = LocalDateTime.of(2006, 1, 2, 10, 20, 30).toInstant(ZoneOffset.UTC); |
| 94 |
assertEquals("20060102T102030Z", VCardDateFormat.BASIC.format(instant)); |
| 95 |
assertEquals("2006-01-02T10:20:30Z", VCardDateFormat.EXTENDED.format(instant)); |
| 96 |
} |
| 97 |
|
| 98 |
@Test |
| 99 |
public void format_zoned_datetime() { |
| 100 |
ZonedDateTime zonedDateTime = ZonedDateTime.of(2006, 1, 2, 10, 20, 30, 0, ZoneId.of("America/New_York")); |
| 101 |
assertEquals("20060102T102030-0500", VCardDateFormat.BASIC.format(zonedDateTime)); |
| 102 |
assertEquals("2006-01-02T10:20:30-05:00", VCardDateFormat.EXTENDED.format(zonedDateTime)); |
| 103 |
} |
| 104 |
|
| 105 |
@Test |
| 106 |
public void format_different_locales() { |
| 107 |
OffsetDateTime date = OffsetDateTime.of(2006, 1, 2, 10, 20, 30, 0, ZoneOffset.ofHours(1)); |
| 108 |
|
| 109 |
Locale defaultLocale = Locale.getDefault(); |
| 110 |
try { |
| 111 |
for (Locale locale : Locale.getAvailableLocales()) { |
| 112 |
Locale.setDefault(locale); |
| 113 |
assertLocale(locale, VCardDateFormat.BASIC, date, "20060102T102030+0100"); |
| 114 |
assertLocale(locale, VCardDateFormat.EXTENDED, date, "2006-01-02T10:20:30+01:00"); |
| 115 |
} |
| 116 |
} finally { |
| 117 |
Locale.setDefault(defaultLocale); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
private static void assertLocale(Locale locale, VCardDateFormat df, OffsetDateTime date, String expected) { |
| 122 |
String actual = df.format(date); |
| 123 |
String message = "Test failed for " + df.name() + " with locale \"" + locale + "\"."; |
| 124 |
assertEquals(message, expected, actual); |
| 125 |
} |
| 126 |
|
| 127 |
@Test |
| 128 |
public void parse_date() { |
| 129 |
LocalDate date = LocalDate.of(2012, 7, 1); |
| 130 |
assertEquals(date, VCardDateFormat.parse("20120701")); |
| 131 |
assertEquals(date, VCardDateFormat.parse("2012-07-01")); |
| 132 |
} |
| 133 |
|
| 134 |
@Test |
| 135 |
public void parse_datetime() { |
| 136 |
LocalDateTime datetime = LocalDateTime.of(2012, 7, 1, 7, 1, 30); |
| 137 |
assertEquals(datetime, VCardDateFormat.parse("20120701T070130")); |
| 138 |
assertEquals(datetime, VCardDateFormat.parse("2012-07-01T07:01:30")); |
| 139 |
} |
| 140 |
|
| 141 |
@Test |
| 142 |
public void parse_instant() { |
| 143 |
Instant instant = LocalDateTime.of(2012, 7, 1, 7, 1, 30).toInstant(ZoneOffset.UTC); |
| 144 |
assertEquals(instant, VCardDateFormat.parse("20120701T070130Z")); |
| 145 |
assertEquals(instant, VCardDateFormat.parse("2012-07-01T07:01:30Z")); |
| 146 |
} |
| 147 |
|
| 148 |
@Test |
| 149 |
public void parse_offset() { |
| 150 |
LocalDateTime datetime = LocalDateTime.of(2012, 7, 1, 7, 1, 30); |
| 151 |
OffsetDateTime positiveOffset = OffsetDateTime.of(datetime, ZoneOffset.ofHours(3)); |
| 152 |
OffsetDateTime negativeOffset = OffsetDateTime.of(datetime, ZoneOffset.ofHours(-3)); |
| 153 |
|
| 154 |
assertEquals(positiveOffset, VCardDateFormat.parse("20120701T070130+0300")); |
| 155 |
assertEquals(positiveOffset, VCardDateFormat.parse("20120701T070130+03")); |
| 156 |
assertEquals(negativeOffset, VCardDateFormat.parse("20120701T070130-0300")); |
| 157 |
assertEquals(negativeOffset, VCardDateFormat.parse("20120701T070130-03")); |
| 158 |
|
| 159 |
assertEquals(positiveOffset, VCardDateFormat.parse("2012-07-01T07:01:30+03:00")); |
| 160 |
assertEquals(positiveOffset, VCardDateFormat.parse("2012-07-01T07:01:30+03")); |
| 161 |
assertEquals(negativeOffset, VCardDateFormat.parse("2012-07-01T07:01:30-03:00")); |
| 162 |
assertEquals(negativeOffset, VCardDateFormat.parse("2012-07-01T07:01:30-03")); |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
@Test |
| 169 |
public void parse_hcard() { |
| 170 |
OffsetDateTime datetime = OffsetDateTime.of(LocalDateTime.of(2012, 7, 1, 7, 1, 30), ZoneOffset.ofHours(3)); |
| 171 |
assertEquals(datetime, VCardDateFormat.parse("2012-07-01T07:01:30+0300")); |
| 172 |
} |
| 173 |
|
| 174 |
@Test |
| 175 |
public void parse_datetime_nanoseconds() { |
| 176 |
Instant instant = LocalDateTime.of(2012, 7, 1, 7, 1, 30).toInstant(ZoneOffset.UTC); |
| 177 |
|
| 178 |
Instant instantWithNanos = instant.plus(100_000_000, ChronoUnit.NANOS); |
| 179 |
assertEquals(instantWithNanos, VCardDateFormat.parse("20120701T070130.1Z")); |
| 180 |
|
| 181 |
instantWithNanos = instant.plus(123_900_000, ChronoUnit.NANOS); |
| 182 |
assertEquals(instantWithNanos, VCardDateFormat.parse("20120701T070130.1239Z")); |
| 183 |
|
| 184 |
instantWithNanos = instant.plus(123_456_789, ChronoUnit.NANOS); |
| 185 |
assertEquals(instantWithNanos, VCardDateFormat.parse("20120701T070130.1234567888Z")); |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
@Test |
| 192 |
public void parse_single_digit_month_and_date() throws Exception { |
| 193 |
{ |
| 194 |
LocalDate date = LocalDate.of(2012, 7, 1); |
| 195 |
|
| 196 |
assertEquals(date, VCardDateFormat.parse("2012-07-1")); |
| 197 |
assertEquals(date, VCardDateFormat.parse("2012-7-01")); |
| 198 |
assertEquals(date, VCardDateFormat.parse("2012-7-1")); |
| 199 |
|
| 200 |
try { |
| 201 |
VCardDateFormat.parse("201271"); |
| 202 |
fail(); |
| 203 |
} catch (IllegalArgumentException expected) { |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
{ |
| 208 |
LocalDate ambiguous = LocalDate.of(2012, 11, 3); |
| 209 |
|
| 210 |
assertEquals(ambiguous, VCardDateFormat.parse("2012-11-3")); |
| 211 |
|
| 212 |
try { |
| 213 |
VCardDateFormat.parse("2012113"); |
| 214 |
fail(); |
| 215 |
} catch (IllegalArgumentException expected) { |
| 216 |
} |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
@Test(expected = IllegalArgumentException.class) |
| 221 |
public void parse_invalid() { |
| 222 |
VCardDateFormat.parse("invalid"); |
| 223 |
} |
| 224 |
|
| 225 |
@Test(expected = IllegalArgumentException.class) |
| 226 |
public void parse_invalid_month() { |
| 227 |
VCardDateFormat.parse("19879215"); |
| 228 |
} |
| 229 |
|
| 230 |
@Test(expected = IllegalArgumentException.class) |
| 231 |
public void parse_invalid_date() { |
| 232 |
VCardDateFormat.parse("19871233"); |
| 233 |
} |
| 234 |
|
| 235 |
@Test(expected = IllegalArgumentException.class) |
| 236 |
public void parse_invalid_hour() { |
| 237 |
VCardDateFormat.parse("19871231T240000"); |
| 238 |
} |
| 239 |
|
| 240 |
@Test(expected = IllegalArgumentException.class) |
| 241 |
public void parse_invalid_minute() { |
| 242 |
VCardDateFormat.parse("19871231T006100"); |
| 243 |
} |
| 244 |
|
| 245 |
@Test(expected = IllegalArgumentException.class) |
| 246 |
public void parse_invalid_second() { |
| 247 |
VCardDateFormat.parse("19871231T000061"); |
| 248 |
} |
| 249 |
|
| 250 |
@Test |
| 251 |
public void hasTime() { |
| 252 |
assertFalse(VCardDateFormat.hasTime(LocalDate.now())); |
| 253 |
assertTrue(VCardDateFormat.hasTime(LocalDateTime.now())); |
| 254 |
assertTrue(VCardDateFormat.hasTime(OffsetDateTime.now())); |
| 255 |
assertTrue(VCardDateFormat.hasTime(Instant.now())); |
| 256 |
} |
| 257 |
} |