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 (+79 lines)
Line 0 Link Here
1
package org.apache.jmeter.functions;
2
3
import java.text.DateFormat;
4
import java.text.SimpleDateFormat;
5
import java.util.Collection;
6
import java.util.Date;
7
import java.util.LinkedList;
8
import java.util.List;
9
10
import org.apache.jmeter.engine.util.CompoundVariable;
11
import org.apache.jmeter.samplers.SampleResult;
12
import org.apache.jmeter.samplers.Sampler;
13
import org.apache.jmeter.util.JMeterUtils;
14
import org.slf4j.Logger;
15
import org.slf4j.LoggerFactory;
16
17
/**
18
 * DateConvert function to change date format Can optionally store it in a
19
 * variable.
20
 * 
21
 * @author orim
22
 *
23
 */
24
public class DateConvert extends AbstractFunction {
25
	private static final Logger log = LoggerFactory.getLogger(DateConvert.class);
26
	/**
27
	 * The algorithm names in this section can be specified when generating an
28
	 * instance of MessageDigest: MD5 SHA-1 SHA-256 SHA-384 SHA-512
29
	 */
30
	private static final List<String> desc = new LinkedList<>();
31
	private static final String KEY = "__dateConvert";
32
33
	// Number of parameters expected - used to reject invalid calls
34
	private static final int MIN_PARAMETER_COUNT = 3;
35
	private static final int MAX_PARAMETER_COUNT = 4;
36
37
	static {
38
		desc.add(JMeterUtils.getResString("date_string"));
39
		desc.add(JMeterUtils.getResString("date_format_old"));
40
		desc.add(JMeterUtils.getResString("date_format_new"));
41
		desc.add(JMeterUtils.getResString("function_name_paropt"));
42
	}
43
	private CompoundVariable[] values;
44
45
	@Override
46
	public List<String> getArgumentDesc() {
47
		return desc;
48
	}
49
50
	@Override
51
	public String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException {
52
		String dateString = values[0].execute();
53
		String oldDateFormat = values[1].execute();
54
		String newDateFormat = values[2].execute();
55
		DateFormat srcDf = new SimpleDateFormat(oldDateFormat);
56
		Date date;
57
		try {
58
			date = srcDf.parse(dateString);
59
			DateFormat destDf = new SimpleDateFormat(newDateFormat);
60
			String newDate = destDf.format(date);
61
			addVariableValue(newDate, values, 3);
62
			return newDate;
63
		} catch (Exception e) {
64
			log.error("Error calling {} function with value {}, old format {}, new format {}, ", KEY, dateString, oldDateFormat, newDateFormat, e);
65
		}
66
		return null;
67
	}
68
69
	@Override
70
	public void setParameters(Collection<CompoundVariable> parameters) throws InvalidVariableException {
71
		checkParameterCount(parameters, MIN_PARAMETER_COUNT, MAX_PARAMETER_COUNT);
72
		values = parameters.toArray(new CompoundVariable[parameters.size()]);
73
	}
74
75
	@Override
76
	public String getReferenceKey() {
77
		return KEY;
78
	}
79
}
(-)test/src/org/apache/jmeter/functions/TestDateConvert.java (+89 lines)
Line 0 Link Here
1
package org.apache.jmeter.functions;
2
3
import static org.junit.Assert.assertEquals;
4
5
import java.util.Collection;
6
import java.util.LinkedList;
7
8
import org.apache.jmeter.engine.util.CompoundVariable;
9
import org.apache.jmeter.junit.JMeterTestCase;
10
import org.apache.jmeter.samplers.SampleResult;
11
import org.apache.jmeter.threads.JMeterContext;
12
import org.apache.jmeter.threads.JMeterContextService;
13
import org.apache.jmeter.threads.JMeterVariables;
14
import org.junit.Before;
15
import org.junit.Test;
16
17
/**
18
 * Test Date Convert Function
19
 * 
20
 * @author orim
21
 *
22
 */
23
public class TestDateConvert extends JMeterTestCase {
24
25
	protected AbstractFunction dateConvert;
26
27
	private SampleResult result;
28
29
	private Collection<CompoundVariable> params;
30
31
	private JMeterVariables vars;
32
33
	private JMeterContext jmctx;
34
35
	@Before
36
	public void setUp() {
37
		dateConvert = new DateConvert();
38
		result = new SampleResult();
39
		jmctx = JMeterContextService.getContext();
40
		String data = "dummy data";
41
		result.setResponseData(data, null);
42
		vars = new JMeterVariables();
43
		jmctx.setVariables(vars);
44
		jmctx.setPreviousResult(result);
45
		params = new LinkedList<>();
46
	}
47
48
	@Test
49
	public void testParameterCount512() throws Exception {
50
		checkInvalidParameterCounts(dateConvert, 3, 4);
51
	}
52
53
	@Test
54
	public void testDateConvert() throws Exception {
55
		params.add(new CompoundVariable("2017-01-02"));
56
		params.add(new CompoundVariable("yyyy-mm-dd"));
57
		params.add(new CompoundVariable("dd-mm-yyyy"));
58
		dateConvert.setParameters(params);
59
		String returnValue = dateConvert.execute(result, null);
60
		assertEquals("02-01-2017", returnValue);
61
	}
62
63
	@Test
64
	public void testDateConvertWithVariable() throws Exception {
65
		params.add(new CompoundVariable("2017-01-02"));
66
		params.add(new CompoundVariable("yyyy-mm-dd"));
67
		params.add(new CompoundVariable("dd-mm-yyyy"));
68
		params.add(new CompoundVariable("varName"));
69
		dateConvert.setParameters(params);
70
		dateConvert.execute(result, null);
71
		assertEquals("02-01-2017", vars.get("varName"));
72
	}
73
74
	@Test(expected = InvalidVariableException.class)
75
	public void testDateConvertError() throws Exception {
76
		params.add(new CompoundVariable("2017-01-02"));
77
		params.add(new CompoundVariable("yyyy-mm-dd"));
78
		dateConvert.setParameters(params);
79
		dateConvert.execute(result, null);
80
	}
81
82
	public void testDateConvertErrorFormat() throws Exception {
83
		params.add(new CompoundVariable("2017-01-02"));
84
		params.add(new CompoundVariable("yyyy-mm-dd"));
85
		params.add(new CompoundVariable("abcd"));
86
		dateConvert.setParameters(params);
87
		assertEquals(dateConvert.execute(result, null), null);
88
	}
89
}
(-)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