/* 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.*;
import com.ricebridge.xmlman.in.*;

import org.jostraca.util.*;

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

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

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


/** Test cases for functions. 
 *    <p>Test file is <a href="function.xml">function .xml</a>.</p> 
 *    <p>The <b><a href="FunctionTest.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 FunctionTest extends TestCase {

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

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

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



  private static ArrayList sRecordSpecList = new ArrayList();
  private static ArrayList sRecordDataList = new ArrayList();


  static {
    // last - always true
    sRecordSpecList.add( new RecordSpec( "/root/foo[last()]", new String[] { "@n" }  ) );
    sRecordDataList.add( ListUtil.make( new String[] { "f1" },
                                        new String[] { "f2" }) );

    // but this is still useful
    sRecordSpecList.add( new RecordSpec( "/root/foo", new String[] { "@n", "bar[last()]/@n" }  ) );
    sRecordDataList.add( ListUtil.make( new String[] { "f1", "1b2" },
                                        new String[] { "f2", "2b2" }) );

    sRecordSpecList.add( new RecordSpec( "/root/foo", new String[] { "@n", "rb:trim(text()[last()])" }  ) );
    sRecordDataList.add( ListUtil.make( new String[] { "f1", "f1t2" },
                                        new String[] { "f2", "f2t2" }) );


    // position
    sRecordSpecList.add( new RecordSpec( "/root/foo[1=position()]", new String[] { "@n" }  ) );
    sRecordDataList.add( ListUtil.make( new String[] { "f1" } ) );

    sRecordSpecList.add( new RecordSpec( "/root/foo[2]", new String[] { "@n" }  ) );
    sRecordDataList.add( ListUtil.make( new String[] { "f2" } ) );

    sRecordSpecList.add( new RecordSpec( "/root/foo", new String[] { "@n", "bar[2=position()]/@n" }  ) );
    sRecordDataList.add( ListUtil.make( new String[] { "f1", "1b2" },
                                        new String[] { "f2", "2b2" } ) );

    sRecordSpecList.add( new RecordSpec( "/root/foo", new String[] { "@n", "bar[1]/@n" }  ) );
    sRecordDataList.add( ListUtil.make( new String[] { "f1", "1b1" },
                                        new String[] { "f2", "2b1" } ) );


    // string
    sRecordSpecList.add( new RecordSpec( "/root/foo", new String[] { "@n", "string(.)" }  ) );
    sRecordDataList.add( ListUtil.make( new String[] { "f1", "f1t1\n    \n    \n    f1t2\n  " },
                                        new String[] { "f2", "f2t1\n    \n    \n    f2t2\n  " } ) );


    // concat
    sRecordSpecList.add( new RecordSpec( "/root/foo[1]", new String[] { "concat(@n,@v)", "concat(@n,@v,@id,@xml:lang)" }  ) );
    sRecordDataList.add( ListUtil.make( new String[] { "f11","f11i1en" } ) );

    
    // starts-with
    sRecordSpecList.add( new RecordSpec( "/root/foo[1]", new String[] { "starts-with(@n,'f')", "starts-with(@n,'b')" }  ) );
    sRecordDataList.add( ListUtil.make( new String[] { "true","false" } ) );

    // contains
    sRecordSpecList.add( new RecordSpec( "/root/foo[1]", new String[] { "contains(@n,'f')", "contains(@n,'b')" }  ) );
    sRecordDataList.add( ListUtil.make( new String[] { "true","false" } ) );

    // substring-before
    sRecordSpecList.add( new RecordSpec( "/root/foo[1]", new String[] { "substring-before(@n,'1')" }  ) );
    sRecordDataList.add( ListUtil.make( new String[] { "f" } ) );

    // substring-after
    sRecordSpecList.add( new RecordSpec( "/root/foo[1]", new String[] { "substring-after(@n,'f')" }  ) );
    sRecordDataList.add( ListUtil.make( new String[] { "1" } ) );

    // substring
    sRecordSpecList.add( new RecordSpec( "/root/foo[1]", new String[] { "substring(.,2,2)" }  ) );
    sRecordDataList.add( ListUtil.make( new String[] { "1t" } ) );

    // string-length
    sRecordSpecList.add( new RecordSpec( "/root/foo[1]", new String[] { "string-length(.)" }  ) );
    sRecordDataList.add( ListUtil.make( new String[] { "26" } ) );

    // normalize-space
    sRecordSpecList.add( new RecordSpec( "/root/foo[1]", new String[] { "normalize-space(.)" }  ) );
    sRecordDataList.add( ListUtil.make( new String[] { "f1t1 f1t2" } ) );
    
    // translate
    sRecordSpecList.add( new RecordSpec( "/root/foo[1]", new String[] { "translate(@n,'f','F')" }  ) );
    sRecordDataList.add( ListUtil.make( new String[] { "F1" } ) );


    // boolean
    sRecordSpecList.add( new RecordSpec( "/root/foo[1]", new String[] { "boolean(@n)","boolean(@v)" }  ) );
    sRecordDataList.add( ListUtil.make( new String[] { "true","true" } ) );

    // not
    sRecordSpecList.add( new RecordSpec( "/root/foo[1]", new String[] { "not(-1)","not(0)","not(1)" }  ) );
    sRecordDataList.add( ListUtil.make( new String[] { "false","true","false" } ) );

    // true
    sRecordSpecList.add( new RecordSpec( "/root/foo[1]", new String[] { "true()" }  ) );
    sRecordDataList.add( ListUtil.make( new String[] { "true" } ) );

    // false
    sRecordSpecList.add( new RecordSpec( "/root/foo[1]", new String[] { "false()" }  ) );
    sRecordDataList.add( ListUtil.make( new String[] { "false" } ) );
    
    
    // lang
    sRecordSpecList.add( new RecordSpec( "/root/foo", new String[] { "@n", "lang('en')" }  ) );
    sRecordDataList.add( ListUtil.make( new String[] { "f1", "true" },
                                        new String[] { "f2", "false" } ) );

    sRecordSpecList.add( new RecordSpec( "/root/foo[lang('de')]", new String[] { "@n" }  ) );
    sRecordDataList.add( ListUtil.make( new String[] { "f2" } ) );

    
    // number
    sRecordSpecList.add( new RecordSpec( "/root/foo[1]", new String[] { "number(1+2)", "number(@v)", "number(@n)" }  ) );
    sRecordDataList.add( ListUtil.make( new String[] { "3", "1", "NaN" } ) );

    // floor
    sRecordSpecList.add( new RecordSpec( "/root/foo[1]", new String[] { "floor(1.1)", "floor(1.9)", }  ) );
    sRecordDataList.add( ListUtil.make( new String[] { "1", "1" } ) );

    // ceiling
    sRecordSpecList.add( new RecordSpec( "/root/foo[1]", new String[] { "ceiling(1.1)", "ceiling(1.9)", }  ) );
    sRecordDataList.add( ListUtil.make( new String[] { "2", "2" } ) );

    // round
    sRecordSpecList.add( new RecordSpec( "/root/foo[1]", new String[] { "round(1.1)", "round(1.9)", "round(1.5)", "round(2.5)"} ) );
    sRecordDataList.add( ListUtil.make( new String[] { "1", "2", "2", "3" } ) );
    
  } 
  


  // test methods

  public void testUnsupported() {
    XmlInputHandler xih = new XmlInputHandler();

    // count - because: requires nodeset
    try { 
      RecordSpec rs = new RecordSpec("/root/foo", new String[]{"@n","count(.)"}); 
      xih.prepare(rs);
      fail(); 
    }
    catch( XmlManagerException xme ) { assertEquals(XmlManagerException.CODE_unsup_func,xme.getCode()); }

    // id - because: returns nodeset
    try { 
      RecordSpec rs = new RecordSpec("id('a1')", new String[]{"@n"}); 
      xih.prepare(rs);
      fail(); 
    }
    catch( XmlManagerException xme ) { assertEquals(XmlManagerException.CODE_bad_record_path,xme.getCode()); }

    // sum - because: requires nodeset
    try { 
      RecordSpec rs = new RecordSpec("//foo", new String[]{"sum(@n)"}); 
      xih.prepare(rs);
      fail(); 
    }
    catch( XmlManagerException xme ) { assertEquals(XmlManagerException.CODE_unsup_func,xme.getCode()); }
  }



  public void testFunctions() throws Exception {
    XmlSpec xs = new XmlSpec();
    TestUtil.testRecords( "com/ricebridge/xmlman/in/test/function.xml", xs, sRecordSpecList, sRecordDataList, this );
  }

}





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