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