blob: 97a6a1199adccb3f20806c13580c27dff420de0d [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.events;
import org.eclipse.persistence.testing.models.employee.domain.*;
import org.eclipse.persistence.expressions.*;
import org.eclipse.persistence.sessions.*;
import org.eclipse.persistence.descriptors.DescriptorEventAdapter;
import org.eclipse.persistence.descriptors.DescriptorEvent;
import org.eclipse.persistence.testing.framework.*;
/**
* Added for Bug 2916836
* Test to ensure changes made to change sets on in preWrite events
* for Insert queries are reflected in the cache.
*/
public class PreInsertModifyChangeSetTest extends AutoVerifyTestCase {
protected static int callCount = 0;
protected Employee employee = null;
// The following is an anonymous class which is used for event listening
// uses the preInsert event to change a change set.
protected DescriptorEventAdapter eventAdapter = new DescriptorEventAdapter() {
@Override
public void preInsert(DescriptorEvent event) {
if (event.getQuery().getDescriptor() != null) {
event.updateAttributeWithObject("salary", callCount);
++callCount;
}
}
};
public PreInsertModifyChangeSetTest() {
setDescription("Test to ensure change sets modified by events actually affect the cache.");
}
@Override
public void setup() {
getSession().getDescriptor(Employee.class).getEventManager().addListener(eventAdapter);
beginTransaction();
}
@Override
public void test() {
// Create an employee. This employee's last name will be modifed in a preWrite evetn
employee = new Employee();
employee.setFirstName("Homer");
employee.setLastName("Sampson");
employee.setSalary(200000);
UnitOfWork uow = getSession().acquireUnitOfWork();
uow.registerObject(employee);
uow.commit();
}
@Override
public void verify() {
ExpressionBuilder emp = new ExpressionBuilder();
Expression exp = emp.get("id").equal(employee.getId());
Employee result = (Employee)getSession().readObject(Employee.class, exp);
// verify the modified salary appears.
if (result.getSalary() == 200000) {
throw new TestErrorException("Update of change set in preInsert event did not correctly change the cache.");
}
if (result.getSalary() != 0) {
throw new TestErrorException("preInsert Called multiple times for same object");
}
callCount = 0;
}
@Override
public void reset() {
rollbackTransaction();
getSession().getDescriptor(Employee.class).getEventManager().removeListener(eventAdapter);
}
}