Skip to content

Commit

Permalink
OGM-946 Refactor *UpdateTest
Browse files Browse the repository at this point in the history
  1. Remove unecessary em.merge()
  2. Use lambda for transaction demarcation
  3. It works with Infinispan
  • Loading branch information
DavideD committed Nov 1, 2017
1 parent cedeb80 commit a86e777
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ public class Bus {
public Bus() {
}

public Bus(Integer id) {
this.id = id;
}

public Integer getId() {
return id;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ public class PostUpdatableBus extends Bus {
private boolean postUpdated;
private boolean postUpdatedByListener;

public PostUpdatableBus() {
}

public PostUpdatableBus(int id, String field) {
super( id );
this.field = field;
}

public String getField() {
return field;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@
*/
package org.hibernate.ogm.backendtck.callbacks;

import static org.hibernate.ogm.utils.GridDialectType.INFINISPAN;
import static org.hibernate.ogm.utils.GridDialectType.INFINISPAN_REMOTE;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.util.function.Consumer;
import java.util.function.Function;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;

import org.hibernate.ogm.utils.SkipByGridDialect;
import org.hibernate.HibernateException;
import org.hibernate.ogm.utils.jpa.OgmJpaTestCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

@SkipByGridDialect(value = { INFINISPAN,INFINISPAN_REMOTE }, comment = "The dialect has strangenesses with 'postupdate' callback")
public class PostUpdateTest extends OgmJpaTestCase {

private static final String INITIAL = "initial";
Expand All @@ -37,95 +37,93 @@ public void setUp() {
@After
@Override
public void removeEntities() throws Exception {
em.getTransaction().begin();
em.remove( em.find( PostUpdatableBus.class, 1 ) );
em.getTransaction().commit();
em.close();
try {
inTx( em, ( EntityManager em ) -> em.remove( em.find( PostUpdatableBus.class, 1 ) ) );
}
finally {
em.close();
}
}

/**
* Update an entity which uses a @PostUpdate annotated method
* to set boolean field to 'true'.
*/
@Test
public void testFieldSetInPostUpdate() {
em.getTransaction().begin();

PostUpdatableBus bus = new PostUpdatableBus();
bus.setId( 1 );
bus.setField( INITIAL );

em.persist( bus );
em.getTransaction().commit();
em.clear();

em.getTransaction().begin();

bus = em.find( PostUpdatableBus.class, bus.getId() );

assertNotNull( bus );
assertEquals( bus.getField(), INITIAL );
assertFalse( bus.isPostUpdated() );
bus.setField( UPDATED );

em.merge( bus );

em.getTransaction().commit();

assertTrue( bus.isPostUpdated() );
em.clear();

em.getTransaction().begin();

bus = em.find( PostUpdatableBus.class, bus.getId() );
assertNotNull( bus );
assertEquals( bus.getField(), UPDATED );
// @PostUpdate executed after the database UPDATE operation
assertFalse( bus.isPostUpdated() );

em.getTransaction().commit();
public void testFieldSetInPostUpdateAnnotation() {
// Persist
inTx( em, ( EntityManager em ) -> em.persist( new PostUpdatableBus( 1, INITIAL ) ) );

// Update
Function<EntityManager, PostUpdatableBus> function = new Function<EntityManager, PostUpdatableBus>() {

@Override
public PostUpdatableBus apply(EntityManager t) {
PostUpdatableBus bus = em.find( PostUpdatableBus.class, 1 );
assertEquals( bus.getField(), INITIAL );
assertFalse( bus.isPostUpdated() );
bus.setField( UPDATED );
return bus;
}
};

PostUpdatableBus postUpdatedBus = inTx( em, function );
assertTrue( postUpdatedBus.isPostUpdated() );
}

@Test
public void testFieldSetInPostUpdateByListener() {
em.getTransaction().begin();

PostUpdatableBus bus = new PostUpdatableBus();
bus.setId( 1 );
bus.setField( INITIAL );

em.persist( bus );
em.getTransaction().commit();
em.clear();

em.getTransaction().begin();

bus = em.find( PostUpdatableBus.class, bus.getId() );

assertNotNull( bus );
assertEquals( bus.getField(), INITIAL );
assertFalse( bus.isPostUpdatedByListener() );

bus.setField( UPDATED );
em.merge( bus );
em.getTransaction().commit();

assertTrue( bus.isPostUpdatedByListener() );

em.clear();

em.getTransaction().begin();
// Persist
inTx( em, ( EntityManager em ) -> em.persist( new PostUpdatableBus( 1, INITIAL ) ) );

// Update
Function<EntityManager, PostUpdatableBus> function = new Function<EntityManager, PostUpdatableBus>() {

@Override
public PostUpdatableBus apply(EntityManager t) {
PostUpdatableBus bus = em.find( PostUpdatableBus.class, 1 );
assertEquals( bus.getField(), INITIAL );
assertFalse( bus.isPostUpdatedByListener() );
bus.setField( UPDATED );
return bus;
}
};

PostUpdatableBus postUpdatedBus = inTx( em, function );
assertTrue( postUpdatedBus.isPostUpdatedByListener() );
}

bus = em.find( PostUpdatableBus.class, bus.getId() );
assertNotNull( bus );
assertEquals( bus.getField(), UPDATED );
assertFalse( bus.isPostUpdatedByListener() );
public static <T extends Bus> T inTx(EntityManager em, Function<EntityManager, T> consumer) {
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
T bus = consumer.apply( em );
tx.commit();
em.clear();
return bus;
}
catch (HibernateException hex) {
if ( tx != null && tx.isActive() ) {
tx.rollback();
}
throw hex;
}
}

em.getTransaction().commit();
public static void inTx(EntityManager em, Consumer<EntityManager> consumer) {
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
consumer.accept( em );
tx.commit();
em.clear();
}
catch (HibernateException hex) {
if ( tx != null && tx.isActive() ) {
tx.rollback();
}
throw hex;
}
}

@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] { PostUpdatableBus.class };
return new Class<?>[]{ PostUpdatableBus.class };
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ public void testFieldSetInPreUpdate() {
assertFalse( bus.isPreUpdated() );

bus.setField( UPDATED );
em.merge( bus );
em.getTransaction().commit();

assertTrue( bus.isPreUpdated() );
Expand Down Expand Up @@ -105,7 +104,6 @@ public void testFieldSetInPreUpdateByListener() {
assertFalse( bus.isPreUpdatedByListener() );

bus.setField( UPDATED );
em.merge( bus );
em.getTransaction().commit();

assertTrue( bus.isPreUpdatedByListener() );
Expand Down

0 comments on commit a86e777

Please sign in to comment.