-
Notifications
You must be signed in to change notification settings - Fork 16
Deduplicate LocalTime
#285
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
leonardehrenfried
wants to merge
4
commits into
entur:master
Choose a base branch
from
leonardehrenfried:deduplicate-local-time
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c46f171
Deduplicate LocalTime instances
leonardehrenfried ecefd1d
Improve de-duplicating by comparing parsed objects, not input strings
leonardehrenfried 0435f50
Add test for identical time values
leonardehrenfried 065f158
Add documentation for caching local times
leonardehrenfried File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
119 changes: 119 additions & 0 deletions
119
src/test/java/org/rutebanken/util/LocalTimeISO8601XmlAdapterTest.java
This file contains hidden or 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,119 @@ | ||
| package org.rutebanken.util; | ||
|
|
||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.time.LocalTime; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| public class LocalTimeISO8601XmlAdapterTest { | ||
|
|
||
| private final LocalTimeISO8601XmlAdapter adapter = new LocalTimeISO8601XmlAdapter(); | ||
|
|
||
| @Test | ||
| public void testUnmarshalBasicTime() { | ||
| LocalTime result = adapter.unmarshal("14:30:00"); | ||
| assertEquals(LocalTime.of(14, 30, 0), result); | ||
| } | ||
|
|
||
| @Test | ||
| public void testUnmarshalTimeWithMilliseconds() { | ||
| LocalTime result = adapter.unmarshal("14:30:00.123"); | ||
| assertEquals(LocalTime.of(14, 30, 0, 123_000_000), result); | ||
| } | ||
|
|
||
| @Test | ||
| public void testUnmarshalTimeWithOffset() { | ||
| LocalTime result = adapter.unmarshal("14:30:00+02:00"); | ||
| assertEquals(LocalTime.of(14, 30, 0), result); | ||
| } | ||
|
|
||
| @Test | ||
| public void testUnmarshalTimeWithMillisecondsAndOffset() { | ||
| LocalTime result = adapter.unmarshal("14:30:00.456+01:00"); | ||
| assertEquals(LocalTime.of(14, 30, 0, 456_000_000), result); | ||
| } | ||
|
|
||
| @Test | ||
| public void testUnmarshalMidnight() { | ||
| LocalTime result = adapter.unmarshal("00:00:00"); | ||
| assertEquals(LocalTime.MIDNIGHT, result); | ||
| } | ||
|
|
||
| @Test | ||
| public void testUnmarshalNoon() { | ||
| LocalTime result = adapter.unmarshal("12:00:00"); | ||
| assertEquals(LocalTime.NOON, result); | ||
| } | ||
|
|
||
| @Test | ||
| public void testUnmarshalEndOfDay() { | ||
| LocalTime result = adapter.unmarshal("23:59:59"); | ||
| assertEquals(LocalTime.of(23, 59, 59), result); | ||
| } | ||
|
|
||
| @Test | ||
| public void testMarshalBasicTime() { | ||
| String result = adapter.marshal(LocalTime.of(14, 30, 0)); | ||
| assertEquals("14:30:00", result); | ||
| } | ||
|
|
||
| @Test | ||
| public void testMarshalTimeWithNanoseconds() { | ||
| String result = adapter.marshal(LocalTime.of(14, 30, 0, 123_000_000)); | ||
| assertEquals("14:30:00.123", result); | ||
| } | ||
|
|
||
| @Test | ||
| public void testMarshalMidnight() { | ||
| String result = adapter.marshal(LocalTime.MIDNIGHT); | ||
| assertEquals("00:00:00", result); | ||
| } | ||
|
|
||
| @Test | ||
| public void testMarshalNoon() { | ||
| String result = adapter.marshal(LocalTime.NOON); | ||
| assertEquals("12:00:00", result); | ||
| } | ||
|
|
||
| @Test | ||
| public void testMarshalNull() { | ||
| String result = adapter.marshal(null); | ||
| assertNull(result); | ||
| } | ||
|
|
||
| @Test | ||
| public void testRoundTrip() { | ||
| LocalTime original = LocalTime.of(15, 45, 30, 500_000_000); | ||
| String marshalled = adapter.marshal(original); | ||
| LocalTime unmarshalled = adapter.unmarshal(marshalled); | ||
| assertEquals(original, unmarshalled); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCaching() { | ||
| String timeString = "10:20:30"; | ||
| LocalTime first = adapter.unmarshal(timeString); | ||
| LocalTime second = adapter.unmarshal(timeString); | ||
| assertSame(first, second, "Same string should return cached instance"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCachingIdenticalValue() { | ||
| var equalInputs = List.of( | ||
| "12:20:00", | ||
| "12:20:00+02:00", | ||
| "12:20:00.000", | ||
| "12:20:00.000+02:00" | ||
| ); | ||
|
|
||
| var parsed = equalInputs.stream() | ||
| .map(adapter::unmarshal) | ||
| .collect(Collectors.toList()); | ||
| var first = parsed.get(0); | ||
|
|
||
| parsed.forEach(time -> assertSame(first, time, "Same time value should return same instance")); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add a test with different variants like: "10:20:30" vs "10:20:30+02:00"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.