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