jmeter-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mchassagn...@apache.org
Subject svn commit: r1802968 - in /jmeter/trunk: src/core/org/apache/jmeter/resources/ src/functions/org/apache/jmeter/functions/ test/src/org/apache/jmeter/functions/ xdocs/usermanual/
Date Tue, 25 Jul 2017 15:28:24 GMT
Author: mchassagneux
Date: Tue Jul 25 15:28:24 2017
New Revision: 1802968

URL: http://svn.apache.org/viewvc?rev=1802968&view=rev
Log:
Add locale parameter to timeShift function
Bugzilla Id: 61341

Modified:
    jmeter/trunk/src/core/org/apache/jmeter/resources/messages.properties
    jmeter/trunk/src/core/org/apache/jmeter/resources/messages_fr.properties
    jmeter/trunk/src/functions/org/apache/jmeter/functions/TimeShift.java
    jmeter/trunk/test/src/org/apache/jmeter/functions/TestTimeShiftFunction.java
    jmeter/trunk/xdocs/usermanual/functions.xml

Modified: jmeter/trunk/src/core/org/apache/jmeter/resources/messages.properties
URL: http://svn.apache.org/viewvc/jmeter/trunk/src/core/org/apache/jmeter/resources/messages.properties?rev=1802968&r1=1802967&r2=1802968&view=diff
==============================================================================
--- jmeter/trunk/src/core/org/apache/jmeter/resources/messages.properties (original)
+++ jmeter/trunk/src/core/org/apache/jmeter/resources/messages.properties Tue Jul 25 15:28:24
2017
@@ -590,6 +590,7 @@ ldapext_sample_title=LDAP Extended Reque
 ldapext_testing_title=LDAP Extended Request
 library=Library
 load=Load
+locale_format=String format of a locale (ex\: fr_FR , en_EN) (optional)
 log_errors_only=Errors
 log_file=Location of log File
 log_function_comment=Additional comment (optional)

Modified: jmeter/trunk/src/core/org/apache/jmeter/resources/messages_fr.properties
URL: http://svn.apache.org/viewvc/jmeter/trunk/src/core/org/apache/jmeter/resources/messages_fr.properties?rev=1802968&r1=1802967&r2=1802968&view=diff
==============================================================================
--- jmeter/trunk/src/core/org/apache/jmeter/resources/messages_fr.properties (original)
+++ jmeter/trunk/src/core/org/apache/jmeter/resources/messages_fr.properties Tue Jul 25 15:28:24
2017
@@ -580,6 +580,7 @@ ldapext_sample_title=Requ\u00EAte LDAP \
 ldapext_testing_title=Requ\u00EAte LDAP \u00E9tendue
 library=Librairie
 load=Charger
+locale_format=Text representant la langue et le pays (ex\: fr_FR , en_EN) (optionnel)
 log_errors_only=Erreurs
 log_file=Emplacement du fichier de journal (log)
 log_function_comment=Commentaire (facultatif)

Modified: jmeter/trunk/src/functions/org/apache/jmeter/functions/TimeShift.java
URL: http://svn.apache.org/viewvc/jmeter/trunk/src/functions/org/apache/jmeter/functions/TimeShift.java?rev=1802968&r1=1802967&r2=1802968&view=diff
==============================================================================
--- jmeter/trunk/src/functions/org/apache/jmeter/functions/TimeShift.java (original)
+++ jmeter/trunk/src/functions/org/apache/jmeter/functions/TimeShift.java Tue Jul 25 15:28:24
2017
@@ -31,7 +31,9 @@ import java.time.temporal.ChronoField;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.List;
+import java.util.Locale;
 
+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;
@@ -51,8 +53,8 @@ import com.github.benmanes.caffeine.cach
  * https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html
  * (optional - defaults to epoch time in millisecond) - date to shift formated
  * as first param (optional - defaults now) - amount of (seconds, minutes,
- * hours, days ) to add (optional - default nothing is add ) -
- * variable name ( optional )
+ * hours, days ) to add (optional - default nothing is add ) - a string of the locale for
the format
+ * ( optional ) - variable name ( optional )
  *
  * Returns: a formatted date with the specified number of (seconds, minutes,
  * hours, days or months ) added. - value is also saved in the variable for
@@ -66,18 +68,57 @@ public class TimeShift extends AbstractF
     private static final String KEY = "__timeShift"; // $NON-NLS-1$
 
     private static final List<String> desc = Arrays.asList(JMeterUtils.getResString("time_format_shift"),
-            JMeterUtils.getResString("date_to_shift"), JMeterUtils.getResString("value_to_shift"),
+            JMeterUtils.getResString("date_to_shift"), JMeterUtils.getResString("value_to_shift"),
JMeterUtils.getResString("locale_format"),
             JMeterUtils.getResString("function_name_paropt"));
 
     // Ensure that these are set, even if no paramters are provided
     private String format = ""; //$NON-NLS-1$
-    private CompoundVariable dateToShiftCompound; //$NON-NLS-1$
-    private CompoundVariable amountToShiftCompound; //$NON-NLS-1$
+    private CompoundVariable dateToShiftCompound; // $NON-NLS-1$
+    private CompoundVariable amountToShiftCompound; // $NON-NLS-1$
+    private Locale locale = JMeterUtils.getLocale(); // $NON-NLS-1$
     private String variableName = ""; //$NON-NLS-1$
     private ZoneId systemDefaultZoneID = ZoneId.systemDefault();
 
+    
+    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());
+        }
+        
+        
+    }
+    
     /** Date time format cache handler **/
-    private Cache<String, DateTimeFormatter> dateTimeFormatterCache = null;
+    private Cache<LocaleFormatObject, DateTimeFormatter> dateTimeFormatterCache = null;
 
     public TimeShift() {
         super();
@@ -90,10 +131,12 @@ public class TimeShift extends AbstractF
         String amountToShift = amountToShiftCompound.execute().trim();
         String dateToShift = dateToShiftCompound.execute().trim();
         LocalDateTime localDateTimeToShift = LocalDateTime.now(systemDefaultZoneID);
+
         DateTimeFormatter formatter = null;
         if (!StringUtils.isEmpty(format)) {
             try {
-                formatter = dateTimeFormatterCache.get(format, key -> createFormatter((String)key));
+                LocaleFormatObject lfo = new LocaleFormatObject(format, locale);
+                formatter = dateTimeFormatterCache.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/time/format/DateTimeFormatter.html)",
format, ex); // $NON-NLS-1$
                 return "";
@@ -139,17 +182,18 @@ public class TimeShift extends AbstractF
         return dateString;
     }
 
-    private DateTimeFormatter createFormatter(String format) {
+    private DateTimeFormatter createFormatter(LocaleFormatObject format) {
 
         log.debug("Create a new instance of DateTimeFormatter for format '{}' in the cache",
format);
-        return new DateTimeFormatterBuilder().appendPattern(format).parseDefaulting(ChronoField.NANO_OF_SECOND,
0)
+        return new DateTimeFormatterBuilder().appendPattern(format.getFormat()).parseDefaulting(ChronoField.NANO_OF_SECOND,
0)
                 .parseDefaulting(ChronoField.MILLI_OF_SECOND, 0).parseDefaulting(ChronoField.SECOND_OF_MINUTE,
0)
                 .parseDefaulting(ChronoField.MINUTE_OF_HOUR, 0).parseDefaulting(ChronoField.HOUR_OF_DAY,
0)
                 .parseDefaulting(ChronoField.DAY_OF_MONTH, 1).parseDefaulting(ChronoField.MONTH_OF_YEAR,
1)
-                .parseDefaulting(ChronoField.YEAR_OF_ERA, Year.now().getValue()).toFormatter(JMeterUtils.getLocale());
+                .parseDefaulting(ChronoField.YEAR_OF_ERA, Year.now().getValue()).toFormatter(format.getLocale());
+
     }
 
-    protected static Cache<String, DateTimeFormatter> buildCache() {
+    protected static Cache<LocaleFormatObject, DateTimeFormatter> buildCache() {
         Caffeine<Object, Object> cacheBuilder = Caffeine.newBuilder();
         cacheBuilder.maximumSize(100);
         return cacheBuilder.build();
@@ -159,19 +203,25 @@ public class TimeShift extends AbstractF
     @Override
     public void setParameters(Collection<CompoundVariable> parameters) throws InvalidVariableException
{
 
-        checkParameterCount(parameters, 0, 4);
+        checkParameterCount(parameters, 4, 5);
         Object[] values = parameters.toArray();
 
         format = ((CompoundVariable) values[0]).execute().trim();
         dateToShiftCompound = (CompoundVariable) values[1];
         amountToShiftCompound = (CompoundVariable) values[2];
-        variableName = ((CompoundVariable) values[3]).execute().trim();
-
+        if (values.length == 4) {
+            variableName = ((CompoundVariable) values[3]).execute().trim();
+        } else {
+            String localeAsString = ((CompoundVariable) values[3]).execute().trim();
+            if (!localeAsString.equals("")) {
+                locale = LocaleUtils.toLocale(localeAsString);
+            }
+            variableName = ((CompoundVariable) values[4]).execute().trim();
+        }
         // Create the cache
         if (dateTimeFormatterCache == null) {
-            dateTimeFormatterCache =  buildCache();
+            dateTimeFormatterCache = buildCache();
         }
-
     }
 
     /** {@inheritDoc} */

Modified: jmeter/trunk/test/src/org/apache/jmeter/functions/TestTimeShiftFunction.java
URL: http://svn.apache.org/viewvc/jmeter/trunk/test/src/org/apache/jmeter/functions/TestTimeShiftFunction.java?rev=1802968&r1=1802967&r2=1802968&view=diff
==============================================================================
--- jmeter/trunk/test/src/org/apache/jmeter/functions/TestTimeShiftFunction.java (original)
+++ jmeter/trunk/test/src/org/apache/jmeter/functions/TestTimeShiftFunction.java Tue Jul 25
15:28:24 2017
@@ -158,5 +158,26 @@ public class TestTimeShiftFunction exten
         assertThat(randomFutureDate, within(5, ChronoUnit.SECONDS, checkFutureDate) );
         
     }
-
+    
+    
+    @Test
+    public void testNowPlusOneDayWithLocale() throws Exception {
+        Collection<CompoundVariable> params = makeParams("YYYY-MMMM-dd", "2017-juillet-01",
"P1D", "fr_FR", "");
+        function.setParameters(params);
+        value = function.execute(result, null);
+        assertThat(value, is(equalTo("2017-juillet-02")));
+        params = makeParams("YYYY-MMMM-dd", "2017-July-01", "P1D", "en_EN", "");
+        function.setParameters(params);
+        value = function.execute(result, null);
+        assertThat(value, is(equalTo("2017-July-02")));
+        params = makeParams("YYYY-MMMM-dd", "2017-julio-01", "P1D", "es_ES", "");
+        function.setParameters(params);
+        value = function.execute(result, null);
+        assertThat(value, is(equalTo("2017-julio-02")));
+        params = makeParams("YYYY-MMMM-dd", "2017-Juli-01", "P1D", "de_DE", "");
+        function.setParameters(params);
+        value = function.execute(result, null);
+        assertThat(value, is(equalTo("2017-Juli-02")));
+    }
+   
 }

Modified: jmeter/trunk/xdocs/usermanual/functions.xml
URL: http://svn.apache.org/viewvc/jmeter/trunk/xdocs/usermanual/functions.xml?rev=1802968&r1=1802967&r2=1802968&view=diff
==============================================================================
--- jmeter/trunk/xdocs/usermanual/functions.xml (original)
+++ jmeter/trunk/xdocs/usermanual/functions.xml Tue Jul 25 15:28:24 2017
@@ -1506,6 +1506,7 @@ becomes:
 <properties>
         <property name="Format" required="No">
         The format to be passed to DateTimeFormatter.
+        See https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html
         If omitted, the function uses milliseconds since epoch format.
         </property>
         <property name="Date to shift" required="No">
@@ -1522,6 +1523,11 @@ becomes:
         <li><code>-P6H3M</code> parses as -6 hours and -3 minutes</li>
         </ul>
         </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. ex: en_EN
+        See http://www.oracle.com/technetwork/java/javase/javase7locales-334809.html
+        If omitted, by default the function use the ApacheJMeter locale one.
+        </property>
         <property name="Name of variable" required="No">The name of the variable to
set.</property>
 </properties>
 </component>



Mime
View raw message