You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jmeter.apache.org by fs...@apache.org on 2022/07/09 12:15:35 UTC

[jmeter] 01/02: Guess the delimiter of the CSV source, when configured one seems wrong.

This is an automated email from the ASF dual-hosted git repository.

fschumacher pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/jmeter.git

commit b22b00c69459dad4f7b02518c6a5e1f4bcfebad4
Author: Felix Schumacher <fe...@internetallee.de>
AuthorDate: Sat Jul 9 14:14:09 2022 +0200

    Guess the delimiter of the CSV source, when configured one seems wrong.
    
    This is in line with the behaviour of CSVSaveService.
    
    Bugzilla Id: 66140
---
 .../jmeter/report/core/SampleMetaDataParser.java   | 38 ++++++++++++++++++++--
 xdocs/changes.xml                                  |  2 ++
 2 files changed, 37 insertions(+), 3 deletions(-)

diff --git a/src/core/src/main/java/org/apache/jmeter/report/core/SampleMetaDataParser.java b/src/core/src/main/java/org/apache/jmeter/report/core/SampleMetaDataParser.java
index aeab11b89c..bc96a660d5 100644
--- a/src/core/src/main/java/org/apache/jmeter/report/core/SampleMetaDataParser.java
+++ b/src/core/src/main/java/org/apache/jmeter/report/core/SampleMetaDataParser.java
@@ -17,6 +17,10 @@
 
 package org.apache.jmeter.report.core;
 
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
 /**
@@ -26,14 +30,42 @@ import java.util.regex.Pattern;
  */
 public class SampleMetaDataParser {
 
-    private char separator;
+    private final char separator;
+
+    private final Logger logger = LoggerFactory.getLogger(this.getClass());
+
+    private static final Pattern DELIMITER_PATTERN = Pattern
+            // This assumes the header names are all single words with no spaces
+            // word followed by 0 or more repeats of (non-word char + word)
+            // where the non-word char (\2) is the same
+            // e.g. abc|def|ghi but not abd|def~ghi
+            .compile("\\w+((\\W)\\w+)?(\\2\\w+)*(\\2\"\\w+\")*" // $NON-NLS-1$
+                    // last entries may be quoted strings
+            );
 
     public SampleMetaDataParser(char separator) {
         this.separator = separator;
     }
 
     public SampleMetadata parse(String headRow) {
-        String[] cols = headRow.split(Pattern.quote(Character.toString(separator)));
-        return new SampleMetadata(separator, cols);
+        char useSep = separator;
+        if (headRow.indexOf(useSep) < 0) {
+            Matcher matcher = DELIMITER_PATTERN.matcher(headRow);
+            if (matcher.matches()) {
+                String guessedSep = matcher.group(2);
+                if (guessedSep.length() != 1) {
+                    throw new IllegalArgumentException(
+                            "We guessed a delimiter of '"+guessedSep+"', but we support only one-character-separators");
+                }
+                useSep = guessedSep.charAt(0);
+                logger.warn("Use guessed delimiter '{}' instead of configured '{}'. "
+                        +"Please configure the property 'jmeter.save.saveservice.default_delimiter={}'",
+                        useSep,
+                        separator,
+                        useSep);
+            }
+        }
+        String[] cols = headRow.split(Pattern.quote(Character.toString(useSep)));
+        return new SampleMetadata(useSep, cols);
     }
 }
diff --git a/xdocs/changes.xml b/xdocs/changes.xml
index 65147fb68d..89aa8ddc62 100644
--- a/xdocs/changes.xml
+++ b/xdocs/changes.xml
@@ -137,6 +137,8 @@ Summary
 
 <h3>Report / Dashboard</h3>
 <ul>
+  <li><bug>66140</bug>Guess the delimiter of the CSV source, when configured one seems wrong.
+    This is in line with the behaviour of CSVSaveService.</li>
 </ul>
 
 <h3>Documentation</h3>