package com.ricebridge.xmlman.test;
import com.ricebridge.xmlman.*;
import com.ricebridge.xmlman.in.*;
import com.ricebridge.xmlman.in.test.*;
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 LoadTest extends TestCase {
public LoadTest( String pName ) {
super( pName );
}
public static TestSuite suite() {
return new TestSuite( LoadTest.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.load( xmlFile, rs );
StringBuffer sb = new StringBuffer();
for( int record = 0; record < data.size(); record++ ) {
sb.append( "record: "+record+"\n" );
String[] fields = (String[]) data.get(record);
for( int field = 0; field < fields.length; field++ ) {
sb.append( "field "+field+" has value: "+fields[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.load( xmlFile, rs );
assertEquals( loadcanon, canon(data) );
data = xmlManager.load( xmlFile.getAbsolutePath(), rs );
assertEquals( loadcanon, canon(data) );
InputStream is = new FileInputStream( xmlFile );
data = xmlManager.load( is, rs );
assertEquals( loadcanon, canon(data) );
is.close();
InputStream fis = new FileInputStream( xmlFile );
InputSource isrc = new InputSource( fis );
data = xmlManager.load( isrc, rs );
assertEquals( loadcanon, canon(data) );
fis.close();
data = xmlManager.loadFromURI( TestUtil.makeURI(xmlFile), rs );
assertEquals( loadcanon, canon(data) );
data = xmlManager.loadFromString( 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 = new RecordSpec( "/foo/bar",
new String[] {"'b'","@name","que"},
new String[] {"Type","Name","Value"},
new RecordSpec( "/foo/far",
new String[] {"'f'","@name","zoo"},
new String[] {"These","Headers","Ignored"} )
);
List data = xmlManager.load( xmlFile, rs );
assertEquals( loadcanon, canon(data) );
data = xmlManager.load( xmlFile.getAbsolutePath(), rs );
assertEquals( loadcanon, canon(data) );
InputStream is = new FileInputStream( xmlFile );
data = xmlManager.load( is, rs );
assertEquals( loadcanon, canon(data) );
is.close();
InputStream fis = new FileInputStream( xmlFile );
InputSource isrc = new InputSource( fis );
data = xmlManager.load( isrc, rs );
assertEquals( loadcanon, canon(data) );
fis.close();
data = xmlManager.loadFromURI( TestUtil.makeURI(xmlFile), rs );
assertEquals( loadcanon, canon(data) );
data = xmlManager.loadFromString( FileUtil.readFile(xmlFile), rs );
assertEquals( loadcanon, canon(data) );
xmlManager = new XmlManager(rs);
data = xmlManager.load( xmlFile );
assertEquals( loadcanon, canon(data) );
data = xmlManager.load( xmlFile.getAbsolutePath() );
assertEquals( loadcanon, canon(data) );
is = new FileInputStream( xmlFile );
data = xmlManager.load( is );
assertEquals( loadcanon, canon(data) );
is.close();
fis = new FileInputStream( xmlFile );
isrc = new InputSource( fis );
data = xmlManager.load( isrc );
assertEquals( loadcanon, canon(data) );
fis.close();
data = xmlManager.loadFromURI( TestUtil.makeURI(xmlFile) );
assertEquals( loadcanon, canon(data) );
data = xmlManager.loadFromString( 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.load( xmlFile );
assertEquals( loadcanon, canon(data) );
assertEquals( 2, xmlManager.getStats().getTotalRecords() );
data = xmlManager.load( 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.load( 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.load( xmlFile );
assertEquals( loadcanon, canon(data) );
assertEquals( 4, xmlManager.getStats().getTotalRecords() );
loadcanon = "[b1, q1], [b2, q2], ";
xmlFile = TestUtil.findFile( fileprefix+"load.xml" );
data = xmlManager.load( xmlFile.getAbsolutePath() );
assertEquals( loadcanon, canon(data) );
assertEquals( 2, xmlManager.getStats().getTotalRecords() );
data = xmlManager.load( 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.load( 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.load( 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.load( is );
assertEquals( loadcanon, canon(data) );
assertEquals( 2, xmlManager.getStats().getTotalRecords() );
is.close();
is = new FileInputStream( xmlFile );
data = xmlManager.load( 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.load( 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.load( 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.load( isrc );
assertEquals( loadcanon, canon(data) );
assertEquals( 2, xmlManager.getStats().getTotalRecords() );
fis.close();
fis = new FileInputStream( xmlFile );
isrc = new InputSource( fis );
data = xmlManager.load( 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.load( 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.load( 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.loadFromURI( TestUtil.makeURI( xmlFile ) );
assertEquals( loadcanon, canon(data) );
assertEquals( 2, xmlManager.getStats().getTotalRecords() );
data = xmlManager.loadFromURI( 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.loadFromURI( 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.loadFromURI( 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.loadFromString( FileUtil.readFile(xmlFile) );
assertEquals( loadcanon, canon(data) );
assertEquals( 2, xmlManager.getStats().getTotalRecords() );
data = xmlManager.loadFromString( 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.loadFromString( 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.loadFromString( FileUtil.readFile(xmlFile) );
assertEquals( loadcanon, canon(data) );
assertEquals( 4, xmlManager.getStats().getTotalRecords() );
}
private String canon( List pData ) throws Exception {
StringBuffer sb = new StringBuffer();
for( Iterator dI = pData.iterator(); dI.hasNext(); ) {
String[] da = (String[]) dI.next();
sb.append( Arrays.asList(da)+", " );
}
return sb.toString();
}
}