Fix #23 - Covers: unit tests for: stateless updates which fails when executing without...

This commit is contained in:
Rob Bygrave
2014-04-23 21:16:30 +12:00
parent b012720a5e
commit a7df7da984
12 changed files with 222 additions and 28 deletions
@@ -0,0 +1,42 @@
package com.avaje.ebeaninternal.server.deploy;
import java.util.List;
import org.junit.Assert;
import org.junit.Test;
import com.avaje.ebean.BaseTestCase;
import com.avaje.ebean.Ebean;
import com.avaje.ebean.bean.BeanCollection;
import com.avaje.ebean.bean.EntityBean;
import com.avaje.ebean.bean.EntityBeanIntercept;
import com.avaje.ebeaninternal.api.SpiEbeanServer;
import com.avaje.tests.model.basic.Contact;
import com.avaje.tests.model.basic.Customer;
public class TestCollectionLoadedStatus extends BaseTestCase {
@Test
public void test() {
SpiEbeanServer server = (SpiEbeanServer)Ebean.getServer(null);
BeanDescriptor<Customer> custDesc = server.getBeanDescriptor(Customer.class);
Customer customer = new Customer();
EntityBean eb = (EntityBean)customer;
EntityBeanIntercept ebi = eb._ebean_getIntercept();
BeanProperty contactsProperty = custDesc.getBeanProperty("contacts");
Assert.assertFalse(ebi.isLoadedProperty(contactsProperty.getPropertyIndex()));
Object contactsViaInternal = contactsProperty.getValue(eb);
Assert.assertNull(contactsViaInternal);
Assert.assertFalse(ebi.isLoadedProperty(contactsProperty.getPropertyIndex()));
List<Contact> contacts = customer.getContacts();
Assert.assertNotNull(contacts);
Assert.assertTrue(contacts instanceof BeanCollection);
Assert.assertTrue(ebi.isLoadedProperty(contactsProperty.getPropertyIndex()));
}
}
@@ -23,12 +23,8 @@ public class CustomerPersistAdapter extends BeanPersistAdapter {
@Override
public boolean preUpdate(BeanPersistRequest<?> request) {
Customer customer = (Customer) request.getBean();
if (customer.getContacts() != null) {
String test = "accessed";
}
// Do nothing intentionally. TestStatelessUpdate needs
// to control if customer contacts is 'touched'
return true;
}
@@ -1,7 +1,8 @@
package com.avaje.tests.update;
import com.avaje.tests.model.basic.Contact;
import com.avaje.tests.model.basic.Customer;
import java.util.ArrayList;
import java.util.Collections;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
@@ -9,11 +10,11 @@ import org.junit.Test;
import com.avaje.ebean.BaseTestCase;
import com.avaje.ebean.Ebean;
import com.avaje.ebean.EbeanServer;
import com.avaje.tests.model.basic.Contact;
import com.avaje.tests.model.basic.Customer;
import com.avaje.tests.model.basic.EBasic;
import com.avaje.tests.model.basic.EBasic.Status;
import java.util.ArrayList;
public class TestStatelessUpdate extends BaseTestCase {
private EbeanServer server;
@@ -105,10 +106,10 @@ public class TestStatelessUpdate extends BaseTestCase {
}
/**
* Many relations mustn't be deleted when having a {@link com.avaje.ebean.event.BeanPersistAdapter} which is accessing this many field.
* Many relations mustn't be deleted when they are not loaded.
*/
@Test
public void testStatelessUpdateWithPersistAdapterAndIgnoreNullValues() {
public void testStatelessUpdateIgnoreNullCollection() {
// arrange
Contact contact = new Contact();
@@ -126,12 +127,89 @@ public class TestStatelessUpdate extends BaseTestCase {
customerWithChange.setId(customer.getId());
customerWithChange.setName("new name");
server.update(customerWithChange, null, null, true, false);
// contacts is not loaded
Assert.assertFalse(containsContacts(customerWithChange));
server.update(customerWithChange);
Customer result = Ebean.find(Customer.class, customer.getId());
// assert
// assert null list was ignored (missing children not deleted)
Assert.assertNotNull(result.getContacts());
Assert.assertFalse("the contacts mustn't be deleted", result.getContacts().isEmpty());
}
/**
* When BeanCollection is inadvertantly initialised and empty then ignore it
* Specifically a non-BeanCollection (like ArrayList) is not ignored in terms
* of deleting missing children.
*/
@Test
public void testStatelessUpdateIgnoreEmptyBeanCollection() {
// arrange
Contact contact = new Contact();
contact.setFirstName("wobu :P");
Customer customer = new Customer();
customer.setName("something");
customer.setContacts(new ArrayList<Contact>());
customer.getContacts().add(contact);
server.save(customer);
// act
Customer customerWithChange = new Customer();
customerWithChange.setId(customer.getId());
customerWithChange.setName("new name");
// with Ebean enhancement this loads the an empty contacts BeanList
customerWithChange.getContacts();
// contacts has been initialised to empty BeanList
Assert.assertTrue(containsContacts(customerWithChange));
server.update(customerWithChange);
Customer result = Ebean.find(Customer.class, customer.getId());
// assert empty bean list was ignore (missing children not deleted)
Assert.assertNotNull(result.getContacts());
Assert.assertFalse("the contacts mustn't be deleted", result.getContacts().isEmpty());
}
@Test
public void testStatelessUpdateDeleteChildrenForNonBeanCollection() {
// arrange
Contact contact = new Contact();
contact.setFirstName("wobu :P");
Customer customer = new Customer();
customer.setName("something");
customer.setContacts(new ArrayList<Contact>());
customer.getContacts().add(contact);
server.save(customer);
// act
Customer customerWithChange = new Customer();
customerWithChange.setId(customer.getId());
customerWithChange.setName("new name");
// with Ebean enhancement this loads the an empty contacts BeanList
customerWithChange.setContacts(Collections.<Contact> emptyList());
Assert.assertTrue(containsContacts(customerWithChange));
server.update(customerWithChange);
Customer result = Ebean.find(Customer.class, customer.getId());
// assert empty bean list was ignore (missing children not deleted)
Assert.assertNotNull(result.getContacts());
Assert.assertTrue("the contacts were deleted", result.getContacts().isEmpty());
}
private boolean containsContacts(Customer cust) {
return server.getBeanState(cust).getLoadedProps().contains("contacts");
}
}