Package: BaseRunner
BaseRunner
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
BaseRunner(Logger, PersistenceFactory, DataAccessor) |
|
|
|
|
|
||||||||||||||||||||
fieldSpecification(Class, String) |
|
|
|
|
|
||||||||||||||||||||
findRequired(Class, Object) |
|
|
|
|
|
||||||||||||||||||||
findRequired(Class, Object, Descriptor) |
|
|
|
|
|
||||||||||||||||||||
getEntityManager(String, boolean) |
|
|
|
|
|
||||||||||||||||||||
getEntityManager(String, boolean, Map) |
|
|
|
|
|
||||||||||||||||||||
init() |
|
|
|
|
|
||||||||||||||||||||
lambda$0(Object[]) |
|
|
|
|
|
||||||||||||||||||||
lambda$1(OWLClassC) |
|
|
|
|
|
||||||||||||||||||||
persist(Object[]) |
|
|
|
|
|
||||||||||||||||||||
persistCWithLists(OWLClassC) |
|
|
|
|
|
||||||||||||||||||||
persistTestData(Collection, EntityManager) |
|
|
|
|
|
||||||||||||||||||||
static {...} |
|
|
|
|
|
||||||||||||||||||||
tearDown() |
|
|
|
|
|
||||||||||||||||||||
transactional(Runnable) |
|
|
|
|
|
||||||||||||||||||||
transactionalThrowing(Executable) |
|
|
|
|
|
||||||||||||||||||||
verifyExists(Class, Object) |
|
|
|
|
|
||||||||||||||||||||
verifyIndividualWasRemoved(URI) |
|
|
|
|
|
||||||||||||||||||||
verifyStatementsNotPresent(Collection, EntityManager) |
|
|
|
|
|
||||||||||||||||||||
verifyStatementsPresent(Collection, EntityManager) |
|
|
|
|
|
||||||||||||||||||||
verifyValueDatatype(URI, String, String) |
|
|
|
|
|
Coverage
1: /**
2: * Copyright (C) 2020 Czech Technical University in Prague
3: * <p>
4: * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
5: * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
6: * version.
7: * <p>
8: * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
9: * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
10: * details. You should have received a copy of the GNU General Public License along with this program. If not, see
11: * <http://www.gnu.org/licenses/>.
12: */
13: package cz.cvut.kbss.jopa.test.runner;
14:
15: import cz.cvut.kbss.jopa.model.EntityManager;
16: import cz.cvut.kbss.jopa.model.descriptors.Descriptor;
17: import cz.cvut.kbss.jopa.model.metamodel.FieldSpecification;
18: import cz.cvut.kbss.jopa.test.*;
19: import cz.cvut.kbss.jopa.test.environment.DataAccessor;
20: import cz.cvut.kbss.jopa.test.environment.PersistenceFactory;
21: import cz.cvut.kbss.jopa.test.environment.Quad;
22: import org.junit.jupiter.api.AfterEach;
23: import org.junit.jupiter.api.function.Executable;
24: import org.slf4j.Logger;
25:
26: import java.net.URI;
27: import java.util.*;
28:
29: import static org.junit.jupiter.api.Assertions.*;
30:
31:
32: public abstract class BaseRunner {
33:
34: protected static final URI CONTEXT_ONE = URI.create("http://krizik.felk.cvut.cz/jopa/contexts#One");
35: protected static final URI CONTEXT_TWO = URI.create("http://krizik.felk.cvut.cz/jopa/contexts#Two");
36:
37:
38: protected final Logger logger;
39:
40: protected EntityManager em;
41:
42: protected OWLClassA entityA;
43: protected OWLClassB entityB;
44: protected OWLClassC entityC;
45: protected OWLClassD entityD;
46: // Generated IRI
47: protected OWLClassE entityE;
48: // Two relationships, cascade
49: protected OWLClassG entityG;
50: protected OWLClassH entityH;
51: // Lazy reference to OWLClassA
52: protected OWLClassI entityI;
53: protected OWLClassM entityM;
54: protected OWLClassN entityN;
55: protected OWLClassP entityP;
56: // Mapped superclass
57: protected OWLClassQ entityQ;
58: // Query based attribute
59: protected OWLClassWithQueryAttr entityWithQueryAttr;
60: protected OWLClassWithQueryAttr2 entityWithQueryAttr2;
61: protected OWLClassWithQueryAttr3 entityWithQueryAttr3;
62: protected OWLClassWithQueryAttr4 entityWithQueryAttr4;
63: protected OWLClassWithQueryAttr5 entityWithQueryAttr5;
64: protected OWLClassWithQueryAttr6 entityWithQueryAttr6;
65:
66: protected final DataAccessor dataAccessor;
67: protected final PersistenceFactory persistenceFactory;
68:
69: public BaseRunner(Logger logger, PersistenceFactory persistenceFactory, DataAccessor dataAccessor) {
70:• assert logger != null;
71: this.logger = logger;
72: this.persistenceFactory = persistenceFactory;
73: this.dataAccessor = dataAccessor;
74: init();
75: }
76:
77: /**
78: * Initializes the test entities in the following manner:
79: * <pre>
80: * <ul>
81: * <li>entityA contains non-empty types</li>
82: * <li>entityB's properties are null</li>
83: * <li>entityC's simple and referenced lists are null</li>
84: * <li>entityD's reference to OWLClassA is set to entityA</li>
85: * <li>entityE's URI is left null for ID generation</li>
86: * <li>entityG's reference to OWLClassH is set to entityH</li>
87: * <li>entityH's reference to OWLClassA is set to entityA</li>
88: * <li>entityI's reference to OWLClassA is set to entityA</li>
89: * <li>entityM has all fields set to some values</li>
90: * <li>entityN has required fields set</li>
91: * <li>entityP's properties and uri are null</li>
92: * <li>entityQ's reference to OWLClassA is set to entityA</li>
93: * </ul>
94: * </pre>
95: */
96: private void init() {
97: this.entityA = new OWLClassA(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityA"));
98: entityA.setStringAttribute("entityAStringAttribute");
99: final Set<String> types = new HashSet<>();
100: types.add("http://krizik.felk.cvut.cz/ontologies/jopa/entities#OWLClassU");
101: entityA.setTypes(types);
102: this.entityB = new OWLClassB();
103: entityB.setUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityB"));
104: entityB.setStringAttribute("entityBStringAttribute");
105: this.entityC = new OWLClassC();
106: entityC.setUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityC"));
107: this.entityD = new OWLClassD(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityD"));
108: entityD.setOwlClassA(entityA);
109: this.entityE = new OWLClassE();
110: entityE.setStringAttribute("entityEStringAttribute");
111: this.entityH = new OWLClassH();
112: entityH.setUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityH"));
113: entityH.setOwlClassA(entityA);
114: this.entityG = new OWLClassG();
115: entityG.setUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityG"));
116: entityG.setOwlClassH(entityH);
117: this.entityI = new OWLClassI();
118: entityI.setUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityI"));
119: entityI.setOwlClassA(entityA);
120: this.entityM = new OWLClassM();
121: entityM.initializeTestValues(true);
122: this.entityN = new OWLClassN();
123: entityN.setStringAttribute("entityNStringAttribute");
124: this.entityP = new OWLClassP();
125: this.entityQ = new OWLClassQ();
126: entityQ.setStringAttribute("entityQStringAttribute");
127: entityQ.setParentString("entityQParentStringAttribute");
128: entityQ.setLabel("entityQLabel");
129: entityQ.setOwlClassA(entityA);
130: this.entityWithQueryAttr = new OWLClassWithQueryAttr();
131: entityWithQueryAttr.setUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityWithQueryAttr"));
132: entityWithQueryAttr.setStringAttribute("EntityWithQueryAttrStringAttribute");
133: this.entityWithQueryAttr2 = new OWLClassWithQueryAttr2();
134: entityWithQueryAttr2.setUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityWithQueryAttr2"));
135: entityWithQueryAttr2.setEntityAttribute(entityA);
136: this.entityWithQueryAttr3 = new OWLClassWithQueryAttr3();
137: entityWithQueryAttr3.setUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityWithQueryAttr3"));
138: this.entityWithQueryAttr4 = new OWLClassWithQueryAttr4();
139: entityWithQueryAttr4.setUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityWithQueryAttr4"));
140: entityWithQueryAttr4.setStringAttribute("EntityWithQueryAttrStringAttribute4");
141: this.entityWithQueryAttr5 = new OWLClassWithQueryAttr5();
142: entityWithQueryAttr5.setUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityWithQueryAttr5"));
143: this.entityWithQueryAttr6 = new OWLClassWithQueryAttr6();
144: entityWithQueryAttr6.setUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityWithQueryAttr6"));
145: }
146:
147: @AfterEach
148: public void tearDown() {
149:• if (em != null && em.isOpen()) {
150:• if (em.getTransaction().isActive()) {
151: em.getTransaction().rollback();
152: }
153: em.close();
154: em.getEntityManagerFactory().close();
155: }
156: }
157:
158: /**
159: * Persists the specified instance(s) in a separate transaction.
160: *
161: * @param entity Entity to persist
162: */
163: protected void persist(Object... entity) {
164: transactional(() -> {
165:• for (Object ent : entity) {
166: em.persist(ent);
167: }
168: });
169: }
170:
171: /**
172: * Runs the specified action in a transaction on the current entity manager.
173: *
174: * @param action The code to run
175: */
176: protected void transactional(Runnable action) {
177: em.getTransaction().begin();
178: action.run();
179: em.getTransaction().commit();
180: }
181:
182: /**
183: * Runs the specified action in a transaction on the current entity manager.
184: * <p>
185: * This allows using also actions which throw checked exceptions.
186: *
187: * @param action The code to run
188: * @see #transactional(Runnable)
189: */
190: protected void transactionalThrowing(Executable action) {
191: try {
192: em.getTransaction().begin();
193: action.execute();
194: em.getTransaction().commit();
195: } catch (Throwable e) {
196: throw new RuntimeException(e);
197: }
198: }
199:
200: /**
201: * Verifies that no statements with the specified individual as subject exist in the ontology any more.
202: *
203: * @param identifier Individual identifier
204: */
205: protected void verifyIndividualWasRemoved(URI identifier) {
206: // TODO There is a bug in OWL2Query - the query returns true, because it finds the top object and data property assertion for an individual
207: // which doesn't exist anymore (but is a part of the query)
208: final boolean remains = em.createNativeQuery("ASK WHERE { ?instance ?y ?z . }", Boolean.class)
209: .setParameter("instance", identifier).getSingleResult();
210: assertFalse(remains);
211: }
212:
213: protected EntityManager getEntityManager(String repositoryName, boolean cacheEnabled) {
214: return getEntityManager(repositoryName, cacheEnabled, Collections.emptyMap());
215: }
216:
217: protected EntityManager getEntityManager(String repositoryName, boolean cacheEnabled,
218: Map<String, String> properties) {
219: return persistenceFactory.getEntityManager(repositoryName, cacheEnabled, properties);
220: }
221:
222: protected void persistTestData(Collection<Quad> data, EntityManager em) throws Exception {
223: dataAccessor.persistTestData(data, em);
224: }
225:
226: protected void verifyStatementsPresent(Collection<Quad> expected, EntityManager em) throws Exception {
227: dataAccessor.verifyDataPresent(expected, em);
228: }
229:
230: protected void verifyStatementsNotPresent(Collection<Quad> notExpected, EntityManager em) throws Exception {
231: dataAccessor.verifyDataNotPresent(notExpected, em);
232: }
233:
234: // Utility methods to reduce duplication
235:
236: /**
237: * Persists the specified instance of {@link OWLClassC} together will all items in the lists (if specified).
238: *
239: * @param instance Instace to persist
240: */
241: void persistCWithLists(OWLClassC instance) {
242: transactional(() -> {
243: em.persist(instance);
244:• if (instance.getSimpleList() != null) {
245: instance.getSimpleList().forEach(em::persist);
246: }
247:• if (instance.getReferencedList() != null) {
248: instance.getReferencedList().forEach(em::persist);
249: }
250: });
251: }
252:
253: /**
254: * Finds instance and checks for it not being {@code null}.
255: *
256: * @return The found instance
257: */
258: <T> T findRequired(Class<T> type, Object identifier) {
259: final T result = em.find(type, identifier);
260: assertNotNull(result);
261: return result;
262: }
263:
264: /**
265: * Finds instance and checks for it not being {@code null}.
266: *
267: * @return The found instance
268: */
269: <T> T findRequired(Class<T> type, Object identifier, Descriptor descriptor) {
270: final T result = em.find(type, identifier, descriptor);
271: assertNotNull(result);
272: return result;
273: }
274:
275: <T> void verifyExists(Class<T> type, Object identifier) {
276: assertNotNull(em.find(type, identifier));
277: }
278:
279: protected void verifyValueDatatype(URI identifier, String property, String expectedDatatype) {
280: assertTrue(em.createNativeQuery("ASK WHERE { ?x ?property ?value . " +
281: "FILTER (DATATYPE(?value) = ?datatype) }", Boolean.class)
282: .setParameter("x", identifier)
283: .setParameter("property", URI.create(property))
284: .setParameter("datatype", URI.create(expectedDatatype)).getSingleResult());
285: }
286:
287: protected <T> FieldSpecification<? super T, ?> fieldSpecification(Class<T> cls, String attName) {
288: return em.getMetamodel().entity(cls).getFieldSpecification(attName);
289: }
290: }