/* 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.data.BeanSpec;

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.*;


/** Test cases for {@link com.ricebridge.xmlman.XmlManager}.
 *    <p>Test files are <a href="atom.xml">atom.xml</a>, 
 *  <a href="rss-rbnews.xml">rss-rbnews.xml</a>,
 *  <a href="load.xml">load.xml</a>. </p>  
 *    <p>The <b><a href="XmlManagerTest.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 XmlManagerTest extends TestCase {

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

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

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



  // test cases

  public void testSimple() throws Exception {
    File xmlfile = null;

    xmlfile = TestUtil.findFile( "com/ricebridge/xmlman/test/rss-rbnews.xml" );
    XmlManager xmlman = new XmlManager();
    RecordSpec rs     = new RecordSpec("//item", new String[] { "/rss/channel/title", "title", "link" } );
    List       data   = xmlman.load( xmlfile, rs );
    System.out.println( TestUtil.dataToString(data) );
  }


  public void testBeans() throws Exception {
    File xmlfile = null;

    xmlfile = TestUtil.findFile( "com/ricebridge/xmlman/test/rss-rbnews.xml" );
    XmlManager xmlman = new XmlManager();
    RecordSpec rs     = new RecordSpec("//item", 
                                       new String[] { "pubDate", "title", "link" },
                                       new String[] { "PubDate", "Title", "Link" } );
    List       data   = xmlman.loadBeans( xmlfile, rs, new BeanSpec(ItemBean.class) );
    System.out.println( data );
  }


  private static String sAtomNS       = "http://www.w3.org/2005/Atom";
  private static String sAtomFilePath = "com/ricebridge/xmlman/test/atom.xml";

  // paths derived from http://www.xml.com/pub/a/2004/04/07/dive.html
  private static RecordSpec sAtomFeedRS 
    = new RecordSpec("/a:feed", 
                     new String[] { 
                       "'feed'",
                       "a:title", // feed-level title

                       // article def. seems too strict
                       // if you use a default ns, then attr are not in it!
                       "a:link[not(@rel) or @rel='alternate']/@href",

                       "@xml:base",

                       "a:subtitle/@type",
                       "rb:xml(a:subtitle)",
                     } );

  private static RecordSpec sAtomEntryRS 
    = new RecordSpec("/a:feed/a:entry", 
                     new String[] { 
                       "'entry'",
                       "a:title",       // entry-level title 

                       "a:link[not(@rel) or @rel='alternate']/@href",

                       "a:summary/@type",
                       "rb:xml(a:summary)",

                       "a:content/@type",
                       "rb:xml(a:content)",
                     } );



  public void testAtom() throws Exception {
    XmlManager xmlman  = new XmlManager();
    XmlSpec    xmlspec = xmlman.getXmlSpec();
    xmlspec.addNamespace("a",sAtomNS);
    xmlspec.addNamespace("xml","http://www.w3.org/XML/1998/namespace");

    File xmlfile = TestUtil.findFile( sAtomFilePath );
    List data = xmlman.load( xmlfile, new RecordSpec( sAtomFeedRS, sAtomEntryRS ) );
    System.out.println( "data:"+TestUtil.dataToString(data) );
  }



  private static String sLoadFilePath = "com/ricebridge/xmlman/test/load.xml";

  public void testLoad() throws Exception {
    XmlManager xmlman   = new XmlManager();
    List       data     = null;

    RecordSpec rs_bar   = new RecordSpec("/foo/bar", new String[] { "@name", "que" } );
    List       data_bar = ListUtil.make( new String[] {"b1", "q1"},
                                         new String[] {"b2", "q2"} );

    RecordSpec rs_far   = new RecordSpec("/foo/far", new String[] { "@name", "zoo" } );
    List       data_far = ListUtil.make( new String[] {"f1", "z1"} );

    List data_barfar = new ArrayList();
    data_barfar.addAll( data_bar );
    data_barfar.addAll( data_far );

    RecordSpec rs = new RecordSpec( rs_bar, rs_far );

    File xmlfile = TestUtil.findFile( sLoadFilePath );

    // file path 
    String xmlfilepath = xmlfile.getAbsolutePath();
    data = xmlman.load( xmlfilepath, rs_bar );
    assertEquals( rs_bar+TestUtil.dataToString(data_bar), rs_bar+TestUtil.dataToString(data) );
    data = xmlman.load( xmlfilepath, rs );
    assertEquals( rs_bar+TestUtil.dataToString(data_barfar), rs_bar+TestUtil.dataToString(data) );

    // file
    data = xmlman.load( xmlfile, rs_bar );
    assertEquals( rs_bar+TestUtil.dataToString(data_bar), rs_bar+TestUtil.dataToString(data) );
    data = xmlman.load( xmlfile, rs );
    assertEquals( rs_bar+TestUtil.dataToString(data_barfar), rs_bar+TestUtil.dataToString(data) );

    // inputstream
    InputStream is = new FileInputStream( xmlfile );
    data = xmlman.load( is, rs_bar );
    assertEquals( rs_bar+TestUtil.dataToString(data_bar), rs_bar+TestUtil.dataToString(data) );
    is.close();
    is = new FileInputStream( xmlfile );
    data = xmlman.load( is, rs );
    assertEquals( rs_bar+TestUtil.dataToString(data_barfar), rs_bar+TestUtil.dataToString(data) );
    is.close();

    // inputsource
    is = new FileInputStream( xmlfile );
    InputSource isrc = new InputSource(is);
    data = xmlman.load( isrc, rs_bar );
    assertEquals( rs_bar+TestUtil.dataToString(data_bar), rs_bar+TestUtil.dataToString(data) );
    is.close();
    is = new FileInputStream( xmlfile );
    isrc = new InputSource(is);
    data = xmlman.load( isrc, rs );
    assertEquals( rs_bar+TestUtil.dataToString(data_barfar), rs_bar+TestUtil.dataToString(data) );
    is.close();

    // uri 
    String uri = xmlfile.getAbsolutePath();
    if( !uri.startsWith("/") ) { uri = "/"+uri; }
    uri = "file://"+uri;
    uri = uri.replace('\\','/');
    data = xmlman.loadFromURI( uri, rs_bar );
    assertEquals( rs_bar+TestUtil.dataToString(data_bar), rs_bar+TestUtil.dataToString(data) );
    data = xmlman.loadFromURI( uri, rs );
    assertEquals( rs_bar+TestUtil.dataToString(data_barfar), rs_bar+TestUtil.dataToString(data) );

    // string 
    String xmlstring = FileUtil.readFile( xmlfile );
    data = xmlman.loadFromString( xmlstring, rs_bar );
    assertEquals( rs_bar+TestUtil.dataToString(data_bar), rs_bar+TestUtil.dataToString(data) );
    data = xmlman.loadFromString( xmlstring, rs );
    assertEquals( rs_bar+TestUtil.dataToString(data_barfar), rs_bar+TestUtil.dataToString(data) );

  }


  
  public static class ItemBean {
    private String iTitle;
    private String iLink;
    private String iPubDate;

    public void setTitle( String pTitle ) {
      iTitle = pTitle;
    }

    public String getTitle() {
      return iTitle;
    }

    public void setLink( String pLink ) {
      iLink = pLink;
    }

    public String getLink() {
      return iLink;
    }

    public void setPubDate( String pPubDate ) {
      iPubDate = pPubDate;
    }

    public String getPubDate() {
      return iPubDate;
    }

    public String toString() {
      return "Item[t="+getTitle()+",l="+getLink()+",pd="+getPubDate()+"]";
    }
  }


}





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