use of org.eclipse.persistence.testing.framework.TestErrorException in project eclipselink by eclipse-ee4j.
the class DeepMergeCloneSerializedTest method test.
/*
* This test creates an object and registers it with a unit of work. It then serializes that
* object and deserializes it. Adds an object onto the origional then performs serialization
* sequence again. Then deepMergeClone is attempted and the results are compared to verify that
* the merge worked.
*/
@Override
public void test() {
try {
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
ObjectOutputStream stream = new ObjectOutputStream(byteStream);
// create the phoneNumber object
Employee empClone;
Session session = getSession();
org.eclipse.persistence.sessions.UnitOfWork uow = session.acquireUnitOfWork();
this.empObject = (Employee) session.readObject(Employee.class, new org.eclipse.persistence.expressions.ExpressionBuilder().get("firstName").equal("Bob"));
ClassDescriptor descriptor = session.getDescriptor(this.empObject);
if (descriptor.isProtectedIsolation() && descriptor.shouldIsolateProtectedObjectsInUnitOfWork() && session instanceof IsolatedClientSession) {
// this will have read a version of the protected Entity into the Isolated Cache even though the test wants to isolated to UOW
// replace with actual shared cache version
this.empObject = (Employee) ((AbstractSession) session).getParentIdentityMapSession(descriptor, false, true).getIdentityMapAccessor().getFromIdentityMap(this.empObject);
}
// force instantiations of value holders before serialization
this.empObject.getPhoneNumbers();
if (this.empObject.getManager() != null) {
this.empObject.getManager().getManagedEmployees();
}
this.empObject.getResponsibilitiesList();
// serialize object by writing to a stream
stream.writeObject(this.empObject);
stream.flush();
byte[] arr = byteStream.toByteArray();
ByteArrayInputStream inByteStream = new ByteArrayInputStream(arr);
ObjectInputStream inObjStream = new ObjectInputStream(inByteStream);
Employee deserialEmp;
// deserialize the object
try {
deserialEmp = (Employee) inObjStream.readObject();
} catch (ClassNotFoundException e) {
throw new TestErrorException("Could not deserialize object " + e.toString());
}
// add a new manager, test 1-m's
Employee newManager = new org.eclipse.persistence.testing.models.employee.domain.Employee();
if (deserialEmp.getManager() != null) {
deserialEmp.getManager().removeManagedEmployee(deserialEmp);
this.removedPhone = (PhoneNumber) deserialEmp.getPhoneNumbers().firstElement();
deserialEmp.getPhoneNumbers().removeElement(deserialEmp.getPhoneNumbers().firstElement());
}
newManager.addManagedEmployee(deserialEmp);
// add the PhoneNumber object to the origional clone, test 1-1
PhoneNumber phone = new org.eclipse.persistence.testing.models.employee.domain.PhoneNumber();
phone.setNumber("5555897");
phone.setType("Fax");
phone.setOwner(deserialEmp);
deserialEmp.addPhoneNumber(phone);
this.addedPhone = phone;
deserialEmp.setLastName("Willford");
this.gender = deserialEmp.getGender();
if (deserialEmp.getGender().equals("Female")) {
deserialEmp.setMale();
} else {
deserialEmp.setFemale();
}
this.endDate = deserialEmp.getPeriod().getEndDate();
deserialEmp.getPeriod().setEndDate(new java.sql.Date(System.currentTimeMillis() + 300000L));
this.endTime = deserialEmp.getEndTime();
deserialEmp.setEndTime(Helper.timeFromHourMinuteSecond(15, 2, 3));
deserialEmp.addResponsibility("A Very New Respons");
byteStream = new ByteArrayOutputStream();
stream = new ObjectOutputStream(byteStream);
// send the ammended object back through the serialization process
stream.writeObject(deserialEmp);
stream.flush();
arr = byteStream.toByteArray();
inByteStream = new ByteArrayInputStream(arr);
inObjStream = new ObjectInputStream(inByteStream);
try {
deserialEmp = (Employee) inObjStream.readObject();
} catch (ClassNotFoundException e) {
throw new TestErrorException("Could not deserialize object " + e.toString());
}
// merge the ammended clone with the unit of work
empClone = (Employee) uow.deepMergeClone(deserialEmp);
uow.commit();
uow = session.acquireUnitOfWork();
// do the serialization for the second time
byteStream = new ByteArrayOutputStream();
stream = new ObjectOutputStream(byteStream);
stream.writeObject(this.empObject);
stream.flush();
arr = byteStream.toByteArray();
inByteStream = new ByteArrayInputStream(arr);
inObjStream = new ObjectInputStream(inByteStream);
// attempt to deserialize the object
try {
deserialEmp = (Employee) inObjStream.readObject();
} catch (ClassNotFoundException e) {
throw new TestErrorException("Could not deserialize object " + e.toString());
}
deserialEmp.setFirstName("Danny");
this.origional = deserialEmp;
byteStream = new ByteArrayOutputStream();
stream = new ObjectOutputStream(byteStream);
// send the ammended object back through the serialization process
stream.writeObject(deserialEmp);
stream.flush();
arr = byteStream.toByteArray();
inByteStream = new ByteArrayInputStream(arr);
inObjStream = new ObjectInputStream(inByteStream);
try {
deserialEmp = (Employee) inObjStream.readObject();
} catch (ClassNotFoundException e) {
throw new TestErrorException("Could not deserialize object " + e.toString());
}
deserialEmp = (Employee) uow.deepMergeClone(deserialEmp);
uow.commit();
this.mergedClone = deserialEmp;
} catch (IOException e) {
throw new TestErrorException("Error running Test " + e.toString());
}
}
use of org.eclipse.persistence.testing.framework.TestErrorException in project eclipselink by eclipse-ee4j.
the class DoubleNestedUnitOfWorkDeleteConformedNestedNewObjectTest method test.
@Override
public void test() {
UnitOfWork uow = getSession().acquireUnitOfWork();
UnitOfWork nestedUow1 = uow.acquireUnitOfWork();
UnitOfWork nestedNestedUOW = nestedUow1.acquireUnitOfWork();
Employee employee = (Employee) new EmployeePopulator().basicEmployeeExample1();
employee.setId(new BigDecimal(15));
Employee nestedEmployee = (Employee) nestedNestedUOW.registerObject(employee);
nestedNestedUOW.commit();
nestedUow1.commit();
nestedUow1 = uow.acquireUnitOfWork();
nestedNestedUOW = nestedUow1.acquireUnitOfWork();
ReadObjectQuery query = new ReadObjectQuery();
query.setReferenceClass(Employee.class);
query.setSelectionCriteria(new org.eclipse.persistence.expressions.ExpressionBuilder().get("id").equal(new BigDecimal(15)));
query.conformResultsInUnitOfWork();
nestedEmployee = (Employee) nestedNestedUOW.executeQuery(query);
nestedNestedUOW.deleteObject(nestedEmployee);
nestedNestedUOW.commit();
nestedUow1.commit();
if (!((UnitOfWorkImpl) uow).getNewObjectsCloneToOriginal().isEmpty()) {
throw new TestErrorException("Failed to unregister the Object in the nested unit of work");
}
}
use of org.eclipse.persistence.testing.framework.TestErrorException in project eclipselink by eclipse-ee4j.
the class DoubleNestedUnitOfWorkRegisterNewObjectTest method test.
@Override
public void test() {
UnitOfWork uow = getSession().acquireUnitOfWork();
UnitOfWork nestedUow1 = uow.acquireUnitOfWork();
UnitOfWork nestedNestedUOW = nestedUow1.acquireUnitOfWork();
Employee employee = (Employee) new EmployeePopulator().basicEmployeeExample1();
employee.setId(new BigDecimal(15));
Employee nestedEmployee = (Employee) nestedNestedUOW.registerObject(employee);
nestedNestedUOW.commit();
nestedUow1.commit();
nestedUow1 = uow.acquireUnitOfWork();
nestedNestedUOW = nestedUow1.acquireUnitOfWork();
ReadObjectQuery query = new ReadObjectQuery();
query.setReferenceClass(Employee.class);
query.setSelectionCriteria(new org.eclipse.persistence.expressions.ExpressionBuilder().get("id").equal(new BigDecimal(15)));
query.conformResultsInUnitOfWork();
nestedEmployee = (Employee) nestedNestedUOW.executeQuery(query);
nestedNestedUOW.deleteObject(nestedEmployee);
nestedNestedUOW.commit();
nestedUow1.commit();
if (!((UnitOfWorkImpl) uow).getNewObjectsCloneToOriginal().isEmpty()) {
throw new TestErrorException("Failed to unregister the Object in the nested unit of work");
}
}
use of org.eclipse.persistence.testing.framework.TestErrorException in project eclipselink by eclipse-ee4j.
the class ExceptionsRaisedUnitOfWorkTest method test.
@Override
public void test() {
// Read some object from the database.
boolean exceptionCaught = false;
Employee employee = (Employee) getSession().readAllObjects(Employee.class).firstElement();
UnitOfWork firstUOW = getSession().acquireUnitOfWork();
// Read some object from the database.
Vector employees = firstUOW.readAllObjects(Employee.class);
Employee uowEmployee = (Employee) employees.elementAt(2);
uowEmployee.setAddress(employee.getAddress());
// commit the unit of work
try {
firstUOW.commit();
} catch (QueryException exception) {
exceptionCaught = true;
}
if (!exceptionCaught) {
throw new TestErrorException("Performed illegal operations in UOW yet no exception was thrown");
}
}
use of org.eclipse.persistence.testing.framework.TestErrorException in project eclipselink by eclipse-ee4j.
the class NestedUnitOfWorkDeleteConformedNestedNewObjectTest method test.
@Override
public void test() {
UnitOfWork uow = getSession().acquireUnitOfWork();
UnitOfWork nestedUow1 = uow.acquireUnitOfWork();
Employee employee = (Employee) new EmployeePopulator().basicEmployeeExample1();
employee.setId(new BigDecimal(15));
nestedUow1.registerObject(employee);
nestedUow1.commit();
nestedUow1 = uow.acquireUnitOfWork();
ReadAllQuery query = new ReadAllQuery();
query.setReferenceClass(Employee.class);
query.setSelectionCriteria(new org.eclipse.persistence.expressions.ExpressionBuilder().get("id").equal(new BigDecimal(15)));
query.conformResultsInUnitOfWork();
Vector results = (Vector) nestedUow1.executeQuery(query);
if (results.size() > 1) {
throw new TestErrorException("The read all query on primary key returned more than one object see bug # 3183379");
}
Employee nestedEmployee = (Employee) results.firstElement();
nestedUow1.deleteObject(nestedEmployee);
nestedUow1.commit();
if (!((UnitOfWorkImpl) uow).getNewObjectsCloneToOriginal().isEmpty()) {
throw new TestErrorException("Failed to unregister the Object in the nested unit of work");
}
}
Aggregations