Skip to content

TRUNK-6557: Add unit test for OpenmrsDateFormat 2-digit-year rejection#6123

Open
KrimsonSun wants to merge 1 commit into
openmrs:masterfrom
KrimsonSun:TRUNK-6557-openmrs-date-format-tests
Open

TRUNK-6557: Add unit test for OpenmrsDateFormat 2-digit-year rejection#6123
KrimsonSun wants to merge 1 commit into
openmrs:masterfrom
KrimsonSun:TRUNK-6557-openmrs-date-format-tests

Conversation

@KrimsonSun
Copy link
Copy Markdown

Description of what I changed

Adds OpenmrsDateFormatTest with a single focused unit test for the central
correctness guarantee of OpenmrsDateFormat.

OpenmrsDateFormat is a strict-mode wrapper around SimpleDateFormat whose
reason for existing is to reject two-digit years when the pattern explicitly
asks for four. Under plain SimpleDateFormat, parsing "12/25/25" against
pattern "MM/dd/yyyy" silently produces the year 25 AD — the kind of
clinical-timestamp bug that only surfaces months later in reports. Until now
that guarantee was not pinned down by any test, so a future refactor of the
regex check in parse() could weaken the contract unnoticed.

The new test asserts both the rejection (a ParseException is thrown) and
the human-facing error message (must contain "year must have 4 digits"),
since downstream UIs may rely on that wording to explain the rejection to
a user typing a short-form date.

No production code is changed.

Verification

./mvnw -pl api test -Dtest=OpenmrsDateFormatTest \
       -Dsurefire.failIfNoSpecifiedTests=false
# [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
# [INFO] BUILD SUCCESS

Issue I worked on

see https://openmrs.atlassian.net/browse/TRUNK-6557

Checklist: I completed these to help reviewers :)

  • My IDE is configured to follow the code style of this project.
  • I have added tests to cover my changes.
  • I ran mvn clean package right before creating this pull request and added all formatting changes to my commit.
  • All new and existing tests passed.
  • My pull request is based on the latest changes of the master branch.

OpenmrsDateFormat is a strict-mode wrapper around SimpleDateFormat whose
central reason for existing is to reject two-digit years when the pattern
explicitly asks for four. Under plain SimpleDateFormat, parsing "12/25/25"
against pattern "MM/dd/yyyy" silently produces the year 25 AD — the kind
of clinical-timestamp bug that only surfaces months later in reports.

This adds OpenmrsDateFormatTest with a single focused test that pins down
that guarantee: parse() must throw ParseException with a message that
calls out the 4-digit-year requirement. Asserting the message text guards
both the rejection itself and the human-facing wording a downstream UI
may show to a user.

No production code is changed.
@sonarqubecloud
Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant