3 import javax.persistence.EntityManager;
4 import javax.persistence.EntityManagerFactory;
5 import javax.persistence.EntityTransaction;
6 import javax.persistence.Persistence;
8 import org.junit.rules.ExternalResource;
9 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory;
12 import dst.ass1.jpa.dao.IDAOFactory;
13 import dst.ass1.jpa.dao.impl.DAOFactory;
14 import dst.ass1.jpa.model.IModelFactory;
15 import dst.ass1.jpa.model.impl.ModelFactory;
16 import dst.ass1.jpa.util.Constants;
18 // DO NOT MODIFY THIS CLASS.
21 * The ORMService class is used as a JUnit rule that, before each test run,
22 * a) creates an entity manager factory from the persistence unit referenced in {@link Constants#JPA_PERSISTENCE_UNIT},
23 * b) creates an entity manager using the factory,
24 * c) creates dao and model factory instances, and
25 * d) creates a database gateway for the entity manager.
27 * It provides methods to access these services, and it closes the entity manager and corresponding factory after each
28 * test run. If you pass an {@link ITestData} instance to the constructor, the {@link ORMService} will also insert test
29 * data via a transaction before each run, and truncate tables after each run.
31 public class ORMService extends ExternalResource {
33 private static final Logger LOG = LoggerFactory.getLogger(ORMService.class);
35 private static EntityManagerFactory emf;
36 private EntityManager em;
38 private DatabaseGateway databaseGateway;
40 private IModelFactory modelFactory;
41 private IDAOFactory daoFactory;
43 private ITestData testData;
44 private boolean insertTestData;
45 private boolean truncateTables;
48 * Creates a new ORMService. By default, the ORMService rule only creates the database connection and other
49 * ORM facilities. If you want to also insert a test fixture, use {@link #ORMService(ITestData)}.
52 this(null, false, false);
56 * Creates a new ORMService that also inserts the given {@link ITestData} before each run and truncates the tables
59 * @param testData the test data to insert
61 public ORMService(ITestData testData) {
62 this(testData, true, true);
65 public ORMService(ITestData testData, boolean insertTestData, boolean truncateTables) {
66 this.testData = testData;
67 this.insertTestData = insertTestData;
68 this.truncateTables = truncateTables;
71 public EntityManager getEntityManager() {
75 public EntityManager createEntityManager() {
76 return emf.createEntityManager();
79 public IModelFactory getModelFactory() {
83 public IDAOFactory getDaoFactory() {
87 public DatabaseGateway getDatabaseGateway() {
88 return databaseGateway;
92 * Alias for {@link #getEntityManager()}.
94 * @return an entity manager
96 public EntityManager em() {
97 return getEntityManager();
101 protected void before() throws Throwable {
102 LOG.debug("Creating EntityManagerFactory");
103 emf = createEntityManagerFactory();
105 LOG.debug("Creating EntityManager");
106 em = emf.createEntityManager();
107 databaseGateway = new DatabaseGateway(em);
109 LOG.debug("Initializing factories");
110 // initialize factories
111 modelFactory = new ModelFactory();
112 daoFactory = new DAOFactory(em);
114 if (testData != null && insertTestData) {
115 insertTestData(testData);
120 protected void after() {
121 if (truncateTables) {
126 LOG.debug("Closing EntityManager");
128 } catch (Exception e) {
129 LOG.error("Error while closing entity manager", e);
133 LOG.debug("Closing EntityManagerFactory");
135 } catch (Exception e) {
136 LOG.error("Error while closing entity manager factory", e);
140 protected void insertTestData(ITestData data) {
141 EntityTransaction tx = getEntityManager().getTransaction();
145 data.insert(getModelFactory(), getEntityManager());
147 } catch (Exception e) {
152 } catch (Exception rollbackException) {
153 LOG.error("Error while rolling back transaction after exception", rollbackException);
155 throw new IllegalStateException("Couldn't insert fixture. Can't proceed with tests", e); // rethrow original exception
160 protected void truncateTables() {
162 LOG.debug("Truncating database tables");
163 getDatabaseGateway().truncateTables();
164 } catch (Exception e) {
165 LOG.error("Error while trying to truncate tables after test", e);
169 private static EntityManagerFactory createEntityManagerFactory() {
170 return Persistence.createEntityManagerFactory(Constants.JPA_PERSISTENCE_UNIT);