/* 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.in.test;


import com.ricebridge.xmlman.RecordSpec;
import com.ricebridge.xmlman.in.*;

import org.jostraca.util.*;

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

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


/** Test cases for {@link XmlOutputHandler}. 
 *    <p>The <b><a href="XmlOutputHandlerTest.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 XmlOutputHandlerTest extends TestCase {

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

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

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




  
  // test methods

  public OutNode makeNodeTree01() {
    OutNode root = new OutNode("root",1);
    OutNode a    = new OutNode("a",1);
    root.addChild(a);
    OutNode b    = new OutNode("b",1);
    a.addChild(b);
    OutNode c    = new OutNode("c",1);
    b.addChild(c);
    OutNode d    = new OutNode("d",1);
    b.addChild(d);
    OutNode e    = new OutNode("e",1);
    d.addChild(e);
    return root;
  }

  
  public void testTraverse() throws Exception {
    System.out.println(  );
    OutNode root = makeNodeTree01();
    XmlOutputHandler oh = new XmlOutputHandler();
    
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    XmlWriter xw = new XmlWriter();
    xw.setOutput(baos,"UTF-8");
    xw.setIndent(true);
    xw.startDocument("");
    xw.startElement("root",new ArrayList(),null);
    oh.setXmlWriter(xw);

    oh.traverse( root, false );
    oh.traverse( root, false );
    oh.traverse( root, false );

    xw.endElement("root");
    xw.endDocument("");

    System.out.println( baos.toString("UTF-8") );
  }


  public void testFindSubNode() throws Exception {
    OutNode root = makeNodeTree01();

    XmlOutputHandler oh = new XmlOutputHandler();
    OutNode f = oh.findSubNode( root, "a" );
    assertEquals( "a", f.getName() );

    f = oh.findSubNode( root, "d" );
    assertEquals( "d", f.getName() );
  }


  public void testCreateNodeTree() throws Exception {
    XmlOutputHandler oh = new XmlOutputHandler();

    ArrayList opl = new ArrayList();
    opl.add( new TestOutPath( new OutData( TestUtil.makeNameInfo("an") ), true, "a", OutPath.CHILD ) );
    opl.add( new TestOutPath( new OutData( TestUtil.makeNameInfo("bn") ), true, "a", OutPath.CHILD, "b", OutPath.CHILD ) );
    opl.add( new TestOutPath( new OutData(true), true, "a", OutPath.CHILD, "b", OutPath.CHILD, "d", OutPath.CHILD ) );
    opl.add( new TestOutPath( new OutData( TestUtil.makeNameInfo("dn") ), true, "d", OutPath.DESCEND ) );

    TestOutPath rec = new TestOutPath( new OutData(), true, "a", OutPath.CHILD, "b", OutPath.CHILD, "c", OutPath.CHILD );
    rec.setIsRecord(true);
    opl.add( rec );

    opl.add( new TestOutPath( new OutData( TestUtil.makeNameInfo("foo") ), false ) );
    opl.add( new TestOutPath( new OutData( TestUtil.makeNameInfo("en") ), false, "e", OutPath.CHILD ) );
    opl.add( new TestOutPath( new OutData(true), false, "e", OutPath.CHILD, "f", OutPath.CHILD ) );
    opl.add( new TestOutPath( new OutData( TestUtil.makeNameInfo("fn") ), false, "f", OutPath.DESCEND ) );
    opl.add( new TestOutPath( new OutData( TestUtil.makeNameInfo("gn") ), false, "g", OutPath.DESCEND ) );
    
    OutNode recn = oh.createNodeTree( opl );
    oh.makeChangeLists( recn );
    OutNode r    = oh.findRoot(recn);
    System.out.println( r.debugString() );
  }


  public void testWrite() {
    XmlOutputHandler oh = new XmlOutputHandler();
    RecordSpec rs = new RecordSpec( "/a/b/c", new String[] {"@cn","d/@dn","e","//f/@fn","/a/b/@bn", "/a/b/g/@gn"} );

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    XmlWriter xw = new XmlWriter();
    xw.setOutput(baos,"UTF-8");
    oh.setXmlWriter(xw);

    oh.prepare(rs);

    OutNode r    = oh.findRoot();
    System.out.println( r.debugString() );

    oh.startWrite();

    oh.writeRecord( new String[] {"c1","d1","e1","fn1","b1","g1"} );
    oh.writeRecord( new String[] {"c2","d2","e2","fn1","b1","g1"} );
    oh.writeRecord( new String[] {"c3","d3","e3","fn2","b1","g1"} );
    oh.writeRecord( new String[] {"c4","d4","e4","fn2","b1","g1"} );
    oh.writeRecord( new String[] {"c5","d5","e5","fn2","b1","g2"} );
    oh.writeRecord( new String[] {"c6","d6","e6","fn2","b1","g2"} );
    oh.writeRecord( new String[] {"c7","d7","e7","fn2","b2","g2"} );
    oh.writeRecord( new String[] {"c8","d8","e8","fn2","b2","g2"} );
    oh.writeRecord( new String[] {"c9","d9","e9","fn2","b2","g2"} );

    oh.endWrite(true);

    System.out.println( baos.toString() );
  }


  public void testNoRecords() {
    XmlOutputHandler oh = new XmlOutputHandler();
    RecordSpec rs = new RecordSpec( "/a/b/c", new String[] {"@cn","d/@dn","e","//f/@fn","/a/b/@bn", "/a/b/g/@gn"} );


    XmlWriter xw = new XmlWriter();
    oh.setXmlWriter(xw);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    xw.setOutput(baos,"UTF-8");
    oh.prepare(rs);
    oh.startWrite();
    oh.endWrite(true);
    System.out.println( rs+":NORECORDS:"+baos.toString() );

    baos = new ByteArrayOutputStream();
    xw.setOutput(baos,"UTF-8");
    rs = new RecordSpec( "/a/b/c", new String[] {"@c"} );
    oh.prepare(rs);
    oh.startWrite();
    oh.endWrite(true);
    System.out.println( rs+":NORECORDS:"+baos.toString() );

    baos = new ByteArrayOutputStream();
    xw.setOutput(baos,"UTF-8");
    rs = new RecordSpec( "/a/b", new String[] {"@b"} );
    oh.prepare(rs);
    oh.startWrite();
    oh.endWrite(true);
    System.out.println( rs+":NORECORDS:"+baos.toString() );
  }


  public void testValidRecordPath() {
    XmlOutputHandler oh = new XmlOutputHandler();
    RecordSpec rs = new RecordSpec( "", new String[] {"b"} );

    try {
      oh.prepare(rs);
      fail();
    }
    catch( Exception e ) {
      System.out.println( e );
    }
  }
}






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