public void testRemoveFromFaultedToMany() {
// Create Company1 in OSC1
String companyName = "Company" + ERXRandomGUID.newGid();
EOEditingContext editingContext_osc1 = ERXEC.newEditingContext(ERXObjectStoreCoordinatorPool._pool().nextObjectStore());
Company company_osc1 = Company.createCompany(editingContext_osc1, companyName);
editingContext_osc1.saveChanges();
sleep();
// Create and Save Employee1 and Employee2 for Company1 in OSC1
String employeeFirstName1 = "Employee" + ERXRandomGUID.newGid();
String employeeLastName1 = "Jones";
Employee employee1_osc1 = Employee.createEmployee(editingContext_osc1, employeeFirstName1, employeeLastName1, Boolean.FALSE, company_osc1);
String employeeFirstName2 = "Employee" + ERXRandomGUID.newGid();
String employeeLastName2 = "Jones";
Employee employee2_osc1 = Employee.createEmployee(editingContext_osc1, employeeFirstName2, employeeLastName2, Boolean.FALSE, company_osc1);
editingContext_osc1.saveChanges();
sleep();
// Fetch employees for Company1 in OSC1
assertContainsExactlyEOs(new NSArray<Employee>(new Employee[] { employee1_osc1, employee2_osc1 }), company_osc1.employees());
// Fetch Company1 in OSC2
EOEditingContext editingContext_osc2 = ERXEC.newEditingContext(ERXObjectStoreCoordinatorPool._pool().nextObjectStore());
Company company_osc2 = Company.fetchCompany(editingContext_osc2, Company.NAME_KEY, companyName);
assertNotNull(company_osc2);
// Fetch and check employees for Company1 in OSC2
assertContainsExactlyEOs(new NSArray<Employee>(new Employee[] { employee1_osc1, employee2_osc1 }), company_osc2.employees());
// gonna break
NSArray employees_osc2 = company_osc2.employees();
Employee employee2_osc2 = (Employee) employees_osc2.objectAtIndex(0);
Employee employee1_osc2 = (Employee) employees_osc2.objectAtIndex(1);
Employee secondEmployee_osc2;
if (ERXEOControlUtilities.eoEquals(employee1_osc1, employee1_osc2)) {
secondEmployee_osc2 = employee2_osc2;
}
else {
secondEmployee_osc2 = employee1_osc2;
}
NSArray<Employee> holdingOnToEmployees = company_osc2.employees();
assertEquals(2, holdingOnToEmployees.count());
for (Employee employee : holdingOnToEmployees) {
assertNotNull(employee);
assertNotNull(employee.firstName());
}
NSArray<Employee> holdingOnToUntouchedEmployees = company_osc2.employees();
assertEquals(2, holdingOnToUntouchedEmployees.count());
// Delete Employee1 in OSC1 and Save
editingContext_osc1.deleteObject(employee1_osc1);
editingContext_osc1.saveChanges();
sleep();
// Test that the deleted object is technically still in our EC
assertEquals(2, holdingOnToEmployees.count());
for (Employee employee : holdingOnToEmployees) {
assertNotNull(employee);
assertNotNull(employee.firstName());
}
// Test that the deleted object is technically still in our EC
assertEquals(2, holdingOnToUntouchedEmployees.count());
for (Employee employee : holdingOnToUntouchedEmployees) {
assertNotNull(employee);
assertNotNull(employee.firstName());
}
// for (Employee employee : holdingOnToEmployees) {
// employee.setName(employee.name() + " Modified");
// }
// try {
// editingContext_osc2.saveChanges();
// throw new AssertionFailedError("This should have failed.");
// }
// catch (EOGeneralAdaptorException e) {
// // expected
// }
// Fetch and check employees for Company1 in OSC1
assertContainsExactlyEOs(new NSArray<Employee>(new Employee[] { employee2_osc1 }), company_osc1.employees());
// Fetch and check employees for Company1 in OSC2
assertContainsExactlyEOs(new NSArray<Employee>(new Employee[] { employee2_osc1 }), company_osc2.employees());
// ... Do someting with the deleted object in OSC2
// Delete the remaining employee in OSC2 and Save
editingContext_osc2.deleteObject(secondEmployee_osc2);
editingContext_osc2.saveChanges();
sleep();
// Fetch and check employees for Company1 in OSC1
assertContainsExactlyEOs(NSArray.EmptyArray, company_osc1.employees());
// Fetch and check employees for Company1 in OSC2
assertContainsExactlyEOs(NSArray.EmptyArray, company_osc2.employees());
}