Skip to content

Package: PolymorphicSelectQueryRunner

PolymorphicSelectQueryRunner

nameinstructionbranchcomplexitylinemethod
PolymorphicSelectQueryRunner(Logger)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
lambda$0(EntityManager, OWLClassT)
M: 0 C: 21
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
selectByTypeLoadsAllIndividualsAsMostConcreteSubclassInstances()
M: 0 C: 74
100%
M: 1 C: 5
83%
M: 1 C: 3
75%
M: 0 C: 20
100%
M: 0 C: 1
100%
selectLoadsInstanceAsGivenTypeWhenItIsConcreteAndFoundInTypesOfIndividual()
M: 0 C: 58
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 15
100%
M: 0 C: 1
100%
selectLoadsInstanceOfMostConcreteSubclassOfAbstractEntity()
M: 0 C: 34
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 10
100%
M: 0 C: 1
100%
selectLoadsInstanceOfMostConcreteSubclassOfConcreteEntity()
M: 0 C: 34
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 10
100%
M: 0 C: 1
100%
tearDown()
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
verifyEntityTAttributes(OWLClassT, OWLClassT)
M: 0 C: 28
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 6
100%
M: 0 C: 1
100%

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.query.runner;
14:
15: import cz.cvut.kbss.jopa.model.EntityManager;
16: import cz.cvut.kbss.jopa.test.OWLClassS;
17: import cz.cvut.kbss.jopa.test.OWLClassSParent;
18: import cz.cvut.kbss.jopa.test.OWLClassT;
19: import cz.cvut.kbss.jopa.test.Vocabulary;
20: import cz.cvut.kbss.jopa.test.environment.Generators;
21: import cz.cvut.kbss.jopa.test.query.QueryTestEnvironment;
22: import cz.cvut.kbss.jopa.vocabulary.RDFS;
23: import org.junit.jupiter.api.AfterEach;
24: import org.junit.jupiter.api.Test;
25: import org.slf4j.Logger;
26:
27: import java.net.URI;
28: import java.util.Arrays;
29: import java.util.Collections;
30: import java.util.HashSet;
31: import java.util.List;
32:
33: import static org.junit.jupiter.api.Assertions.*;
34:
35:
36: public abstract class PolymorphicSelectQueryRunner extends BaseQueryRunner {
37:
38: protected PolymorphicSelectQueryRunner(Logger logger) {
39: super(logger);
40: }
41:
42: @AfterEach
43: public void tearDown() {
44: getEntityManager().clear();
45: getEntityManager().getEntityManagerFactory().getCache().evictAll();
46: }
47:
48: @Test
49: public void selectLoadsInstanceOfMostConcreteSubclassOfAbstractEntity() {
50: final OWLClassT t = Generators.getRandomItem(QueryTestEnvironment.getData(OWLClassT.class));
51: final EntityManager em = getEntityManager();
52: final OWLClassSParent result =
53: em.createNativeQuery("SELECT ?x WHERE { ?x ?hasName ?name . }", OWLClassSParent.class)
54: .setParameter("hasName", URI.create(RDFS.LABEL))
55: .setParameter("name", t.getName(), "en").getSingleResult();
56: assertNotNull(result);
57: assertTrue(result instanceof OWLClassT);
58: verifyEntityTAttributes(t, (OWLClassT) result);
59: }
60:
61: private static void verifyEntityTAttributes(OWLClassT expected, OWLClassT actual) {
62: assertEquals(expected.getUri(), actual.getUri());
63: assertEquals(expected.getName(), actual.getName());
64: assertEquals(expected.getDescription(), actual.getDescription());
65: assertEquals(expected.getIntAttribute(), actual.getIntAttribute());
66: assertEquals(expected.getOwlClassA().getUri(), actual.getOwlClassA().getUri());
67: }
68:
69: @Test
70: public void selectLoadsInstanceOfMostConcreteSubclassOfConcreteEntity() {
71: final OWLClassT t = Generators.getRandomItem(QueryTestEnvironment.getData(OWLClassT.class));
72: final EntityManager em = getEntityManager();
73: final OWLClassS result =
74: em.createNativeQuery("SELECT ?x WHERE { ?x ?hasName ?name . }", OWLClassS.class)
75: .setParameter("hasName", URI.create(RDFS.LABEL))
76: .setParameter("name", t.getName(), "en").getSingleResult();
77: assertNotNull(result);
78: assertTrue(result instanceof OWLClassT);
79: verifyEntityTAttributes(t, (OWLClassT) result);
80: }
81:
82: @Test
83: void selectByTypeLoadsAllIndividualsAsMostConcreteSubclassInstances() {
84: final List<OWLClassT> data = QueryTestEnvironment.getData(OWLClassT.class);
85: final EntityManager em = getEntityManager();
86: // This will cause the type resolver to have to do some work
87: em.getTransaction().begin();
88: data.forEach(t -> {
89: t.setTypes(new HashSet<>(Arrays.asList(Vocabulary.C_OWL_CLASS_S_PARENT, Vocabulary.C_OWL_CLASS_S)));
90: em.merge(t);
91: });
92: em.getTransaction().commit();
93: final List<OWLClassSParent> result =
94: em.createNativeQuery("SELECT ?x WHERE { ?x a ?type . }", OWLClassSParent.class)
95: .setParameter("type", URI.create(
96: Vocabulary.C_OWL_CLASS_S_PARENT)).getResultList();
97: assertEquals(data.size(), result.size());
98:
99: boolean found;
100:• for (OWLClassT t : data) {
101: found = false;
102:• for (OWLClassSParent tt : result) {
103:• if (t.getUri().equals(tt.getUri())) {
104: found = true;
105: assertTrue(tt instanceof OWLClassT);
106: verifyEntityTAttributes(t, (OWLClassT) tt);
107: break;
108: }
109: }
110: assertTrue(found);
111: }
112: }
113:
114: @Test
115: void selectLoadsInstanceAsGivenTypeWhenItIsConcreteAndFoundInTypesOfIndividual() {
116: final OWLClassT t = Generators.getRandomItem(QueryTestEnvironment.getData(OWLClassT.class));
117: final EntityManager em = getEntityManager();
118: em.getTransaction().begin();
119: t.setTypes(Collections.singleton(Vocabulary.C_OWL_CLASS_S));
120: em.merge(t);
121: em.getTransaction().commit();
122:
123: final OWLClassS result = em.createNativeQuery("SELECT ?x WHERE { ?x ?hasInt ?int. }", OWLClassS.class)
124: .setParameter("hasInt", URI.create(Vocabulary.P_T_INTEGER_ATTRIBUTE))
125: .setParameter("int", t.getIntAttribute()).getSingleResult();
126: assertNotNull(result);
127: assertFalse(result instanceof OWLClassT);
128: assertEquals(t.getName(), result.getName());
129: assertEquals(t.getDescription(), result.getDescription());
130: assertTrue(result.getTypes().contains(Vocabulary.C_OWL_CLASS_T));
131: }
132: }