| 1 |
package ezvcard.util; |
| 2 |
|
| 3 |
import static ezvcard.util.PartialDate.builder; |
| 4 |
import static org.junit.Assert.assertEquals; |
| 5 |
import static org.junit.Assert.assertFalse; |
| 6 |
import static org.junit.Assert.assertTrue; |
| 7 |
import static org.junit.Assert.fail; |
| 8 |
|
| 9 |
import java.time.ZoneOffset; |
| 10 |
import java.util.Arrays; |
| 11 |
import java.util.List; |
| 12 |
import java.util.Locale; |
| 13 |
|
| 14 |
import nl.jqno.equalsverifier.EqualsVerifier; |
| 15 |
|
| 16 |
import org.junit.Test; |
| 17 |
|
| 18 |
|
| 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 |
|
| 49 |
|
| 50 |
public class PartialDateTest { |
| 51 |
@Test |
| 52 |
public void builder_month() { |
| 53 |
PartialDate.Builder builder = builder(); |
| 54 |
final int start = 0, end = 13; |
| 55 |
|
| 56 |
try { |
| 57 |
builder.month(start); |
| 58 |
fail(); |
| 59 |
} catch (IllegalArgumentException expected) { |
| 60 |
} |
| 61 |
|
| 62 |
for (int i = start + 1; i < end; i++) { |
| 63 |
builder.month(i); |
| 64 |
} |
| 65 |
|
| 66 |
try { |
| 67 |
builder.month(end); |
| 68 |
fail(); |
| 69 |
} catch (IllegalArgumentException expected) { |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
@Test |
| 74 |
public void builder_date() { |
| 75 |
PartialDate.Builder builder = builder(); |
| 76 |
final int start = 0, end = 32; |
| 77 |
|
| 78 |
try { |
| 79 |
builder.date(start); |
| 80 |
fail(); |
| 81 |
} catch (IllegalArgumentException expected) { |
| 82 |
} |
| 83 |
|
| 84 |
for (int i = start + 1; i < end; i++) { |
| 85 |
builder.date(i); |
| 86 |
} |
| 87 |
|
| 88 |
try { |
| 89 |
builder.date(end); |
| 90 |
fail(); |
| 91 |
} catch (IllegalArgumentException expected) { |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
@Test |
| 96 |
public void builder_hour() { |
| 97 |
PartialDate.Builder builder = builder(); |
| 98 |
final int start = -1, end = 24; |
| 99 |
|
| 100 |
try { |
| 101 |
builder.hour(start); |
| 102 |
fail(); |
| 103 |
} catch (IllegalArgumentException expected) { |
| 104 |
} |
| 105 |
|
| 106 |
for (int i = start + 1; i < end; i++) { |
| 107 |
builder.hour(i); |
| 108 |
} |
| 109 |
|
| 110 |
try { |
| 111 |
builder.hour(end); |
| 112 |
fail(); |
| 113 |
} catch (IllegalArgumentException expected) { |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
@Test |
| 118 |
public void builder_minute() { |
| 119 |
PartialDate.Builder builder = builder(); |
| 120 |
final int start = -1, end = 60; |
| 121 |
|
| 122 |
try { |
| 123 |
builder.minute(start); |
| 124 |
fail(); |
| 125 |
} catch (IllegalArgumentException expected) { |
| 126 |
} |
| 127 |
|
| 128 |
for (int i = start + 1; i < end; i++) { |
| 129 |
builder.minute(i); |
| 130 |
} |
| 131 |
|
| 132 |
try { |
| 133 |
builder.minute(end); |
| 134 |
fail(); |
| 135 |
} catch (IllegalArgumentException expected) { |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
@Test |
| 140 |
public void builder_second() { |
| 141 |
PartialDate.Builder builder = builder(); |
| 142 |
final int start = -1, end = 60; |
| 143 |
|
| 144 |
try { |
| 145 |
builder.second(start); |
| 146 |
fail(); |
| 147 |
} catch (IllegalArgumentException expected) { |
| 148 |
} |
| 149 |
|
| 150 |
for (int i = start + 1; i < end; i++) { |
| 151 |
builder.second(i); |
| 152 |
} |
| 153 |
|
| 154 |
try { |
| 155 |
builder.second(end); |
| 156 |
fail(); |
| 157 |
} catch (IllegalArgumentException expected) { |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
@Test |
| 162 |
public void builder_offset_null() { |
| 163 |
PartialDate d1 = builder().year(2020).month(5).date(21).offset(ZoneOffset.ofHoursMinutes(1, 30)).offset(null).build(); |
| 164 |
PartialDate d2 = builder().year(2020).month(5).date(21).build(); |
| 165 |
assertEquals(d1, d2); |
| 166 |
} |
| 167 |
|
| 168 |
@Test |
| 169 |
public void builder_build() { |
| 170 |
try { |
| 171 |
builder().year(2015).date(1).build(); |
| 172 |
fail(); |
| 173 |
} catch (IllegalArgumentException expected) { |
| 174 |
} |
| 175 |
|
| 176 |
try { |
| 177 |
builder().hour(16).second(1).build(); |
| 178 |
fail(); |
| 179 |
} catch (IllegalArgumentException expected) { |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
@Test |
| 184 |
public void builder_copy() { |
| 185 |
PartialDate orig = builder().year(2015).month(3).date(5).hour(12).minute(1).second(20).offset(ZoneOffset.ofHours(-5)).build(); |
| 186 |
PartialDate copy = builder(orig).build(); |
| 187 |
assertEquals(orig, copy); |
| 188 |
} |
| 189 |
|
| 190 |
@Test |
| 191 |
public void toISO8601() { |
| 192 |
|
| 193 |
assertToISO8601(builder().year(1980), "1980", "1980"); |
| 194 |
assertToISO8601(builder().month(4), "--04", "--04"); |
| 195 |
assertToISO8601(builder().date(20), "---20", "---20"); |
| 196 |
assertToISO8601(builder().year(1980).month(4), "1980-04", "1980-04"); |
| 197 |
assertToISO8601(builder().month(4).date(20), "--0420", "--04-20"); |
| 198 |
assertToISO8601(builder().year(1980).month(4).date(20), "19800420", "1980-04-20"); |
| 199 |
assertToISO8601(builder(), "", ""); |
| 200 |
|
| 201 |
|
| 202 |
assertToISO8601(builder().hour(5), "T05", "T05"); |
| 203 |
assertToISO8601(builder().minute(20), "T-20", "T-20"); |
| 204 |
assertToISO8601(builder().second(32), "T--32", "T--32"); |
| 205 |
assertToISO8601(builder().hour(5).minute(20), "T0520", "T05:20"); |
| 206 |
assertToISO8601(builder().minute(20).second(32), "T-2032", "T-20:32"); |
| 207 |
assertToISO8601(builder().hour(5).minute(20).second(32), "T052032", "T05:20:32"); |
| 208 |
assertToISO8601(builder(), "", ""); |
| 209 |
assertToISO8601(builder().minute(20).second(32).offset(ZoneOffset.ofHoursMinutes(-5, -30)), "T-2032-0530", "T-20:32-05:30"); |
| 210 |
assertToISO8601(builder().minute(20).second(32).offset(ZoneOffset.ofHoursMinutes(-5, 0)), "T-2032-0500", "T-20:32-05:00"); |
| 211 |
assertToISO8601(builder().minute(20).second(32).offset(ZoneOffset.ofHoursMinutes(5, 30)), "T-2032+0530", "T-20:32+05:30"); |
| 212 |
assertToISO8601(builder().minute(20).second(32).offset(ZoneOffset.ofHoursMinutes(0, -30)), "T-2032-0030", "T-20:32-00:30"); |
| 213 |
assertToISO8601(builder().minute(20).second(32).offset(ZoneOffset.ofHoursMinutes(0, 30)), "T-2032+0030", "T-20:32+00:30"); |
| 214 |
|
| 215 |
|
| 216 |
assertToISO8601(builder().month(4).date(20).hour(5).offset(ZoneOffset.ofHoursMinutes(-5, 0)), "--0420T05-0500", "--04-20T05-05:00"); |
| 217 |
} |
| 218 |
|
| 219 |
@Test |
| 220 |
public void toISO8601_different_locales() { |
| 221 |
Locale defaultLocale = Locale.getDefault(); |
| 222 |
PartialDate partialDate = PartialDate.builder().hour(5).minute(20).second(32).build(); |
| 223 |
try { |
| 224 |
for (Locale locale : Locale.getAvailableLocales()) { |
| 225 |
Locale.setDefault(locale); |
| 226 |
String expected = "T052032"; |
| 227 |
String actual = partialDate.toISO8601(false); |
| 228 |
assertEquals("Test failed for locale: " + locale, expected, actual); |
| 229 |
} |
| 230 |
} finally { |
| 231 |
Locale.setDefault(defaultLocale); |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
@Test(expected = IllegalArgumentException.class) |
| 236 |
public void parse_empty() { |
| 237 |
PartialDate.parse(""); |
| 238 |
} |
| 239 |
|
| 240 |
@Test(expected = IllegalArgumentException.class) |
| 241 |
public void parse_invalid() { |
| 242 |
PartialDate.parse("invalid"); |
| 243 |
} |
| 244 |
|
| 245 |
@Test(expected = IllegalArgumentException.class) |
| 246 |
public void parse_invalid_month() { |
| 247 |
PartialDate.parse("19871331"); |
| 248 |
} |
| 249 |
|
| 250 |
@Test(expected = IllegalArgumentException.class) |
| 251 |
public void parse_invalid_date() { |
| 252 |
PartialDate.parse("19871233"); |
| 253 |
} |
| 254 |
|
| 255 |
@Test(expected = IllegalArgumentException.class) |
| 256 |
public void parse_invalid_hour() { |
| 257 |
PartialDate.parse("T24:00:00"); |
| 258 |
} |
| 259 |
|
| 260 |
@Test(expected = IllegalArgumentException.class) |
| 261 |
public void parse_invalid_minute() { |
| 262 |
PartialDate.parse("T00:61:00"); |
| 263 |
} |
| 264 |
|
| 265 |
@Test(expected = IllegalArgumentException.class) |
| 266 |
public void parse_invalid_second() { |
| 267 |
PartialDate.parse("T00:00:61"); |
| 268 |
} |
| 269 |
|
| 270 |
@Test(expected = IllegalArgumentException.class) |
| 271 |
public void parse_invalid_offset_hour() { |
| 272 |
PartialDate.parse("T23:00:00-88:00"); |
| 273 |
} |
| 274 |
|
| 275 |
@Test(expected = IllegalArgumentException.class) |
| 276 |
public void parse_invalid_offset_minute() { |
| 277 |
PartialDate.parse("T23:00:00-00:60"); |
| 278 |
} |
| 279 |
|
| 280 |
@Test |
| 281 |
public void parse() { |
| 282 |
assertParse("1980", builder().year(1980)); |
| 283 |
assertParse("--04", builder().month(4)); |
| 284 |
assertParse("---20", builder().date(20)); |
| 285 |
assertParse("1980-04", builder().year(1980).month(4)); |
| 286 |
|
| 287 |
assertParse("--0420", builder().month(4).date(20)); |
| 288 |
assertParse("--04-20", builder().month(4).date(20)); |
| 289 |
assertParse("19800420", builder().year(1980).month(4).date(20)); |
| 290 |
assertParse("1980-04-20", builder().year(1980).month(4).date(20)); |
| 291 |
assertParse("1980-04-20T", builder().year(1980).month(4).date(20)); |
| 292 |
|
| 293 |
assertParse("T05", builder().hour(5)); |
| 294 |
assertParse("T-20", builder().minute(20)); |
| 295 |
assertParse("T--32", builder().second(32)); |
| 296 |
assertParse("T0520", builder().hour(5).minute(20)); |
| 297 |
assertParse("T05:20", builder().hour(5).minute(20)); |
| 298 |
assertParse("T-2032", builder().minute(20).second(32)); |
| 299 |
assertParse("T-20:32", builder().minute(20).second(32)); |
| 300 |
assertParse("T052032", builder().hour(5).minute(20).second(32)); |
| 301 |
assertParse("T05:20:32", builder().hour(5).minute(20).second(32)); |
| 302 |
assertParse("T-2032-0530", builder().minute(20).second(32).offset(ZoneOffset.ofHoursMinutes(-5, -30))); |
| 303 |
assertParse("T-20:32-05:30", builder().minute(20).second(32).offset(ZoneOffset.ofHoursMinutes(-5, -30))); |
| 304 |
assertParse("T-2032-0500", builder().minute(20).second(32).offset(ZoneOffset.ofHoursMinutes(-5, 0))); |
| 305 |
assertParse("T-20:32-05:00", builder().minute(20).second(32).offset(ZoneOffset.ofHoursMinutes(-5, 0))); |
| 306 |
assertParse("T-2032-05", builder().minute(20).second(32).offset(ZoneOffset.ofHoursMinutes(-5, 0))); |
| 307 |
assertParse("T-20:32-05", builder().minute(20).second(32).offset(ZoneOffset.ofHoursMinutes(-5, 0))); |
| 308 |
assertParse("T-20:32+05:30", builder().minute(20).second(32).offset(ZoneOffset.ofHoursMinutes(5, 30))); |
| 309 |
assertParse("T-20:32+00:30", builder().minute(20).second(32).offset(ZoneOffset.ofHoursMinutes(0, 30))); |
| 310 |
assertParse("T-20:32-00:30", builder().minute(20).second(32).offset(ZoneOffset.ofHoursMinutes(0, -30))); |
| 311 |
assertParse("--0420T05-0500", builder().month(4).date(20).hour(5).offset(ZoneOffset.ofHoursMinutes(-5, 0))); |
| 312 |
assertParse("--04-20T05-05:00", builder().month(4).date(20).hour(5).offset(ZoneOffset.ofHoursMinutes(-5, 0))); |
| 313 |
} |
| 314 |
|
| 315 |
@Test |
| 316 |
public void getUtcOffset() { |
| 317 |
|
| 318 |
List<ZoneOffset> offsets = Arrays.asList( |
| 319 |
ZoneOffset.ofHoursMinutes(-5, 0), |
| 320 |
ZoneOffset.ofHoursMinutes(-5, -30), |
| 321 |
ZoneOffset.ofHoursMinutes(0, 30), |
| 322 |
ZoneOffset.ofHoursMinutes(0, -30), |
| 323 |
ZoneOffset.ofHoursMinutes(5, 0), |
| 324 |
ZoneOffset.ofHoursMinutes(5, 30) |
| 325 |
); |
| 326 |
|
| 327 |
|
| 328 |
for (ZoneOffset offset : offsets) { |
| 329 |
PartialDate date = builder().offset(offset).build(); |
| 330 |
assertEquals(offset, date.getUtcOffset()); |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
@Test |
| 335 |
public void hasDateComponent() { |
| 336 |
assertTrue(builder().year(1980).build().hasDateComponent()); |
| 337 |
assertFalse(builder().hour(5).build().hasDateComponent()); |
| 338 |
} |
| 339 |
|
| 340 |
@Test |
| 341 |
public void hasTimeComponent() { |
| 342 |
assertFalse(builder().year(1980).build().hasTimeComponent()); |
| 343 |
assertTrue(builder().hour(5).build().hasTimeComponent()); |
| 344 |
} |
| 345 |
|
| 346 |
@Test |
| 347 |
public void equals_contract() { |
| 348 |
EqualsVerifier.forClass(PartialDate.class).usingGetClass().verify(); |
| 349 |
} |
| 350 |
|
| 351 |
private static void assertToISO8601(PartialDate.Builder dateBuilder, String expectedBasic, String expectedExtended) { |
| 352 |
PartialDate date = dateBuilder.build(); |
| 353 |
assertEquals(expectedBasic, date.toISO8601(false)); |
| 354 |
assertEquals(expectedExtended, date.toISO8601(true)); |
| 355 |
} |
| 356 |
|
| 357 |
private static void assertParse(String input, PartialDate.Builder expectedBuilder) { |
| 358 |
PartialDate expected = expectedBuilder.build(); |
| 359 |
PartialDate actual = PartialDate.parse(input); |
| 360 |
assertEquals(expected, actual); |
| 361 |
} |
| 362 |
} |