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 SaveTableModelTest extends TestCase {
public SaveTableModelTest( String pName ) {
super( pName );
}
public static TestSuite suite() {
return new TestSuite( SaveTableModelTest.class );
}
public static void main( String[] pArgs ) {
TestRunner.run( suite() );
}
public void testExample() throws Exception {
File xmlFile = TestUtil.findFile( "com/ricebridge/xmlman/test/save.xml" );
XmlManager xmlManager = new XmlManager();
RecordSpec rs = new RecordSpec( "/root/record", new String[] {"@name","foo","bar"},
new String[] {"Name","Foo","Bar"} );
ArrayList data = new ArrayList();
data.add( new String[] {"r1","f1","b1"} );
data.add( new String[] {"r2","f2","b2"} );
data.add( new String[] {"r3","f3","b3"} );
XmlTableModel tm = new XmlTableModel( data );
xmlManager.saveTableModel( xmlFile, rs, tm );
assertEquals( "<root>\n <record name=\"r1\">\n <foo>f1</foo>\n <bar>b1</bar>\n </record>\n <record name=\"r2\">\n <foo>f2</foo>\n <bar>b2</bar>\n </record>\n <record name=\"r3\">\n <foo>f3</foo>\n <bar>b3</bar>\n </record>\n</root>",
FileUtil.readFile(xmlFile) );
}
public void testSingle() throws Exception {
ArrayList data = new ArrayList();
data.add( new String[] {"b1","q1"} );
data.add( new String[] {"b2","q2"} );
XmlTableModel tm = new XmlTableModel( data );
String savecanon = "<foo><bar name=\"b1\"><que>q1</que></bar><bar name=\"b2\"><que>q2</que></bar></foo>";
File xmlFile = null;
xmlFile = TestUtil.findFile( "com/ricebridge/xmlman/test/save.xml" );
XmlManager xmlManager = new XmlManager();
XmlSpec xs = xmlManager.getXmlSpec();
xs.setIndent(false);
RecordSpec rs = new RecordSpec( "/foo/bar",
new String[] {"@name","que"},
new String[] {"Name","Que"} );
FileUtil.writeFile(xmlFile,"");
xmlManager.saveTableModel( xmlFile, rs, tm );
assertEquals( savecanon, FileUtil.readFile(xmlFile) );
FileUtil.writeFile(xmlFile,"");
xmlManager.saveTableModel( xmlFile.getAbsolutePath(), rs, tm );
assertEquals( savecanon, FileUtil.readFile(xmlFile) );
FileUtil.writeFile(xmlFile,"");
OutputStream os = new FileOutputStream( xmlFile );
xmlManager.saveTableModel( os, rs, tm );
assertEquals( savecanon, FileUtil.readFile(xmlFile) );
os.close();
String xmlstr = xmlManager.saveTableModelToString( rs, tm );
assertEquals( savecanon, xmlstr );
}
public void testRepeat() throws Exception {
ArrayList data1 = new ArrayList();
data1.add( new String[] {"b1","q1"} );
data1.add( new String[] {"b2","q2"} );
XmlTableModel tm1 = new XmlTableModel( data1 );
String savecanon1 = "<foo><bar name=\"b1\"><que>q1</que></bar><bar name=\"b2\"><que>q2</que></bar></foo>";
File xmlFile = null;
xmlFile = TestUtil.findFile( "com/ricebridge/xmlman/test/save.xml" );
RecordSpec rs1 = new RecordSpec( "/foo/bar", new String[] {"@name","que"} );
XmlManager xmlManager = new XmlManager( rs1 );
xmlManager.getXmlSpec().setIndent(false);
FileUtil.writeFile(xmlFile,"");
xmlManager.saveTableModel( xmlFile, tm1 );
assertEquals( savecanon1, FileUtil.readFile(xmlFile) );
FileUtil.writeFile(xmlFile,"");
xmlManager.saveTableModel( xmlFile.getAbsolutePath(), tm1 );
assertEquals( savecanon1, FileUtil.readFile(xmlFile) );
FileUtil.writeFile(xmlFile,"");
OutputStream os = new FileOutputStream( xmlFile );
xmlManager.saveTableModel( os, tm1 );
assertEquals( savecanon1, FileUtil.readFile(xmlFile) );
os.close();
String xmlstr = xmlManager.saveTableModelToString( tm1 );
assertEquals( savecanon1, xmlstr );
ArrayList data2 = new ArrayList();
data2.add( new String[] {"2b1","2q1"} );
data2.add( new String[] {"2b2","2q2"} );
data2.add( new String[] {"2b3","2q3"} );
XmlTableModel tm2 = new XmlTableModel( data2 );
String savecanon2 = "<foo><bar name=\"2b1\"><que>2q1</que></bar><bar name=\"2b2\"><que>2q2</que></bar>"
+"<bar name=\"2b3\"><que>2q3</que></bar></foo>";
FileUtil.writeFile(xmlFile,"");
xmlManager.saveTableModel( xmlFile, tm2 );
assertEquals( savecanon2, FileUtil.readFile(xmlFile) );
FileUtil.writeFile(xmlFile,"");
xmlManager.saveTableModel( xmlFile.getAbsolutePath(), tm2 );
assertEquals( savecanon2, FileUtil.readFile(xmlFile) );
FileUtil.writeFile(xmlFile,"");
os = new FileOutputStream( xmlFile );
xmlManager.saveTableModel( os, tm2 );
assertEquals( savecanon2, FileUtil.readFile(xmlFile) );
os.close();
xmlstr = xmlManager.saveTableModelToString( tm2 );
assertEquals( savecanon2, xmlstr );
ArrayList data3 = new ArrayList();
data3.add( new String[] {"3b1","3q1"} );
data3.add( new String[] {"3b2","3q2"} );
data3.add( new String[] {"3b3","3q3"} );
data3.add( new String[] {"3b4","3q4"} );
XmlTableModel tm3 = new XmlTableModel( data3 );
String savecanon3 = "<foo><bar name=\"3b1\"><que>3q1</que></bar><bar name=\"3b2\"><que>3q2</que></bar>"
+"<bar name=\"3b3\"><que>3q3</que></bar><bar name=\"3b4\"><que>3q4</que></bar></foo>";
FileUtil.writeFile(xmlFile,"");
xmlManager.saveTableModel( xmlFile, tm3 );
assertEquals( savecanon3, FileUtil.readFile(xmlFile) );
FileUtil.writeFile(xmlFile,"");
xmlManager.saveTableModel( xmlFile.getAbsolutePath(), tm3 );
assertEquals( savecanon3, FileUtil.readFile(xmlFile) );
FileUtil.writeFile(xmlFile,"");
os = new FileOutputStream( xmlFile );
xmlManager.saveTableModel( os, tm3 );
assertEquals( savecanon3, FileUtil.readFile(xmlFile) );
os.close();
xmlstr = xmlManager.saveTableModelToString( tm3 );
assertEquals( savecanon3, xmlstr );
}
}