001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.jmeter.functions;
020:
021: import java.io.FileNotFoundException;
022:
023: import org.apache.jmeter.junit.JMeterTestCase;
024:
025: /**
026: * File data container for CSV (and similar delimited) files Data is accessible
027: * via row and column number
028: *
029: * @version $Revision: 493796 $
030: */
031: public class TestFileRowColContainer {
032:
033: public static class Test extends JMeterTestCase {
034:
035: public Test(String a) {
036: super (a);
037: }
038:
039: public void testNull() throws Exception {
040: try {
041: new FileRowColContainer("testfiles/xyzxyz");
042: fail("Should not find the file");
043: } catch (FileNotFoundException e) {
044: }
045: }
046:
047: public void testrowNum() throws Exception {
048: FileRowColContainer f = new FileRowColContainer(
049: "testfiles/test.csv");
050: assertNotNull(f);
051: assertEquals("Expected 4 lines", 4, f.getSize());
052:
053: assertEquals(0, f.nextRow());
054: assertEquals(1, f.nextRow());
055: assertEquals(2, f.nextRow());
056: assertEquals(3, f.nextRow());
057: assertEquals(0, f.nextRow());
058:
059: }
060:
061: public void testColumns() throws Exception {
062: FileRowColContainer f = new FileRowColContainer(
063: "testfiles/test.csv");
064: assertNotNull(f);
065: assertTrue("Not empty", f.getSize() > 0);
066:
067: int myRow = f.nextRow();
068: assertEquals(0, myRow);
069: assertEquals("a1", f.getColumn(myRow, 0));
070: assertEquals("d1", f.getColumn(myRow, 3));
071:
072: try {
073: f.getColumn(myRow, 4);
074: fail("Expected out of bounds");
075: } catch (IndexOutOfBoundsException e) {
076: }
077: myRow = f.nextRow();
078: assertEquals(1, myRow);
079: assertEquals("b2", f.getColumn(myRow, 1));
080: assertEquals("c2", f.getColumn(myRow, 2));
081: }
082:
083: public void testColumnsComma() throws Exception {
084: FileRowColContainer f = new FileRowColContainer(
085: "testfiles/test.csv", ",");
086: assertNotNull(f);
087: assertTrue("Not empty", f.getSize() > 0);
088:
089: int myRow = f.nextRow();
090: assertEquals(0, myRow);
091: assertEquals("a1", f.getColumn(myRow, 0));
092: assertEquals("d1", f.getColumn(myRow, 3));
093:
094: try {
095: f.getColumn(myRow, 4);
096: fail("Expected out of bounds");
097: } catch (IndexOutOfBoundsException e) {
098: }
099: myRow = f.nextRow();
100: assertEquals(1, myRow);
101: assertEquals("b2", f.getColumn(myRow, 1));
102: assertEquals("c2", f.getColumn(myRow, 2));
103: }
104:
105: public void testColumnsTab() throws Exception {
106: FileRowColContainer f = new FileRowColContainer(
107: "testfiles/test.tsv", "\t");
108: assertNotNull(f);
109: assertTrue("Not empty", f.getSize() > 0);
110:
111: int myRow = f.nextRow();
112: assertEquals(0, myRow);
113: assertEquals("a1", f.getColumn(myRow, 0));
114: assertEquals("d1", f.getColumn(myRow, 3));
115:
116: try {
117: f.getColumn(myRow, 4);
118: fail("Expected out of bounds");
119: } catch (IndexOutOfBoundsException e) {
120: }
121: myRow = f.nextRow();
122: assertEquals(1, myRow);
123: assertEquals("b2", f.getColumn(myRow, 1));
124: assertEquals("c2", f.getColumn(myRow, 2));
125: }
126:
127: public void testEmptyCols() throws Exception {
128: FileRowColContainer f = new FileRowColContainer(
129: "testfiles/testempty.csv");
130: assertNotNull(f);
131: assertEquals("Expected 4 lines", 4, f.getSize());
132:
133: int myRow = f.nextRow();
134: assertEquals(0, myRow);
135: assertEquals("", f.getColumn(myRow, 0));
136: assertEquals("d1", f.getColumn(myRow, 3));
137:
138: myRow = f.nextRow();
139: assertEquals(1, myRow);
140: assertEquals("", f.getColumn(myRow, 1));
141: assertEquals("c2", f.getColumn(myRow, 2));
142:
143: myRow = f.nextRow();
144: assertEquals(2, myRow);
145: assertEquals("b3", f.getColumn(myRow, 1));
146: assertEquals("", f.getColumn(myRow, 2));
147:
148: myRow = f.nextRow();
149: assertEquals(3, myRow);
150: assertEquals("b4", f.getColumn(myRow, 1));
151: assertEquals("c4", f.getColumn(myRow, 2));
152: assertEquals("", f.getColumn(myRow, 3));
153: }
154: }
155: }
|