jmeter-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ago...@apache.org
Subject svn commit: r1806899 - in /jmeter/trunk: src/functions/org/apache/jmeter/functions/RandomDate.java test/src/org/apache/jmeter/functions/TestTimeRandomDateFunction.java
Date Fri, 01 Sep 2017 08:49:46 GMT
Author: agomes
Date: Fri Sep  1 08:49:46 2017
New Revision: 1806899

URL: http://svn.apache.org/viewvc?rev=1806899&view=rev
Log:
Add a random date within a specific date range function : forget to commit some files

Added:
    jmeter/trunk/src/functions/org/apache/jmeter/functions/RandomDate.java
    jmeter/trunk/test/src/org/apache/jmeter/functions/TestTimeRandomDateFunction.java

Added: jmeter/trunk/src/functions/org/apache/jmeter/functions/RandomDate.java
URL: http://svn.apache.org/viewvc/jmeter/trunk/src/functions/org/apache/jmeter/functions/RandomDate.java?rev=1806899&view=auto
==============================================================================
--- jmeter/trunk/src/functions/org/apache/jmeter/functions/RandomDate.java (added)
+++ jmeter/trunk/src/functions/org/apache/jmeter/functions/RandomDate.java Fri Sep  1 08:49:46
2017
@@ -0,0 +1,243 @@
+/*
+ * 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 java.time.LocalDate;
+import java.time.Year;
+import java.time.ZoneId;
+import java.time.format.DateTimeFormatter;
+import java.time.format.DateTimeFormatterBuilder;
+import java.time.format.DateTimeParseException;
+import java.time.temporal.ChronoField;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.List;
+import java.util.Locale;
+import java.util.concurrent.ThreadLocalRandom;
+
+import org.apache.commons.lang3.LocaleUtils;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.jmeter.engine.util.CompoundVariable;
+import org.apache.jmeter.samplers.SampleResult;
+import org.apache.jmeter.samplers.Sampler;
+import org.apache.jmeter.threads.JMeterVariables;
+import org.apache.jmeter.util.JMeterUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.github.benmanes.caffeine.cache.Cache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+
+/**
+ * RandomDate Function permit to generate a date in a specific range
+ *
+ * Parameters: - Time format @see https://docs.oracle.com/javase/8/docs/api/java
+ * ime/format/DateTimeFormatter.html (optional - default yyyy-MM-dd) - Start
+ * date formated as first param (optional - defaults now) - End date - a string
+ * of the locale for the format ( optional ) - variable name ( optional )
+ *
+ * Returns: a formatted date with the specified number of (days, month, year) -
+ * value is also saved in the variable for later re-use.
+ * 
+ * @since 3.3
+ */
+
+public class RandomDate extends AbstractFunction {
+
+    private static final Logger log = LoggerFactory.getLogger(RandomDate.class);
+
+    private static final String KEY = "__RandomDate"; // $NON-NLS-1$
+
+    private static final int MIN_PARAMETER_COUNT = 1;
+
+    private static final int MAX_PARAMETER_COUNT = 5;
+
+    private static final List<String> desc = Arrays.asList(JMeterUtils.getResString("time_format_random"),
+            JMeterUtils.getResString("date_start"), JMeterUtils.getResString("date_end"),
+            JMeterUtils.getResString("locale_format"), JMeterUtils.getResString("function_name_paropt"));
+
+    // Ensure that these are set, even if no parameters are provided
+    private String format = ""; //$NON-NLS-1$
+    private Locale locale = JMeterUtils.getLocale(); // $NON-NLS-1$
+    private String variableName = ""; //$NON-NLS-1$
+    private ZoneId systemDefaultZoneID = ZoneId.systemDefault(); // $NON-NLS-1$
+    private String dateStart; // $NON-NLS-1$
+    private String dateEnd; // $NON-NLS-1$
+    private Object[] values;
+
+    private static final class LocaleFormatObject {
+
+        private String format;
+        private Locale locale;
+
+        public LocaleFormatObject(String format, Locale locale) {
+            this.format = format;
+            this.locale = locale;
+        }
+
+        public String getFormat() {
+            return format;
+        }
+
+        public Locale getLocale() {
+            return locale;
+        }
+
+        @Override
+        public int hashCode() {
+            return format.hashCode() + locale.hashCode();
+        }
+
+        @Override
+        public boolean equals(Object other) {
+            if (!(other instanceof LocaleFormatObject)) {
+                return false;
+            }
+
+            LocaleFormatObject otherError = (LocaleFormatObject) other;
+            return format.equals(otherError.getFormat())
+                    && locale.getDisplayName().equals(otherError.getLocale().getDisplayName());
+        }
+
+        /**
+         * @see java.lang.Object#toString()
+         */
+        @Override
+        public String toString() {
+            return "LocaleFormatObject [format=" + format + ", locale=" + locale + "]";
+        }
+
+    }
+
+    /** Date time format cache handler **/
+    private Cache<LocaleFormatObject, DateTimeFormatter> dateRandomFormatterCache =
null;
+
+    public RandomDate() {
+        super();
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException
{
+        long localStartDate = 0;
+        long localEndDate = 0;
+
+        DateTimeFormatter formatter = null;
+        format = ((CompoundVariable) values[0]).execute().trim();
+        String localeAsString = ((CompoundVariable) values[3]).execute().trim();
+        if (!localeAsString.trim().isEmpty()) {
+            locale = LocaleUtils.toLocale(localeAsString);
+        }
+
+        if (!StringUtils.isEmpty(format)) {
+            try {
+                LocaleFormatObject lfo = new LocaleFormatObject(format, locale);
+                formatter = dateRandomFormatterCache.get(lfo, key -> createFormatter((LocaleFormatObject)
key));
+            } catch (IllegalArgumentException ex) {
+                log.error(
+                        "Format date pattern '{}' is invalid (see https://docs.oracle.com/javase/8/docs/api/java
   ime/format/DateTimeFormatter.html)",
+                        format, ex); // $NON-NLS-1$
+                return "";
+            }
+        } else {
+            try {
+                LocaleFormatObject lfo = new LocaleFormatObject("yyyy-MM-dd", locale);
+                formatter = dateRandomFormatterCache.get(lfo, key -> createFormatter((LocaleFormatObject)
key));
+            } catch (IllegalArgumentException ex) {
+                log.error(
+                        "Format date pattern '{}' is invalid (see https://docs.oracle.com/javase/8/docs/api/java
   ime/format/DateTimeFormatter.html)",
+                        format, ex); // $NON-NLS-1$
+                return "";
+            }
+        }
+
+        dateStart = ((CompoundVariable) values[1]).execute().trim();
+        if (!dateStart.isEmpty()) {
+            try {
+                localStartDate = LocalDate.parse(dateStart, formatter).toEpochDay();
+            } catch (DateTimeParseException | NumberFormatException ex) {
+                log.error("Failed to parse the date '{}' to shift with formatter '{}'", dateStart,
formatter, ex); // $NON-NLS-1$
+            }
+        } else {
+            try {
+                localStartDate = LocalDate.now(systemDefaultZoneID).toEpochDay();
+            } catch (DateTimeParseException | NumberFormatException ex) {
+                log.error("Failed to parse the date '{}' to shift with formatter '{}'", dateStart,
formatter, ex); // $NON-NLS-1$
+            }
+        }
+
+        dateEnd = ((CompoundVariable) values[2]).execute().trim();
+        try {
+            localEndDate = LocalDate.parse(dateEnd, formatter).toEpochDay();
+        } catch (DateTimeParseException | NumberFormatException ex) {
+            log.error("Failed to parse the date '{}' to shift with formatter '{}'", dateEnd,
formatter, ex); // $NON-NLS-1$
+        }
+
+        // Generate the random date
+        String dateString = "";
+        long randomDay = ThreadLocalRandom.current().nextLong(localStartDate, localEndDate);
+        try {
+            dateString = LocalDate.ofEpochDay(randomDay).format(formatter);
+        } catch (DateTimeParseException | NumberFormatException ex) {
+            log.error("Failed to parse the date '{}' to shift with formatter '{}'", randomDay,
formatter, ex); // $NON-NLS-1$
+        }
+
+        variableName = ((CompoundVariable) values[4]).execute().trim();
+        if (!StringUtils.isEmpty(variableName)) {
+            JMeterVariables vars = getVariables();
+            if (vars != null) {// vars will be null on TestPlan
+                vars.put(variableName, dateString);
+            }
+        }
+        return dateString;
+    }
+
+    private DateTimeFormatter createFormatter(LocaleFormatObject format) {
+        log.debug("Create a new instance of DateTimeFormatter for format '{}' in the cache",
format);
+        return new DateTimeFormatterBuilder().appendPattern(format.getFormat())
+                .parseDefaulting(ChronoField.DAY_OF_MONTH, 1).parseDefaulting(ChronoField.MONTH_OF_YEAR,
1)
+                .parseDefaulting(ChronoField.YEAR_OF_ERA, Year.now().getValue()).toFormatter(format.getLocale());
+
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public void setParameters(Collection<CompoundVariable> parameters) throws InvalidVariableException
{
+
+        checkParameterCount(parameters, MIN_PARAMETER_COUNT, MAX_PARAMETER_COUNT);
+        values = parameters.toArray();
+        // Create the cache
+        if (dateRandomFormatterCache == null) {
+            dateRandomFormatterCache = Caffeine.newBuilder().maximumSize(100).build();
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public String getReferenceKey() {
+        return KEY;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public List<String> getArgumentDesc() {
+        return desc;
+    }
+
+}

Added: jmeter/trunk/test/src/org/apache/jmeter/functions/TestTimeRandomDateFunction.java
URL: http://svn.apache.org/viewvc/jmeter/trunk/test/src/org/apache/jmeter/functions/TestTimeRandomDateFunction.java?rev=1806899&view=auto
==============================================================================
--- jmeter/trunk/test/src/org/apache/jmeter/functions/TestTimeRandomDateFunction.java (added)
+++ jmeter/trunk/test/src/org/apache/jmeter/functions/TestTimeRandomDateFunction.java Fri
Sep  1 08:49:46 2017
@@ -0,0 +1,120 @@
+package org.apache.jmeter.functions;
+
+import static org.apache.jmeter.functions.FunctionTestHelper.makeParams;
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+
+import java.time.LocalDate;
+import java.time.format.DateTimeFormatter;
+import java.util.Collection;
+
+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 TestTimeRandomDateFunction extends JMeterTestCase {
+
+    private AbstractFunction 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);
+        result = new SampleResult();
+        jmctx.setPreviousResult(result);
+        function = new RandomDate();
+    }
+
+    @Test
+    public void testParameterCount() throws Exception {
+        checkInvalidParameterCounts(function, 1, 5);
+    }
+
+    @Test
+    public 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 max = LocalDate.parse(EndDate, formatter);
+        assertTrue(now.isBefore(result) && result.isBefore(max));
+    }
+
+    @Test
+    public 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());
+    }
+
+    @Test
+    public 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 max = LocalDate.parse(EndDate, formatter);
+        assertTrue(now.isBefore(result) && result.isBefore(max));
+    }
+
+    @Test
+    public 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());
+    }
+
+    @Test
+    public void testFormatDate3() throws Exception {
+        String StartDate = "29 Aug 2111";
+        String EndDate = "30 Aug 2111";
+        String FormatDate = "dd MMM yyyy";
+        String localeAsString = "en_EN";
+        Collection<CompoundVariable> params = makeParams(FormatDate, StartDate, EndDate,
localeAsString, "");
+        function.setParameters(params);
+        value = function.execute(result, null);
+        assertThat(value, is(equalTo("29 Aug 2111")));
+    }
+
+    @Test
+    public void testFrenchFormatDate() throws Exception {
+        String StartDate = "29 mars 2111";
+        String EndDate = "30 mars 2111";
+        String FormatDate = "dd MMM yyyy";
+        String localeAsString = "fr_FR";
+        Collection<CompoundVariable> params = makeParams(FormatDate, StartDate, EndDate,
localeAsString, "");
+        function.setParameters(params);
+        value = function.execute(result, null);
+        assertThat(value, is(equalTo("29 mars 2111")));
+    }
+}



Mime
View raw message