/* Copyright (c) 2006 Ricebridge. All Rights Reserved. * * This file is available under the terms and conditions of the * Ricebridge "Open Source API" policy; Ricebridge grants use of this * copyrighted work under the terms of a BSD-style license only. See * http://www.opensource.org/licenses/bsd-license.php for more * information. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the following * disclaimer in the documentation and/or other materials provided * with the distribution. * * - Neither the name of the Ricebridge nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED * OF THE POSSIBILITY OF SUCH DAMAGE. */ package com.ricebridge.csvman.test; import com.ricebridge.csvman.*; import org.jostraca.util.*; import junit.framework.*; import junit.textui.*; import java.util.*; import java.io.*; import java.sql.*; /** Test cases for {@link com.ricebridge.csvman.CsvManager} {@link LineListener}-based loading. * <p>Test files are <a href="csvfiles/load01.csv">load01.csv</a>, * <a href="csvfiles/load02.csv">load02.csv</a>, <a href="csvfiles/load03.csv">load03.csv</a> * and <a href="csvfiles/example.csv">example.csv</a>.</p> * <p>The <b><a href="LoadLineListenerTest.java.html">Source Code</a></b> of this Java class * is available under a <a href="http://www.opensource.org/licenses/bsd-license.php">BSD-style license</a>.</p> */ public class LoadLineListenerTest extends TestCase { // test framework public LoadLineListenerTest( String pName ) { super( pName ); } public static TestSuite suite() { return new TestSuite( LoadLineListenerTest.class ); } public static void main( String[] pArgs ) { TestRunner.run( suite() ); } // test cases public void testExample() throws Exception { File csvFile = TestUtil.getTextCsvFile( "example" ); CsvManager csvManager = new CsvManager(); SimpleLineListener sll = new SimpleLineListener(); csvManager.load( csvFile, sll ); assertEquals( "1:[r1, f1, b1] 2:[r2, f2, b2] 3:[r3, f3, b3] ", sll.getData() ); } public void testSingle() throws Exception { String loadcanon = "1:[b1, q1] 2:[b2, q2] "; File csvFile = TestUtil.getTextCsvFile( "load01" ); CsvManager csvManager = new CsvManager(); SimpleLineListener srl = new SimpleLineListener(); csvManager.load( csvFile, srl ); assertEquals( loadcanon, canon(srl) ); srl = new SimpleLineListener(); csvManager.load( csvFile.getAbsolutePath(), srl ); assertEquals( loadcanon, canon(srl) ); srl = new SimpleLineListener(); InputStream is = new FileInputStream( csvFile ); csvManager.load( is, srl ); assertEquals( loadcanon, canon(srl) ); is.close(); srl = new SimpleLineListener(); Reader rdr = new FileReader( csvFile ); csvManager.load( rdr, srl ); assertEquals( loadcanon, canon(srl) ); rdr.close(); srl = new SimpleLineListener(); csvManager.loadFromString( FileUtil.readFile(csvFile), srl ); assertEquals( loadcanon, canon(srl) ); } /* public void testMultiple() throws Exception { String loadcanon = "1:[b, b1, q1] 2:[b, b2, q2] 1:[f, f1, z1] "; File csvFile = null; csvFile = TestUtil.findFile( "com/ricebridge/csvman/test/load.csv" ); CsvManager csvManager = new CsvManager(); LineSpec rs_b = new LineSpec( "/foo/bar", new String[] {"'b'","@name","que"}, new String[] {"Type","Name","Value"} ); LineSpec rs_f = new LineSpec( "/foo/far", new String[] {"'f'","@name","zoo"}, new String[] {"These","Headers","Ignored"} ); LineSpec rs = new LineSpec( rs_b, rs_f ); SimpleLineListener srl = new SimpleLineListener(); csvManager.load( csvFile, srl ); assertEquals( loadcanon, canon(srl) ); srl = new SimpleLineListener(); csvManager.load( csvFile.getAbsolutePath(), srl ); assertEquals( loadcanon, canon(srl) ); srl = new SimpleLineListener(); InputStream is = new FileInputStream( csvFile ); csvManager.load( is, srl ); assertEquals( loadcanon, canon(srl) ); is.close(); srl = new SimpleLineListener(); InputStream fis = new FileInputStream( csvFile ); InputSource isrc = new InputSource( fis ); csvManager.load( isrc, srl ); assertEquals( loadcanon, canon(srl) ); fis.close(); srl = new SimpleLineListener(); csvManager.loadFromURI( TestUtil.makeURI(csvFile), srl ); assertEquals( loadcanon, canon(srl) ); srl = new SimpleLineListener(); csvManager.loadFromString( FileUtil.readFile(csvFile), srl ); assertEquals( loadcanon, canon(srl) ); // prepared csvManager = new CsvManager(rs); srl = new SimpleLineListener(); csvManager.load( csvFile, srl ); assertEquals( loadcanon, canon(srl) ); srl = new SimpleLineListener(); csvManager.load( csvFile.getAbsolutePath(), srl ); assertEquals( loadcanon, canon(srl) ); srl = new SimpleLineListener(); is = new FileInputStream( csvFile ); csvManager.load( is, srl ); assertEquals( loadcanon, canon(srl) ); is.close(); srl = new SimpleLineListener(); fis = new FileInputStream( csvFile ); isrc = new InputSource( fis ); csvManager.load( isrc, srl ); assertEquals( loadcanon, canon(srl) ); fis.close(); srl = new SimpleLineListener(); csvManager.loadFromURI( TestUtil.makeURI(csvFile), srl ); assertEquals( loadcanon, canon(srl) ); srl = new SimpleLineListener(); csvManager.loadFromString( FileUtil.readFile(csvFile), srl ); assertEquals( loadcanon, canon(srl) ); } public void testRepeat() throws Exception { String loadheads = "Name,Que,"; String fileprefix = "com/ricebridge/csvman/test/"; File csvFile = null; csvFile = TestUtil.findFile( fileprefix+"load.csv" ); LineSpec rs = new LineSpec( "/foo/bar", new String[] {"@name","que"}, new String[] {"Name","Que"} ); CsvManager csvManager = new CsvManager(rs); String loadcanon1 = "1:[b1, q1] 2:[b2, q2] "; String loadcanon2 = "1:[2b1, 2q1] 2:[2b2, 2q2] 3:[2b3, 2q3] "; String loadcanon3 = "1:[3b1, 3q1] 2:[3b2, 3q2] 3:[3b3, 3q3] 4:[3b4, 3q4] "; SimpleLineListener srl = new SimpleLineListener(); // file String loadcanon = loadcanon1; csvManager.load( csvFile, srl ); assertEquals( loadcanon, canon(srl) ); assertEquals( 2, csvManager.getStats().getTotalLines() ); csvManager.load( csvFile, srl ); assertEquals( loadcanon, canon(srl) ); assertEquals( 2, csvManager.getStats().getTotalLines() ); loadcanon = loadcanon2; csvFile = TestUtil.findFile( fileprefix+"load02.csv" ); csvManager.load( csvFile, srl ); assertEquals( loadcanon, canon(srl) ); assertEquals( 3, csvManager.getStats().getTotalLines() ); loadcanon = loadcanon3; csvFile = TestUtil.findFile( fileprefix+"load03.csv" ); csvManager.load( csvFile, srl ); assertEquals( loadcanon, canon(srl) ); assertEquals( 4, csvManager.getStats().getTotalLines() ); // file path loadcanon = loadcanon1; csvFile = TestUtil.findFile( fileprefix+"load.csv" ); csvManager.load( csvFile.getAbsolutePath(), srl ); assertEquals( loadcanon, canon(srl) ); assertEquals( 2, csvManager.getStats().getTotalLines() ); csvManager.load( csvFile.getAbsolutePath(), srl ); assertEquals( loadcanon, canon(srl) ); assertEquals( 2, csvManager.getStats().getTotalLines() ); loadcanon = loadcanon2; csvFile = TestUtil.findFile( fileprefix+"load02.csv" ); csvManager.load( csvFile.getAbsolutePath(), srl ); assertEquals( loadcanon, canon(srl) ); assertEquals( 3, csvManager.getStats().getTotalLines() ); loadcanon = loadcanon3; csvFile = TestUtil.findFile( fileprefix+"load03.csv" ); csvManager.load( csvFile.getAbsolutePath(), srl ); assertEquals( loadcanon, canon(srl) ); assertEquals( 4, csvManager.getStats().getTotalLines() ); // inputstream loadcanon = loadcanon1; csvFile = TestUtil.findFile( fileprefix+"load.csv" ); InputStream is = new FileInputStream( csvFile ); csvManager.load( is, srl ); assertEquals( loadcanon, canon(srl) ); assertEquals( 2, csvManager.getStats().getTotalLines() ); is.close(); is = new FileInputStream( csvFile ); csvManager.load( is, srl ); assertEquals( loadcanon, canon(srl) ); assertEquals( 2, csvManager.getStats().getTotalLines() ); is.close(); loadcanon = loadcanon2; csvFile = TestUtil.findFile( fileprefix+"load02.csv" ); is = new FileInputStream( csvFile ); csvManager.load( is, srl ); assertEquals( loadcanon, canon(srl) ); assertEquals( 3, csvManager.getStats().getTotalLines() ); is.close(); loadcanon = loadcanon3; csvFile = TestUtil.findFile( fileprefix+"load03.csv" ); is = new FileInputStream( csvFile ); csvManager.load( is, srl ); assertEquals( loadcanon, canon(srl) ); assertEquals( 4, csvManager.getStats().getTotalLines() ); is.close(); // inputsource loadcanon = loadcanon1; csvFile = TestUtil.findFile( fileprefix+"load.csv" ); FileInputStream fis = new FileInputStream( csvFile ); InputSource isrc = new InputSource( fis ); csvManager.load( isrc, srl ); assertEquals( loadcanon, canon(srl) ); assertEquals( 2, csvManager.getStats().getTotalLines() ); fis.close(); fis = new FileInputStream( csvFile ); isrc = new InputSource( fis ); csvManager.load( isrc, srl ); assertEquals( loadcanon, canon(srl) ); assertEquals( 2, csvManager.getStats().getTotalLines() ); fis.close(); loadcanon = loadcanon2; csvFile = TestUtil.findFile( fileprefix+"load02.csv" ); fis = new FileInputStream( csvFile ); isrc = new InputSource( fis ); csvManager.load( isrc, srl ); assertEquals( loadcanon, canon(srl) ); assertEquals( 3, csvManager.getStats().getTotalLines() ); fis.close(); loadcanon = loadcanon3; csvFile = TestUtil.findFile( fileprefix+"load03.csv" ); fis = new FileInputStream( csvFile ); isrc = new InputSource( fis ); csvManager.load( isrc, srl ); assertEquals( loadcanon, canon(srl) ); assertEquals( 4, csvManager.getStats().getTotalLines() ); fis.close(); // uri loadcanon = loadcanon1; csvFile = TestUtil.findFile( fileprefix+"load.csv" ); csvManager.loadFromURI( TestUtil.makeURI( csvFile ), srl ); assertEquals( loadcanon, canon(srl) ); assertEquals( 2, csvManager.getStats().getTotalLines() ); csvManager.loadFromURI( TestUtil.makeURI( csvFile ), srl ); assertEquals( loadcanon, canon(srl) ); assertEquals( 2, csvManager.getStats().getTotalLines() ); loadcanon = loadcanon2; csvFile = TestUtil.findFile( fileprefix+"load02.csv" ); csvManager.loadFromURI( TestUtil.makeURI( csvFile ), srl ); assertEquals( loadcanon, canon(srl) ); assertEquals( 3, csvManager.getStats().getTotalLines() ); loadcanon = loadcanon3; csvFile = TestUtil.findFile( fileprefix+"load03.csv" ); csvManager.loadFromURI( TestUtil.makeURI( csvFile ), srl ); assertEquals( loadcanon, canon(srl) ); assertEquals( 4, csvManager.getStats().getTotalLines() ); // string loadcanon = loadcanon1; csvFile = TestUtil.findFile( fileprefix+"load.csv" ); csvManager.loadFromString( FileUtil.readFile(csvFile), srl ); assertEquals( loadcanon, canon(srl) ); assertEquals( 2, csvManager.getStats().getTotalLines() ); csvManager.loadFromString( FileUtil.readFile(csvFile), srl ); assertEquals( loadcanon, canon(srl) ); assertEquals( 2, csvManager.getStats().getTotalLines() ); loadcanon = loadcanon2; csvFile = TestUtil.findFile( fileprefix+"load02.csv" ); csvManager.loadFromString( FileUtil.readFile(csvFile), srl ); assertEquals( loadcanon, canon(srl) ); assertEquals( 3, csvManager.getStats().getTotalLines() ); loadcanon = loadcanon3; csvFile = TestUtil.findFile( fileprefix+"load03.csv" ); csvManager.loadFromString( FileUtil.readFile(csvFile), srl ); assertEquals( loadcanon, canon(srl) ); assertEquals( 4, csvManager.getStats().getTotalLines() ); } */ private String canon( SimpleLineListener pSimpleLineListener ) throws Exception { return pSimpleLineListener.getData(); } public static class SimpleLineListener extends CustomLineListener { StringBuffer sb = null; protected void startProcessImpl() { sb = new StringBuffer(); } public BadLine handleLineImpl( String[] pLine, int pNumFields, long pLineNumber, String pOrigLine ) { sb.append( pLineNumber+":"+Arrays.asList( pLine )+" " ); return null; } public String getData() { return sb.toString(); } } }