forked from FasterXML/jackson-modules-java8
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for jsonformat in duration deserializer based on Duration::of…
…(long,TemporalUnit). ref FasterXML#184
- Loading branch information
1 parent
0b6a711
commit 7ecd6ce
Showing
6 changed files
with
240 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
...rc/test/java/com/fasterxml/jackson/datatype/jsr310/deser/DurationUnitParserEmptyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.fasterxml.jackson.datatype.jsr310.deser; | ||
|
||
import com.fasterxml.jackson.datatype.jsr310.deser.DurationDeserializer.DurationUnitParser; | ||
import org.junit.Test; | ||
|
||
import static java.util.Optional.empty; | ||
import static org.junit.Assert.assertEquals; | ||
|
||
public class DurationUnitParserEmptyTest { | ||
|
||
@Test | ||
public void shouldReturnEmpty_whenNull() { | ||
assertEquals(empty(), DurationUnitParser.from(null)); | ||
} | ||
|
||
@Test | ||
public void shouldReturnEmpty_whenEmptyString() { | ||
assertEquals(empty(), DurationUnitParser.from("")); | ||
} | ||
|
||
@Test | ||
public void shouldReturnEmpty_whenSpaces() { | ||
assertEquals(empty(), DurationUnitParser.from(" ")); | ||
} | ||
|
||
@Test | ||
public void shouldReturnEmpty_whenDoesNotMatchAnyTemporalUnit() { | ||
assertEquals(empty(), DurationUnitParser.from("DOESNOTMATCH")); | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
...ime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/DurationUnitParserTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package com.fasterxml.jackson.datatype.jsr310.deser; | ||
|
||
import com.fasterxml.jackson.datatype.jsr310.deser.DurationDeserializer.DurationUnitParser; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.Parameterized; | ||
import org.junit.runners.Parameterized.Parameters; | ||
|
||
import java.time.temporal.ChronoUnit; | ||
import java.time.temporal.TemporalUnit; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
|
||
import static java.util.Arrays.asList; | ||
import static java.util.Optional.of; | ||
import static org.junit.Assert.assertEquals; | ||
|
||
@RunWith(Parameterized.class) | ||
public class DurationUnitParserTest { | ||
|
||
private final String stringPattern; | ||
private final TemporalUnit temporalUnit; | ||
|
||
public DurationUnitParserTest(String stringPattern, TemporalUnit temporalUnit) { | ||
this.stringPattern = stringPattern; | ||
this.temporalUnit = temporalUnit; | ||
} | ||
|
||
@Test | ||
public void shouldMapToTemporalUnit() { | ||
Optional<DurationUnitParser> durationPattern = DurationUnitParser.from(stringPattern); | ||
|
||
assertEquals(of(temporalUnit), durationPattern.map(dp -> dp.unit)); | ||
} | ||
|
||
@Parameters | ||
public static Collection<Object[]> testCases() { | ||
List<Object[]> baseTestCases = asList( | ||
asArray("Nanos", ChronoUnit.NANOS), | ||
asArray("Micros", ChronoUnit.MICROS), | ||
asArray("Millis", ChronoUnit.MILLIS), | ||
asArray("Seconds", ChronoUnit.SECONDS), | ||
asArray("Minutes", ChronoUnit.MINUTES), | ||
asArray("Hours", ChronoUnit.HOURS), | ||
asArray("HalfDays", ChronoUnit.HALF_DAYS), | ||
asArray("Days", ChronoUnit.DAYS), | ||
asArray("Weeks", ChronoUnit.WEEKS), | ||
asArray("Months", ChronoUnit.MONTHS), | ||
asArray("Years", ChronoUnit.YEARS), | ||
asArray("Decades", ChronoUnit.DECADES), | ||
asArray("Centuries", ChronoUnit.CENTURIES), | ||
asArray("Millennia", ChronoUnit.MILLENNIA), | ||
asArray("Eras", ChronoUnit.ERAS), | ||
asArray("Forever", ChronoUnit.FOREVER) | ||
); | ||
|
||
List<Object[]> lowerCaseTestCases = baseTestCases.stream() | ||
.map(testCase -> asArray(testCase[0].toString().toLowerCase(), testCase[1])) | ||
.collect(Collectors.toList()); | ||
|
||
List<Object[]> upperCaseTestCases = baseTestCases.stream() | ||
.map(testCase -> asArray(testCase[0].toString().toUpperCase(), testCase[1])) | ||
.collect(Collectors.toList()); | ||
|
||
List<Object[]> testCases = new ArrayList<>(baseTestCases); | ||
testCases.addAll(lowerCaseTestCases); | ||
testCases.addAll(upperCaseTestCases); | ||
return testCases; | ||
} | ||
|
||
private static Object[] asArray(Object... values) { | ||
return values; | ||
} | ||
} |