1 18 19 package org.apache.jmeter.functions; 20 21 import java.io.Serializable ; 22 import java.util.Collection ; 23 import java.util.LinkedList ; 24 import java.util.List ; 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 57 public class CSVRead extends AbstractFunction implements Serializable 58 { 59 transient private static final Logger log = LoggingManager.getLoggerForClass(); 60 61 private static final String KEY = "__CSVRead"; 63 private static final List desc = new LinkedList (); 64 65 66 private Object [] values; 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 clone() 78 { 79 CSVRead newReader = new CSVRead(); 80 return newReader; 81 } 82 83 86 public synchronized String execute( 87 SampleResult previousResult, 88 Sampler currentSampler) 89 throws InvalidVariableException 90 { 91 String myValue = ""; 92 93 String fileName = 94 ((org.apache.jmeter.engine.util.CompoundVariable) values[0]) 95 .execute(); 96 String columnOrNext = 97 ((org.apache.jmeter.engine.util.CompoundVariable) values[1]) 98 .execute(); 99 100 log.debug("execute (" + fileName + " , " + columnOrNext + ") "); 101 102 if (columnOrNext.startsWith("*")) 104 { 105 FileWrapper.open(fileName,columnOrNext); 106 109 return ""; 110 } 111 112 if (columnOrNext.equals("next()") || columnOrNext.equals("next")) 114 { 115 FileWrapper.endRow(fileName); 116 117 124 return ""; 125 } 126 127 try 128 { 129 int columnIndex = Integer.parseInt(columnOrNext); myValue = FileWrapper.getColumn(fileName,columnIndex); 131 } 132 catch (NumberFormatException e) 133 { 134 log.warn(Thread.currentThread().getName()+" - can't parse column number: " 135 + columnOrNext + " "+ e.toString()); 136 } 137 catch (IndexOutOfBoundsException 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 152 public List getArgumentDesc() 153 { 154 return desc; 155 } 156 157 160 public String getReferenceKey() 161 { 162 return KEY; 163 } 164 165 168 public void setParameters(Collection 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 191 FileWrapper.clearAll(); 193 } 194 } | Popular Tags |