/* 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 com.ricebridge.data.Text;


import junit.framework.*;
import junit.textui.*;

import java.util.*;


/** Test cases for {@link com.ricebridge.csvman.CsvLoader} when loading bad data.
 *    <p>The <b><a href="CsvLoaderBadLineTest.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 CsvLoaderBadLineTest extends TestCase {

  // test framework
  
  public CsvLoaderBadLineTest( String pName ) {
    super( pName );
  }

  public static TestSuite suite() {
    return new TestSuite( CsvLoaderBadLineTest.class );
  }

  public static void main( String[] pArgs ) {
    TestRunner.run( suite() );
  }



  // test cases

  public void testFailModes() throws Exception {
    String badcsv = "1,a\n2,\"b\"b\n3,\"c\"";

    CsvManager   csvManager = new CsvManager();
    CsvLoader    loader     = csvManager.makeLoader( new Text(badcsv) );

    loader.begin();
    assertTrue( loader.hasNext() );
    assertEquals( "[1, a]", Arrays.asList(loader.next()).toString() );
    assertTrue( loader.hasNext() );

    try {
      loader.next();
      fail();
    }
    catch( Exception e ) {
      assertEquals( "An incorrectly formatted line caused processing to halt. The line was: '2,\"b\"b'. The parse error was: 'unexpected token: b'.", e.toString() ); 
      CsvManagerException ce = (CsvManagerException) e;
      BadLine badline = ce.getBadLine();
      assertEquals( "[CSV:BadLine:2:unexpected token: b:SYNTAX:2,\"b\"b]", badline.toString() );
    }

    loader.end();


    csvManager.setIgnoreBadLines(true);
    loader = csvManager.makeLoader( new Text(badcsv) );

    loader.begin();
    assertTrue( loader.hasNext() );
    assertEquals( "[1, a]", Arrays.asList(loader.next()).toString() );
    assertTrue( loader.hasNext() );

    try {
      loader.next();
      fail();
    }
    catch( Exception e ) {
      assertEquals( "An incorrectly formatted line caused processing to halt. The line was: '2,\"b\"b'. The parse error was: 'unexpected token: b'.", e.toString() ); 
    }

    assertTrue( loader.hasNext() );
    assertEquals( "[3, c]", Arrays.asList(loader.next()).toString() );
    assertTrue( !loader.hasNext() );
    loader.end();
  }



  public void testContinueAttempt() {
    String badcsv = "1,a\n2,\"b\"b\n3,\"c\"";

    CsvManager   csvManager = new CsvManager();
    CsvLoader    loader     = csvManager.makeLoader( new Text(badcsv) );

    ArrayList lines = new ArrayList();
    Exception nextE = null;
    Exception hasNextE = null;
    

    int numlines = 0;

    loader.begin();
    try {
      while( loader.hasNext() ) {
        try {
          numlines++;
          lines.add( Arrays.asList( loader.next() ) );
        }
        catch( Exception e ) {
          if( null != nextE ) {
            fail();
          }
          else {
            nextE = e;
          }
        }
      }
    }
    catch( Exception e ) {
      if( null != hasNextE ) {
        fail();
      }
      else {
        hasNextE = e;
      }
    }

    
    loader.end();

    assertEquals( 2, numlines );
    assertEquals( "An incorrectly formatted line caused processing to halt. The line was: '2,\"b\"b'. The parse error was: 'unexpected token: b'.", nextE.toString() );
    assertEquals( "An incorrectly formatted line caused processing to halt. The line was: '2,\"b\"b'. The parse error was: 'unexpected token: b'.", hasNextE.toString() );
  }
  
  


  private List makeData( CsvLoader pCsvLoader ) throws Exception {
    ArrayList data = new ArrayList();

    pCsvLoader.begin();
    while( pCsvLoader.hasNext() ) {
      String[] fields = pCsvLoader.next();
      data.add( fields );
    }
    pCsvLoader.end();

    return data;
  }

}
Syntax Highlighting created using the com.Ostermiller.Syntax package.
Wednesday, June 20 2007 at 22:16