use of org.eclipse.persistence.testing.models.jpa.advanced.Address in project eclipselink by eclipse-ee4j.
the class OptimisticLockForceIncrementTestSuite method testVersionIncrementMultipleEntities.
public void testVersionIncrementMultipleEntities() {
QuerySQLTracker counter = null;
try {
counter = new QuerySQLTracker(getServerSession());
EntityManager em = getEntityManagerFactory().createEntityManager();
em.getTransaction().begin();
List<Address> addresses = em.createQuery("select a from Address a").getResultList();
counter.getSqlStatements().clear();
assertNotNull("Null query results returned", addresses);
assertNotSame("No query results returned", addresses.size(), 0);
Address entity1 = addresses.get(0);
Address entity2 = addresses.get(1);
assertNotNull("Entity 1: Address cannot be null", entity1);
assertNotNull("Entity 2: Address cannot be null", entity2);
int startVersion1 = entity1.getVersion();
int startVersion2 = entity2.getVersion();
em.lock(entity1, LockModeType.OPTIMISTIC_FORCE_INCREMENT);
// update entity 2
entity2.setCity("Kamloops");
entity2.setProvince("BC");
entity2.setCountry("Canada");
em.flush();
// update entity 1
entity1.setCity("New Glasgow");
entity1.setProvince("NS");
entity1.setCountry("Canada");
em.flush();
int expectedVersion1 = (startVersion1 + 1);
int actualVersion1 = entity1.getVersion();
int expectedVersion2 = (startVersion2 + 1);
int actualVersion2 = entity2.getVersion();
em.getTransaction().rollback();
em.close();
assertEquals("Entity 1: Version number incremented incorrectly: ", expectedVersion1, actualVersion1);
assertEquals("Entity 2: Version number incremented incorrectly: ", expectedVersion2, actualVersion2);
// 3 SQL update statements expected
assertEquals("3 SQL update statement execution(s)", 3, countNumberOfUpdateStatements(counter));
} finally {
if (counter != null) {
counter.remove();
}
}
}
use of org.eclipse.persistence.testing.models.jpa.advanced.Address in project eclipselink by eclipse-ee4j.
the class OptimisticLockForceIncrementTestSuite method testVersionIncrementNoChanges.
public void testVersionIncrementNoChanges() {
if (usesSOP()) {
// extra version increment.
return;
}
QuerySQLTracker counter = null;
try {
counter = new QuerySQLTracker(getServerSession());
EntityManager em = getEntityManagerFactory().createEntityManager();
em.getTransaction().begin();
List<Address> addresses = em.createQuery("select a from Address a").getResultList();
assertNotNull("Null query results returned", addresses);
assertNotSame("No query results returned", addresses.size(), 0);
Address entity = addresses.get(0);
assertNotNull("Entity: Address cannot be null", entity);
counter.getSqlStatements().clear();
int startVersion = entity.getVersion();
em.lock(entity, LockModeType.OPTIMISTIC_FORCE_INCREMENT);
em.flush();
em.getTransaction().commit();
int expectedVersion = (startVersion + 1);
int actualVersion = entity.getVersion();
em.close();
assertEquals("Version number incremented incorrectly: ", expectedVersion, actualVersion);
// SQL statements expected - 1 x update
assertEquals("1 SQL update statement execution(s)", 1, countNumberOfUpdateStatements(counter));
} finally {
if (counter != null) {
counter.remove();
}
}
}
use of org.eclipse.persistence.testing.models.jpa.advanced.Address in project eclipselink by eclipse-ee4j.
the class OptimisticLockForceIncrementTestSuite method testVersionIncrementPromoteLock.
public void testVersionIncrementPromoteLock() {
QuerySQLTracker counter = null;
try {
counter = new QuerySQLTracker(getServerSession());
EntityManager em = getEntityManagerFactory().createEntityManager();
em.getTransaction().begin();
List<Address> addresses = em.createQuery("select a from Address a").getResultList();
counter.getSqlStatements().clear();
assertNotNull("Null query results returned", addresses);
assertNotSame("No query results returned", addresses.size(), 0);
Address entity = addresses.get(0);
assertNotNull("Entity: Address cannot be null", entity);
int startVersion = entity.getVersion();
em.lock(entity, LockModeType.OPTIMISTIC);
entity.setCity("Banff");
entity.setProvince("AB");
entity.setCountry("Canada");
em.flush();
em.lock(entity, LockModeType.OPTIMISTIC_FORCE_INCREMENT);
entity.setCity("London");
entity.setProvince("ON");
entity.setCountry("Canada");
em.getTransaction().commit();
int expectedVersion = (startVersion + 1);
int actualVersion = entity.getVersion();
em.close();
assertEquals("Version number incremented incorrectly: ", expectedVersion, actualVersion);
// SQL statements expected
assertEquals("2 SQL update statement execution(s)", 2, countNumberOfUpdateStatements(counter));
} finally {
if (counter != null) {
counter.remove();
}
}
}
use of org.eclipse.persistence.testing.models.jpa.advanced.Address in project eclipselink by eclipse-ee4j.
the class OptimisticLockForceIncrementTestSuite method testVersionIncrementPreFlushChanges.
public void testVersionIncrementPreFlushChanges() {
if (usesSOP()) {
// extra version increment.
return;
}
QuerySQLTracker counter = null;
try {
counter = new QuerySQLTracker(getServerSession());
EntityManager em = getEntityManagerFactory().createEntityManager();
em.getTransaction().begin();
List<Address> addresses = em.createQuery("select a from Address a").getResultList();
counter.getSqlStatements().clear();
assertNotNull("Null query results returned", addresses);
assertNotSame("No query results returned", addresses.size(), 0);
Address entity = addresses.get(0);
assertNotNull("Entity: Address cannot be null", entity);
int startVersion = entity.getVersion();
em.lock(entity, LockModeType.OPTIMISTIC_FORCE_INCREMENT);
entity.setCity("Vancouver");
entity.setProvince("BC");
entity.setCountry("Canada");
em.flush();
em.getTransaction().commit();
int expectedVersion = (startVersion + 1);
int actualVersion = entity.getVersion();
em.close();
assertEquals("Version number incremented incorrectly: ", expectedVersion, actualVersion);
assertEquals("Entity's name should be changed", "Vancouver", entity.getCity());
// SQL statements expected - 1 x update
assertEquals("1 SQL update statement execution(s)", 1, countNumberOfUpdateStatements(counter));
} finally {
if (counter != null) {
counter.remove();
}
}
}
use of org.eclipse.persistence.testing.models.jpa.advanced.Address in project eclipselink by eclipse-ee4j.
the class PessimisticLockEntityRefreshTestSuite method doFindWithLockModes.
/**
* Test utility method
* @param lockMode1 - the lock mode for the first find()
* @param lockMode2 - the lock mode for the second find()
*/
public void doFindWithLockModes(LockModeType lockMode1, LockModeType lockMode2) {
if (!isSelectForUpateSupported()) {
return;
}
EntityManager em = createEntityManager();
beginTransaction(em);
try {
Address entity = new Address("Wellington St.", "Ottawa", "ON", "Canada", "K1A0A6");
em.persist(entity);
em.flush();
int entityId = entity.getID();
entity = em.find(Address.class, entityId, lockMode1);
assertNotNull("Test data: entity should not be null", entity);
entity.setCountry("changed");
Address entityRetrievedAgain = em.find(Address.class, entityId, lockMode2);
assertNotNull("Test data: entity retrieved again should not be null", entityRetrievedAgain);
assertTrue("Same entity instance should be returned from find", entity == entityRetrievedAgain);
assertEquals("Field value is expected to be the same", "changed", entityRetrievedAgain.getCountry());
assertEquals("Version field is expected to be the same", entity.getVersion(), entityRetrievedAgain.getVersion());
} catch (RuntimeException ex) {
throw ex;
} finally {
closeEntityManagerAndTransaction(em);
}
}
Aggregations