/* 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 save handling.
 *    <p>The <b><a href="SaveResultSetTest.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 SaveResultSetTest extends TestCase {

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

  public static TestSuite suite() {
    return new TestSuite( SaveResultSetTest.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/save.xml" );
    XmlManager   xmlManager = new XmlManager();
    RecordSpec   rs         = new RecordSpec( "/root/record", new String[] {"@name","foo","bar"},
                                              new String[] {"Name","Foo","Bar"} );
    ArrayList    data       = new ArrayList();
    data.add( new String[] {"r1","f1","b1"} );
    data.add( new String[] {"r2","f2","b2"} );
    data.add( new String[] {"r3","f3","b3"} );
    XmlResultSet resultset = new XmlResultSet( data );

    xmlManager.saveResultSet( xmlFile, rs, resultset );

    assertEquals( "<root>\n  <record name=\"r1\">\n    <foo>f1</foo>\n    <bar>b1</bar>\n  </record>\n  <record name=\"r2\">\n    <foo>f2</foo>\n    <bar>b2</bar>\n  </record>\n  <record name=\"r3\">\n    <foo>f3</foo>\n    <bar>b3</bar>\n  </record>\n</root>", 
                  FileUtil.readFile(xmlFile) );
  }


  public void testSingle() throws Exception {
    ArrayList data = new ArrayList();
    data.add( new String[] {"b1","q1"} );
    data.add( new String[] {"b2","q2"} );
    XmlResultSet resultset = new XmlResultSet( data );

    String savecanon = "<foo><bar name=\"b1\"><que>q1</que></bar><bar name=\"b2\"><que>q2</que></bar></foo>";

    File xmlFile = null;
    xmlFile = TestUtil.findFile( "com/ricebridge/xmlman/test/save.xml" );
    XmlManager xmlManager = new XmlManager();
    XmlSpec    xs         = xmlManager.getXmlSpec();
    xs.setIndent(false);

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

    FileUtil.writeFile(xmlFile,"");
    xmlManager.saveResultSet( xmlFile, rs, resultset );
    assertEquals( savecanon, FileUtil.readFile(xmlFile) );

    FileUtil.writeFile(xmlFile,"");
    xmlManager.saveResultSet( xmlFile.getAbsolutePath(), rs, resultset );
    assertEquals( savecanon, FileUtil.readFile(xmlFile) );

    FileUtil.writeFile(xmlFile,"");
    OutputStream os = new FileOutputStream( xmlFile );
    xmlManager.saveResultSet( os, rs, resultset );
    assertEquals( savecanon, FileUtil.readFile(xmlFile) );
    os.close();

    String xmlstr = xmlManager.saveResultSetToString( rs, resultset );
    assertEquals( savecanon, xmlstr );
  }



  public void testRepeat() throws Exception {
    ArrayList data1 = new ArrayList();
    data1.add( new String[] {"b1","q1"} );
    data1.add( new String[] {"b2","q2"} );
    XmlResultSet resultset1 = new XmlResultSet( data1 );
    String savecanon1 = "<foo><bar name=\"b1\"><que>q1</que></bar><bar name=\"b2\"><que>q2</que></bar></foo>";

    File xmlFile = null;
    xmlFile = TestUtil.findFile( "com/ricebridge/xmlman/test/save.xml" );

    RecordSpec rs1 = new RecordSpec( "/foo/bar", new String[] {"@name","que"} );
    XmlManager xmlManager = new XmlManager( rs1 );
    xmlManager.getXmlSpec().setIndent(false);

    FileUtil.writeFile(xmlFile,"");
    xmlManager.saveResultSet( xmlFile, resultset1 );
    assertEquals( savecanon1, FileUtil.readFile(xmlFile) );

    FileUtil.writeFile(xmlFile,"");
    xmlManager.saveResultSet( xmlFile.getAbsolutePath(), resultset1 );
    assertEquals( savecanon1, FileUtil.readFile(xmlFile) );

    FileUtil.writeFile(xmlFile,"");
    OutputStream os = new FileOutputStream( xmlFile );
    xmlManager.saveResultSet( os, resultset1 );
    assertEquals( savecanon1, FileUtil.readFile(xmlFile) );
    os.close();

    String xmlstr = xmlManager.saveResultSetToString( resultset1 );
    assertEquals( savecanon1, xmlstr );


    ArrayList data2 = new ArrayList();
    data2.add( new String[] {"2b1","2q1"} );
    data2.add( new String[] {"2b2","2q2"} );
    data2.add( new String[] {"2b3","2q3"} );
    XmlResultSet resultset2 = new XmlResultSet( data2 );
    String savecanon2 = "<foo><bar name=\"2b1\"><que>2q1</que></bar><bar name=\"2b2\"><que>2q2</que></bar>"
      +"<bar name=\"2b3\"><que>2q3</que></bar></foo>";

    FileUtil.writeFile(xmlFile,"");
    xmlManager.saveResultSet( xmlFile, resultset2 );
    assertEquals( savecanon2, FileUtil.readFile(xmlFile) );

    FileUtil.writeFile(xmlFile,"");
    xmlManager.saveResultSet( xmlFile.getAbsolutePath(), resultset2 );
    assertEquals( savecanon2, FileUtil.readFile(xmlFile) );

    FileUtil.writeFile(xmlFile,"");
    os = new FileOutputStream( xmlFile );
    xmlManager.saveResultSet( os, resultset2 );
    assertEquals( savecanon2, FileUtil.readFile(xmlFile) );
    os.close();

    xmlstr = xmlManager.saveResultSetToString( resultset2 );
    assertEquals( savecanon2, xmlstr );


    ArrayList data3 = new ArrayList();
    data3.add( new String[] {"3b1","3q1"} );
    data3.add( new String[] {"3b2","3q2"} );
    data3.add( new String[] {"3b3","3q3"} );
    data3.add( new String[] {"3b4","3q4"} );
    XmlResultSet resultset3 = new XmlResultSet( data3 );
    String savecanon3 = "<foo><bar name=\"3b1\"><que>3q1</que></bar><bar name=\"3b2\"><que>3q2</que></bar>"
      +"<bar name=\"3b3\"><que>3q3</que></bar><bar name=\"3b4\"><que>3q4</que></bar></foo>";

    FileUtil.writeFile(xmlFile,"");
    xmlManager.saveResultSet( xmlFile, resultset3 );
    assertEquals( savecanon3, FileUtil.readFile(xmlFile) );

    FileUtil.writeFile(xmlFile,"");
    xmlManager.saveResultSet( xmlFile.getAbsolutePath(), resultset3 );
    assertEquals( savecanon3, FileUtil.readFile(xmlFile) );

    FileUtil.writeFile(xmlFile,"");
    os = new FileOutputStream( xmlFile );
    xmlManager.saveResultSet( os, resultset3 );
    assertEquals( savecanon3, FileUtil.readFile(xmlFile) );
    os.close();

    xmlstr = xmlManager.saveResultSetToString( resultset3 );
    assertEquals( savecanon3, xmlstr );
  }


}





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