1 18 19 package org.apache.jmeter.functions; 20 21 import java.io.BufferedReader ; 22 import java.io.FileNotFoundException ; 23 import java.io.FileReader ; 24 import java.io.IOException ; 25 import java.util.ArrayList ; 26 import java.util.StringTokenizer ; 27 28 import org.apache.jmeter.junit.JMeterTestCase; 29 import org.apache.jmeter.util.JMeterUtils; 30 import org.apache.jorphan.logging.LoggingManager; 31 import org.apache.log.Logger; 32 33 39 public class FileRowColContainer 40 { 41 42 transient private static Logger log = LoggingManager.getLoggerForClass(); 43 44 45 private ArrayList fileData; 47 private String fileName; 49 public static final String DELIMITER = 50 JMeterUtils.getPropDefault("csvread.delimiter",","); 52 53 private int nextRow; 54 55 56 private String delimiter; 57 58 private FileRowColContainer() { 60 } 61 62 public FileRowColContainer(String file,String delim) 63 throws IOException ,FileNotFoundException 64 { 65 log.debug("FRCC("+file+","+delim+")"); 66 fileName = file; 67 delimiter = delim; 68 nextRow = 0; 69 load(); 70 } 71 72 public FileRowColContainer(String file) 73 throws IOException ,FileNotFoundException 74 { 75 log.debug("FRCC("+file+")["+DELIMITER+"]"); 76 fileName = file; 77 delimiter = DELIMITER; 78 nextRow = 0; 79 load(); 80 } 81 82 83 private void load() 84 throws IOException ,FileNotFoundException 85 { 86 fileData = new ArrayList (); 87 88 BufferedReader myBread=null; 89 try 90 { 91 FileReader fis = new FileReader (fileName); 92 myBread = new BufferedReader (fis); 93 String line = myBread.readLine(); 94 97 while (line != null && line.length() > 0) 98 { 99 fileData.add(splitLine(line,delimiter)); 100 line = myBread.readLine(); 101 } 102 } 103 catch (FileNotFoundException e) 104 { 105 fileData = null; 106 log.warn(e.toString()); 107 throw e; 108 } 109 catch (IOException e) 110 { 111 fileData = null; 112 log.warn(e.toString()); 113 throw e; 114 } 115 finally 116 { 117 if (myBread != null) myBread.close(); 118 } 119 } 120 121 129 public String getColumn(int row,int col) throws IndexOutOfBoundsException 130 { 131 String colData; 132 colData = (String ) ((ArrayList ) fileData.get(row)).get(col); 133 log.debug(fileName+"("+row+","+col+"): "+colData); 134 return colData; 135 } 136 137 144 public int nextRow() 145 { 146 int row = nextRow; 147 nextRow++; 148 if (nextRow >= fileData.size()) { 150 nextRow = 0; 151 } 152 log.debug ("Row: "+ row); 153 return row; 154 } 155 156 157 163 private static ArrayList splitLine(String theLine,String delim) 164 { 165 ArrayList result = new ArrayList (); 166 StringTokenizer tokener = new StringTokenizer (theLine,delim,true); 167 169 boolean lastWasDelim = true; 170 while(tokener.hasMoreTokens()) 171 { 172 String token = tokener.nextToken(); 173 if(token.equals(delim)) 174 { 175 if(lastWasDelim) 176 { 177 result.add(""); 179 } 180 lastWasDelim = true; 181 } 182 else 183 { 184 lastWasDelim = false; 185 result.add(token); 186 } 187 } 188 if (lastWasDelim) { 190 result.add(""); 191 } 192 return result; 193 } 194 195 198 public String getFileName() 199 { 200 return fileName; 201 } 202 203 206 final String getDelimiter() { 207 return delimiter; 208 } 209 210 212 public static class Test extends JMeterTestCase 213 { 214 215 static{ 216 LoggingManager.setPriority("DEBUG","jmeter.functions.FileRowColContainer"); 217 } 219 220 221 public Test(String a) 222 { 223 super(a); 224 } 225 226 public void testNull() throws Exception 227 { 228 try 229 { 230 new FileRowColContainer("testfiles/xyzxyz"); 231 fail("Should not find the file"); 232 } 233 catch (FileNotFoundException e) 234 { 235 } 236 } 237 238 public void testrowNum() throws Exception 239 { 240 FileRowColContainer f = new FileRowColContainer("testfiles/test.csv"); 241 assertNotNull(f); 242 assertEquals("Expected 4 lines",4,f.fileData.size()); 243 244 int myRow=f.nextRow(); 245 assertEquals(0,myRow); 246 assertEquals(1,f.nextRow); 247 248 myRow = f.nextRow(); 249 assertEquals(1,myRow); 250 assertEquals(2,f.nextRow); 251 252 myRow = f.nextRow(); 253 assertEquals(2,myRow); 254 assertEquals(3,f.nextRow); 255 256 myRow = f.nextRow(); 257 assertEquals(3,myRow); 258 assertEquals(0,f.nextRow); 259 260 myRow = f.nextRow(); 261 assertEquals(0,myRow); 262 assertEquals(1,f.nextRow); 263 264 } 265 266 public void testColumns() throws Exception 267 { 268 FileRowColContainer f = new FileRowColContainer("testfiles/test.csv"); 269 assertNotNull(f); 270 assertTrue("Not empty",f.fileData.size() > 0); 271 272 int myRow=f.nextRow(); 273 assertEquals(0,myRow); 274 assertEquals("a1",f.getColumn(myRow,0)); 275 assertEquals("d1",f.getColumn(myRow,3)); 276 277 try { 278 f.getColumn(myRow,4); 279 fail("Expected out of bounds"); 280 } 281 catch (IndexOutOfBoundsException e) 282 { 283 } 284 myRow=f.nextRow(); 285 assertEquals(1,myRow); 286 assertEquals("b2",f.getColumn(myRow,1)); 287 assertEquals("c2",f.getColumn(myRow,2)); 288 } 289 290 public void testColumnsComma() throws Exception 291 { 292 FileRowColContainer f = new FileRowColContainer("testfiles/test.csv",","); 293 assertNotNull(f); 294 assertTrue("Not empty",f.fileData.size() > 0); 295 296 int myRow=f.nextRow(); 297 assertEquals(0,myRow); 298 assertEquals("a1",f.getColumn(myRow,0)); 299 assertEquals("d1",f.getColumn(myRow,3)); 300 301 try { 302 f.getColumn(myRow,4); 303 fail("Expected out of bounds"); 304 } 305 catch (IndexOutOfBoundsException e) 306 { 307 } 308 myRow=f.nextRow(); 309 assertEquals(1,myRow); 310 assertEquals("b2",f.getColumn(myRow,1)); 311 assertEquals("c2",f.getColumn(myRow,2)); 312 } 313 314 public void testColumnsTab() throws Exception 315 { 316 FileRowColContainer f = new FileRowColContainer("testfiles/test.tsv","\t"); 317 assertNotNull(f); 318 assertTrue("Not empty",f.fileData.size() > 0); 319 320 int myRow=f.nextRow(); 321 assertEquals(0,myRow); 322 assertEquals("a1",f.getColumn(myRow,0)); 323 assertEquals("d1",f.getColumn(myRow,3)); 324 325 try { 326 f.getColumn(myRow,4); 327 fail("Expected out of bounds"); 328 } 329 catch (IndexOutOfBoundsException e) 330 { 331 } 332 myRow=f.nextRow(); 333 assertEquals(1,myRow); 334 assertEquals("b2",f.getColumn(myRow,1)); 335 assertEquals("c2",f.getColumn(myRow,2)); 336 } 337 338 public void testEmptyCols() throws Exception 339 { 340 FileRowColContainer f = new FileRowColContainer("testfiles/testempty.csv"); 341 assertNotNull(f); 342 assertEquals("Expected 4 lines",4,f.fileData.size()); 343 344 int myRow=f.nextRow(); 345 assertEquals(0,myRow); 346 assertEquals("",f.getColumn(myRow,0)); 347 assertEquals("d1",f.getColumn(myRow,3)); 348 349 myRow=f.nextRow(); 350 assertEquals(1,myRow); 351 assertEquals("",f.getColumn(myRow,1)); 352 assertEquals("c2",f.getColumn(myRow,2)); 353 354 myRow=f.nextRow(); 355 assertEquals(2,myRow); 356 assertEquals("b3",f.getColumn(myRow,1)); 357 assertEquals("",f.getColumn(myRow,2)); 358 359 myRow=f.nextRow(); 360 assertEquals(3,myRow); 361 assertEquals("b4",f.getColumn(myRow,1)); 362 assertEquals("c4",f.getColumn(myRow,2)); 363 assertEquals("",f.getColumn(myRow,3)); 364 } 365 } 366 } | Popular Tags |