Skip to content

Commit

Permalink
Release 2.19.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Jörg Flade committed Feb 16, 2025
1 parent 76fa418 commit cb1ad76
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ class ValidDateContextMatcher(
bddDateTimeFormats = dateTimeFormatCollection
)
if (jsonDate != null) {
return jsonDate.toLocalDate().format(DateTimeFormatter.ISO_LOCAL_DATE).equals(parameterFromContext)
return jsonDate.toLocalDate()
.format(DateTimeFormatter.ISO_LOCAL_DATE) == parameterFromContext
}
return false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ class ValidDateMatcher(
private val dateTimeFormatCollection: Collection<BddCucumberDateTimeFormat>
) : BaseMatcher<Any>() {
override fun matches(item: Any): Boolean {
return DateUtils.isValidMandatoryDate(dateObject = item, bddDateTimeFormats = dateTimeFormatCollection)
return DateUtils.isValidMandatoryDate(
dateObject = item,
bddDateTimeFormats = dateTimeFormatCollection
)
}

override fun describeMismatch(item: Any, description: Description) {
Expand Down
72 changes: 39 additions & 33 deletions src/main/kotlin/com/ragin/bdd/cucumber/utils/DateUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -72,44 +72,50 @@ object DateUtils {
bddDateTimeFormats: Collection<BddCucumberDateTimeFormat>
): LocalDateTime? {
var localDateTime: LocalDateTime? = null
if (dateObject is Long) {
// Read Long (timestamp) with LocalDateTime to ensure that it is a valid date
return LocalDateTime.of(
Instant.ofEpochMilli(dateObject).atZone(ZoneId.of("Europe/Berlin")).toLocalDate(),
LocalTime.MIN
)
} else if (dateObject is BigDecimal) {
// Read BigDecimal (e.g. yyyyddmm) with LocalDateTime to ensure that it is a valid date
return LocalDateTime.of(
Instant.ofEpochMilli(dateObject.toLong()).atZone(ZoneId.of("Europe/Berlin")).toLocalDate(),
LocalTime.MIN
)
} else if (dateObject is String) {
// Parse string with date to ensure that it is a valid date
// first, check, that it not contains sth. like "null"
if ("null".equals(dateObject.toString(), ignoreCase = true)) {
return null
when (dateObject) {
is Long -> {
// Read Long (timestamp) with LocalDateTime to ensure that it is a valid date
return LocalDateTime.of(
Instant.ofEpochMilli(dateObject).atZone(ZoneId.of("Europe/Berlin")).toLocalDate(),
LocalTime.MIN
)
}

// check known date formats if one fits to the object
for (formatter in createDateFormatters(bddDateTimeFormats = bddDateTimeFormats)) {
log.debug { "Try to parse $dateObject with the format $formatter" }
localDateTime = parseDate(date = dateObject.toString(), formatter = formatter)
is BigDecimal -> {
// Read BigDecimal (e.g. yyyyddmm) with LocalDateTime to ensure that it is a valid date
return LocalDateTime.of(
Instant.ofEpochMilli(dateObject.toLong()).atZone(ZoneId.of("Europe/Berlin")).toLocalDate(),
LocalTime.MIN
)
}

// if parsing date was null, parse String as dateTime
if (null == localDateTime) {
localDateTime = parseDateTime(dateTime = dateObject.toString(), formatter = formatter)
is String -> {
// Parse string with date to ensure that it is a valid date
// first, check, that it not contains sth. like "null"
if ("null".equals(dateObject.toString(), ignoreCase = true)) {
return null
}

// if it is not null the String was parsed and is valid!
if (null != localDateTime) {
break
// check known date formats if one fits to the object
for (formatter in createDateFormatters(bddDateTimeFormats = bddDateTimeFormats)) {
log.debug { "Try to parse $dateObject with the format $formatter" }
localDateTime = parseDate(date = dateObject.toString(), formatter = formatter)

// if parsing date was null, parse String as dateTime
if (null == localDateTime) {
localDateTime = parseDateTime(dateTime = dateObject.toString(), formatter = formatter)
}

// if it is not null the String was parsed and is valid!
if (null != localDateTime) {
break
}
}
}

// seems not to be a valid date
if (null == localDateTime) {
throw DateTimeParseException("No parser for $dateObject", dateObject.toString(), 0)
// seems not to be a valid date
if (null == localDateTime) {
throw DateTimeParseException("No parser for $dateObject", dateObject.toString(), 0)
}
}
}
return localDateTime
Expand All @@ -125,7 +131,7 @@ object DateUtils {
private fun parseDate(date: String, formatter: DateTimeFormatter): LocalDateTime? {
return try {
LocalDateTime.of(LocalDate.parse(date, formatter), LocalTime.MIN)
} catch (e: DateTimeParseException) {
} catch (_: DateTimeParseException) {
log.debug { "Failed to parse as date $date with the format $formatter" }
null
}
Expand All @@ -141,7 +147,7 @@ object DateUtils {
private fun parseDateTime(dateTime: String, formatter: DateTimeFormatter): LocalDateTime? {
return try {
LocalDateTime.parse(dateTime, formatter)
} catch (e: DateTimeParseException) {
} catch (_: DateTimeParseException) {
log.debug { "Failed to parse as dateTime $dateTime with the format $formatter" }
null
}
Expand Down

0 comments on commit cb1ad76

Please sign in to comment.