View | Details | Raw Unified | Return to bug 61735
Collapse All | Expand All

(-)src/core/org/apache/jmeter/resources/messages.properties (+3 lines)
Lines 250-256 Link Here
250
database_url=JDBC URL\:
250
database_url=JDBC URL\:
251
database_url_jdbc_props=Database URL and JDBC Driver
251
database_url_jdbc_props=Database URL and JDBC Driver
252
date_end=End date
252
date_end=End date
253
date_format_old=Current date format
254
date_format_new=New date format
253
date_start=Start date (optional) (default: now)
255
date_start=Start date (optional) (default: now)
256
date_string=Date string
254
date_to_shift=Date to shift (optional) (default \: now )
257
date_to_shift=Date to shift (optional) (default \: now )
255
ddn=DN
258
ddn=DN
256
de=German
259
de=German
(-)src/functions/org/apache/jmeter/functions/DateConvert.java (+97 lines)
Line 0 Link Here
1
/*
2
 * Licensed to the Apache Software Foundation (ASF) under one or more
3
 * contributor license agreements.  See the NOTICE file distributed with
4
 * this work for additional information regarding copyright ownership.
5
 * The ASF licenses this file to You under the Apache License, Version 2.0
6
 * (the "License"); you may not use this file except in compliance with
7
 * the License.  You may obtain a copy of the License at
8
 *
9
 *   http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 *
17
 */
18
19
package org.apache.jmeter.functions;
20
21
import java.text.DateFormat;
22
import java.text.SimpleDateFormat;
23
import java.util.Collection;
24
import java.util.Date;
25
import java.util.LinkedList;
26
import java.util.List;
27
28
import org.apache.jmeter.engine.util.CompoundVariable;
29
import org.apache.jmeter.samplers.SampleResult;
30
import org.apache.jmeter.samplers.Sampler;
31
import org.apache.jmeter.util.JMeterUtils;
32
import org.slf4j.Logger;
33
import org.slf4j.LoggerFactory;
34
35
/**
36
 * DateConvert function to change date format Can optionally store it in a
37
 * variable.
38
 * 
39
 * @author orim
40
 *
41
 */
42
public class DateConvert extends AbstractFunction {
43
	private static final Logger log = LoggerFactory.getLogger(DateConvert.class);
44
	/**
45
	 * The algorithm names in this section can be specified when generating an
46
	 * instance of MessageDigest: MD5 SHA-1 SHA-256 SHA-384 SHA-512
47
	 */
48
	private static final List<String> desc = new LinkedList<>();
49
	private static final String KEY = "__dateConvert";
50
51
	// Number of parameters expected - used to reject invalid calls
52
	private static final int MIN_PARAMETER_COUNT = 3;
53
	private static final int MAX_PARAMETER_COUNT = 4;
54
55
	static {
56
		desc.add(JMeterUtils.getResString("date_string"));
57
		desc.add(JMeterUtils.getResString("date_format_old"));
58
		desc.add(JMeterUtils.getResString("date_format_new"));
59
		desc.add(JMeterUtils.getResString("function_name_paropt"));
60
	}
61
	private CompoundVariable[] values;
62
63
	@Override
64
	public List<String> getArgumentDesc() {
65
		return desc;
66
	}
67
68
	@Override
69
	public String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException {
70
		String dateString = values[0].execute();
71
		String oldDateFormat = values[1].execute();
72
		String newDateFormat = values[2].execute();
73
		DateFormat srcDf = new SimpleDateFormat(oldDateFormat);
74
		Date date;
75
		try {
76
			date = srcDf.parse(dateString);
77
			DateFormat destDf = new SimpleDateFormat(newDateFormat);
78
			String newDate = destDf.format(date);
79
			addVariableValue(newDate, values, 3);
80
			return newDate;
81
		} catch (Exception e) {
82
			log.error("Error calling {} function with value {}, old format {}, new format {}, ", KEY, dateString, oldDateFormat, newDateFormat, e);
83
		}
84
		return null;
85
	}
86
87
	@Override
88
	public void setParameters(Collection<CompoundVariable> parameters) throws InvalidVariableException {
89
		checkParameterCount(parameters, MIN_PARAMETER_COUNT, MAX_PARAMETER_COUNT);
90
		values = parameters.toArray(new CompoundVariable[parameters.size()]);
91
	}
92
93
	@Override
94
	public String getReferenceKey() {
95
		return KEY;
96
	}
97
}
(-)test/src/org/apache/jmeter/functions/TestDateConvert.java (+107 lines)
Line 0 Link Here
1
/*
2
 * Licensed to the Apache Software Foundation (ASF) under one or more
3
 * contributor license agreements.  See the NOTICE file distributed with
4
 * this work for additional information regarding copyright ownership.
5
 * The ASF licenses this file to You under the Apache License, Version 2.0
6
 * (the "License"); you may not use this file except in compliance with
7
 * the License.  You may obtain a copy of the License at
8
 *
9
 *   http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 *
17
 */
18
19
package org.apache.jmeter.functions;
20
21
import static org.junit.Assert.assertEquals;
22
23
import java.util.Collection;
24
import java.util.LinkedList;
25
26
import org.apache.jmeter.engine.util.CompoundVariable;
27
import org.apache.jmeter.junit.JMeterTestCase;
28
import org.apache.jmeter.samplers.SampleResult;
29
import org.apache.jmeter.threads.JMeterContext;
30
import org.apache.jmeter.threads.JMeterContextService;
31
import org.apache.jmeter.threads.JMeterVariables;
32
import org.junit.Before;
33
import org.junit.Test;
34
35
/**
36
 * Test Date Convert Function
37
 * 
38
 * @author orim
39
 *
40
 */
41
public class TestDateConvert extends JMeterTestCase {
42
43
	protected AbstractFunction dateConvert;
44
45
	private SampleResult result;
46
47
	private Collection<CompoundVariable> params;
48
49
	private JMeterVariables vars;
50
51
	private JMeterContext jmctx;
52
53
	@Before
54
	public void setUp() {
55
		dateConvert = new DateConvert();
56
		result = new SampleResult();
57
		jmctx = JMeterContextService.getContext();
58
		String data = "dummy data";
59
		result.setResponseData(data, null);
60
		vars = new JMeterVariables();
61
		jmctx.setVariables(vars);
62
		jmctx.setPreviousResult(result);
63
		params = new LinkedList<>();
64
	}
65
66
	@Test
67
	public void testParameterCount512() throws Exception {
68
		checkInvalidParameterCounts(dateConvert, 3, 4);
69
	}
70
71
	@Test
72
	public void testDateConvert() throws Exception {
73
		params.add(new CompoundVariable("2017-01-02"));
74
		params.add(new CompoundVariable("yyyy-mm-dd"));
75
		params.add(new CompoundVariable("dd-mm-yyyy"));
76
		dateConvert.setParameters(params);
77
		String returnValue = dateConvert.execute(result, null);
78
		assertEquals("02-01-2017", returnValue);
79
	}
80
81
	@Test
82
	public void testDateConvertWithVariable() throws Exception {
83
		params.add(new CompoundVariable("2017-01-02"));
84
		params.add(new CompoundVariable("yyyy-mm-dd"));
85
		params.add(new CompoundVariable("dd-mm-yyyy"));
86
		params.add(new CompoundVariable("varName"));
87
		dateConvert.setParameters(params);
88
		dateConvert.execute(result, null);
89
		assertEquals("02-01-2017", vars.get("varName"));
90
	}
91
92
	@Test(expected = InvalidVariableException.class)
93
	public void testDateConvertError() throws Exception {
94
		params.add(new CompoundVariable("2017-01-02"));
95
		params.add(new CompoundVariable("yyyy-mm-dd"));
96
		dateConvert.setParameters(params);
97
		dateConvert.execute(result, null);
98
	}
99
100
	public void testDateConvertErrorFormat() throws Exception {
101
		params.add(new CompoundVariable("2017-01-02"));
102
		params.add(new CompoundVariable("yyyy-mm-dd"));
103
		params.add(new CompoundVariable("abcd"));
104
		dateConvert.setParameters(params);
105
		assertEquals(dateConvert.execute(result, null), null);
106
	}
107
}
(-)xdocs/changes.xml (+1 lines)
Lines 124-129 Link Here
124
    <li><bug>61738</bug>Function Helper Dialog : Add Copy in Generate and clarify labels</li>
124
    <li><bug>61738</bug>Function Helper Dialog : Add Copy in Generate and clarify labels</li>
125
    <li><bug>61593</bug>Remove Detail, Add, Add from Clipboard, Delete buttons in Function Helper GUI</li>
125
    <li><bug>61593</bug>Remove Detail, Add, Add from Clipboard, Delete buttons in Function Helper GUI</li>
126
    <li><bug>61724</bug>Add <code>__digest</code> function to provide computing of Hashes (SHA-XXX, MDX). Based on a contribution by orimarko at gmail.com</li>
126
    <li><bug>61724</bug>Add <code>__digest</code> function to provide computing of Hashes (SHA-XXX, MDX). Based on a contribution by orimarko at gmail.com</li>
127
    <li><bug>61735</bug>Add <code>__dateConvert</code> function to provide date formats conversions. Based on a contribution by orimarko at gmail.com</li>
127
</ul>
128
</ul>
128
129
129
<h3>I18N</h3>
130
<h3>I18N</h3>
(-)xdocs/usermanual/functions.xml (+15 lines)
Lines 1599-1604 Link Here
1599
            the variable to set.</property>
1599
            the variable to set.</property>
1600
    </properties>
1600
    </properties>
1601
</component>
1601
</component>
1602
<component index="&sect-num;.5.35" name="__dateConvert">
1603
    <description>
1604
        <p>The dateConvert function returns a date in a new format
1605
            with the optional variable name.</p>
1606
    </description>
1607
    <properties>
1608
        <property name="Date String" required="Yes">
1609
            The original date string to be used to convert
1610
        </property>
1611
        <property name="Old Date Format" required="Yes"> The original date format</property>
1612
        <property name="New Date Format" required="Yes"> The new date format</property>
1613
        <property name="Name of variable" required="No">The name of
1614
            the variable to set.</property>
1615
    </properties>
1616
</component>
1602
</subsection>
1617
</subsection>
1603
1618
1604
<subsection name="&sect-num;.6 Pre-defined Variables" anchor="predefinedvars">
1619
<subsection name="&sect-num;.6 Pre-defined Variables" anchor="predefinedvars">

Return to bug 61735