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 com.ricebridge.data.sc.DefaultStringConverter;
import org.jostraca.util.*;
import junit.framework.*;
import junit.textui.*;
import org.xml.sax.InputSource;
import java.util.*;
import java.io.*;
import java.text.*;
public class LoadBeansTest extends TestCase {
public LoadBeansTest( String pName ) {
super( pName );
}
public static TestSuite suite() {
return new TestSuite( LoadBeansTest.class );
}
public static void main( String[] pArgs ) {
TestRunner.run( suite() );
}
public void testExample() throws Exception {
File xmlFile = null;
xmlFile = TestUtil.findFile( "com/ricebridge/xmlman/test/example.xml" );
XmlManager xmlManager = new XmlManager();
RecordSpec rs = new RecordSpec( "/root/record",
new String[] {"@name","foo","bar"},
new String[] {"Name","Foo","Bar"} );
BeanSpec bs = new BeanSpec( BeanRecord.class );
List beans = xmlManager.loadBeans( xmlFile, rs, bs );
assertEquals( "[r1:f1:b1, r2:f2:b2, r3:f3:b3]", beans.toString() );
}
public void testConversions() throws Exception {
File xmlFile = null;
xmlFile = TestUtil.findFile( "com/ricebridge/xmlman/test/beans.xml" );
XmlManager xmlManager = new XmlManager();
RecordSpec rs = new RecordSpec( "/root/record",
new String[] {"@name","int","short","long","float","double","boolean","char","byte","string","date"},
new String[] {"Name","Int","Short","Long","Float","Double","Boolean","Char","Byte","String","Date"} );
HashMap scm = new HashMap();
scm.put( "Date", new DateConverter() );
BeanSpec bs = new BeanSpec( ConvRecord.class, scm );
List beans = xmlManager.loadBeans( xmlFile, rs, bs );
assertEquals( "[r1:1:10:100:1.1:1.01:true:a:1:s1:Sat Jan 01 00:00:00 GMT 2005, r2:2:20:200:2.2:2.02:true:b:2:s2:Wed Feb 02 00:00:00 GMT 2005, r3:3:30:300:3.3:3.03:true:c:3:s3:Thu Mar 03 00:00:00 GMT 2005]", beans.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"} );
BeanSpec bs = new BeanSpec( SingleBean.class );
List beans = xmlManager.loadBeans( xmlFile, rs, bs );
assertEquals( loadcanon, canon(beans) );
beans = xmlManager.loadBeans( xmlFile.getAbsolutePath(), rs, bs );
assertEquals( loadcanon, canon(beans) );
InputStream is = new FileInputStream( xmlFile );
beans = xmlManager.loadBeans( is, rs, bs );
assertEquals( loadcanon, canon(beans) );
is.close();
InputStream fis = new FileInputStream( xmlFile );
InputSource isrc = new InputSource( fis );
beans = xmlManager.loadBeans( isrc, rs, bs );
assertEquals( loadcanon, canon(beans) );
fis.close();
beans = xmlManager.loadBeansFromURI( TestUtil.makeURI(xmlFile), rs, bs );
assertEquals( loadcanon, canon(beans) );
beans = xmlManager.loadBeansFromString( FileUtil.readFile(xmlFile), rs, bs );
assertEquals( loadcanon, canon(beans) );
}
public void testRepeat() throws Exception {
String loadheads = "Name,Que,";
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 loadcanon1 = "[b1:q1, b2:q2]";
String loadcanon2 = "[2b1:2q1, 2b2:2q2, 2b3:2q3]";
String loadcanon3 = "[3b1:3q1, 3b2:3q2, 3b3:3q3, 3b4:3q4]";
BeanSpec bs = new BeanSpec( SingleBean.class );
String loadcanon = loadcanon1;
List beans = xmlManager.loadBeans( xmlFile, bs );
assertEquals( loadcanon, canon(beans) );
assertEquals( 2, xmlManager.getStats().getTotalRecords() );
beans = xmlManager.loadBeans( xmlFile, bs );
assertEquals( loadcanon, canon(beans) );
assertEquals( 2, xmlManager.getStats().getTotalRecords() );
loadcanon = loadcanon2;
xmlFile = TestUtil.findFile( fileprefix+"load02.xml" );
beans = xmlManager.loadBeans( xmlFile, bs );
assertEquals( loadcanon, canon(beans) );
assertEquals( 3, xmlManager.getStats().getTotalRecords() );
loadcanon = loadcanon3;
xmlFile = TestUtil.findFile( fileprefix+"load03.xml" );
beans = xmlManager.loadBeans( xmlFile, bs );
assertEquals( loadcanon, canon(beans) );
assertEquals( 4, xmlManager.getStats().getTotalRecords() );
loadcanon = loadcanon1;
xmlFile = TestUtil.findFile( fileprefix+"load.xml" );
beans = xmlManager.loadBeans( xmlFile.getAbsolutePath(), bs );
assertEquals( loadcanon, canon(beans) );
assertEquals( 2, xmlManager.getStats().getTotalRecords() );
beans = xmlManager.loadBeans( xmlFile.getAbsolutePath(), bs );
assertEquals( loadcanon, canon(beans) );
assertEquals( 2, xmlManager.getStats().getTotalRecords() );
loadcanon = loadcanon2;
xmlFile = TestUtil.findFile( fileprefix+"load02.xml" );
beans = xmlManager.loadBeans( xmlFile.getAbsolutePath(), bs );
assertEquals( loadcanon, canon(beans) );
assertEquals( 3, xmlManager.getStats().getTotalRecords() );
loadcanon = loadcanon3;
xmlFile = TestUtil.findFile( fileprefix+"load03.xml" );
beans = xmlManager.loadBeans( xmlFile.getAbsolutePath(), bs );
assertEquals( loadcanon, canon(beans) );
assertEquals( 4, xmlManager.getStats().getTotalRecords() );
loadcanon = loadcanon1;
xmlFile = TestUtil.findFile( fileprefix+"load.xml" );
InputStream is = new FileInputStream( xmlFile );
beans = xmlManager.loadBeans( is, bs );
assertEquals( loadcanon, canon(beans) );
assertEquals( 2, xmlManager.getStats().getTotalRecords() );
is.close();
is = new FileInputStream( xmlFile );
beans = xmlManager.loadBeans( is, bs );
assertEquals( loadcanon, canon(beans) );
assertEquals( 2, xmlManager.getStats().getTotalRecords() );
is.close();
loadcanon = loadcanon2;
xmlFile = TestUtil.findFile( fileprefix+"load02.xml" );
is = new FileInputStream( xmlFile );
beans = xmlManager.loadBeans( is, bs );
assertEquals( loadcanon, canon(beans) );
assertEquals( 3, xmlManager.getStats().getTotalRecords() );
is.close();
loadcanon = loadcanon3;
xmlFile = TestUtil.findFile( fileprefix+"load03.xml" );
is = new FileInputStream( xmlFile );
beans = xmlManager.loadBeans( is, bs );
assertEquals( loadcanon, canon(beans) );
assertEquals( 4, xmlManager.getStats().getTotalRecords() );
is.close();
loadcanon = loadcanon1;
xmlFile = TestUtil.findFile( fileprefix+"load.xml" );
FileInputStream fis = new FileInputStream( xmlFile );
InputSource isrc = new InputSource( fis );
beans = xmlManager.loadBeans( isrc, bs );
assertEquals( loadcanon, canon(beans) );
assertEquals( 2, xmlManager.getStats().getTotalRecords() );
fis.close();
fis = new FileInputStream( xmlFile );
isrc = new InputSource( fis );
beans = xmlManager.loadBeans( isrc, bs );
assertEquals( loadcanon, canon(beans) );
assertEquals( 2, xmlManager.getStats().getTotalRecords() );
fis.close();
loadcanon = loadcanon2;
xmlFile = TestUtil.findFile( fileprefix+"load02.xml" );
fis = new FileInputStream( xmlFile );
isrc = new InputSource( fis );
beans = xmlManager.loadBeans( isrc, bs );
assertEquals( loadcanon, canon(beans) );
assertEquals( 3, xmlManager.getStats().getTotalRecords() );
fis.close();
loadcanon = loadcanon3;
xmlFile = TestUtil.findFile( fileprefix+"load03.xml" );
fis = new FileInputStream( xmlFile );
isrc = new InputSource( fis );
beans = xmlManager.loadBeans( isrc, bs );
assertEquals( loadcanon, canon(beans) );
assertEquals( 4, xmlManager.getStats().getTotalRecords() );
fis.close();
loadcanon = loadcanon1;
xmlFile = TestUtil.findFile( fileprefix+"load.xml" );
beans = xmlManager.loadBeansFromURI( TestUtil.makeURI( xmlFile ), bs );
assertEquals( loadcanon, canon(beans) );
assertEquals( 2, xmlManager.getStats().getTotalRecords() );
beans = xmlManager.loadBeansFromURI( TestUtil.makeURI( xmlFile ), bs );
assertEquals( loadcanon, canon(beans) );
assertEquals( 2, xmlManager.getStats().getTotalRecords() );
loadcanon = loadcanon2;
xmlFile = TestUtil.findFile( fileprefix+"load02.xml" );
beans = xmlManager.loadBeansFromURI( TestUtil.makeURI( xmlFile ), bs );
assertEquals( loadcanon, canon(beans) );
assertEquals( 3, xmlManager.getStats().getTotalRecords() );
loadcanon = loadcanon3;
xmlFile = TestUtil.findFile( fileprefix+"load03.xml" );
beans = xmlManager.loadBeansFromURI( TestUtil.makeURI( xmlFile ), bs );
assertEquals( loadcanon, canon(beans) );
assertEquals( 4, xmlManager.getStats().getTotalRecords() );
loadcanon = loadcanon1;
xmlFile = TestUtil.findFile( fileprefix+"load.xml" );
beans = xmlManager.loadBeansFromString( FileUtil.readFile(xmlFile), bs );
assertEquals( loadcanon, canon(beans) );
assertEquals( 2, xmlManager.getStats().getTotalRecords() );
beans = xmlManager.loadBeansFromString( FileUtil.readFile(xmlFile), bs );
assertEquals( loadcanon, canon(beans) );
assertEquals( 2, xmlManager.getStats().getTotalRecords() );
loadcanon = loadcanon2;
xmlFile = TestUtil.findFile( fileprefix+"load02.xml" );
beans = xmlManager.loadBeansFromString( FileUtil.readFile(xmlFile), bs );
assertEquals( loadcanon, canon(beans) );
assertEquals( 3, xmlManager.getStats().getTotalRecords() );
loadcanon = loadcanon3;
xmlFile = TestUtil.findFile( fileprefix+"load03.xml" );
beans = xmlManager.loadBeansFromString( FileUtil.readFile(xmlFile), bs );
assertEquals( loadcanon, canon(beans) );
assertEquals( 4, xmlManager.getStats().getTotalRecords() );
}
private String canon( List pBeans ) throws Exception {
return pBeans.toString();
}
public static class BeanRecord {
private String iName;
private String iFoo;
private String iBar;
public void setName( String pName ) { iName = pName; }
public String getName() { return iName; }
public void setFoo( String pFoo ) { iFoo = pFoo; }
public String getFoo() { return iFoo; }
public void setBar( String pBar ) { iBar = pBar; }
public String getBar() { return iBar; }
public String toString() {
return iName+":"+iFoo+":"+iBar;
}
}
public static class ConvRecord {
private String iName;
private int iInt;
private short iShort;
private long iLong;
private float iFloat;
private double iDouble;
private boolean iBoolean;
private char iChar;
private byte iByte;
private String iString;
private Date iDate;
public void setName( String pName ) { iName = pName; }
public String getName() { return iName; }
public void setInt( int pInt ) { iInt = pInt; }
public int getInt() { return iInt; }
public void setShort( short pShort ) { iShort = pShort; }
public short getShort() { return iShort; }
public void setLong( long pLong ) { iLong = pLong; }
public long getLong() { return iLong; }
public void setFloat( float pFloat ) { iFloat = pFloat; }
public float getFloat() { return iFloat; }
public void setDouble( double pDouble ) { iDouble = pDouble; }
public double getDouble() { return iDouble; }
public void setBoolean( boolean pBoolean ) { iBoolean = pBoolean; }
public boolean getBoolean() { return iBoolean; }
public void setChar( char pChar ) { iChar = pChar; }
public char getChar() { return iChar; }
public void setByte( byte pByte ) { iByte = pByte; }
public byte getByte() { return iByte; }
public void setString( String pString ) { iString = pString; }
public String getString() { return iString; }
public void setDate( Date pDate ) { iDate = pDate; }
public Date getDate() { return iDate; }
public String toString() {
return ""+iName+":"+iInt+":"+iShort+":"+iLong+":"+iFloat+":"+iDouble+":"+iBoolean+":"+iChar+":"+iByte+":"+iString+":"+iDate;
}
}
public static class DateConverter extends DefaultStringConverter {
private Date iDefault = new Date();
private SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
protected Object makeObjectImpl( String pValue ) throws Exception {
return df.parse(pValue);
}
protected Object makeDefaultObjectImpl() {
return iDefault;
}
protected String makeStringImpl( Object pValue ) throws Exception {
return df.format((Date)pValue);
}
protected String makeDefaultStringImpl() {
return iDefault.toString();
}
}
public static class SingleBean {
private String iName;
private String iQue;
public void setName( String pName ) { iName = pName; }
public String getName() { return iName; }
public void setQue( String pQue ) { iQue = pQue; }
public String getQue() { return iQue; }
public String toString() {
return iName+":"+iQue;
}
}
}