content: package org.agoncal.book.javaee7.chapter05;
  
  import org.junit.After;
  import org.junit.Before;
  
  import javax.persistence.EntityManager;
  import javax.persistence.EntityManagerFactory;
  import javax.persistence.EntityTransaction;
  import javax.persistence.Persistence;
  import java.sql.SQLException;
  import java.util.Random;
  
  import static org.agoncal.book.javaee7.chapter05.Constants.PERSISTENCE_UNIT_NAME;
  
  /**
   * @author Antonio Goncalves
   *         APress Book - Beginning Java EE 6 with Glassfish 3
   *         http://www.apress.com/
   *         http://www.antoniogoncalves.org
   *         --
   */
  public abstract class AbstractPersistentTest {
  
    // ======================================
    // =             Attributes             =
    // ======================================
  
    protected static EntityManagerFactory emf = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
    protected EntityManager em;
    protected EntityTransaction tx;
  
    // ======================================
    // =          Lifecycle Methods         =
    // ======================================
  
    @Before
    public void initEntityManager() throws Exception {
      em = emf.createEntityManager();
      tx = em.getTransaction();
    }
  
    @After
    public void closeEntityManager() throws SQLException {
      if (em != null) em.close();
    }
  
    protected Long getRandomId() {
      return Math.abs(new Random().nextLong());
    }
  }
  