Search in sources :

Example 26 with TestErrorException

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());
    }
}
Also used : ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) TestErrorException(org.eclipse.persistence.testing.framework.TestErrorException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) IsolatedClientSession(org.eclipse.persistence.internal.sessions.IsolatedClientSession) Employee(org.eclipse.persistence.testing.models.employee.domain.Employee) ByteArrayInputStream(java.io.ByteArrayInputStream) PhoneNumber(org.eclipse.persistence.testing.models.employee.domain.PhoneNumber) Session(org.eclipse.persistence.sessions.Session) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession) IsolatedClientSession(org.eclipse.persistence.internal.sessions.IsolatedClientSession) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession) ObjectInputStream(java.io.ObjectInputStream)

Example 27 with TestErrorException

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");
    }
}
Also used : UnitOfWork(org.eclipse.persistence.sessions.UnitOfWork) Employee(org.eclipse.persistence.testing.models.employee.domain.Employee) EmployeePopulator(org.eclipse.persistence.testing.models.employee.domain.EmployeePopulator) ReadObjectQuery(org.eclipse.persistence.queries.ReadObjectQuery) TestErrorException(org.eclipse.persistence.testing.framework.TestErrorException) UnitOfWorkImpl(org.eclipse.persistence.internal.sessions.UnitOfWorkImpl) BigDecimal(java.math.BigDecimal)

Example 28 with TestErrorException

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");
    }
}
Also used : UnitOfWork(org.eclipse.persistence.sessions.UnitOfWork) Employee(org.eclipse.persistence.testing.models.employee.domain.Employee) EmployeePopulator(org.eclipse.persistence.testing.models.employee.domain.EmployeePopulator) ReadObjectQuery(org.eclipse.persistence.queries.ReadObjectQuery) TestErrorException(org.eclipse.persistence.testing.framework.TestErrorException) UnitOfWorkImpl(org.eclipse.persistence.internal.sessions.UnitOfWorkImpl) BigDecimal(java.math.BigDecimal)

Example 29 with TestErrorException

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");
    }
}
Also used : UnitOfWork(org.eclipse.persistence.sessions.UnitOfWork) QueryException(org.eclipse.persistence.exceptions.QueryException) Employee(org.eclipse.persistence.testing.models.employee.domain.Employee) TestErrorException(org.eclipse.persistence.testing.framework.TestErrorException) Vector(java.util.Vector)

Example 30 with TestErrorException

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");
    }
}
Also used : UnitOfWork(org.eclipse.persistence.sessions.UnitOfWork) Employee(org.eclipse.persistence.testing.models.employee.domain.Employee) EmployeePopulator(org.eclipse.persistence.testing.models.employee.domain.EmployeePopulator) ReadAllQuery(org.eclipse.persistence.queries.ReadAllQuery) TestErrorException(org.eclipse.persistence.testing.framework.TestErrorException) UnitOfWorkImpl(org.eclipse.persistence.internal.sessions.UnitOfWorkImpl) Vector(java.util.Vector) BigDecimal(java.math.BigDecimal)

Aggregations

TestErrorException (org.eclipse.persistence.testing.framework.TestErrorException)227 UnitOfWork (org.eclipse.persistence.sessions.UnitOfWork)79 Employee (org.eclipse.persistence.testing.models.employee.domain.Employee)50 ReadObjectQuery (org.eclipse.persistence.queries.ReadObjectQuery)27 ExpressionBuilder (org.eclipse.persistence.expressions.ExpressionBuilder)22 Vector (java.util.Vector)20 EntityMapValue (org.eclipse.persistence.testing.models.collections.map.EntityMapValue)20 Expression (org.eclipse.persistence.expressions.Expression)19 UnitOfWorkImpl (org.eclipse.persistence.internal.sessions.UnitOfWorkImpl)18 Iterator (java.util.Iterator)17 AggregateMapKey (org.eclipse.persistence.testing.models.collections.map.AggregateMapKey)16 EntityMapKey (org.eclipse.persistence.testing.models.collections.map.EntityMapKey)15 Project (org.eclipse.persistence.sessions.Project)13 Session (org.eclipse.persistence.sessions.Session)11 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)7 QueryException (org.eclipse.persistence.exceptions.QueryException)7 TestWarningException (org.eclipse.persistence.testing.framework.TestWarningException)7 Address (org.eclipse.persistence.testing.models.employee.domain.Address)7 Test (org.junit.Test)7 BigDecimal (java.math.BigDecimal)6