Search in sources :

Example 1 with PhoneNumber

use of org.eclipse.persistence.testing.models.employee.domain.PhoneNumber 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 2 with PhoneNumber

use of org.eclipse.persistence.testing.models.employee.domain.PhoneNumber in project eclipselink by eclipse-ee4j.

the class NestedUnitOfWorkDeleteFromNestedObjectTest method test.

@Override
public void test() {
    UnitOfWork uow = getSession().acquireUnitOfWork();
    UnitOfWork nestedUow1 = uow.acquireUnitOfWork();
    ReadAllQuery query = new ReadAllQuery();
    query.setReferenceClass(Employee.class);
    ExpressionBuilder expressionBuilder = new ExpressionBuilder();
    query.setSelectionCriteria(expressionBuilder.get("firstName").equal("Bob").and(expressionBuilder.get("lastName").equal("Smith")));
    query.conformResultsInUnitOfWork();
    Vector<Employee> results = (Vector<Employee>) uow.executeQuery(query);
    Employee employee = results.firstElement();
    Employee employeeNested = (Employee) nestedUow1.registerObject(employee);
    assertTrue(employeeNested.getPhoneNumbers().size() > 0);
    for (PhoneNumber item : new Vector<PhoneNumber>(employeeNested.getPhoneNumbers())) {
        if (item != null) {
            nestedUow1.deleteObject(item);
            employeeNested.removePhoneNumber(item);
        }
    }
    nestedUow1.deleteObject(employeeNested);
    nestedUow1.commitAndResume();
    if (employee.getPhoneNumbers().size() != 0) {
        throw new TestErrorException("Objects removal from the nested unit of work is not merged into outer/main unit of work. Number of remaining objects is: " + employee.getPhoneNumbers().size());
    }
}
Also used : UnitOfWork(org.eclipse.persistence.sessions.UnitOfWork) Employee(org.eclipse.persistence.testing.models.employee.domain.Employee) ReadAllQuery(org.eclipse.persistence.queries.ReadAllQuery) TestErrorException(org.eclipse.persistence.testing.framework.TestErrorException) PhoneNumber(org.eclipse.persistence.testing.models.employee.domain.PhoneNumber) ExpressionBuilder(org.eclipse.persistence.expressions.ExpressionBuilder) Vector(java.util.Vector)

Example 3 with PhoneNumber

use of org.eclipse.persistence.testing.models.employee.domain.PhoneNumber in project eclipselink by eclipse-ee4j.

the class NestedUnitOfWorkTest method changeUnitOfWorkWorkingCopy.

protected void changeUnitOfWorkWorkingCopy() {
    Employee employee = (Employee) this.unitOfWorkWorkingCopy;
    // Transformation
    employee.setNormalHours(new java.sql.Time[2]);
    employee.setStartTime(Helper.timeFromHourMinuteSecond(1, 1, 1));
    employee.setEndTime(Helper.timeFromHourMinuteSecond(1, 1, 1));
    // Aggregate
    employee.setPeriod(new EmploymentPeriod(Helper.dateFromYearMonthDate(1901, 1, 1), Helper.dateFromYearMonthDate(1902, 2, 2)));
    // One to many private
    employee.setPhoneNumbers(new Vector());
    employee.addPhoneNumber(new PhoneNumber("home", "613", "2263374"));
    employee.addPhoneNumber(new PhoneNumber("office", "416", "8224599"));
    // Many to many
    employee.setProjects(new Vector());
    employee.addProject((Project) this.unitOfWork.readObject(SmallProject.class));
    employee.addProject((Project) this.unitOfWork.readObject(LargeProject.class));
    // Direct collection
    employee.setResponsibilitiesList(new Vector());
    employee.addResponsibility("make coffee");
    employee.addResponsibility("buy donuts");
    // One to one private/public
    employee.setAddress(new Address());
    // make sure that the employee is not his own manager
    employee.setManager((Employee) this.unitOfWork.readObject(Employee.class, (new ExpressionBuilder()).get("id").notEqual(employee.getId())));
}
Also used : EmploymentPeriod(org.eclipse.persistence.testing.models.employee.domain.EmploymentPeriod) Employee(org.eclipse.persistence.testing.models.employee.domain.Employee) Address(org.eclipse.persistence.testing.models.employee.domain.Address) PhoneNumber(org.eclipse.persistence.testing.models.employee.domain.PhoneNumber) ExpressionBuilder(org.eclipse.persistence.expressions.ExpressionBuilder) Vector(java.util.Vector)

Example 4 with PhoneNumber

use of org.eclipse.persistence.testing.models.employee.domain.PhoneNumber in project eclipselink by eclipse-ee4j.

the class NestedUOWWithNewObjectRegisteredTwiceTest method test.

@Override
public void test() {
    Employee empRO = (Employee) getSession().readObject(Employee.class, new ExpressionBuilder().get("firstName").equal(firstName));
    // start transaction T0
    UnitOfWork uowT0 = getSession().acquireUnitOfWork();
    Employee empT0 = (Employee) uowT0.registerObject(empRO);
    // create phone number and commit in a nested unit of work
    UnitOfWork uowT01 = uowT0.acquireUnitOfWork();
    Employee empT01 = (Employee) uowT01.registerObject(empT0);
    PhoneNumber pN = (PhoneNumber) uowT01.registerObject(new PhoneNumber());
    pN.setAreaCode(areaCode);
    empT01.addPhoneNumber(pN);
    uowT01.commit();
    // the phone number is a new object in uowT0
    PhoneNumber pNT0 = (PhoneNumber) empT0.getPhoneNumbers().get(0);
    // start another nested unit of work
    UnitOfWork uowT02 = uowT0.acquireUnitOfWork();
    uowT02.setShouldNewObjectsBeCached(true);
    // load empT0 from the parent cache and clone and register it
    Employee empT02 = (Employee) uowT02.registerObject(empT0);
    // get detail via relation.  this will trigger the value holder and clone and register phone number in uowT02
    pNT02REL = (PhoneNumber) empT02.getPhoneNumbers().get(0);
    // get detail via registration.  this should return the same clone as referenced by pNT02REL
    pNT02REG = (PhoneNumber) uowT02.registerObject(pNT0);
    uowT0.commit();
}
Also used : UnitOfWork(org.eclipse.persistence.sessions.UnitOfWork) Employee(org.eclipse.persistence.testing.models.employee.domain.Employee) PhoneNumber(org.eclipse.persistence.testing.models.employee.domain.PhoneNumber) ExpressionBuilder(org.eclipse.persistence.expressions.ExpressionBuilder)

Example 5 with PhoneNumber

use of org.eclipse.persistence.testing.models.employee.domain.PhoneNumber in project eclipselink by eclipse-ee4j.

the class AllChangeSetsTest method test.

@Override
public void test() {
    UnitOfWork uow = getSession().acquireUnitOfWork();
    Vector employees = uow.readAllObjects(Employee.class);
    // let's require at least 5 object
    if (employees.size() <= 4) {
        throw new TestProblemException("only " + employees.size() + " employees were read, need at least 5");
    }
    // Existing object has changed - should be counted
    Employee emp = (Employee) employees.firstElement();
    emp.setFirstName("Changed");
    changedObjectsCount++;
    Address add = emp.getAddress();
    if (emp.getAddress() != null) {
        // Existing object has changed - should be counted
        add.setCity("Changed");
        changedObjectsCount++;
    }
    // New object - should be counted
    Employee newEmp = new Employee();
    Employee newEmpClone = (Employee) uow.registerObject(newEmp);
    newEmpClone.setFirstName("New");
    changedObjectsCount++;
    Iterator it = emp.getPhoneNumbers().iterator();
    while (it.hasNext()) {
        PhoneNumber phone = (PhoneNumber) it.next();
        // to force reading the phone in and therefore registering in uow
        // Registered but not changed - should NOT be counted
        String code = phone.getAreaCode();
    }
    // forcedUpdate with false - should NOT be counted (doesn't affect cache)
    Employee emp1 = (Employee) employees.elementAt(1);
    uow.forceUpdateToVersionField(emp1, false);
    // forcedUpdate with true - should be counted
    Employee emp2 = (Employee) employees.elementAt(2);
    uow.forceUpdateToVersionField(emp2, true);
    changedObjectsCount++;
    uow.commit();
}
Also used : UnitOfWork(org.eclipse.persistence.sessions.UnitOfWork) Employee(org.eclipse.persistence.testing.models.employee.domain.Employee) Address(org.eclipse.persistence.testing.models.employee.domain.Address) TestProblemException(org.eclipse.persistence.testing.framework.TestProblemException) Iterator(java.util.Iterator) PhoneNumber(org.eclipse.persistence.testing.models.employee.domain.PhoneNumber) Vector(java.util.Vector)

Aggregations

PhoneNumber (org.eclipse.persistence.testing.models.employee.domain.PhoneNumber)24 Employee (org.eclipse.persistence.testing.models.employee.domain.Employee)19 UnitOfWork (org.eclipse.persistence.sessions.UnitOfWork)9 Vector (java.util.Vector)7 ExpressionBuilder (org.eclipse.persistence.expressions.ExpressionBuilder)3 TestErrorException (org.eclipse.persistence.testing.framework.TestErrorException)3 Address (org.eclipse.persistence.testing.models.employee.domain.Address)3 EmploymentPeriod (org.eclipse.persistence.testing.models.employee.domain.EmploymentPeriod)3 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)2 DatabaseException (org.eclipse.persistence.exceptions.DatabaseException)2 EmployeePopulator (org.eclipse.persistence.testing.models.employee.domain.EmployeePopulator)2 SmallProject (org.eclipse.persistence.testing.models.employee.domain.SmallProject)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 ObjectInputStream (java.io.ObjectInputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 ConcurrentModificationException (java.util.ConcurrentModificationException)1 Enumeration (java.util.Enumeration)1 Iterator (java.util.Iterator)1