Author: sebb
Date: Mon Feb  8 23:05:38 2010
New Revision: 907839

URL: http://svn.apache.org/viewvc?rev=907839&view=rev
Log:
Bug 46790 - CSV Data Set Config should be able to parse CSV headers

Added:
    jakarta/jmeter/trunk/bin/testfiles/testheader.csv
Modified:
    jakarta/jmeter/trunk/src/components/org/apache/jmeter/config/CSVDataSet.java
    jakarta/jmeter/trunk/src/core/org/apache/jmeter/services/FileServer.java
    jakarta/jmeter/trunk/test/src/org/apache/jmeter/config/TestCVSDataSet.java
    jakarta/jmeter/trunk/xdocs/changes.xml
    jakarta/jmeter/trunk/xdocs/usermanual/component_reference.xml

Added: jakarta/jmeter/trunk/bin/testfiles/testheader.csv
URL: 
http://svn.apache.org/viewvc/jakarta/jmeter/trunk/bin/testfiles/testheader.csv?rev=907839&view=auto
==============================================================================
--- jakarta/jmeter/trunk/bin/testfiles/testheader.csv (added)
+++ jakarta/jmeter/trunk/bin/testfiles/testheader.csv Mon Feb  8 23:05:38 2010
@@ -0,0 +1,5 @@
+A|B|C|"D|1"
+a1|b1|c1|d1
+a2|b2|c2|d2
+a3|b3|c3|d3
+a4|b4|c4|d4
\ No newline at end of file

Modified: 
jakarta/jmeter/trunk/src/components/org/apache/jmeter/config/CSVDataSet.java
URL: 
http://svn.apache.org/viewvc/jakarta/jmeter/trunk/src/components/org/apache/jmeter/config/CSVDataSet.java?rev=907839&r1=907838&r2=907839&view=diff
==============================================================================
--- 
jakarta/jmeter/trunk/src/components/org/apache/jmeter/config/CSVDataSet.java 
(original)
+++ 
jakarta/jmeter/trunk/src/components/org/apache/jmeter/config/CSVDataSet.java 
Mon Feb  8 23:05:38 2010
@@ -91,10 +91,9 @@
         recycle = true;
         return this;
     }
-    /*
-     * (non-Javadoc)
-     *
-     * @see 
org.apache.jmeter.engine.event.LoopIterationListener#iterationStart(org.apache.jmeter.engine.event.LoopIterationEvent)
+
+    /**
+     * {...@inheritdoc}
      */
     public void iterationStart(LoopIterationEvent iterEvent) {
         FileServer server = FileServer.getFileServer();
@@ -117,8 +116,18 @@
                     alias = _fileName+"@"+mode; // user-specified key
                     break;
             }
-            server.reserveFile(_fileName, getFileEncoding(), alias);
-            vars = JOrphanUtils.split(getVariableNames(), ","); // $NON-NLS-1$
+            final String names = getVariableNames();
+            if (names == null || names.length()==0) {
+                String header = server.reserveFile(_fileName, 
getFileEncoding(), alias, true);                
+                try {
+                    vars = CSVSaveService.csvSplitString(header, 
getDelimiter().charAt(0));
+                } catch (IOException e) {
+                    log.warn("Could not split CSV header line",e);
+                }                
+            } else {
+                server.reserveFile(_fileName, getFileEncoding(), alias);
+                vars = JOrphanUtils.split(names, ","); // $NON-NLS-1$          
      
+            }
         }
         try {
             String delim = getDelimiter();

Modified: 
jakarta/jmeter/trunk/src/core/org/apache/jmeter/services/FileServer.java
URL: 
http://svn.apache.org/viewvc/jakarta/jmeter/trunk/src/core/org/apache/jmeter/services/FileServer.java?rev=907839&r1=907838&r2=907839&view=diff
==============================================================================
--- jakarta/jmeter/trunk/src/core/org/apache/jmeter/services/FileServer.java 
(original)
+++ jakarta/jmeter/trunk/src/core/org/apache/jmeter/services/FileServer.java 
Mon Feb  8 23:05:38 2010
@@ -118,7 +118,7 @@
      * @param charsetName - the character set encoding to use for the file 
(may be null)
      */
     public synchronized void reserveFile(String filename, String charsetName) {
-        reserveFile(filename, charsetName, filename);
+        reserveFile(filename, charsetName, filename, false);
     }
 
     /**
@@ -130,23 +130,45 @@
      * @param alias - the name to be used to access the object (must not be 
null)
      */
     public synchronized void reserveFile(String filename, String charsetName, 
String alias) {
+        reserveFile(filename, charsetName, alias, false);
+    }
+
+    /**
+     * Creates an association between a filename and a File inputOutputObject,
+     * and stores it for later use - unless it is already stored.
+     *
+     * @param filename - relative (to base) or absolute file name (must not be 
null)
+     * @param charsetName - the character set encoding to use for the file 
(may be null)
+     * @param alias - the name to be used to access the object (must not be 
null)
+     * @param hasHeader true if the file has a header line describing the 
contents
+     */
+    public synchronized String reserveFile(String filename, String 
charsetName, String alias, boolean hasHeader) {
         if (filename == null){
             throw new IllegalArgumentException("Filename must not be null");
         }
         if (alias == null){
             throw new IllegalArgumentException("Alias must not be null");
         }
-        if (!files.containsKey(alias)) {
+        FileEntry fileEntry = files.get(alias);
+        if (fileEntry == null) {
             File f = new File(filename);
-            FileEntry file =
+            fileEntry =
                 new FileEntry(f.isAbsolute() ? f : new File(base, 
filename),null,charsetName);
             if (filename.equals(alias)){
                 log.info("Stored: "+filename);
             } else {
                 log.info("Stored: "+filename+" Alias: "+alias);
             }
-            files.put(alias, file);
+            files.put(alias, fileEntry);
+            if (hasHeader){
+                try {
+                    fileEntry.headerLine=readLine(alias, false);
+                } catch (IOException e) {
+                    throw new IllegalArgumentException("Could not read file 
header line",e);
+                }
+            }
         }
+        return fileEntry.headerLine;
     }
 
    /**
@@ -297,9 +319,10 @@
     }
 
     private static class FileEntry{
-        private File file;
+        private String headerLine;
+        private final File file;
         private Object inputOutputObject; // Reader/Writer
-        private String charSetEncoding;
+        private final String charSetEncoding;
         FileEntry(File f, Object o, String e){
             file=f;
             inputOutputObject=o;

Modified: 
jakarta/jmeter/trunk/test/src/org/apache/jmeter/config/TestCVSDataSet.java
URL: 
http://svn.apache.org/viewvc/jakarta/jmeter/trunk/test/src/org/apache/jmeter/config/TestCVSDataSet.java?rev=907839&r1=907838&r2=907839&view=diff
==============================================================================
--- jakarta/jmeter/trunk/test/src/org/apache/jmeter/config/TestCVSDataSet.java 
(original)
+++ jakarta/jmeter/trunk/test/src/org/apache/jmeter/config/TestCVSDataSet.java 
Mon Feb  8 23:05:38 2010
@@ -32,25 +32,26 @@
 
 public class TestCVSDataSet extends JMeterTestCase {
 
-    public TestCVSDataSet() {
-        super();
-    }
-
+    private JMeterVariables threadVars;
+    
     public TestCVSDataSet(String arg0) {
         super(arg0);
     }
 
     @Override
+    public void setUp(){
+        JMeterContext jmcx = JMeterContextService.getContext();
+        jmcx.setVariables(new JMeterVariables());
+        threadVars = jmcx.getVariables();        
+        threadVars.put("b", "value");
+    }
+
+    @Override
     public void tearDown() throws IOException{
         FileServer.getFileServer().closeFiles();
     }
     
     public void testopen() throws Exception {
-        JMeterContext jmcx = JMeterContextService.getContext();
-        jmcx.setVariables(new JMeterVariables());
-        JMeterVariables threadVars = jmcx.getVariables();
-        threadVars.put("b", "value");
-
         CSVDataSet csv = new CSVDataSet();
         csv.setFilename("No.such.filename");
         csv.setVariableNames("a,b,c");
@@ -92,6 +93,26 @@
         assertEquals("c1",threadVars.get("c"));
     }
 
+    // Test CSV file with a header line
+    public void testHeaderOpen(){
+        CSVDataSet csv = new CSVDataSet();
+        csv.setFilename("testfiles/testheader.csv");
+        csv.setDelimiter("|");
+        assertNull(csv.getVariableNames());
+        csv.iterationStart(null);
+        assertNull(threadVars.get("a"));
+        assertEquals("a1",threadVars.get("A"));
+        assertEquals("b1",threadVars.get("B"));
+        assertEquals("c1",threadVars.get("C"));
+        assertEquals("d1",threadVars.get("D 1"));
+        csv.iterationStart(null);
+        assertNull(threadVars.get("a"));
+        assertEquals("a2",threadVars.get("A"));
+        assertEquals("b2",threadVars.get("B"));
+        assertEquals("c2",threadVars.get("C"));
+        assertEquals("d2",threadVars.get("D 1"));
+    }
+
     private CSVDataSet initCSV(){
         CSVDataSet csv = new CSVDataSet();
         csv.setFilename("testfiles/test.csv");
@@ -99,11 +120,8 @@
         csv.setDelimiter(",");
         return csv;
     }
+
     public void testShareMode(){
-        JMeterContext jmcx = JMeterContextService.getContext();
-        jmcx.setVariables(new JMeterVariables());
-        JMeterVariables threadVars = jmcx.getVariables();
-        threadVars.put("b", "value");
         
         new CSVDataSetBeanInfo(); // needs to be initialised
         CSVDataSet csv0 = initCSV();

Modified: jakarta/jmeter/trunk/xdocs/changes.xml
URL: 
http://svn.apache.org/viewvc/jakarta/jmeter/trunk/xdocs/changes.xml?rev=907839&r1=907838&r2=907839&view=diff
==============================================================================
--- jakarta/jmeter/trunk/xdocs/changes.xml (original)
+++ jakarta/jmeter/trunk/xdocs/changes.xml Mon Feb  8 23:05:38 2010
@@ -189,6 +189,7 @@
 <li>Bug 48511 - add parent,child,all selection to regex extractor</li>
 <li>Add Sampler scope selection to XPathExtractor</li>
 <li>Regular Expression Extractor, Response Assertion and Size Assertion can 
now be applied to a JMeter variable</li>
+<li>Bug 46790 - CSV Data Set Config should be able to parse CSV headers</li>
 </ul>
 
 <h3>Functions</h3>

Modified: jakarta/jmeter/trunk/xdocs/usermanual/component_reference.xml
URL: 
http://svn.apache.org/viewvc/jakarta/jmeter/trunk/xdocs/usermanual/component_reference.xml?rev=907839&r1=907838&r2=907839&view=diff
==============================================================================
--- jakarta/jmeter/trunk/xdocs/usermanual/component_reference.xml (original)
+++ jakarta/jmeter/trunk/xdocs/usermanual/component_reference.xml Mon Feb  8 
23:05:38 2010
@@ -2577,6 +2577,10 @@
        Previously it was necessary to choose a delimiter that was not used in 
any values.
        </p>
        <p>
+       Versions of JMeter after 2.3.4 support CSV files which have a header 
line defining the column names.
+       To enable this, leave the "Variable Names" field empty. The correct 
delimiter must be provided.
+       </p>
+       <p>
        By default, the file is only opened once, and each thread will use a 
different line from the file.
        However the order in which lines are passed to threads depends on the 
order in which they execute,
        which may vary between iterations.
@@ -2620,7 +2624,11 @@
   If the OS does not distinguish between upper and lower case, csvData.TXT 
would also be opened separately. 
   </property>
   <property name="File Encoding" required="No">The encoding to be used to read 
the file, if not the platform default.</property>
-  <property name="Variable Names" required="Yes">List of variable names 
(comma-delimited)</property>
+  <property name="Variable Names" required="Yes">List of variable names 
(comma-delimited).
+  Versions of JMeter after 2.3.4 support CSV header lines:
+  if the variable name field empty, then the first line of the file is read 
and interpreted as the list of column names.
+  The names must be separated by the delimiter character. They can be quoted 
using double-quotes.
+  </property>
   <property name="Delimiter" required="Yes">Delimiter to be used to split the 
records in the file.
   If there are fewer values on the line than there are variables the remaining 
variables are not updated -
   so they will retain their previous value (if any).</property>



---------------------------------------------------------------------
To unsubscribe, e-mail: jmeter-dev-unsubscr...@jakarta.apache.org
For additional commands, e-mail: jmeter-dev-h...@jakarta.apache.org

Reply via email to