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