Github user max3163 commented on a diff in the pull request: https://github.com/apache/jmeter/pull/291#discussion_r113760549 --- Diff: test/src/org/apache/jmeter/functions/TestTimeShiftFunction.java --- @@ -0,0 +1,140 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.apache.jmeter.functions; + +import static org.apache.jmeter.functions.FunctionTestHelper.makeParams; +import static org.exparity.hamcrest.date.LocalDateMatchers.sameDay; +import static org.exparity.hamcrest.date.LocalDateTimeMatchers.within; +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +import java.time.Instant; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.temporal.ChronoUnit; +import java.util.Collection; +import java.util.TimeZone; + +import org.apache.jmeter.engine.util.CompoundVariable; +import org.apache.jmeter.junit.JMeterTestCase; +import org.apache.jmeter.samplers.SampleResult; +import org.apache.jmeter.threads.JMeterContext; +import org.apache.jmeter.threads.JMeterContextService; +import org.apache.jmeter.threads.JMeterVariables; +import org.junit.Before; +import org.junit.Test; + +public class TestTimeShiftFunction extends JMeterTestCase { + private Function function; + + private SampleResult result; + + private JMeterVariables vars; + + private JMeterContext jmctx = null; + + private String value; + + @Before + public void setUp() { + jmctx = JMeterContextService.getContext(); + vars = new JMeterVariables(); + jmctx.setVariables(vars); + jmctx.setPreviousResult(result); + result = new SampleResult(); + function = new TimeShift(); + } + + @Test + public void testDatePlusOneDay() throws Exception { + Collection<CompoundVariable> params = makeParams("yyyy-dd-MM", "2017-01-01", "1d", ""); + function.setParameters(params); + value = function.execute(result, null); + assertThat(value, is(equalTo("2017-02-01"))); + } + + @Test + public void testDatePlusOneDayInVariable() throws Exception { + Collection<CompoundVariable> params = makeParams("yyyy-dd-MM", "2017-01-01", "1d", "VAR"); + function.setParameters(params); + function.execute(result, null); + assertThat(vars.get("VAR"), is(equalTo("2017-02-01"))); + } + + @Test + public void testDatePlusComplexPeriod() throws Exception { + Collection<CompoundVariable> params = makeParams("yyyy-dd-MM HH:m", "2017-01-01 12:00", "+1M+1d-1H-5m", "VAR"); + function.setParameters(params); + String value = function.execute(result, null); + assertThat(value, is(equalTo("2017-02-02 10:55"))); + } + + @Test + public void testDefault() throws Exception { + Collection<CompoundVariable> params = makeParams("", "", "", ""); + function.setParameters(params); + value = function.execute(result, null); + long resultat = Long.parseLong(value); + LocalDateTime nowFromFunction = LocalDateTime.ofInstant(Instant.ofEpochMilli(resultat), TimeZone + .getDefault().toZoneId()); + assertThat(nowFromFunction, within(5, ChronoUnit.SECONDS, LocalDateTime.now())); + } + + @Test + public void testNowPlusOneDay() throws Exception { + Collection<CompoundVariable> params = makeParams("YYYY-MM-dd", "", "1d", ""); + function.setParameters(params); + value = function.execute(result, null); + LocalDate tomorrow = LocalDate.now().plusDays(1); + LocalDate tomorrowFromFunction = LocalDate.parse(value); + assertThat(tomorrowFromFunction, sameDay(tomorrow)); + } + + @Test + public void testNowWithComplexPeriod() throws Exception { + Collection<CompoundVariable> params = makeParams("YYYY-MM-dd'T'HH:mm:ss", "", "+1M+1d-1H-5m5s", ""); + function.setParameters(params); + value = function.execute(result, null); + LocalDateTime futureDate = LocalDateTime.now().plusMonths(1).plusDays(1).plusHours(-1).plusMinutes(-5).plusSeconds(5); + LocalDateTime futureDateFromFunction = LocalDateTime.parse(value); --- End diff -- the test use the default one. The string must represent a valid date-time and is parsed using DateTimeFormatter.ISO_LOCAL_DATE_TIME.
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---