Skip to content

Method: PersistenceUnitTestRunner(Logger, PersistenceFactory, DataAccessor)

1: /**
2: * Copyright (C) 2016 Czech Technical University in Prague
3: * <p>
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: * <p>
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.jopa.test.runner;
16:
17: import cz.cvut.kbss.jopa.model.EntityManager;
18: import cz.cvut.kbss.jopa.test.OWLClassA;
19: import cz.cvut.kbss.jopa.test.environment.DataAccessor;
20: import cz.cvut.kbss.jopa.test.environment.Generators;
21: import cz.cvut.kbss.jopa.test.environment.PersistenceFactory;
22: import org.junit.Test;
23: import org.slf4j.Logger;
24:
25: import java.net.URI;
26: import java.util.ArrayList;
27: import java.util.List;
28:
29: import static org.junit.Assert.assertEquals;
30: import static org.junit.Assert.assertNotNull;
31:
32: public abstract class PersistenceUnitTestRunner extends BaseRunner {
33:
34: public PersistenceUnitTestRunner(Logger logger, PersistenceFactory persistenceFactory, DataAccessor dataAccessor) {
35: super(logger, persistenceFactory, dataAccessor);
36: }
37:
38: @Test
39: public void multiplePersistenceUnitsSaveDataIndependently() {
40: final List<EntityManager> ems = initPersistenceUnits();
41: try {
42: for (EntityManager em : ems) {
43: em.getTransaction().begin();
44: em.persist(entityA);
45: em.getTransaction().commit();
46: em.clear();
47: }
48:
49: for (EntityManager em : ems) {
50: assertNotNull(em.find(OWLClassA.class, entityA.getUri()));
51: // Cannot use count query, because OWL2Query does not support it
52: final List<?> res = em.createNativeQuery("SELECT ?x WHERE {?x a ?type .}")
53: .setParameter("type", URI.create(OWLClassA.getClassIri())).getResultList();
54: assertEquals(1, res.size());
55: }
56: } finally {
57: ems.forEach(em -> {
58: em.close();
59: em.getEntityManagerFactory().close();
60: });
61: }
62: }
63:
64: private List<EntityManager> initPersistenceUnits() {
65: final List<EntityManager> ems = new ArrayList<>();
66: for (int i = 0; i < Generators.randomPositiveInt(2, 5); i++) {
67: ems.add(getEntityManager("MultiplePUsTest" + i, false));
68: }
69: return ems;
70: }
71: }