Save This Page
Home » jakarta-jmeter-2.3.4_src » org.apache.jmeter.functions » [javadoc | source]
    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *   http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS,
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    * See the License for the specific language governing permissions and
   15    * limitations under the License.
   16    *
   17    */
   18   
   19   package org.apache.jmeter.functions;
   20   
   21   import java.util.Collection;
   22   import java.util.LinkedList;
   23   import java.util.List;
   24   
   25   import org.apache.jmeter.engine.util.CompoundVariable;
   26   import org.apache.jmeter.samplers.SampleResult;
   27   import org.apache.jmeter.samplers.Sampler;
   28   import org.apache.jmeter.util.JMeterUtils;
   29   import org.apache.jorphan.logging.LoggingManager;
   30   import org.apache.log.Logger;
   31   
   32   /**
   33    * The function represented by this class allows data to be read from CSV files.
   34    * Syntax is similar to StringFromFile function. The function allows the test to
   35    * line-thru the data in the CSV file - one line per each test. E.g. inserting
   36    * the following in the test scripts :
   37    *
   38    * ${_CSVRead(c:/BOF/abcd.csv,0)} // read (first) line of 'c:/BOF/abcd.csv' ,
   39    * return the 1st column ( represented by the '0'),
   40    * ${_CSVRead(c:/BOF/abcd.csv,1)} // read (first) line of 'c:/BOF/abcd.csv' ,
   41    * return the 2nd column ( represented by the '1'),
   42    * ${_CSVRead(c:/BOF/abcd.csv,next())} // Go to next line of 'c:/BOF/abcd.csv'
   43    *
   44    * NOTE: A single instance of each different file is opened and used for all
   45    * threads.
   46    *
   47    * To open the same file twice, use the alias function: __CSVRead(abc.csv,*ONE);
   48    * __CSVRead(abc.csv,*TWO);
   49    *
   50    * __CSVRead(*ONE,1); etc
   51    *
   52    */
   53   public class CSVRead extends AbstractFunction {
   54       private static final Logger log = LoggingManager.getLoggerForClass();
   55   
   56       private static final String KEY = "__CSVRead"; // Function name //$NON-NLS-1$
   57   
   58       private static final List desc = new LinkedList();
   59   
   60       private Object[] values; // Parameter list
   61   
   62       static {
   63           desc.add(JMeterUtils.getResString("csvread_file_file_name")); //$NON-NLS-1$
   64           desc.add(JMeterUtils.getResString("column_number")); //$NON-NLS-1$
   65       }
   66   
   67       public CSVRead() {
   68       }
   69   
   70       /**
   71        * @see org.apache.jmeter.functions.Function#execute(SampleResult, Sampler)
   72        */
   73       public synchronized String execute(SampleResult previousResult, Sampler currentSampler)
   74               throws InvalidVariableException {
   75           String myValue = ""; //$NON-NLS-1$
   76   
   77           String fileName = ((org.apache.jmeter.engine.util.CompoundVariable) values[0]).execute();
   78           String columnOrNext = ((org.apache.jmeter.engine.util.CompoundVariable) values[1]).execute();
   79   
   80           log.debug("execute (" + fileName + " , " + columnOrNext + ")   ");
   81   
   82           // Process __CSVRead(filename,*ALIAS)
   83           if (columnOrNext.startsWith("*")) { //$NON-NLS-1$
   84               FileWrapper.open(fileName, columnOrNext);
   85               /*
   86                * All done, so return
   87                */
   88               return ""; //$NON-NLS-1$
   89           }
   90   
   91           // if argument is 'next' - go to the next line
   92           if (columnOrNext.equals("next()") || columnOrNext.equals("next")) { //$NON-NLS-1$ //$NON-NLS-2$
   93               FileWrapper.endRow(fileName);
   94   
   95               /*
   96                * All done now ,so return the empty string - this allows the caller
   97                * to append __CSVRead(file,next) to the last instance of
   98                * __CSVRead(file,col)
   99                *
  100                * N.B. It is important not to read any further lines at this point,
  101                * otherwise the wrong line can be retrieved when using multiple
  102                * threads.
  103                */
  104               return ""; //$NON-NLS-1$
  105           }
  106   
  107           try {
  108               int columnIndex = Integer.parseInt(columnOrNext); // what column
  109                                                                   // is wanted?
  110               myValue = FileWrapper.getColumn(fileName, columnIndex);
  111           } catch (NumberFormatException e) {
  112               log.warn(Thread.currentThread().getName() + " - can't parse column number: " + columnOrNext + " "
  113                       + e.toString());
  114           } catch (IndexOutOfBoundsException e) {
  115               log.warn(Thread.currentThread().getName() + " - invalid column number: " + columnOrNext + " at row "
  116                       + FileWrapper.getCurrentRow(fileName) + " " + e.toString());
  117           }
  118   
  119           log.debug("execute value: " + myValue);
  120   
  121           return myValue;
  122       }
  123   
  124       /**
  125        * @see org.apache.jmeter.functions.Function#getArgumentDesc()
  126        */
  127       public List getArgumentDesc() {
  128           return desc;
  129       }
  130   
  131       /**
  132        * @see org.apache.jmeter.functions.Function#getReferenceKey()
  133        */
  134       public String getReferenceKey() {
  135           return KEY;
  136       }
  137   
  138       /**
  139        * @see org.apache.jmeter.functions.Function#setParameters(Collection)
  140        */
  141       public synchronized void setParameters(Collection parameters) throws InvalidVariableException {
  142           log.debug("setParameter - Collection.size=" + parameters.size());
  143   
  144           values = parameters.toArray();
  145   
  146           if (log.isDebugEnabled()) {
  147               for (int i = 0; i < parameters.size(); i++) {
  148                   log.debug("i:" + ((CompoundVariable) values[i]).execute());
  149               }
  150           }
  151   
  152           checkParameterCount(parameters, 2);
  153   
  154           /*
  155            * Need to reset the containers for repeated runs; about the only way
  156            * for functions to detect that a run is starting seems to be the
  157            * setParameters() call.
  158            */
  159           FileWrapper.clearAll();// TODO only clear the relevant entry - if possible...
  160   
  161       }
  162   }

Save This Page
Home » jakarta-jmeter-2.3.4_src » org.apache.jmeter.functions » [javadoc | source]