Skip to content

Commit

Permalink
Use newer time API to make error prone happy
Browse files Browse the repository at this point in the history
  • Loading branch information
FSchumacher committed Apr 18, 2022
1 parent cf4ae32 commit 9fb66c4
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 70 deletions.
Expand Up @@ -20,7 +20,7 @@
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.util.Date;
import java.time.Instant;

import org.apache.jmeter.control.LoopController;
import org.apache.jmeter.samplers.AbstractSampler;
Expand Down Expand Up @@ -156,9 +156,9 @@ void testBug63490EndTestWhenDelayIsTooLongForScheduler() {
jMeterThread.setScheduled(true);
jMeterThread.setEndTime(System.currentTimeMillis() + maxDuration);
jMeterThread.setThreadGroup(threadGroup);
long startTime = new Date().getTime();
Instant startTime = Instant.now();
jMeterThread.run();
long duration = new Date().getTime() - startTime;
long duration = Instant.now().toEpochMilli() - startTime.toEpochMilli();

assertFalse("Sampler should not be called", dummySampler.isCalled());

Expand Down
Expand Up @@ -21,10 +21,9 @@
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Collection;

Expand All @@ -34,6 +33,7 @@
import org.apache.jmeter.threads.JMeterContext;
import org.apache.jmeter.threads.JMeterContextService;
import org.apache.jmeter.threads.JMeterVariables;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand All @@ -46,7 +46,7 @@ public class TestTimeRandomDateFunction extends JMeterTestCase {
private String value;

@BeforeEach
public void setUp() {
void setUp() {
jmctx = JMeterContextService.getContext();
vars = new JMeterVariables();
jmctx.setVariables(vars);
Expand All @@ -56,59 +56,59 @@ public void setUp() {
}

@Test
public void testParameterCount() throws Exception {
void testParameterCount() throws Exception {
checkInvalidParameterCounts(function, 3, 5);
}

@Test
public void testDefault() throws Exception {
void testDefault() throws Exception {
String endDate = "2099-01-01";
String formatDate = "yyyy-dd-MM";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(formatDate);
Collection<CompoundVariable> params = makeParams(formatDate, "", endDate, "", "");
function.setParameters(params);
value = function.execute(result, null);
LocalDate result = LocalDate.parse(value, formatter);
LocalDate now = LocalDate.now();
LocalDate now = LocalDate.now(ZoneId.systemDefault());
LocalDate max = LocalDate.parse(endDate, formatter);
assertTrue(now.isBefore(result) && result.isBefore(max));
Assertions.assertTrue(now.isBefore(result) && result.isBefore(max));
}

@Test
public void testDefault2() throws Exception {
void testDefault2() throws Exception {
String endDate = "2099-01-01";
Collection<CompoundVariable> params = makeParams("yyyy-dd-MM", "", endDate, "", "");
function.setParameters(params);
value = function.execute(result, null);
assertEquals(10, value.length());
Assertions.assertEquals(10, value.length());
}

@Test
public void testFormatDate() throws Exception {
void testFormatDate() throws Exception {
String endDate = "01 01 2099";
String formatDate = "dd MM yyyy";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(formatDate);
Collection<CompoundVariable> params = makeParams(formatDate, "", endDate, "", "");
function.setParameters(params);
value = function.execute(result, null);
LocalDate result = LocalDate.parse(value, formatter);
LocalDate now = LocalDate.now();
LocalDate now = LocalDate.now(ZoneId.systemDefault());
LocalDate max = LocalDate.parse(endDate, formatter);
assertTrue(now.isBefore(result) && result.isBefore(max));
Assertions.assertTrue(now.isBefore(result) && result.isBefore(max));
}

@Test
public void testFormatDate2() throws Exception {
void testFormatDate2() throws Exception {
String endDate = "01012099";
String formatDate = "ddMMyyyy";
Collection<CompoundVariable> params = makeParams(formatDate, "", endDate, "", "");
function.setParameters(params);
value = function.execute(result, null);
assertEquals(8, value.length());
Assertions.assertEquals(8, value.length());
}

@Test
public void testFormatDate3() throws Exception {
void testFormatDate3() throws Exception {
String startDate = "29 Aug 2111";
String endDate = "30 Aug 2111";
String formatDate = "dd MMM yyyy";
Expand All @@ -120,7 +120,7 @@ public void testFormatDate3() throws Exception {
}

@Test
public void testFrenchFormatDate() throws Exception {
void testFrenchFormatDate() throws Exception {
String startDate = "29 mars 2111";
String endDate = "30 mars 2111";
String formatDate = "dd MMM yyyy";
Expand All @@ -132,7 +132,7 @@ public void testFrenchFormatDate() throws Exception {
}

@Test
public void testEmptyFormatDate() throws Exception {
void testEmptyFormatDate() throws Exception {
String startDate = "2111-03-29";
String endDate = "2111-03-30";
String formatDate = "";
Expand All @@ -144,7 +144,7 @@ public void testEmptyFormatDate() throws Exception {
}

@Test
public void testEndDateBeforeStartDate() throws Exception {
void testEndDateBeforeStartDate() throws Exception {
String startDate = "2111-03-29";
String endDate = "2011-03-30";
String formatDate = "";
Expand All @@ -156,7 +156,7 @@ public void testEndDateBeforeStartDate() throws Exception {
}

@Test
public void testEndDateBeforeStartDateNullVariable() throws Exception {
void testEndDateBeforeStartDateNullVariable() throws Exception {
String startDate = "2111-03-29";
String endDate = "2111-03-30";
String formatDate = "";
Expand All @@ -168,7 +168,7 @@ public void testEndDateBeforeStartDateNullVariable() throws Exception {
}

@Test
public void testEndDateBeforeStartDateWithVariable() throws Exception {
void testEndDateBeforeStartDateWithVariable() throws Exception {
String startDate = "2111-03-29";
String endDate = "2111-03-30";
String formatDate = "";
Expand All @@ -181,7 +181,7 @@ public void testEndDateBeforeStartDateWithVariable() throws Exception {
}

@Test
public void testInvalidFormat() throws Exception {
void testInvalidFormat() throws Exception {
String startDate = "2111-03-29";
String endDate = "2011-03-30";
String formatDate = "abcd";
Expand All @@ -193,7 +193,7 @@ public void testInvalidFormat() throws Exception {
}

@Test
public void testInvalidStartDateFormat() throws Exception {
void testInvalidStartDateFormat() throws Exception {
String startDate = "23-2111-03";
String endDate = "2011-03-30";
String formatDate = "abcd";
Expand All @@ -205,7 +205,7 @@ public void testInvalidStartDateFormat() throws Exception {
}

@Test
public void testInvalidEndDateFormat() throws Exception {
void testInvalidEndDateFormat() throws Exception {
String startDate = "2011-03-30";
String endDate = "23-2111-03";
String formatDate = "abcd";
Expand Down
Expand Up @@ -98,15 +98,15 @@ void testDefault() throws Exception {
long resultat = Long.parseLong(value);
LocalDateTime nowFromFunction = LocalDateTime.ofInstant(Instant.ofEpochMilli(resultat),
TimeZone.getDefault().toZoneId());
assertThat(nowFromFunction, within(5, ChronoUnit.SECONDS, LocalDateTime.now()));
assertThat(nowFromFunction, within(5, ChronoUnit.SECONDS, LocalDateTime.now(ZoneId.systemDefault())));
}

@Test
void testNowPlusOneDay() throws Exception {
Collection<CompoundVariable> params = makeParams("yyyy-MM-dd", "", "P1d", "");
function.setParameters(params);
value = function.execute(result, null);
LocalDate tomorrow = LocalDate.now().plusDays(1);
LocalDate tomorrow = LocalDate.now(ZoneId.systemDefault()).plusDays(1);
LocalDate tomorrowFromFunction = LocalDate.parse(value);
assertThat(tomorrowFromFunction, sameDay(tomorrow));
}
Expand All @@ -120,16 +120,18 @@ void testNowWithComplexPeriod() throws Exception {
Collection<CompoundVariable> params = makeParams("yyyy-MM-dd'T'HH:mm:ss", "", "P10DT-1H-5M5S", "");
function.setParameters(params);
value = function.execute(result, null);
LocalDateTime futureDate = LocalDateTime.now().plusDays(10).plusHours(-1).plusMinutes(-5).plusSeconds(5);
LocalDateTime futureDate = LocalDateTime.now(ZoneId.systemDefault())
.plusDays(10).plusHours(-1).plusMinutes(-5).plusSeconds(5);
LocalDateTime futureDateFromFunction = LocalDateTime.parse(value);
assertThat(futureDateFromFunction, within(1, ChronoUnit.SECONDS, futureDate));
}

private BooleanSupplier dstChangeAhead(String duration) {
private static BooleanSupplier dstChangeAhead(String duration) {
return () -> {
ZoneId defaultZoneId = ZoneId.systemDefault();
Instant now = LocalDateTime.now().atZone(defaultZoneId).toInstant();
Instant then = LocalDateTime.now().plus(Duration.parse(duration)).atZone(defaultZoneId).toInstant();
Instant now = LocalDateTime.now(defaultZoneId).atZone(defaultZoneId).toInstant();
Instant then = LocalDateTime.now(defaultZoneId).plus(Duration.parse(duration))
.atZone(defaultZoneId).toInstant();
ZoneRules rules = defaultZoneId.getRules();
Duration nowDST = rules.getDaylightSavings(now);
Duration thenDST = rules.getDaylightSavings(then);
Expand Down Expand Up @@ -180,7 +182,7 @@ void testWrongAmountToAdd() throws Exception {
long resultat = Long.parseLong(value);
LocalDateTime nowFromFunction = LocalDateTime.ofInstant(Instant.ofEpochMilli(resultat),
TimeZone.getDefault().toZoneId());
assertThat(nowFromFunction, within(5, ChronoUnit.SECONDS, LocalDateTime.now()));
assertThat(nowFromFunction, within(5, ChronoUnit.SECONDS, LocalDateTime.now(ZoneId.systemDefault())));
}

@Test
Expand All @@ -200,13 +202,13 @@ void testRandomPeriod() throws Exception {
function.setParameters(params);
value = function.execute(result, null);
LocalDateTime randomFutureDate = LocalDateTime.parse(value);
LocalDateTime checkFutureDate = LocalDateTime.now().plusMinutes(randomInt);
LocalDateTime checkFutureDate = LocalDateTime.now(ZoneId.systemDefault()).plusMinutes(randomInt);
assertThat(randomFutureDate, within(5, ChronoUnit.SECONDS, checkFutureDate));
randomInt = r.ints(1, 60).limit(1).findFirst().getAsInt();
vars.put("random", String.valueOf(randomInt));
value = function.execute(result, null);
randomFutureDate = LocalDateTime.parse(value);
checkFutureDate = LocalDateTime.now().plusMinutes(randomInt);
checkFutureDate = LocalDateTime.now(ZoneId.systemDefault()).plusMinutes(randomInt);
assertThat(randomFutureDate, within(5, ChronoUnit.SECONDS, checkFutureDate));
}

Expand Down

0 comments on commit 9fb66c4

Please sign in to comment.