Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/core/org/apache/jmeter/resources/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ error_occurred=Error Occurred
error_title=Error
es=Spanish
escape_html_string=String to escape
escape_xml_string=String to escape
eval_name_param=Text containing variable and function references
evalvar_name_param=Name of variable
example_data=Sample Data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ error_occurred=Une erreur est survenue
error_title=Erreur
es=Espagnol
escape_html_string=Cha\u00EEne d'\u00E9chappement
escape_xml_string=Cha\u00EEne d'\u00E9chappement
eval_name_param=Variable contenant du texte et r\u00E9f\u00E9rences de fonctions
evalvar_name_param=Nom de variable
example_data=Exemple de donn\u00E9e
Expand Down
89 changes: 89 additions & 0 deletions src/functions/org/apache/jmeter/functions/EscapeXml.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* 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.util.Collection;
import java.util.LinkedList;
import java.util.List;

import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.jmeter.engine.util.CompoundVariable;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.samplers.Sampler;
import org.apache.jmeter.util.JMeterUtils;

/**
* <p>Function which escapes the characters in a <code>String</code> using XML 1.0 entities.</p>
*
* <p>
* For example:
* </p>
* <p><code>"bread" &amp; 'butter'</code></p>
* becomes:
* <p>
* <code>&amp;quot;bread&amp;quot; &amp;amp; &amp;apos;butter&amp;apos;</code>.
* </p>
*
*
* @see StringEscapeUtils#escapeXml10(String) (Commons Lang)
* @since 3.2
*/
public class EscapeXml extends AbstractFunction {

private static final List<String> desc = new LinkedList<String>();

private static final String KEY = "__escapeXml"; //$NON-NLS-1$

static {
desc.add(JMeterUtils.getResString("escape_xml_string")); //$NON-NLS-1$
}

private Object[] values;

public EscapeXml() {
}

/** {@inheritDoc} */
@Override
public String execute(SampleResult previousResult, Sampler currentSampler)
throws InvalidVariableException {

String rawString = ((CompoundVariable) values[0]).execute();
return StringEscapeUtils.escapeXml10(rawString);

}

/** {@inheritDoc} */
@Override
public void setParameters(Collection<CompoundVariable> parameters) throws InvalidVariableException {
checkParameterCount(parameters, 1);
values = parameters.toArray();
}

/** {@inheritDoc} */
@Override
public String getReferenceKey() {
return KEY;
}

/** {@inheritDoc} */
public List<String> getArgumentDesc() {
return desc;
}
}
37 changes: 26 additions & 11 deletions test/src/org/apache/jmeter/functions/TestSimpleFunctions.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* 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;
Expand Down Expand Up @@ -59,13 +59,13 @@ public void testUUIDParameterCount() throws Exception {
AbstractFunction function = new Uuid();
checkInvalidParameterCounts(function, 0, 0);
}

@Test
public void testThreadNumberParameterCount() throws Exception {
AbstractFunction function = new ThreadNumber();
checkInvalidParameterCounts(function, 0, 0);
}

@Test
public void testEscapeHtmlParameterCount() throws Exception {
AbstractFunction function = new EscapeHtml();
Expand All @@ -77,36 +77,42 @@ public void testUnEscapeHtmlParameterCount() throws Exception {
AbstractFunction function = new UnEscapeHtml();
checkInvalidParameterCounts(function, 1, 1);
}


@Test
public void testEscapeXmlParameterCount() throws Exception {
AbstractFunction function = new EscapeXml();
checkInvalidParameterCounts(function, 1, 1);
}

@Test
public void testUnEscapeParameterCount() throws Exception {
AbstractFunction function = new UnEscape();
checkInvalidParameterCounts(function, 1, 1);
}

@Test
public void testTestPlanParameterCount() throws Exception {
AbstractFunction function = new TestPlanName();
checkInvalidParameterCounts(function, 0, 0);
}

@Test
public void testThreadNumber() throws Exception {
AbstractFunction function = new ThreadNumber();
function.setParameters(params);
String ret = function.execute(result, null);
assertEquals("1", ret);
}


@Test
public void testUuid() throws Exception {
AbstractFunction function = new Uuid();
function.setParameters(params);
String ret = function.execute(result, null);
UUID.fromString(ret);
}

@Test
public void testEscapeHtml() throws Exception {
AbstractFunction function = new EscapeHtml();
Expand All @@ -115,7 +121,7 @@ public void testEscapeHtml() throws Exception {
String ret = function.execute(result, null);
assertEquals("&quot;bread&quot; &amp; &quot;butter&quot;", ret);
}

@Test
public void testUnEscapeHtml() throws Exception {
AbstractFunction function = new UnEscapeHtml();
Expand All @@ -124,7 +130,16 @@ public void testUnEscapeHtml() throws Exception {
String ret = function.execute(result, null);
assertEquals("\"bread\" & \"butter\"", ret);
}


@Test
public void testEscapeXml() throws Exception {
AbstractFunction function = new EscapeXml();
params.add(new CompoundVariable("\"bread\" & <'butter'>"));
function.setParameters(params);
String ret = function.execute(result, null);
assertEquals("&quot;bread&quot; &amp; &lt;&apos;butter&apos;&gt;", ret);
}

@Test
public void testTestPlanName() throws Exception {
AbstractFunction function = new TestPlanName();
Expand Down
23 changes: 23 additions & 0 deletions xdocs/usermanual/functions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ Alternatively, just use <code>/</code> instead for the path separator - e.g. <co
<tr><td>String</td><td> <a href="#__unescape">unescape</a></td><td>Process strings containing Java escapes (e.g. \n &amp; \t)</td><td>2.3.3</td></tr>
<tr><td>String</td><td> <a href="#__unescapeHtml">unescapeHtml</a></td><td>Decode HTML-encoded strings</td><td>2.3.3</td></tr>
<tr><td>String</td><td> <a href="#__escapeHtml">escapeHtml</a></td><td>Encode strings using HTML encoding</td><td>2.3.3</td></tr>
<tr><td>String</td><td> <a href="#__escapeXml">escapeXml</a></td><td>Encode strings using XMl encoding</td><td>3.2</td></tr>
<tr><td>String</td><td> <a href="#__urldecode">urldecode</a></td><td>Decode a application/x-www-form-urlencoded string</td><td>2.10</td></tr>
<tr><td>String</td><td> <a href="#__urlencode">urlencode</a></td><td>Encode a string to a application/x-www-form-urlencoded string</td><td>2.10</td></tr>
<tr><td>String</td><td> <a href="#__TestPlanName">TestPlanName</a></td><td>Return name of current test plan</td><td>2.6</td></tr>
Expand Down Expand Up @@ -1476,6 +1477,28 @@ A reference name - <code>refName</code> - for reusing the value created by this
</properties>
</component>

<component index="&sect-num;.5.33" name="__escapeXml">
<description>
<p>
Function which escapes the characters in a String using XML 1.0 entities.
</p>
<p>
For example,<code>&quot;bread&quot; &amp; &apos;butter&apos;</code>
becomes:
<code>&#38;quot;bread&#38;quot; &#38;amp; &#38;apos;butter&#38;apos;</code>.
</p>
<p>
Uses <code>StringEscapeUtils#escapeXml10(String)</code> from Commons Lang.
</p>
</description>

<properties>
<property name="String to escape" required="Yes">
The string to be escaped.
</property>
</properties>
</component>

</subsection>

<subsection name="&sect-num;.6 Pre-defined Variables" anchor="predefinedvars">
Expand Down