Merge pull request #32223 from valentine-dev

* gh-32223:
  Polish "Support lower-case input in DurationStyle"
  Support lower-case input in DurationStyle

Closes gh-32223
pull/32501/head
Andy Wilkinson 2 years ago
commit b47eb1abfc

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2021 the original author or authors. * Copyright 2012-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -29,6 +29,7 @@ import org.springframework.util.StringUtils;
* Duration format styles. * Duration format styles.
* *
* @author Phillip Webb * @author Phillip Webb
* @author Valentine Wu
* @since 2.0.0 * @since 2.0.0
*/ */
public enum DurationStyle { public enum DurationStyle {
@ -62,7 +63,7 @@ public enum DurationStyle {
/** /**
* ISO-8601 formatting. * ISO-8601 formatting.
*/ */
ISO8601("^[+-]?P.*$") { ISO8601("^[+-]?[pP].*$") {
@Override @Override
public Duration parse(String value, ChronoUnit unit) { public Duration parse(String value, ChronoUnit unit) {

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2021 the original author or authors. * Copyright 2012-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -28,6 +28,7 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException
* Tests for {@link DurationStyle}. * Tests for {@link DurationStyle}.
* *
* @author Phillip Webb * @author Phillip Webb
* @author Valentine Wu
*/ */
class DurationStyleTests { class DurationStyleTests {
@ -39,6 +40,7 @@ class DurationStyleTests {
@Test @Test
void detectAndParseWhenIso8601ShouldReturnDuration() { void detectAndParseWhenIso8601ShouldReturnDuration() {
assertThat(DurationStyle.detectAndParse("pt20.345s")).isEqualTo(Duration.parse("pt20.345s"));
assertThat(DurationStyle.detectAndParse("PT20.345S")).isEqualTo(Duration.parse("PT20.345S")); assertThat(DurationStyle.detectAndParse("PT20.345S")).isEqualTo(Duration.parse("PT20.345S"));
assertThat(DurationStyle.detectAndParse("PT15M")).isEqualTo(Duration.parse("PT15M")); assertThat(DurationStyle.detectAndParse("PT15M")).isEqualTo(Duration.parse("PT15M"));
assertThat(DurationStyle.detectAndParse("+PT15M")).isEqualTo(Duration.parse("PT15M")); assertThat(DurationStyle.detectAndParse("+PT15M")).isEqualTo(Duration.parse("PT15M"));
@ -143,6 +145,7 @@ class DurationStyleTests {
@Test @Test
void detectWhenIso8601ShouldReturnIso8601() { void detectWhenIso8601ShouldReturnIso8601() {
assertThat(DurationStyle.detect("pt20.345s")).isEqualTo(DurationStyle.ISO8601);
assertThat(DurationStyle.detect("PT20.345S")).isEqualTo(DurationStyle.ISO8601); assertThat(DurationStyle.detect("PT20.345S")).isEqualTo(DurationStyle.ISO8601);
assertThat(DurationStyle.detect("PT15M")).isEqualTo(DurationStyle.ISO8601); assertThat(DurationStyle.detect("PT15M")).isEqualTo(DurationStyle.ISO8601);
assertThat(DurationStyle.detect("+PT15M")).isEqualTo(DurationStyle.ISO8601); assertThat(DurationStyle.detect("+PT15M")).isEqualTo(DurationStyle.ISO8601);
@ -161,6 +164,7 @@ class DurationStyleTests {
@Test @Test
void parseIso8601ShouldParse() { void parseIso8601ShouldParse() {
assertThat(DurationStyle.ISO8601.parse("pt20.345s")).isEqualTo(Duration.parse("pt20.345s"));
assertThat(DurationStyle.ISO8601.parse("PT20.345S")).isEqualTo(Duration.parse("PT20.345S")); assertThat(DurationStyle.ISO8601.parse("PT20.345S")).isEqualTo(Duration.parse("PT20.345S"));
assertThat(DurationStyle.ISO8601.parse("PT15M")).isEqualTo(Duration.parse("PT15M")); assertThat(DurationStyle.ISO8601.parse("PT15M")).isEqualTo(Duration.parse("PT15M"));
assertThat(DurationStyle.ISO8601.parse("+PT15M")).isEqualTo(Duration.parse("PT15M")); assertThat(DurationStyle.ISO8601.parse("+PT15M")).isEqualTo(Duration.parse("PT15M"));
@ -173,6 +177,7 @@ class DurationStyleTests {
@Test @Test
void parseIso8601WithUnitShouldIgnoreUnit() { void parseIso8601WithUnitShouldIgnoreUnit() {
assertThat(DurationStyle.ISO8601.parse("pt20.345s", ChronoUnit.SECONDS)).isEqualTo(Duration.parse("pt20.345s"));
assertThat(DurationStyle.ISO8601.parse("PT20.345S", ChronoUnit.SECONDS)).isEqualTo(Duration.parse("PT20.345S")); assertThat(DurationStyle.ISO8601.parse("PT20.345S", ChronoUnit.SECONDS)).isEqualTo(Duration.parse("PT20.345S"));
assertThat(DurationStyle.ISO8601.parse("PT15M", ChronoUnit.SECONDS)).isEqualTo(Duration.parse("PT15M")); assertThat(DurationStyle.ISO8601.parse("PT15M", ChronoUnit.SECONDS)).isEqualTo(Duration.parse("PT15M"));
assertThat(DurationStyle.ISO8601.parse("+PT15M", ChronoUnit.SECONDS)).isEqualTo(Duration.parse("PT15M")); assertThat(DurationStyle.ISO8601.parse("+PT15M", ChronoUnit.SECONDS)).isEqualTo(Duration.parse("PT15M"));

@ -38,6 +38,7 @@ class StringToDurationConverterTests {
@ConversionServiceTest @ConversionServiceTest
void convertWhenIso8601ShouldReturnDuration(ConversionService conversionService) { void convertWhenIso8601ShouldReturnDuration(ConversionService conversionService) {
assertThat(convert(conversionService, "pt20.345s")).isEqualTo(Duration.parse("pt20.345s"));
assertThat(convert(conversionService, "PT20.345S")).isEqualTo(Duration.parse("PT20.345S")); assertThat(convert(conversionService, "PT20.345S")).isEqualTo(Duration.parse("PT20.345S"));
assertThat(convert(conversionService, "PT15M")).isEqualTo(Duration.parse("PT15M")); assertThat(convert(conversionService, "PT15M")).isEqualTo(Duration.parse("PT15M"));
assertThat(convert(conversionService, "+PT15M")).isEqualTo(Duration.parse("PT15M")); assertThat(convert(conversionService, "+PT15M")).isEqualTo(Duration.parse("PT15M"));

Loading…
Cancel
Save