public void testReadModifyDeleteInsertApply()
throws Exception
{
//Read some customers
Command select = Command.FACTORY.createCommand( "Select * from CUSTOMER where LASTNAME = 'Williams'" );
select.setConnection( getConnection() );
DataObject root = select.executeQuery();
DataObject cust1 = (DataObject) root.getList( "CUSTOMER" ).get( 0 );
DataObject cust2 = (DataObject) root.getList( "CUSTOMER" ).get( 1 );
DataObject cust3 = (DataObject) root.getList( "CUSTOMER" ).get( 2 );
//Modify a customer
cust1.set( "LASTNAME", "Pavick" );
int cust1ID = cust1.getInt( "ID" );
//Save IDs before delete
int cust2ID = cust2.getInt( "ID" );
int cust3ID = cust3.getInt( "ID" );
//Delete a couple
cust2.delete();
cust3.delete();
//Create a new customer
DataObject cust4 = root.createDataObject( "CUSTOMER" );
cust4.set( "ID", new Integer( 100 ) );
cust4.set( "ADDRESS", "5528 Wells Fargo Drive" );
cust4.set( "LASTNAME", "Gerkin" );
//Build apply changes command
ApplyChangesCommand apply = Command.FACTORY.createApplyChangesCommand(getConfig("basicCustomerMapping.xml"));
apply.setConnection( getConnection() );
//Flush changes
apply.execute( root );
//Verify deletes
select = Command.FACTORY.createCommand( "Select * from CUSTOMER where ID = :ID" );
select.setConnection( getConnection() );
select.setParameterValue( "ID", new Integer( cust2ID ) );
root = select.executeQuery();
assertTrue( root.getList( "CUSTOMER" ).isEmpty() );
//reparameterize same command
select.setParameterValue( "ID", new Integer( cust3ID ) );
root = select.executeQuery();
assertTrue( root.getList( "CUSTOMER" ).isEmpty() );
//verify insert
select.setParameterValue( "ID", new Integer( 100 ) );
root = select.executeQuery();
assertEquals( 1, root.getList( "CUSTOMER" ).size() );
assertEquals( "5528 Wells Fargo Drive", root.getString( "CUSTOMER[1]/ADDRESS" ) );
assertEquals( "Gerkin", root.getString( "CUSTOMER[1]/LASTNAME" ) );
//verify update
select.setParameterValue( "ID", new Integer( cust1ID ) );
root = select.executeQuery();
assertEquals( "Pavick", root.getString( "CUSTOMER[1]/LASTNAME" ) );
}