You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jmeter.apache.org by pm...@apache.org on 2019/07/24 10:59:38 UTC
[jmeter] branch master updated: Better handling of linebreak in
StringToFile (#478)
This is an automated email from the ASF dual-hosted git repository.
pmouawad pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/jmeter.git
The following commit(s) were added to refs/heads/master by this push:
new dcf63aa Better handling of linebreak in StringToFile (#478)
dcf63aa is described below
commit dcf63aaeb9b3627c953837de6ea3fb75c8991aa9
Author: UBIK LOAD PACK <su...@ubikloadpack.com>
AuthorDate: Wed Jul 24 12:59:32 2019 +0200
Better handling of linebreak in StringToFile (#478)
Allow using \n for line break.
This closes Bug 63219
https://bz.apache.org/bugzilla/show_bug.cgi?id=63219
Contributed by UbikLoadPack Team
---
.../org/apache/jmeter/functions/StringToFile.java | 26 +++++++++++--------
.../apache/jmeter/functions/TestStringtoFile.java | 29 ++++++++++++++--------
xdocs/usermanual/functions.xml | 10 +++++---
3 files changed, 41 insertions(+), 24 deletions(-)
diff --git a/src/functions/org/apache/jmeter/functions/StringToFile.java b/src/functions/org/apache/jmeter/functions/StringToFile.java
index ff78a5f..3b17c8a 100644
--- a/src/functions/org/apache/jmeter/functions/StringToFile.java
+++ b/src/functions/org/apache/jmeter/functions/StringToFile.java
@@ -29,6 +29,7 @@ import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
+import java.util.regex.Pattern;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
@@ -40,13 +41,15 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
- * FileToString Function to read a complete file into a String.
- *
- * Parameters:
- * - file name
- * - append (true/false)
- * - file encoding (optional)
+ * StringToFile Function to write a String to a file
*
+ * Parameters:
+ * <ul>
+ * <li>file name</li>
+ * <li>content</li>
+ * <li>append (true/false)(optional)</li>
+ * <li>file encoding (optional)</li>
+ * </ul>
* Returns: true if ok , false if an error occured
*
* @since 5.2
@@ -56,6 +59,7 @@ public class StringToFile extends AbstractFunction {
private static final List<String> desc = new LinkedList<>();
private static final String KEY = "__StringToFile";//$NON-NLS-1$
private static final ConcurrentHashMap<String, Lock> lockMap = new ConcurrentHashMap<>();
+ private static final Pattern NEW_LINE_PATTERN = Pattern.compile("\\\\n");
static {
desc.add(JMeterUtils.getResString("string_to_file_pathname"));
desc.add(JMeterUtils.getResString("string_to_file_content"));//$NON-NLS-1$
@@ -78,8 +82,13 @@ public class StringToFile extends AbstractFunction {
String content = ((CompoundVariable) values[1]).execute();
boolean append = true;
if (values.length >= 3) {
- append = Boolean.parseBoolean(((CompoundVariable) values[2]).execute().toLowerCase().trim());
+ String appendString = ((CompoundVariable) values[2]).execute().toLowerCase().trim();
+ if (!appendString.isEmpty()) {
+ append = Boolean.parseBoolean(appendString);
+ }
}
+ content = NEW_LINE_PATTERN.matcher(content).replaceAll(System.lineSeparator());
+
Charset charset = StandardCharsets.UTF_8;
if (values.length == 4) {
String charsetParamValue = ((CompoundVariable) values[3]).execute();
@@ -87,13 +96,11 @@ public class StringToFile extends AbstractFunction {
charset = Charset.forName(charsetParamValue);
}
}
-
if (fileName.isEmpty()) {
log.error("File name '{}' is empty", fileName);
return false;
}
log.debug("Writing {} to file {} with charset {} and append {}", content, fileName, charset, append);
-
Lock localLock = new ReentrantLock();
Lock lock = lockMap.putIfAbsent(fileName, localLock);
try {
@@ -119,7 +126,6 @@ public class StringToFile extends AbstractFunction {
}
return true;
}
-
/** {@inheritDoc} */
@Override
public String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException {
diff --git a/test/src/org/apache/jmeter/functions/TestStringtoFile.java b/test/src/org/apache/jmeter/functions/TestStringtoFile.java
index cccf3e0..3732c56 100644
--- a/test/src/org/apache/jmeter/functions/TestStringtoFile.java
+++ b/test/src/org/apache/jmeter/functions/TestStringtoFile.java
@@ -48,7 +48,6 @@ public class TestStringtoFile extends JMeterTestCase {
private static final String FILENAME = "test.txt";
private static final String STRING_TO_WRITE = "test";
private static final String ENCODING = StandardCharsets.UTF_8.toString();
-
@Rule
public TemporaryFolder tempFolder = new TemporaryFolder();
@@ -154,7 +153,8 @@ public class TestStringtoFile extends JMeterTestCase {
public void testWriteToFileRequiredFilePathIsNull() throws Exception {
function.setParameters(functionParams(null, STRING_TO_WRITE, "true", ENCODING));
String returnValue = function.execute(result, null);
- Assert.assertFalse("This method 'Stringtofile' should fail to run with null file", Boolean.parseBoolean(returnValue));
+ Assert.assertFalse("This method 'Stringtofile' should fail to run with null file",
+ Boolean.parseBoolean(returnValue));
}
@Test
@@ -173,8 +173,7 @@ public class TestStringtoFile extends JMeterTestCase {
file.deleteOnExit();
function.setParameters(functionParams(file.getAbsolutePath(), STRING_TO_WRITE, "false", ENCODING));
String returnValue = function.execute(result, null);
- Assert.assertTrue("This method 'Stringtofile' should have successfully run",
- Boolean.parseBoolean(returnValue));
+ Assert.assertTrue("This method 'Stringtofile' should have successfully run", Boolean.parseBoolean(returnValue));
String res = FileUtils.readFileToString(file, ENCODING).trim();
Assert.assertEquals("The string should be 'test'", "test", res);
}
@@ -193,16 +192,26 @@ public class TestStringtoFile extends JMeterTestCase {
}
private Collection<CompoundVariable> functionParams(String... args) {
- return Arrays.asList(args).stream()
- .map(CompoundVariable::new)
- .collect(Collectors.toList());
+ return Arrays.asList(args).stream().map(CompoundVariable::new).collect(Collectors.toList());
}
@Test
public void testDescription() {
- Assert.assertEquals("Function 'stringtofile' should have successfully reading the configuration file 'messages.properties'",
- JMeterUtils.getResString("string_to_file_pathname"),
- function.getArgumentDesc().get(0));
+ Assert.assertEquals(
+ "Function 'stringtofile' should have successfully reading the configuration file 'messages.properties'",
+ JMeterUtils.getResString("string_to_file_pathname"), function.getArgumentDesc().get(0));
}
+ @Test
+ public void testLineBreak() throws Exception {
+ File file = tempFolder.newFile();
+ file.deleteOnExit();
+ function.setParameters(functionParams(file.getAbsolutePath(), "test\\\\ntest", "true", ENCODING));
+ function.execute();
+ String res = FileUtils.readFileToString(file, ENCODING).trim();
+ Assert.assertEquals("When the user type '\n', ine break should be saved in file",
+ "test" + System.lineSeparator() + "test", res);
+ Assert.assertTrue("When the user type '\\n',line break should be saved in file",
+ res.contains(System.lineSeparator()));
+ }
}
diff --git a/xdocs/usermanual/functions.xml b/xdocs/usermanual/functions.xml
index 3c316e0..f38660c 100644
--- a/xdocs/usermanual/functions.xml
+++ b/xdocs/usermanual/functions.xml
@@ -1725,14 +1725,16 @@ returns:
Path to the file name.(The path is absolute)
</property>
<property name="String to write" required="Yes">
- The string to write to the file
+ The string to write to the file. <br/>
+ If you need to insert a line break in your content, use <code>\n</code> in your string.
+ </property>
+ <property name="Append to file?" required="No">
+ The way to write the string, <code>true</code> means append, <code>false</code>
+ means overwrite.
</property>
<property name="File encoding if not UTF-8" required="No">
The encoding to be used to write to the file. If not specified, the default encoding is <code>UTF-8</code>.
</property>
- <property name="Append to file?" required="No">
- The way to write the string, <code>true</code> means append, <code>false</code> means overwrite.
- </property>
</properties>
</component>