KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Header: /home/cvs/jakarta-jmeter/src/functions/org/apache/jmeter/functions/FileRowColContainer.java,v 1.5.2.2 2005/02/18 01:42:49 sebb Exp $
2
/*
3  * Copyright 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.BufferedReader JavaDoc;
22 import java.io.FileNotFoundException JavaDoc;
23 import java.io.FileReader JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.StringTokenizer JavaDoc;
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 /**
34  * File data container for CSV (and similar delimited) files
35  * Data is accessible via row and column number
36  *
37  * @version $Revision: 1.5.2.2 $
38  */

39 public class FileRowColContainer
40 {
41     
42     transient private static Logger log = LoggingManager.getLoggerForClass();
43     
44
45     private ArrayList JavaDoc fileData; // Lines in the file, split into columns
46

47     private String JavaDoc fileName; // name of the file
48

49     public static final String JavaDoc DELIMITER =
50         JMeterUtils.getPropDefault("csvread.delimiter",",");// delimiter defaults to ","
51

52     /** Keeping track of which row is next to be read. */
53     private int nextRow;
54
55     /** Delimiter for this file */
56     private String JavaDoc delimiter;
57
58     private FileRowColContainer()// Not intended to be called directly
59
{
60     }
61
62     public FileRowColContainer(String JavaDoc file,String JavaDoc delim)
63     throws IOException JavaDoc,FileNotFoundException JavaDoc
64     {
65         log.debug("FRCC("+file+","+delim+")");
66         fileName = file;
67         delimiter = delim;
68         nextRow = 0;
69         load();
70     }
71
72     public FileRowColContainer(String JavaDoc file)
73     throws IOException JavaDoc,FileNotFoundException JavaDoc
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 JavaDoc,FileNotFoundException JavaDoc
85     {
86         fileData = new ArrayList JavaDoc();
87
88         BufferedReader JavaDoc myBread=null;
89         try
90         {
91             FileReader JavaDoc fis = new FileReader JavaDoc(fileName);
92             myBread = new BufferedReader JavaDoc(fis);
93             String JavaDoc line = myBread.readLine();
94             /* N.B. Stop reading the file if we get a blank line:
95              * This allows for trailing comments in the file
96              */

97             while (line != null && line.length() > 0)
98             {
99                 fileData.add(splitLine(line,delimiter));
100                 line = myBread.readLine();
101             }
102         }
103         catch (FileNotFoundException JavaDoc e)
104         {
105             fileData = null;
106             log.warn(e.toString());
107             throw e;
108         }
109         catch (IOException JavaDoc 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     /**
122      * Get the string for the column from the current row
123      *
124      * @param row row number (from 0)
125      * @param col column number (from 0)
126      * @return the string (empty if out of bounds)
127      * @throws IndexOutOfBoundsException if the column number is out of bounds
128      */

129     public String JavaDoc getColumn(int row,int col) throws IndexOutOfBoundsException JavaDoc
130     {
131         String JavaDoc colData;
132         colData = (String JavaDoc) ((ArrayList JavaDoc) fileData.get(row)).get(col);
133         log.debug(fileName+"("+row+","+col+"): "+colData);
134         return colData;
135     }
136     
137     /**
138      * Returns the next row to the caller, and updates it,
139      * allowing for wrap round
140      *
141      * @return the first free (unread) row
142      *
143      */

144     public int nextRow()
145     {
146         int row = nextRow;
147         nextRow++;
148         if (nextRow >= fileData.size())// 0-based
149
{
150             nextRow = 0;
151         }
152         log.debug ("Row: "+ row);
153         return row;
154     }
155
156
157     /**
158      * Splits the line according to the specified delimiter
159      *
160      * @return an ArrayList of Strings containing one element for each
161      * value in the line
162      */

163     private static ArrayList JavaDoc splitLine(String JavaDoc theLine,String JavaDoc delim)
164     {
165         ArrayList JavaDoc result = new ArrayList JavaDoc();
166         StringTokenizer JavaDoc tokener = new StringTokenizer JavaDoc(theLine,delim,true);
167         /* the beginning of the line is a "delimiter" so that
168            ,a,b,c returns "" "a" "b" "c" */

169         boolean lastWasDelim = true;
170         while(tokener.hasMoreTokens())
171         {
172             String JavaDoc token = tokener.nextToken();
173             if(token.equals(delim))
174             {
175               if(lastWasDelim)
176               {
177                 // two delimiters in a row; add an empty String
178
result.add("");
179               }
180               lastWasDelim = true;
181             }
182             else
183             {
184               lastWasDelim = false;
185               result.add(token);
186             }
187         }
188         if (lastWasDelim) // Catch the trailing delimiter
189
{
190             result.add("");
191         }
192         return result;
193     }
194     
195     /**
196      * @return the file name for this class
197      */

198     public String JavaDoc getFileName()
199     {
200         return fileName;
201     }
202
203     /**
204      * @return Returns the delimiter.
205      */

206     final String JavaDoc getDelimiter() {
207         return delimiter;
208     }
209
210     ///////////////////////////// TEST CASES /////////////////////////////////////////
211

212     public static class Test extends JMeterTestCase
213     {
214
215         static{
216             LoggingManager.setPriority("DEBUG","jmeter.functions.FileRowColContainer");
217 // LoggingManager.setTarget(new PrintWriter(System.out));
218
}
219
220
221         public Test(String JavaDoc a)
222         {
223             super(a);
224         }
225         
226         public void testNull() throws Exception JavaDoc
227         {
228             try
229             {
230                 new FileRowColContainer("testfiles/xyzxyz");
231                 fail("Should not find the file");
232             }
233             catch (FileNotFoundException JavaDoc e)
234             {
235             }
236         }
237         
238         public void testrowNum() throws Exception JavaDoc
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 JavaDoc
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 JavaDoc 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 JavaDoc
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 JavaDoc 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 JavaDoc
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 JavaDoc 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 JavaDoc
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