-
Notifications
You must be signed in to change notification settings - Fork 107
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
Issue 534 fix date utils test #535
Merged
lognaturel
merged 31 commits into
getodk:master
from
ggalmazor:issue_534_fix_DateUtilsTest
Feb 3, 2020
Merged
Changes from all commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
c2753c2
Change test offsets to something that will fail in all systems
ggalmazor cbe9268
Fix the test by refreshing the base value we use to define expectations
ggalmazor fe64bb2
Use the same assertions for consistency
ggalmazor 6592037
Migrate test to java.time
ggalmazor 0a48fa4
Migrate assertions to something that's easier to reason about
ggalmazor 0d3538e
Move the parseTime test to its own test class
ggalmazor 2252a57
Add default zone to the list and remove unnecessary before/after methods
ggalmazor 72b6797
Migrate DateUtils.parseDateTime tests to use OffsetDateTime.parse
ggalmazor 8e22fa0
Extract the DateUtils.parseDateTime to their own test class
ggalmazor 3d934b7
Add a case without input time zone for completeness
ggalmazor c3bc3b2
Remove ignored test
ggalmazor 218d603
These members are only used in a test. Move to local
ggalmazor 9ae9636
Simplify by reusing the same test date time
ggalmazor f06f589
Simplify. XML date strings can be parsed by LocalDate
ggalmazor 7f49465
Remove indirection for easier understanding of the test
ggalmazor 9558475
Simplify by removing repetition
ggalmazor 294f5bf
Extract sanity check to its own test class
ggalmazor cfbe477
Now the test method's name is more precise and there is no indirection
ggalmazor 5a4ead2
Improve by using human readable dates and java.time parse.
ggalmazor 3df902f
Migrate formatting test to use Locales and values that depend on the JVM
ggalmazor 16f998f
Simplify by leaving only the moving parts
ggalmazor e8e9dc1
Wrap block execution to ensure it runs in a certain locale safely
ggalmazor 86a2c08
Simplify by requiring less state and making the assertion more explicit
ggalmazor 863da8d
Refactor: move creation of variable to where it's used
ggalmazor f5e0a0f
Migrate assertions to assertThat for conformity and natural reading
ggalmazor 79c2014
Extract static part from the assertions block
ggalmazor 5f2c10d
Extract test about DateUtils.format localization to its own class
ggalmazor aae6935
Simplify by replacing the wrapper class with a stream of Locale
ggalmazor e8343b4
Rename for conformity
ggalmazor 8001c81
Extract method to helper class and reuse
ggalmazor 362a664
Migrate test suite to use SystemHelper withLocale and withTimeZone
ggalmazor 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 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
51 changes: 51 additions & 0 deletions
51
src/test/java/org/javarosa/core/model/utils/test/DateUtilsFormatLocalizationTests.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,51 @@ | ||
/* | ||
* Copyright (C) 2009 JavaRosa | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy of | ||
* the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
|
||
package org.javarosa.core.model.utils.test; | ||
|
||
import static java.time.DayOfWeek.SUNDAY; | ||
import static java.time.Month.JANUARY; | ||
import static java.time.format.TextStyle.SHORT; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.javarosa.test.utils.SystemHelper.withLocale; | ||
import static org.junit.Assert.assertThat; | ||
|
||
import java.time.LocalDateTime; | ||
import java.time.OffsetDateTime; | ||
import java.util.Date; | ||
import java.util.Locale; | ||
import java.util.stream.Stream; | ||
import org.javarosa.core.model.utils.DateUtils; | ||
import org.junit.Test; | ||
|
||
public class DateUtilsFormatLocalizationTests { | ||
@Test | ||
public void format_is_localized() { | ||
// Use a Sunday in January for our test | ||
LocalDateTime localDateTime = LocalDateTime.parse("2018-01-07T10:20:30.400"); | ||
Date date = Date.from(localDateTime.toInstant(OffsetDateTime.now().getOffset())); | ||
|
||
Stream.of( | ||
Locale.ENGLISH, | ||
Locale.forLanguageTag("es-ES"), | ||
Locale.FRENCH | ||
).forEach(locale -> withLocale(locale, l -> { | ||
assertThat(DateUtils.format(date, "%b"), is(JANUARY.getDisplayName(SHORT, l))); | ||
assertThat(DateUtils.format(date, "%a"), is(SUNDAY.getDisplayName(SHORT, l))); | ||
})); | ||
} | ||
|
||
} |
61 changes: 61 additions & 0 deletions
61
src/test/java/org/javarosa/core/model/utils/test/DateUtilsFormatSanityCheckTests.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,61 @@ | ||
/* | ||
* Copyright (C) 2009 JavaRosa | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy of | ||
* the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
|
||
package org.javarosa.core.model.utils.test; | ||
|
||
import static java.util.TimeZone.getTimeZone; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.javarosa.core.model.utils.DateUtils.FORMAT_ISO8601; | ||
import static org.javarosa.core.model.utils.DateUtils.formatDateTime; | ||
import static org.javarosa.core.model.utils.DateUtils.parseDateTime; | ||
import static org.javarosa.test.utils.SystemHelper.withTimeZone; | ||
import static org.junit.Assert.assertThat; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.Date; | ||
import java.util.TimeZone; | ||
import java.util.stream.Stream; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.Parameterized; | ||
|
||
@RunWith(Parameterized.class) | ||
public class DateUtilsFormatSanityCheckTests { | ||
@Parameterized.Parameter(value = 0) | ||
public long inputTimestamp; | ||
|
||
@Parameterized.Parameters(name = "Input timestamp: {0}") | ||
public static Collection<Object[]> data() { | ||
return Arrays.asList(new Object[][]{ | ||
{1300139579000L}, | ||
{0} | ||
}); | ||
} | ||
|
||
@Test | ||
public void sanity_check_iso_format_and_parse_back() { | ||
Date input = new Date(inputTimestamp); | ||
Stream.of( | ||
TimeZone.getDefault(), | ||
getTimeZone("UTC"), | ||
getTimeZone("GMT+12"), | ||
getTimeZone("GMT-13"), | ||
getTimeZone("GMT+0230") | ||
).forEach(timeZone -> withTimeZone(timeZone, () -> | ||
assertThat(parseDateTime(formatDateTime(input, FORMAT_ISO8601)), is(input)))); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
src/test/java/org/javarosa/core/model/utils/test/DateUtilsGetXmlStringValueTest.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,44 @@ | ||
/* | ||
* Copyright (C) 2009 JavaRosa | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy of | ||
* the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
|
||
package org.javarosa.core.model.utils.test; | ||
|
||
import static org.hamcrest.Matchers.is; | ||
import static org.javarosa.core.model.utils.DateUtils.getXMLStringValue; | ||
import static org.junit.Assert.assertThat; | ||
|
||
import java.time.LocalDate; | ||
import java.time.LocalDateTime; | ||
import java.time.OffsetDateTime; | ||
import java.util.Date; | ||
import org.junit.Test; | ||
|
||
public class DateUtilsGetXmlStringValueTest { | ||
/** | ||
* This test ensures that the Strings returned | ||
* by the getXMLStringValue function are in | ||
* the proper XML compliant format, which can be | ||
* parsed by LocalDate.parse() | ||
*/ | ||
@Test | ||
public void xml_string_is_well_formatted() { | ||
LocalDateTime nowDateTime = LocalDateTime.now(); | ||
Date nowDate = Date.from(nowDateTime.toInstant(OffsetDateTime.now().getOffset())); | ||
String nowXmlFormatterDate = getXMLStringValue(nowDate); | ||
assertThat(LocalDate.parse(nowXmlFormatterDate), is(nowDateTime.toLocalDate())); | ||
} | ||
|
||
} |
Oops, something went wrong.
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.
This could probably be renamed.
DateUtilsFormatTests
would be better, I think.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.
This one was tricky to name. "Format" tells half of the story. These tests format a date and then parse it to verify we get the same value back.