*/
@SuppressWarnings("unused")
@Test
public void testRollbackWithRemove() {
// check if the database exists and clean before running tests
OObjectDatabaseTx database = new OObjectDatabaseTx(url);
database.open("admin", "admin");
try {
Account account = new Account();
account.setName("John Grisham");
account = database.save(account);
Address address1 = new Address();
address1.setStreet("Mulholland drive");
Address address2 = new Address();
address2.setStreet("Via Veneto");
List<Address> addresses = new ArrayList<Address>();
addresses.add(address1);
addresses.add(address2);
account.setAddresses(addresses);
account = database.save(account);
database.commit();
String originalName = account.getName();
database.begin(TXTYPE.OPTIMISTIC);
Assert.assertEquals(account.getAddresses().size(), 2);
account.getAddresses().remove(1); // delete one of the objects in the Books collection to see how rollback behaves
Assert.assertEquals(account.getAddresses().size(), 1);
account.setName("New Name"); // change an attribute to see if the change is rolled back
account = database.save(account);
Assert.assertEquals(account.getAddresses().size(), 1); // before rollback this is fine because one of the books was removed
database.rollback(); // rollback the transaction
account = database.reload(account, true); // ignore cache, get a copy of author from the datastore
Assert.assertEquals(account.getAddresses().size(), 2); // this is fine, author still linked to 2 books
Assert.assertEquals(account.getName(), originalName); // name is restored
int bookCount = 0;
for (Address b : database.browseClass(Address.class)) {
if (b.getStreet().equals("Mulholland drive") || b.getStreet().equals("Via Veneto"))
bookCount++;
}
Assert.assertEquals(bookCount, 2); // this fails, only 1 entry in the datastore :(
} finally {
database.close();
}
}