/* Copyright (c) 2005 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.xmlman.test;


import com.ricebridge.xmlman.*;
import com.ricebridge.xmlman.in.*;
import com.ricebridge.xmlman.in.test.*;
import com.ricebridge.xmlman.log.*;

import org.jostraca.util.*;

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

import org.xml.sax.InputSource;

import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.SAXParser;

import java.util.*;
import java.io.*;
import java.sql.*;


/** Test cases for {@link com.ricebridge.xmlman.XmlManager} StringArray load handling.
 *    <p>Test files are <a href="load.xml">load.xml</a>, <a href="load02.xml">load02.xml</a>, <a href="load03.xml">load03.xml</a>
 *  and <a href="example.xml">example.xml</a>.</p>
 *    <p>The <b><a href="LoadAsListsTest.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 LoadAsListsTest extends TestCase {

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

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

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



  // test cases

  public void testExample() throws Exception {
    File         xmlFile    = TestUtil.findFile( "com/ricebridge/xmlman/test/example.xml" );
    XmlManager   xmlManager = new XmlManager();
    RecordSpec   rs         = new RecordSpec( "/root/record", new String[] {"@name","foo","bar"} );
    List         data       = xmlManager.loadAsLists( xmlFile, rs );
    StringBuffer sb         = new StringBuffer();

    for( int record = 0; record < data.size(); record++ ) {
      sb.append( "record: "+record+"\n" );
      List fields = (List) data.get(record);
      for( int field = 0; field < fields.size(); field++ ) {
        sb.append( "field "+field+" has value: "+fields.get(field)+"\n" );
      }
    }

    String out = "record: 0\nfield 0 has value: r1\nfield 1 has value: f1\nfield 2 has value: b1\nrecord: 1\nfield 0 has value: r2\nfield 1 has value: f2\nfield 2 has value: b2\nrecord: 2\nfield 0 has value: r3\nfield 1 has value: f3\nfield 2 has value: b3\n";
    assertEquals( out, sb.toString() );
  }


  public void testSingle() throws Exception {
    String loadcanon = "[[b1, q1], [b2, q2]]";

    File xmlFile = null;
    xmlFile = TestUtil.findFile( "com/ricebridge/xmlman/test/load.xml" );
    XmlManager xmlManager = new XmlManager();

    RecordSpec rs = new RecordSpec( "/foo/bar", 
                                    new String[] {"@name","que"},
                                    new String[] {"Name","Que"} );

    List data = xmlManager.loadAsLists( xmlFile, rs );
    assertEquals( loadcanon, canon(data) );

    data = xmlManager.loadAsLists( xmlFile.getAbsolutePath(), rs );
    assertEquals( loadcanon, canon(data) );

    InputStream is = new FileInputStream( xmlFile );
    data = xmlManager.loadAsLists( is, rs );
    assertEquals( loadcanon, canon(data) );
    is.close();

    InputStream fis  = new FileInputStream( xmlFile );
    InputSource isrc = new InputSource( fis );
    data = xmlManager.loadAsLists( isrc, rs );
    assertEquals( loadcanon, canon(data) );
    fis.close();

    data = xmlManager.loadAsListsFromURI( TestUtil.makeURI(xmlFile), rs );
    assertEquals( loadcanon, canon(data) );

    data = xmlManager.loadAsListsFromString( FileUtil.readFile(xmlFile), rs );
    assertEquals( loadcanon, canon(data) );
  }


  public void testMultiple() throws Exception {
    String loadcanon = "[[b, b1, q1], [b, b2, q2], [f, f1, z1]]";

    File xmlFile = null;
    xmlFile = TestUtil.findFile( "com/ricebridge/xmlman/test/load.xml" );
    XmlManager xmlManager = new XmlManager();

    RecordSpec rs_b = new RecordSpec( "/foo/bar", 
                                      new String[] {"'b'","@name","que"},
                                      new String[] {"Type","Name","Value"} );
    RecordSpec rs_f = new RecordSpec( "/foo/far", 
                                      new String[] {"'f'","@name","zoo"},
                                      new String[] {"These","Headers","Ignored"} );
    List rsl = ListUtil.make( rs_b, rs_f );

    RecordSpec rs = new RecordSpec( rs_b, rs_f );

    List data = xmlManager.loadAsLists( xmlFile, rs );
    assertEquals( loadcanon, canon(data) );

    data = xmlManager.loadAsLists( xmlFile.getAbsolutePath(), rs );
    assertEquals( loadcanon, canon(data) );

    InputStream is = new FileInputStream( xmlFile );
    data = xmlManager.loadAsLists( is, rs );
    assertEquals( loadcanon, canon(data) );
    is.close();

    InputStream fis  = new FileInputStream( xmlFile );
    InputSource isrc = new InputSource( fis );
    data = xmlManager.loadAsLists( isrc, rs );
    assertEquals( loadcanon, canon(data) );
    fis.close();

    data = xmlManager.loadAsListsFromURI( TestUtil.makeURI(xmlFile), rs );
    assertEquals( loadcanon, canon(data) );

    data = xmlManager.loadAsListsFromString( FileUtil.readFile(xmlFile), rs );
    assertEquals( loadcanon, canon(data) );


    // prepared
    xmlManager = new XmlManager(rs);

    data = xmlManager.loadAsLists( xmlFile );
    assertEquals( loadcanon, canon(data) );

    data = xmlManager.loadAsLists( xmlFile.getAbsolutePath() );
    assertEquals( loadcanon, canon(data) );

    is = new FileInputStream( xmlFile );
    data = xmlManager.loadAsLists( is );
    assertEquals( loadcanon, canon(data) );
    is.close();

    fis  = new FileInputStream( xmlFile );
    isrc = new InputSource( fis );
    data = xmlManager.loadAsLists( isrc );
    assertEquals( loadcanon, canon(data) );
    fis.close();

    data = xmlManager.loadAsListsFromURI( TestUtil.makeURI(xmlFile) );
    assertEquals( loadcanon, canon(data) );

    data = xmlManager.loadAsListsFromString( FileUtil.readFile(xmlFile) );
    assertEquals( loadcanon, canon(data) );

  }


  public void testRepeat() throws Exception {
    String fileprefix = "com/ricebridge/xmlman/test/";

    File xmlFile = null;
    xmlFile = TestUtil.findFile( fileprefix+"load.xml" );
    RecordSpec rs = new RecordSpec( "/foo/bar", 
                                    new String[] {"@name","que"},
                                    new String[] {"Name","Que"} );

    XmlManager xmlManager = new XmlManager(rs);

    // file
    String loadcanon  = "[[b1, q1], [b2, q2]]";
    List data = xmlManager.loadAsLists( xmlFile );
    assertEquals( loadcanon, canon(data) );
    assertEquals( 2, xmlManager.getStats().getTotalRecords() );

    data = xmlManager.loadAsLists( xmlFile );
    assertEquals( loadcanon, canon(data) );
    assertEquals( 2, xmlManager.getStats().getTotalRecords() );

    loadcanon = "[[2b1, 2q1], [2b2, 2q2], [2b3, 2q3]]";
    xmlFile = TestUtil.findFile( fileprefix+"load02.xml" );
    data = xmlManager.loadAsLists( xmlFile );
    assertEquals( loadcanon, canon(data) );
    assertEquals( 3, xmlManager.getStats().getTotalRecords() );

    loadcanon = "[[3b1, 3q1], [3b2, 3q2], [3b3, 3q3], [3b4, 3q4]]";
    xmlFile = TestUtil.findFile( fileprefix+"load03.xml" );
    data = xmlManager.loadAsLists( xmlFile );
    assertEquals( loadcanon, canon(data) );
    assertEquals( 4, xmlManager.getStats().getTotalRecords() );


    // file path
    loadcanon  = "[[b1, q1], [b2, q2]]";
    xmlFile = TestUtil.findFile( fileprefix+"load.xml" );
    data = xmlManager.loadAsLists( xmlFile.getAbsolutePath() );
    assertEquals( loadcanon, canon(data) );
    assertEquals( 2, xmlManager.getStats().getTotalRecords() );

    data = xmlManager.loadAsLists( xmlFile.getAbsolutePath() );
    assertEquals( loadcanon, canon(data) );
    assertEquals( 2, xmlManager.getStats().getTotalRecords() );

    loadcanon = "[[2b1, 2q1], [2b2, 2q2], [2b3, 2q3]]";
    xmlFile = TestUtil.findFile( fileprefix+"load02.xml" );
    data = xmlManager.loadAsLists( xmlFile.getAbsolutePath() );
    assertEquals( loadcanon, canon(data) );
    assertEquals( 3, xmlManager.getStats().getTotalRecords() );

    loadcanon = "[[3b1, 3q1], [3b2, 3q2], [3b3, 3q3], [3b4, 3q4]]";
    xmlFile = TestUtil.findFile( fileprefix+"load03.xml" );
    data = xmlManager.loadAsLists( xmlFile.getAbsolutePath() );
    assertEquals( loadcanon, canon(data) );
    assertEquals( 4, xmlManager.getStats().getTotalRecords() );


    // inputstream
    loadcanon  = "[[b1, q1], [b2, q2]]";
    xmlFile = TestUtil.findFile( fileprefix+"load.xml" );
    InputStream is = new FileInputStream( xmlFile );
    data = xmlManager.loadAsLists( is );
    assertEquals( loadcanon, canon(data) );
    assertEquals( 2, xmlManager.getStats().getTotalRecords() );
    is.close();

    is = new FileInputStream( xmlFile );
    data = xmlManager.loadAsLists( is );
    assertEquals( loadcanon, canon(data) );
    assertEquals( 2, xmlManager.getStats().getTotalRecords() );
    is.close();

    loadcanon = "[[2b1, 2q1], [2b2, 2q2], [2b3, 2q3]]";
    xmlFile = TestUtil.findFile( fileprefix+"load02.xml" );
    is = new FileInputStream( xmlFile );
    data = xmlManager.loadAsLists( is );
    assertEquals( loadcanon, canon(data) );
    assertEquals( 3, xmlManager.getStats().getTotalRecords() );
    is.close();

    loadcanon = "[[3b1, 3q1], [3b2, 3q2], [3b3, 3q3], [3b4, 3q4]]";
    xmlFile = TestUtil.findFile( fileprefix+"load03.xml" );
    is = new FileInputStream( xmlFile );
    data = xmlManager.loadAsLists( is );
    assertEquals( loadcanon, canon(data) );
    assertEquals( 4, xmlManager.getStats().getTotalRecords() );
    is.close();


    // inputsource
    loadcanon  = "[[b1, q1], [b2, q2]]";
    xmlFile = TestUtil.findFile( fileprefix+"load.xml" );
    FileInputStream fis = new FileInputStream( xmlFile );
    InputSource isrc = new InputSource( fis );
    data = xmlManager.loadAsLists( isrc );
    assertEquals( loadcanon, canon(data) );
    assertEquals( 2, xmlManager.getStats().getTotalRecords() );
    fis.close();

    fis = new FileInputStream( xmlFile );
    isrc = new InputSource( fis );
    data = xmlManager.loadAsLists( isrc );
    assertEquals( loadcanon, canon(data) );
    assertEquals( 2, xmlManager.getStats().getTotalRecords() );
    fis.close();

    loadcanon = "[[2b1, 2q1], [2b2, 2q2], [2b3, 2q3]]";
    xmlFile = TestUtil.findFile( fileprefix+"load02.xml" );
    fis = new FileInputStream( xmlFile );
    isrc = new InputSource( fis );
    data = xmlManager.loadAsLists( isrc );
    assertEquals( loadcanon, canon(data) );
    assertEquals( 3, xmlManager.getStats().getTotalRecords() );
    fis.close();

    loadcanon = "[[3b1, 3q1], [3b2, 3q2], [3b3, 3q3], [3b4, 3q4]]";
    xmlFile = TestUtil.findFile( fileprefix+"load03.xml" );
    fis = new FileInputStream( xmlFile );
    isrc = new InputSource( fis );
    data = xmlManager.loadAsLists( isrc );
    assertEquals( loadcanon, canon(data) );
    assertEquals( 4, xmlManager.getStats().getTotalRecords() );
    fis.close();


    // uri
    loadcanon  = "[[b1, q1], [b2, q2]]";
    xmlFile = TestUtil.findFile( fileprefix+"load.xml" );
    data = xmlManager.loadAsListsFromURI( TestUtil.makeURI( xmlFile ) );
    assertEquals( loadcanon, canon(data) );
    assertEquals( 2, xmlManager.getStats().getTotalRecords() );

    data = xmlManager.loadAsListsFromURI( TestUtil.makeURI( xmlFile ) );
    assertEquals( loadcanon, canon(data) );
    assertEquals( 2, xmlManager.getStats().getTotalRecords() );

    loadcanon = "[[2b1, 2q1], [2b2, 2q2], [2b3, 2q3]]";
    xmlFile = TestUtil.findFile( fileprefix+"load02.xml" );
    data = xmlManager.loadAsListsFromURI( TestUtil.makeURI( xmlFile ) );
    assertEquals( loadcanon, canon(data) );
    assertEquals( 3, xmlManager.getStats().getTotalRecords() );

    loadcanon = "[[3b1, 3q1], [3b2, 3q2], [3b3, 3q3], [3b4, 3q4]]";
    xmlFile = TestUtil.findFile( fileprefix+"load03.xml" );
    data = xmlManager.loadAsListsFromURI( TestUtil.makeURI( xmlFile ) );
    assertEquals( loadcanon, canon(data) );
    assertEquals( 4, xmlManager.getStats().getTotalRecords() );


    // string
    loadcanon  = "[[b1, q1], [b2, q2]]";
    xmlFile = TestUtil.findFile( fileprefix+"load.xml" );
    data = xmlManager.loadAsListsFromString( FileUtil.readFile(xmlFile) );
    assertEquals( loadcanon, canon(data) );
    assertEquals( 2, xmlManager.getStats().getTotalRecords() );

    data = xmlManager.loadAsListsFromString( FileUtil.readFile(xmlFile) );
    assertEquals( loadcanon, canon(data) );
    assertEquals( 2, xmlManager.getStats().getTotalRecords() );

    loadcanon = "[[2b1, 2q1], [2b2, 2q2], [2b3, 2q3]]";
    xmlFile = TestUtil.findFile( fileprefix+"load02.xml" );
    data = xmlManager.loadAsListsFromString( FileUtil.readFile(xmlFile) );
    assertEquals( loadcanon, canon(data) );
    assertEquals( 3, xmlManager.getStats().getTotalRecords() );

    loadcanon = "[[3b1, 3q1], [3b2, 3q2], [3b3, 3q3], [3b4, 3q4]]";
    xmlFile = TestUtil.findFile( fileprefix+"load03.xml" );
    data = xmlManager.loadAsListsFromString( FileUtil.readFile(xmlFile) );
    assertEquals( loadcanon, canon(data) );
    assertEquals( 4, xmlManager.getStats().getTotalRecords() );
  }


  private String canon( List pData ) throws Exception {
    return pData.toString();
  }
}





Syntax Highlighting created using the com.Ostermiller.Syntax package.
Thursday, February 23 2006 at 16:47