/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */

package org.apache.jmeter.functions;

import java.io.FileNotFoundException;

import org.apache.jmeter.junit.JMeterTestCase;

/**
 * File data container for CSV (and similar delimited) files Data is accessible
 * via row and column number
 * 
 * @version $Revision$
 */
public class TestFileRowColContainer extends JMeterTestCase {

    public void testNull() throws Exception {
        try {
            new FileRowColContainer("testfiles/xyzxyz");
            fail("Should not find the file");
        } catch (FileNotFoundException e) {
        }
    }

    public void testrowNum() throws Exception {
        FileRowColContainer f = new FileRowColContainer("testfiles/test.csv");
        assertNotNull(f);
        assertEquals("Expected 4 lines", 4, f.getSize());

        assertEquals(0, f.nextRow());
        assertEquals(1, f.nextRow());
        assertEquals(2, f.nextRow());
        assertEquals(3, f.nextRow());
        assertEquals(0, f.nextRow());

    }

    public void testColumns() throws Exception {
        FileRowColContainer f = new FileRowColContainer("testfiles/test.csv");
        assertNotNull(f);
        assertTrue("Not empty", f.getSize() > 0);

        int myRow = f.nextRow();
        assertEquals(0, myRow);
        assertEquals("a1", f.getColumn(myRow, 0));
        assertEquals("d1", f.getColumn(myRow, 3));

        try {
            f.getColumn(myRow, 4);
            fail("Expected out of bounds");
        } catch (IndexOutOfBoundsException e) {
        }
        myRow = f.nextRow();
        assertEquals(1, myRow);
        assertEquals("b2", f.getColumn(myRow, 1));
        assertEquals("c2", f.getColumn(myRow, 2));
    }

    public void testColumnsComma() throws Exception {
        FileRowColContainer f = new FileRowColContainer("testfiles/test.csv", ",");
        assertNotNull(f);
        assertTrue("Not empty", f.getSize() > 0);

        int myRow = f.nextRow();
        assertEquals(0, myRow);
        assertEquals("a1", f.getColumn(myRow, 0));
        assertEquals("d1", f.getColumn(myRow, 3));

        try {
            f.getColumn(myRow, 4);
            fail("Expected out of bounds");
        } catch (IndexOutOfBoundsException e) {
        }
        myRow = f.nextRow();
        assertEquals(1, myRow);
        assertEquals("b2", f.getColumn(myRow, 1));
        assertEquals("c2", f.getColumn(myRow, 2));
    }

    public void testColumnsTab() throws Exception {
        FileRowColContainer f = new FileRowColContainer("testfiles/test.tsv", "\t");
        assertNotNull(f);
        assertTrue("Not empty", f.getSize() > 0);

        int myRow = f.nextRow();
        assertEquals(0, myRow);
        assertEquals("a1", f.getColumn(myRow, 0));
        assertEquals("d1", f.getColumn(myRow, 3));

        try {
            f.getColumn(myRow, 4);
            fail("Expected out of bounds");
        } catch (IndexOutOfBoundsException e) {
        }
        myRow = f.nextRow();
        assertEquals(1, myRow);
        assertEquals("b2", f.getColumn(myRow, 1));
        assertEquals("c2", f.getColumn(myRow, 2));
    }

    public void testEmptyCols() throws Exception {
        FileRowColContainer f = new FileRowColContainer("testfiles/testempty.csv");
        assertNotNull(f);
        assertEquals("Expected 4 lines", 4, f.getSize());

        int myRow = f.nextRow();
        assertEquals(0, myRow);
        assertEquals("", f.getColumn(myRow, 0));
        assertEquals("d1", f.getColumn(myRow, 3));

        myRow = f.nextRow();
        assertEquals(1, myRow);
        assertEquals("", f.getColumn(myRow, 1));
        assertEquals("c2", f.getColumn(myRow, 2));

        myRow = f.nextRow();
        assertEquals(2, myRow);
        assertEquals("b3", f.getColumn(myRow, 1));
        assertEquals("", f.getColumn(myRow, 2));

        myRow = f.nextRow();
        assertEquals(3, myRow);
        assertEquals("b4", f.getColumn(myRow, 1));
        assertEquals("c4", f.getColumn(myRow, 2));
        assertEquals("", f.getColumn(myRow, 3));
    }
}
@@ -1450,32 +1450,45 @@
 RowColContainer(
+findTestPath(
 %22testfiles/test.
@@ -1484,32 +1484,33 @@
 files/test.csv%22)
+)
 ;%0A        assert
@@ -1875,32 +1875,45 @@
 RowColContainer(
+findTestPath(
 %22testfiles/test.
@@ -1913,24 +1913,25 @@
 s/test.csv%22)
+)
 ;%0A        as
@@ -2588,32 +2588,45 @@
 RowColContainer(
+findTestPath(
 %22testfiles/test.
@@ -2629,16 +2629,17 @@
 est.csv%22
+)
 , %22,%22);%0A
@@ -3304,32 +3304,45 @@
 RowColContainer(
+findTestPath(
 %22testfiles/test.
@@ -3345,16 +3345,17 @@
 est.tsv%22
+)
 , %22%5Ct%22);
@@ -4020,32 +4020,45 @@
 RowColContainer(
+findTestPath(
 %22testfiles/teste
@@ -4067,16 +4067,17 @@
 ty.csv%22)
+)
 ;%0A      

Fix up so test case works in Eclipse too