Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public String execute(SampleResult previousResult, Sampler currentSampler) throw
if (localEndDate < localStartDate) {
log.error("End Date '{}' must be greater than Start Date '{}'", dateEnd, dateStart); // $NON-NLS-1$
} else {
long randomDay = ThreadLocalRandom.current().nextLong(localStartDate, localEndDate);
long randomDay = pickRandomDay(localStartDate, localEndDate);
try {
dateString = LocalDate.ofEpochDay(randomDay).format(formatter);
} catch (DateTimeParseException | NumberFormatException ex) {
Expand All @@ -200,6 +200,14 @@ public String execute(SampleResult previousResult, Sampler currentSampler) throw
return dateString;
}

private static long pickRandomDay(long startInclusive, long endExclusive) {
if (startInclusive == endExclusive) {
return startInclusive;
}
return ThreadLocalRandom.current()
.nextLong(startInclusive, endExclusive);
}

@SuppressWarnings("JavaTimeDefaultTimeZone")
private static DateTimeFormatter createFormatter(LocaleFormatObject format) {
log.debug("Create a new instance of DateTimeFormatter for format '{}' in the cache", format);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ void testDefault() throws Exception {
LocalDate result = LocalDate.parse(value, formatter);
LocalDate now = LocalDate.now(ZoneId.systemDefault());
LocalDate max = LocalDate.parse(endDate, formatter);
Assertions.assertTrue(now.isBefore(result) && result.isBefore(max));
Assertions.assertTrue(!result.isBefore(now) && result.isBefore(max));
}

@Test
Expand All @@ -92,7 +92,7 @@ void testFormatDate() throws Exception {
LocalDate result = LocalDate.parse(value, formatter);
LocalDate now = LocalDate.now(ZoneId.systemDefault());
LocalDate max = LocalDate.parse(endDate, formatter);
Assertions.assertTrue(now.isBefore(result) && result.isBefore(max));
Assertions.assertTrue(!result.isBefore(now) && result.isBefore(max));
}

@Test
Expand Down Expand Up @@ -141,6 +141,16 @@ void testEmptyFormatDate() throws Exception {
assertEquals("2111-03-29", value);
}

@Test
void testSameStartAndEndDateReturnsSameValue() throws Exception {
String date = "2111-03-29";
Collection<CompoundVariable> params = makeParams("yyyy-MM-dd", date, date, "", "MY_VAR");
function.setParameters(params);
value = function.execute(result, null);
assertEquals(date, value);
assertEquals(date, vars.get("MY_VAR"));
}

@Test
void testEndDateBeforeStartDate() throws Exception {
String startDate = "2111-03-29";
Expand Down
5 changes: 5 additions & 0 deletions xdocs/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ Summary
<li><pr>5891</pr>Skip Internet Explorer 6-9 conditional comment processing when fetching resource links</li>
</ul>

<h3>Functions</h3>
<ul>
<li><code>__RandomDate</code> now handles identical start and end dates without throwing an exception while preserving Java-like semantics (start inclusive, end exclusive).</li>
</ul>

<!-- =================== Thanks =================== -->

<ch_section>Thanks</ch_section>
Expand Down
4 changes: 2 additions & 2 deletions xdocs/usermanual/functions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,7 @@ the comma after <code>7</code> is escaped.</note>
<properties>
<property name="Time format" required="No">Format string for DateTimeFormatter (default <code>yyyy-MM-dd</code>)</property>
<property name="Start date" required="No">The start date, the default is <em>now</em></property>
<property name="End date" required="Yes">The end date</property>
<property name="End date" required="Yes">The end date (exclusive). Add one day if you need the specified date to appear in the output.</property>
<property name="Locale to use for format" required="No">
The string format of a locale. The language code must be lowercase. The country code must be uppercase. The separator must be an underscore, e.g. <code>en_EN</code>.
See <a href="http://www.oracle.com/technetwork/java/javase/javase7locales-334809.html">http://www.oracle.com/technetwork/java/javase/javase7locales-334809.html</a>.
Expand All @@ -682,7 +682,7 @@ the comma after <code>7</code> is escaped.</note>
</properties>

<p>Examples:
<source>${__RandomDate(,,2050-07-08,,)}</source> will return a random date between <em>now</em> and <code>2050-07-08</code>. For example <code>2039-06-21</code><br/>
<source>${__RandomDate(,,2050-07-08,,)}</source> will return a random date between <em>now</em> (inclusive) and <code>2050-07-08</code> (exclusive). For example <code>2039-06-21</code><br/>
<source>${__RandomDate(dd MM yyyy,,08 07 2050,,)}</source> will return a random date with a custom format like <code>04 03 2034</code><br/>
</p>
</component>
Expand Down