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.*;
public class FunctionTest extends TestCase {
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 {
sRecordSpecList.add( new RecordSpec( "/root/foo[last()]", new String[] { "@n" } ) );
sRecordDataList.add( ListUtil.make( new String[] { "f1" },
new String[] { "f2" }) );
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" }) );
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" } ) );
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 " } ) );
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" } ) );
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" } ) );
sRecordSpecList.add( new RecordSpec( "/root/foo[1]", new String[] { "contains(@n,'f')", "contains(@n,'b')" } ) );
sRecordDataList.add( ListUtil.make( new String[] { "true","false" } ) );
sRecordSpecList.add( new RecordSpec( "/root/foo[1]", new String[] { "substring-before(@n,'1')" } ) );
sRecordDataList.add( ListUtil.make( new String[] { "f" } ) );
sRecordSpecList.add( new RecordSpec( "/root/foo[1]", new String[] { "substring-after(@n,'f')" } ) );
sRecordDataList.add( ListUtil.make( new String[] { "1" } ) );
sRecordSpecList.add( new RecordSpec( "/root/foo[1]", new String[] { "substring(.,2,2)" } ) );
sRecordDataList.add( ListUtil.make( new String[] { "1t" } ) );
sRecordSpecList.add( new RecordSpec( "/root/foo[1]", new String[] { "string-length(.)" } ) );
sRecordDataList.add( ListUtil.make( new String[] { "26" } ) );
sRecordSpecList.add( new RecordSpec( "/root/foo[1]", new String[] { "normalize-space(.)" } ) );
sRecordDataList.add( ListUtil.make( new String[] { "f1t1 f1t2" } ) );
sRecordSpecList.add( new RecordSpec( "/root/foo[1]", new String[] { "translate(@n,'f','F')" } ) );
sRecordDataList.add( ListUtil.make( new String[] { "F1" } ) );
sRecordSpecList.add( new RecordSpec( "/root/foo[1]", new String[] { "boolean(@n)","boolean(@v)" } ) );
sRecordDataList.add( ListUtil.make( new String[] { "true","true" } ) );
sRecordSpecList.add( new RecordSpec( "/root/foo[1]", new String[] { "not(-1)","not(0)","not(1)" } ) );
sRecordDataList.add( ListUtil.make( new String[] { "false","true","false" } ) );
sRecordSpecList.add( new RecordSpec( "/root/foo[1]", new String[] { "true()" } ) );
sRecordDataList.add( ListUtil.make( new String[] { "true" } ) );
sRecordSpecList.add( new RecordSpec( "/root/foo[1]", new String[] { "false()" } ) );
sRecordDataList.add( ListUtil.make( new String[] { "false" } ) );
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" } ) );
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" } ) );
sRecordSpecList.add( new RecordSpec( "/root/foo[1]", new String[] { "floor(1.1)", "floor(1.9)", } ) );
sRecordDataList.add( ListUtil.make( new String[] { "1", "1" } ) );
sRecordSpecList.add( new RecordSpec( "/root/foo[1]", new String[] { "ceiling(1.1)", "ceiling(1.9)", } ) );
sRecordDataList.add( ListUtil.make( new String[] { "2", "2" } ) );
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" } ) );
}
public void testUnsupported() {
XmlInputHandler xih = new XmlInputHandler();
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()); }
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()); }
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 );
}
}