blob: 2ab957e930ff932213b3869dad2579fd06590a4a [file] [log] [blame]
/*
* Copyright (c) 1998, 2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0,
* or the Eclipse Distribution License v. 1.0 which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
*/
// Contributors:
// Oracle - initial API and implementation from Oracle TopLink
package org.eclipse.persistence.testing.tests.unitofwork.writechanges;
import java.math.BigDecimal;
import org.eclipse.persistence.expressions.Expression;
import org.eclipse.persistence.expressions.ExpressionBuilder;
import org.eclipse.persistence.queries.ReadObjectQuery;
import org.eclipse.persistence.sessions.UnitOfWork;
import org.eclipse.persistence.testing.framework.AutoVerifyTestCase;
import org.eclipse.persistence.testing.framework.TestErrorException;
import org.eclipse.persistence.testing.models.employee.domain.Employee;
/**
* Tests that write changes works for non-trivial updates.
* Tests that sequencing is done and primary keys are available after writeChanges().
* Verifies that the object was added correctly and can be safely removed.
* @author smcritch
*/
public class WriteChanges_Commit_NonTrivial_TestCase extends AutoVerifyTestCase {
public BigDecimal id = null;
@Override
public void test() {
UnitOfWork uow = getSession().acquireUnitOfWork();
Employee employee = new Employee();
employee = (Employee)uow.registerObject(employee);
employee.setFirstName("Stephen");
employee.setLastName("McRitchie");
uow.writeChanges();
if (employee.getId().intValue() == 0) {
throw new TestErrorException("Sequence numbers for new objects not available after write changes.");
} else {
id = employee.getId();
}
uow.commit();
}
@Override
public void verify() {
getSession().getIdentityMapAccessor().initializeAllIdentityMaps();
;
Expression expression = (new ExpressionBuilder()).get("id").equal(id);
ReadObjectQuery query = new ReadObjectQuery(Employee.class, expression);
Employee employee = (Employee)getSession().executeQuery(query);
if ((employee == null) || !employee.getFirstName().equals("Stephen")) {
throw new TestErrorException("The employee was not written properly. Found only: " + employee);
}
}
@Override
public void reset() {
if (id == null) {
return;
}
UnitOfWork uow = getSession().acquireUnitOfWork();
Expression expression = (new ExpressionBuilder()).get("id").equal(id);
ReadObjectQuery query = new ReadObjectQuery(Employee.class, expression);
Employee employee = (Employee)uow.executeQuery(query);
uow.deleteObject(employee);
uow.commit();
}
@Override
protected void resetVerify() {
getSession().getIdentityMapAccessor().initializeAllIdentityMaps();
;
Expression expression = (new ExpressionBuilder()).get("id").equal(id);
ReadObjectQuery query = new ReadObjectQuery(Employee.class, expression);
Employee employee = (Employee)getSession().executeQuery(query);
id = null;
if (employee != null) {
throw new TestErrorException("Employee not removed after the test.");
}
}
}