KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > functions > CSVRead


1 // $Header: /home/cvs/jakarta-jmeter/src/functions/org/apache/jmeter/functions/CSVRead.java,v 1.14 2004/03/30 18:07:07 sebb Exp $
2
/*
3  * Copyright 2003-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * 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.io.Serializable JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.LinkedList JavaDoc;
24 import java.util.List JavaDoc;
25
26 import org.apache.jmeter.engine.util.CompoundVariable;
27 import org.apache.jmeter.samplers.SampleResult;
28 import org.apache.jmeter.samplers.Sampler;
29 import org.apache.jmeter.util.JMeterUtils;
30 import org.apache.jorphan.logging.LoggingManager;
31 import org.apache.log.Logger;
32
33 /**
34  * The function represented by this class allows data to be read from CSV
35  * files. Syntax is similar to StringFromFile function. The function allows
36  * the test to line-thru the data in the CSV file - one line per each test.
37  * E.g. inserting the following in the test scripts :
38  *
39  * ${_CSVRead(c:/BOF/abcd.csv,0)} // read (first) line of
40 'c:/BOF/abcd.csv' , return the 1st column ( represented by the '0'),
41  * ${_CSVRead(c:/BOF/abcd.csv,1)} // read (first) line of
42 'c:/BOF/abcd.csv' , return the 2nd column ( represented by the '1'),
43  * ${_CSVRead(c:/BOF/abcd.csv,next())} // Go to next line of
44 'c:/BOF/abcd.csv'
45  *
46  * NOTE: A single instance of each different file is opened and used for all threads.
47  *
48  * To open the same file twice, use the alias function:
49  * __CSVRead(abc.csv,*ONE);
50  * __CSVRead(abc.csv,*TWO);
51  *
52  * __CSVRead(*ONE,1); etc
53  *
54  *
55  * @version $Revision: 1.14 $ Last Updated: $Date: 2004/03/30 18:07:07 $
56  */

57 public class CSVRead extends AbstractFunction implements Serializable JavaDoc
58 {
59     transient private static final Logger log = LoggingManager.getLoggerForClass();
60
61     private static final String JavaDoc KEY = "__CSVRead"; // Function name
62

63     private static final List JavaDoc desc = new LinkedList JavaDoc();
64
65     
66     private Object JavaDoc[] values; // Parameter list
67

68     static {
69         desc.add(JMeterUtils.getResString("csvread_file_file_name"));
70         desc.add(JMeterUtils.getResString("column_number"));
71     }
72
73     public CSVRead()
74     {
75     }
76
77     public Object JavaDoc clone()
78     {
79         CSVRead newReader = new CSVRead();
80         return newReader;
81     }
82
83     /**
84      * @see org.apache.jmeter.functions.Function#execute(SampleResult, Sampler)
85      */

86     public synchronized String JavaDoc execute(
87         SampleResult previousResult,
88         Sampler currentSampler)
89         throws InvalidVariableException
90     {
91         String JavaDoc myValue = "";
92
93         String JavaDoc fileName =
94                 ((org.apache.jmeter.engine.util.CompoundVariable) values[0])
95                     .execute();
96         String JavaDoc columnOrNext =
97                 ((org.apache.jmeter.engine.util.CompoundVariable) values[1])
98                     .execute();
99
100         log.debug("execute (" + fileName + " , " + columnOrNext + ") ");
101
102         // Process __CSVRead(filename,*ALIAS)
103
if (columnOrNext.startsWith("*"))
104         {
105             FileWrapper.open(fileName,columnOrNext);
106             /*
107              * All done, so return
108              */

109             return "";
110         }
111             
112         // if argument is 'next' - go to the next line
113
if (columnOrNext.equals("next()") || columnOrNext.equals("next"))
114         {
115             FileWrapper.endRow(fileName);
116         
117             /*
118              * All done now ,so return the empty string - this allows the caller to
119              * append __CSVRead(file,next) to the last instance of __CSVRead(file,col)
120              *
121              * N.B. It is important not to read any further lines at this point, otherwise
122              * the wrong line can be retrieved when using multiple threads.
123              */

124              return "";
125         }
126
127         try
128         {
129             int columnIndex = Integer.parseInt(columnOrNext); // what column is wanted?
130
myValue = FileWrapper.getColumn(fileName,columnIndex);
131         }
132         catch (NumberFormatException JavaDoc e)
133         {
134             log.warn(Thread.currentThread().getName()+" - can't parse column number: "
135                      + columnOrNext + " "+ e.toString());
136         }
137         catch (IndexOutOfBoundsException JavaDoc e)
138         {
139             log.warn(Thread.currentThread().getName()+" - invalid column number: " + columnOrNext
140                       + " at row " + FileWrapper.getCurrentRow(fileName) + " "
141                       + e.toString());
142         }
143
144         log.debug("execute value: "+myValue);
145         
146         return myValue;
147     }
148
149     /**
150      * @see org.apache.jmeter.functions.Function#getArgumentDesc()
151      */

152     public List JavaDoc getArgumentDesc()
153     {
154         return desc;
155     }
156
157     /**
158      * @see org.apache.jmeter.functions.Function#getReferenceKey()
159      */

160     public String JavaDoc getReferenceKey()
161     {
162         return KEY;
163     }
164     
165     /**
166      * @see org.apache.jmeter.functions.Function#setParameters(Collection)
167      */

168     public void setParameters(Collection JavaDoc parameters)
169         throws InvalidVariableException
170     {
171         log.debug("setParameter - Collection.size=" + parameters.size());
172
173         values = parameters.toArray();
174
175         if (log.isDebugEnabled()){
176             for (int i=0;i <parameters.size();i++){
177                 log.debug("i:"+((CompoundVariable)values[i]).execute());
178             }
179         }
180
181         if (values.length != 2)
182         {
183             throw new InvalidVariableException("Wrong number of parameters; 2 != "+values.length);
184         }
185         
186         /*
187          * Need to reset the containers for repeated runs; about the only way for
188          * functions to detect that a run is starting seems to be the setParameters()
189          * call.
190         */

191         FileWrapper.clearAll();//TODO only clear the relevant entry - if possible...
192

193     }
194 }
Popular Tags