Skip to contentMethod: initPersistenceUnits()
1: /**
2: * Copyright (C) 2016 Czech Technical University in Prague
3: *
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: *
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.Generators;
20: import org.junit.Test;
21: import org.slf4j.Logger;
22:
23: import java.net.URI;
24: import java.util.ArrayList;
25: import java.util.List;
26:
27: import static org.junit.Assert.assertEquals;
28: import static org.junit.Assert.assertNotNull;
29:
30: public abstract class PersistenceUnitTestRunner extends BaseRunner {
31:
32: public PersistenceUnitTestRunner(Logger logger) {
33: super(logger);
34: }
35:
36: @Test
37: public void multiplePersistenceUnitsSaveDataIndependently() {
38: final List<EntityManager> ems = initPersistenceUnits();
39: try {
40: for (EntityManager em : ems) {
41: em.getTransaction().begin();
42: em.persist(entityA);
43: em.getTransaction().commit();
44: em.clear();
45: }
46:
47: for (EntityManager em : ems) {
48: assertNotNull(em.find(OWLClassA.class, entityA.getUri()));
49: // Cannot use count query, because OWL2Query does not support it
50: final List<?> res = em.createNativeQuery("SELECT ?x WHERE {?x a ?type .}")
51: .setParameter("type", URI.create(OWLClassA.getClassIri())).getResultList();
52: assertEquals(1, res.size());
53: }
54: } finally {
55: ems.forEach(em -> {
56: em.close();
57: em.getEntityManagerFactory().close();
58: });
59: }
60: }
61:
62: private List<EntityManager> initPersistenceUnits() {
63: final List<EntityManager> ems = new ArrayList<>();
64:• for (int i = 0; i < Generators.randomPositiveInt(5); i++) {
65: ems.add(getEntityManager("MultiplePUsTest" + i, false));
66: }
67: return ems;
68: }
69: }