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.*;
public class LoadAsListsTest extends TestCase {
public LoadAsListsTest( String pName ) {
super( pName );
}
public static TestSuite suite() {
return new TestSuite( LoadAsListsTest.class );
}
public static void main( String[] pArgs ) {
TestRunner.run( suite() );
}
public void testExample() throws Exception {
File xmlFile = TestUtil.findFile( "com/ricebridge/xmlman/test/example.xml" );
XmlManager xmlManager = new XmlManager();
RecordSpec rs = new RecordSpec( "/root/record", new String[] {"@name","foo","bar"} );
List data = xmlManager.loadAsLists( xmlFile, rs );
StringBuffer sb = new StringBuffer();
for( int record = 0; record < data.size(); record++ ) {
sb.append( "record: "+record+"\n" );
List fields = (List) data.get(record);
for( int field = 0; field < fields.size(); field++ ) {
sb.append( "field "+field+" has value: "+fields.get(field)+"\n" );
}
}
String out = "record: 0\nfield 0 has value: r1\nfield 1 has value: f1\nfield 2 has value: b1\nrecord: 1\nfield 0 has value: r2\nfield 1 has value: f2\nfield 2 has value: b2\nrecord: 2\nfield 0 has value: r3\nfield 1 has value: f3\nfield 2 has value: b3\n";
assertEquals( out, sb.toString() );
}
public void testSingle() throws Exception {
String loadcanon = "[[b1, q1], [b2, q2]]";
File xmlFile = null;
xmlFile = TestUtil.findFile( "com/ricebridge/xmlman/test/load.xml" );
XmlManager xmlManager = new XmlManager();
RecordSpec rs = new RecordSpec( "/foo/bar",
new String[] {"@name","que"},
new String[] {"Name","Que"} );
List data = xmlManager.loadAsLists( xmlFile, rs );
assertEquals( loadcanon, canon(data) );
data = xmlManager.loadAsLists( xmlFile.getAbsolutePath(), rs );
assertEquals( loadcanon, canon(data) );
InputStream is = new FileInputStream( xmlFile );
data = xmlManager.loadAsLists( is, rs );
assertEquals( loadcanon, canon(data) );
is.close();
InputStream fis = new FileInputStream( xmlFile );
InputSource isrc = new InputSource( fis );
data = xmlManager.loadAsLists( isrc, rs );
assertEquals( loadcanon, canon(data) );
fis.close();
data = xmlManager.loadAsListsFromURI( TestUtil.makeURI(xmlFile), rs );
assertEquals( loadcanon, canon(data) );
data = xmlManager.loadAsListsFromString( FileUtil.readFile(xmlFile), rs );
assertEquals( loadcanon, canon(data) );
}
public void testMultiple() throws Exception {
String loadcanon = "[[b, b1, q1], [b, b2, q2], [f, f1, z1]]";
File xmlFile = null;
xmlFile = TestUtil.findFile( "com/ricebridge/xmlman/test/load.xml" );
XmlManager xmlManager = new XmlManager();
RecordSpec rs_b = new RecordSpec( "/foo/bar",
new String[] {"'b'","@name","que"},
new String[] {"Type","Name","Value"} );
RecordSpec rs_f = new RecordSpec( "/foo/far",
new String[] {"'f'","@name","zoo"},
new String[] {"These","Headers","Ignored"} );
List rsl = ListUtil.make( rs_b, rs_f );
RecordSpec rs = new RecordSpec( rs_b, rs_f );
List data = xmlManager.loadAsLists( xmlFile, rs );
assertEquals( loadcanon, canon(data) );
data = xmlManager.loadAsLists( xmlFile.getAbsolutePath(), rs );
assertEquals( loadcanon, canon(data) );
InputStream is = new FileInputStream( xmlFile );
data = xmlManager.loadAsLists( is, rs );
assertEquals( loadcanon, canon(data) );
is.close();
InputStream fis = new FileInputStream( xmlFile );
InputSource isrc = new InputSource( fis );
data = xmlManager.loadAsLists( isrc, rs );
assertEquals( loadcanon, canon(data) );
fis.close();
data = xmlManager.loadAsListsFromURI( TestUtil.makeURI(xmlFile), rs );
assertEquals( loadcanon, canon(data) );
data = xmlManager.loadAsListsFromString( FileUtil.readFile(xmlFile), rs );
assertEquals( loadcanon, canon(data) );
xmlManager = new XmlManager(rs);
data = xmlManager.loadAsLists( xmlFile );
assertEquals( loadcanon, canon(data) );
data = xmlManager.loadAsLists( xmlFile.getAbsolutePath() );
assertEquals( loadcanon, canon(data) );
is = new FileInputStream( xmlFile );
data = xmlManager.loadAsLists( is );
assertEquals( loadcanon, canon(data) );
is.close();
fis = new FileInputStream( xmlFile );
isrc = new InputSource( fis );
data = xmlManager.loadAsLists( isrc );
assertEquals( loadcanon, canon(data) );
fis.close();
data = xmlManager.loadAsListsFromURI( TestUtil.makeURI(xmlFile) );
assertEquals( loadcanon, canon(data) );
data = xmlManager.loadAsListsFromString( FileUtil.readFile(xmlFile) );
assertEquals( loadcanon, canon(data) );
}
public void testRepeat() throws Exception {
String fileprefix = "com/ricebridge/xmlman/test/";
File xmlFile = null;
xmlFile = TestUtil.findFile( fileprefix+"load.xml" );
RecordSpec rs = new RecordSpec( "/foo/bar",
new String[] {"@name","que"},
new String[] {"Name","Que"} );
XmlManager xmlManager = new XmlManager(rs);
String loadcanon = "[[b1, q1], [b2, q2]]";
List data = xmlManager.loadAsLists( xmlFile );
assertEquals( loadcanon, canon(data) );
assertEquals( 2, xmlManager.getStats().getTotalRecords() );
data = xmlManager.loadAsLists( xmlFile );
assertEquals( loadcanon, canon(data) );
assertEquals( 2, xmlManager.getStats().getTotalRecords() );
loadcanon = "[[2b1, 2q1], [2b2, 2q2], [2b3, 2q3]]";
xmlFile = TestUtil.findFile( fileprefix+"load02.xml" );
data = xmlManager.loadAsLists( xmlFile );
assertEquals( loadcanon, canon(data) );
assertEquals( 3, xmlManager.getStats().getTotalRecords() );
loadcanon = "[[3b1, 3q1], [3b2, 3q2], [3b3, 3q3], [3b4, 3q4]]";
xmlFile = TestUtil.findFile( fileprefix+"load03.xml" );
data = xmlManager.loadAsLists( xmlFile );
assertEquals( loadcanon, canon(data) );
assertEquals( 4, xmlManager.getStats().getTotalRecords() );
loadcanon = "[[b1, q1], [b2, q2]]";
xmlFile = TestUtil.findFile( fileprefix+"load.xml" );
data = xmlManager.loadAsLists( xmlFile.getAbsolutePath() );
assertEquals( loadcanon, canon(data) );
assertEquals( 2, xmlManager.getStats().getTotalRecords() );
data = xmlManager.loadAsLists( xmlFile.getAbsolutePath() );
assertEquals( loadcanon, canon(data) );
assertEquals( 2, xmlManager.getStats().getTotalRecords() );
loadcanon = "[[2b1, 2q1], [2b2, 2q2], [2b3, 2q3]]";
xmlFile = TestUtil.findFile( fileprefix+"load02.xml" );
data = xmlManager.loadAsLists( xmlFile.getAbsolutePath() );
assertEquals( loadcanon, canon(data) );
assertEquals( 3, xmlManager.getStats().getTotalRecords() );
loadcanon = "[[3b1, 3q1], [3b2, 3q2], [3b3, 3q3], [3b4, 3q4]]";
xmlFile = TestUtil.findFile( fileprefix+"load03.xml" );
data = xmlManager.loadAsLists( xmlFile.getAbsolutePath() );
assertEquals( loadcanon, canon(data) );
assertEquals( 4, xmlManager.getStats().getTotalRecords() );
loadcanon = "[[b1, q1], [b2, q2]]";
xmlFile = TestUtil.findFile( fileprefix+"load.xml" );
InputStream is = new FileInputStream( xmlFile );
data = xmlManager.loadAsLists( is );
assertEquals( loadcanon, canon(data) );
assertEquals( 2, xmlManager.getStats().getTotalRecords() );
is.close();
is = new FileInputStream( xmlFile );
data = xmlManager.loadAsLists( is );
assertEquals( loadcanon, canon(data) );
assertEquals( 2, xmlManager.getStats().getTotalRecords() );
is.close();
loadcanon = "[[2b1, 2q1], [2b2, 2q2], [2b3, 2q3]]";
xmlFile = TestUtil.findFile( fileprefix+"load02.xml" );
is = new FileInputStream( xmlFile );
data = xmlManager.loadAsLists( is );
assertEquals( loadcanon, canon(data) );
assertEquals( 3, xmlManager.getStats().getTotalRecords() );
is.close();
loadcanon = "[[3b1, 3q1], [3b2, 3q2], [3b3, 3q3], [3b4, 3q4]]";
xmlFile = TestUtil.findFile( fileprefix+"load03.xml" );
is = new FileInputStream( xmlFile );
data = xmlManager.loadAsLists( is );
assertEquals( loadcanon, canon(data) );
assertEquals( 4, xmlManager.getStats().getTotalRecords() );
is.close();
loadcanon = "[[b1, q1], [b2, q2]]";
xmlFile = TestUtil.findFile( fileprefix+"load.xml" );
FileInputStream fis = new FileInputStream( xmlFile );
InputSource isrc = new InputSource( fis );
data = xmlManager.loadAsLists( isrc );
assertEquals( loadcanon, canon(data) );
assertEquals( 2, xmlManager.getStats().getTotalRecords() );
fis.close();
fis = new FileInputStream( xmlFile );
isrc = new InputSource( fis );
data = xmlManager.loadAsLists( isrc );
assertEquals( loadcanon, canon(data) );
assertEquals( 2, xmlManager.getStats().getTotalRecords() );
fis.close();
loadcanon = "[[2b1, 2q1], [2b2, 2q2], [2b3, 2q3]]";
xmlFile = TestUtil.findFile( fileprefix+"load02.xml" );
fis = new FileInputStream( xmlFile );
isrc = new InputSource( fis );
data = xmlManager.loadAsLists( isrc );
assertEquals( loadcanon, canon(data) );
assertEquals( 3, xmlManager.getStats().getTotalRecords() );
fis.close();
loadcanon = "[[3b1, 3q1], [3b2, 3q2], [3b3, 3q3], [3b4, 3q4]]";
xmlFile = TestUtil.findFile( fileprefix+"load03.xml" );
fis = new FileInputStream( xmlFile );
isrc = new InputSource( fis );
data = xmlManager.loadAsLists( isrc );
assertEquals( loadcanon, canon(data) );
assertEquals( 4, xmlManager.getStats().getTotalRecords() );
fis.close();
loadcanon = "[[b1, q1], [b2, q2]]";
xmlFile = TestUtil.findFile( fileprefix+"load.xml" );
data = xmlManager.loadAsListsFromURI( TestUtil.makeURI( xmlFile ) );
assertEquals( loadcanon, canon(data) );
assertEquals( 2, xmlManager.getStats().getTotalRecords() );
data = xmlManager.loadAsListsFromURI( TestUtil.makeURI( xmlFile ) );
assertEquals( loadcanon, canon(data) );
assertEquals( 2, xmlManager.getStats().getTotalRecords() );
loadcanon = "[[2b1, 2q1], [2b2, 2q2], [2b3, 2q3]]";
xmlFile = TestUtil.findFile( fileprefix+"load02.xml" );
data = xmlManager.loadAsListsFromURI( TestUtil.makeURI( xmlFile ) );
assertEquals( loadcanon, canon(data) );
assertEquals( 3, xmlManager.getStats().getTotalRecords() );
loadcanon = "[[3b1, 3q1], [3b2, 3q2], [3b3, 3q3], [3b4, 3q4]]";
xmlFile = TestUtil.findFile( fileprefix+"load03.xml" );
data = xmlManager.loadAsListsFromURI( TestUtil.makeURI( xmlFile ) );
assertEquals( loadcanon, canon(data) );
assertEquals( 4, xmlManager.getStats().getTotalRecords() );
loadcanon = "[[b1, q1], [b2, q2]]";
xmlFile = TestUtil.findFile( fileprefix+"load.xml" );
data = xmlManager.loadAsListsFromString( FileUtil.readFile(xmlFile) );
assertEquals( loadcanon, canon(data) );
assertEquals( 2, xmlManager.getStats().getTotalRecords() );
data = xmlManager.loadAsListsFromString( FileUtil.readFile(xmlFile) );
assertEquals( loadcanon, canon(data) );
assertEquals( 2, xmlManager.getStats().getTotalRecords() );
loadcanon = "[[2b1, 2q1], [2b2, 2q2], [2b3, 2q3]]";
xmlFile = TestUtil.findFile( fileprefix+"load02.xml" );
data = xmlManager.loadAsListsFromString( FileUtil.readFile(xmlFile) );
assertEquals( loadcanon, canon(data) );
assertEquals( 3, xmlManager.getStats().getTotalRecords() );
loadcanon = "[[3b1, 3q1], [3b2, 3q2], [3b3, 3q3], [3b4, 3q4]]";
xmlFile = TestUtil.findFile( fileprefix+"load03.xml" );
data = xmlManager.loadAsListsFromString( FileUtil.readFile(xmlFile) );
assertEquals( loadcanon, canon(data) );
assertEquals( 4, xmlManager.getStats().getTotalRecords() );
}
private String canon( List pData ) throws Exception {
return pData.toString();
}
}