use of org.eclipse.persistence.testing.models.readonly.Address in project eclipselink by eclipse-ee4j.
the class ReadOnlyClassAccessingTestCase method test.
@Override
protected void test() {
// Test acquiring a unit of work.
UnitOfWork uow1 = getSession().acquireUnitOfWork();
if (!uow1.getReadOnlyClasses().isEmpty()) {
throw new TestErrorException(" When acquiring a UnitOfWork from a Session, the read-only classes where not empty as expected.");
}
uow1.release();
// Test acquiring a unit of work with a vector of read-only classes.
Vector classes = new Vector();
classes.addElement(Promoter.class);
classes.addElement(Country.class);
UnitOfWork uow2 = getSession().acquireUnitOfWork();
uow2.removeAllReadOnlyClasses();
uow2.addReadOnlyClasses(classes);
if (!areEqual(uow2.getReadOnlyClasses(), classes)) {
throw new TestErrorException("When acquiring a UnitOfWork from a Session, the read-only classes specified did not get set in the UnitOfWork;");
}
// Test the testing of read-only classes
for (Enumeration enumtr = classes.elements(); enumtr.hasMoreElements(); ) {
if (!uow2.isClassReadOnly((Class) enumtr.nextElement())) {
throw new TestErrorException("Testing whether a class is read-only or not has failed.");
}
}
if (uow2.isClassReadOnly(Vector.class)) {
throw new TestErrorException("Testing whether a class is read-only or not has failed.");
}
// Test the add and remove of read-only classes.
uow2.removeReadOnlyClass(Promoter.class);
if (uow2.isClassReadOnly(Promoter.class)) {
throw new TestErrorException("The method removeReadOnlyClass(Class) failed.");
}
uow2.addReadOnlyClass(Promoter.class);
if (!uow2.isClassReadOnly(Promoter.class)) {
throw new TestErrorException("The method addReadOnlyClass(Class) failed.");
}
// Test the removeAll.
uow2.removeAllReadOnlyClasses();
if ((uow2.isClassReadOnly(Country.class)) || (!uow2.getReadOnlyClasses().isEmpty())) {
throw new TestErrorException("Did not remove all the read-only classes from a UnitOfWork properly");
}
// Check that we cannot make changes to the read-only set after registering an object.
try {
uow2.registerObject(new Address());
uow2.removeAllReadOnlyClasses();
uow2.addReadOnlyClasses(classes);
if (areEqual(uow2.getReadOnlyClasses(), classes)) {
throw new TestErrorException("Shouldn't be able to change the readOnlyClasses of a UnitOfWork after an object was registered.");
}
} catch (org.eclipse.persistence.exceptions.ValidationException ex) {
getSession().logMessage("Caught validation exeception...OK");
} finally {
uow2.release();
}
// Check that the default read-only classes work.
Vector someClasses = new Vector();
someClasses.addElement(Country.class);
someClasses.addElement(Address.class);
getSession().getProject().setDefaultReadOnlyClasses(someClasses);
UnitOfWork uow3 = getSession().acquireUnitOfWork();
if (!areEqual(uow3.getReadOnlyClasses(), someClasses)) {
throw new TestErrorException("The default read-only classes were not set properly when a UnitOfWork was aquired");
}
// Nested units of work should not be able to reduce the set of read-only classes.
UnitOfWork uow4 = uow3.acquireUnitOfWork();
try {
uow4.removeAllReadOnlyClasses();
} catch (org.eclipse.persistence.exceptions.ValidationException ex) {
getSession().logMessage("Check the nested units of work read-only classes. OK");
} finally {
uow3.release();
uow4.release();
}
// You should be able to get the default set of read-only classes from nested UnitOfWork objects.
UnitOfWork uow5 = getSession().acquireUnitOfWork();
UnitOfWork uow6 = uow5.acquireUnitOfWork();
if (!areEqual(((UnitOfWorkImpl) uow5).getDefaultReadOnlyClasses(), ((UnitOfWorkImpl) uow6).getDefaultReadOnlyClasses()))
throw new TestErrorException("Nested UnitOfWorks did not return consistent default read-only classes.");
uow5.release();
uow6.release();
}
use of org.eclipse.persistence.testing.models.readonly.Address in project eclipselink by eclipse-ee4j.
the class ReadOnlyClassDeepMergeCloneTest method test.
@Override
protected void test() {
// Have the countries standing by, outside the unit of work.
Vector countries = getSession().readAllObjects(Country.class);
// Acquire a unit of work with a class read-only.
Vector readOnlyClasses = new Vector();
readOnlyClasses.addElement(Country.class);
UnitOfWork uow = getSession().acquireUnitOfWork();
uow.removeAllReadOnlyClasses();
uow.addReadOnlyClasses(readOnlyClasses);
// Read in an existing Address.
ExpressionBuilder expBuilder = new ExpressionBuilder();
Expression exp = expBuilder.get("country").get("name").equal("United Kingdom");
address = (Address) getSession().readObject(Address.class, exp);
Address addressClone = (Address) uow.registerObject(address);
Address serializedAddress = null;
try {
ByteArrayOutputStream outArray = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(outArray);
out.writeObject(addressClone);
ByteArrayInputStream inArray = new ByteArrayInputStream(outArray.toByteArray());
ObjectInputStream in = new ObjectInputStream(inArray);
serializedAddress = (Address) in.readObject();
} catch (Exception ex) {
throw new TestErrorException("Test not run failed to serialize objects:" + ex.toString());
}
uow.deepMergeClone(serializedAddress);
if (addressClone.getCountry() == serializedAddress.getCountry()) {
throw new TestErrorException("Deepmerge clone merge lost object identity for readonly object");
}
}
use of org.eclipse.persistence.testing.models.readonly.Address in project eclipselink by eclipse-ee4j.
the class ReadOnlyClassDeleteTestCase method setup.
@Override
protected void setup() {
beginTransaction();
// Create a new address and insert it.
address = new Address();
// Make changes to address. (Simulate editing the address in a GUI).
address.setStreetAddress("1212 Madison Street");
address.setCity("Victoria");
address.setZipCode("V3Z 3G1");
// Read a Country object to use in the Address object.
Country aCountry = (Country) getSession().readObject(Country.class);
address.setCountry(aCountry);
// Write it to the db.
beginTransaction();
getDatabaseSession().writeObject(address);
commitTransaction();
}
use of org.eclipse.persistence.testing.models.readonly.Address in project eclipselink by eclipse-ee4j.
the class ReadOnlyClassDeleteTestCase method test.
@Override
protected void test() {
// Have the countries standing by, outside the unit of work.
Vector countries = getSession().readAllObjects(Country.class);
// Acquire a unit of work with a class read-only.
Vector readOnlyClasses = new Vector();
readOnlyClasses.addElement(Country.class);
UnitOfWork uow = getSession().acquireUnitOfWork();
uow.removeAllReadOnlyClasses();
uow.addReadOnlyClasses(readOnlyClasses);
// Read-in the address.
ExpressionBuilder expBuilder = new ExpressionBuilder();
Expression exp = expBuilder.get("streetAddress").equal(address.getStreet());
Address readAddress = (Address) uow.readObject(Address.class, exp);
// Delete the address
uow.deleteObject(readAddress);
uow.commit();
}
use of org.eclipse.persistence.testing.models.readonly.Address in project eclipselink by eclipse-ee4j.
the class ReadOnlyClassDeleteTestCase method verify.
@Override
protected void verify() {
getSession().getIdentityMapAccessor().initializeIdentityMaps();
// The address should be deleted by the Country (which is read-only) should not have been.
ExpressionBuilder expBuilder = new ExpressionBuilder();
Expression exp = expBuilder.get("streetAddress").equal(address.getStreet());
Address dbAddress = (Address) getSession().readObject(Address.class, exp);
if (dbAddress != null) {
throw new TestErrorException("The delete of a non-read-only object referring to a read-only object failed. We should be able to do this!");
}
// Read in the Country that was added to the Address that was deleted.
ExpressionBuilder expBuilder2 = new ExpressionBuilder();
Expression exp2 = expBuilder2.get("name").equal(address.getCountry().getName());
Country aCountry = (Country) getSession().readObject(Country.class, exp2);
if (aCountry == null) {
throw new TestErrorException("during the delete of a non-read-only object referring to a read-only object, the read-only object was also deleted. This should not have happened!");
}
}
Aggregations