Package: RetrieveOperationsWithInheritanceRunner
RetrieveOperationsWithInheritanceRunner
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
RetrieveOperationsWithInheritanceRunner(Logger) |
|
|
|
|
|
||||||||||||||||||||
findLoadsInstanceOfSuperclassFromCacheWhenTypeMatchesAndIsSpecifiedAsReturnType() |
|
|
|
|
|
||||||||||||||||||||
findLoadsMostConcreteSubclassOfAbstractAncestor() |
|
|
|
|
|
||||||||||||||||||||
findLoadsMostSpecificSubclassFromCache() |
|
|
|
|
|
||||||||||||||||||||
findLoadsSubclassOfAbstractParent() |
|
|
|
|
|
||||||||||||||||||||
findLoadsSubclassWhenSuperclassIsPassedInAndTypeCorrespondsToSubclass() |
|
|
|
|
|
||||||||||||||||||||
findLoadsSuperclassInstanceWhenRequestedAndClassAssertionIsPresent() |
|
|
|
|
|
||||||||||||||||||||
findReadsAttributesOfEntitySuperclass() |
|
|
|
|
|
||||||||||||||||||||
findReadsAttributesOfMappedSuperclass() |
|
|
|
|
|
||||||||||||||||||||
findReturnsMostSpecificSubtypeWhenReturnTypeIsAbstractAncestor() |
|
|
|
|
|
||||||||||||||||||||
findReturnsNullWhenMatchingClassIsAbstract() |
|
|
|
|
|
||||||||||||||||||||
findThrowsAmbiguousTypeExceptionWhenIndividualHasMultipleMostSpecificTypes() |
|
|
|
|
|
||||||||||||||||||||
findThrowsAmbiguousTypeExceptionWhenIndividualHasMultipleMostSpecificTypesInCache() |
|
|
|
|
|
||||||||||||||||||||
loadingEntityLoadsCorrectSubtypeInPolymorphicAttribute() |
|
|
|
|
|
||||||||||||||||||||
loadingEntityLoadsExactMatchOfPolymorphicAttribute() |
|
|
|
|
|
||||||||||||||||||||
triplesForA() |
|
|
|
|
|
||||||||||||||||||||
triplesForEntityT() |
|
|
|
|
|
||||||||||||||||||||
verifyEntityTAttributes(OWLClassT) |
|
|
|
|
|
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.runner;
16:
17: import cz.cvut.kbss.jopa.model.EntityManager;
18: import cz.cvut.kbss.jopa.model.descriptors.EntityDescriptor;
19: import cz.cvut.kbss.jopa.oom.exceptions.AmbiguousEntityTypeException;
20: import cz.cvut.kbss.jopa.test.*;
21: import cz.cvut.kbss.jopa.test.environment.Generators;
22: import cz.cvut.kbss.jopa.test.environment.Triple;
23: import org.junit.Test;
24: import org.slf4j.Logger;
25:
26: import java.net.URI;
27: import java.util.ArrayList;
28: import java.util.Collection;
29:
30: import static cz.cvut.kbss.jopa.CommonVocabulary.*;
31: import static org.junit.Assert.*;
32:
33: public abstract class RetrieveOperationsWithInheritanceRunner extends BaseInheritanceRunner {
34:
35: public RetrieveOperationsWithInheritanceRunner(Logger logger) {
36: super(logger);
37: }
38:
39: @Test
40: public void findReadsAttributesOfMappedSuperclass() throws Exception {
41: final Collection<Triple> data = new ArrayList<>();
42: entityQ.setUri(Generators.generateUri());
43: data.add(
44: new Triple(entityQ.getUri(), URI.create(RDF_TYPE), URI.create(Vocabulary.C_OWL_CLASS_Q)));
45: data.add(new Triple(entityQ.getUri(), URI.create(RDFS_LABEL), entityQ.getLabel()));
46: data.add(
47: new Triple(entityQ.getUri(), URI.create(Vocabulary.P_Q_PARENT_STRING_ATTRIBUTE),
48: entityQ.getParentString()));
49: data.add(new Triple(entityQ.getUri(), URI.create(Vocabulary.P_Q_STRING_ATTRIBUTE),
50: entityQ.getStringAttribute()));
51: data.addAll(triplesForA());
52: data.add(new Triple(entityQ.getUri(), URI.create(Vocabulary.P_HAS_OWL_CLASS_A), entityA.getUri()));
53: final EntityManager em = getEntityManager("findReadsAttributesOfMappedSuperclass", false);
54: persistTestData(data, em);
55:
56: final OWLClassQ result = em.find(OWLClassQ.class, entityQ.getUri());
57: assertNotNull(result);
58: assertEquals(entityQ.getStringAttribute(), result.getStringAttribute());
59: assertEquals(entityQ.getLabel(), result.getLabel());
60: assertEquals(entityQ.getParentString(), result.getParentString());
61: assertNotNull(result.getOwlClassA());
62: assertEquals(entityQ.getOwlClassA().getUri(), result.getOwlClassA().getUri());
63: }
64:
65: private Collection<Triple> triplesForA() {
66: final Collection<Triple> data = new ArrayList<>();
67: entityA.setUri(Generators.generateUri());
68: data.add(
69: new Triple(entityA.getUri(), URI.create(RDF_TYPE), URI.create(Vocabulary.C_OWL_CLASS_A)));
70: data.add(new Triple(entityA.getUri(), URI.create(Vocabulary.P_A_STRING_ATTRIBUTE),
71: entityA.getStringAttribute()));
72: return data;
73: }
74:
75: @Test
76: public void findReadsAttributesOfEntitySuperclass() throws Exception {
77: final Collection<Triple> data = triplesForEntityT();
78: final EntityManager em = getEntityManager("findReadsAttributesOfEntitySuperclass", false);
79: persistTestData(data, em);
80:
81: final OWLClassT result = em.find(OWLClassT.class, entityT.getUri());
82: assertNotNull(result);
83: assertEquals(entityT.getName(), result.getName());
84: assertEquals(entityT.getDescription(), result.getDescription());
85: assertEquals(entityT.getIntAttribute(), result.getIntAttribute());
86: assertNotNull(result.getOwlClassA());
87: assertEquals(entityA.getUri(), result.getOwlClassA().getUri());
88: }
89:
90: private Collection<Triple> triplesForEntityT() {
91: final Collection<Triple> data = new ArrayList<>();
92: entityT.setUri(Generators.generateUri());
93: data.add(new Triple(entityT.getUri(), URI.create(RDF_TYPE), URI.create(Vocabulary.C_OWL_CLASS_T)));
94: data.add(new Triple(entityT.getUri(), URI.create(RDFS_LABEL), entityT.getName()));
95: data.add(new Triple(entityT.getUri(), URI.create(DC_DESCRIPTION), entityT.getDescription()));
96: data.add(new Triple(entityT.getUri(), URI.create(Vocabulary.P_T_INTEGER_ATTRIBUTE), entityT.getIntAttribute()));
97: data.addAll(triplesForA());
98: data.add(new Triple(entityT.getUri(), URI.create(Vocabulary.P_HAS_OWL_CLASS_A), entityA.getUri()));
99: return data;
100: }
101:
102: @Test
103: public void findLoadsSuperclassInstanceWhenRequestedAndClassAssertionIsPresent() throws Exception {
104: final EntityManager em = getEntityManager("findLoadsSuperclassInstanceWhenRequestedAndClassAssertionIsPresent",
105: false);
106: final Collection<Triple> data = triplesForEntityT();
107: data.add(new Triple(entityT.getUri(), URI.create(RDF_TYPE), URI.create(Vocabulary.C_OWL_CLASS_S)));
108: persistTestData(data, em);
109:
110: final OWLClassS result = em.find(OWLClassS.class, entityT.getUri());
111: assertNotNull(result);
112: assertEquals(entityT.getName(), result.getName());
113: assertEquals(entityT.getDescription(), result.getDescription());
114: assertTrue(result.getTypes().contains(Vocabulary.C_OWL_CLASS_T));
115: }
116:
117: @Test
118: public void findLoadsSubclassWhenSuperclassIsPassedInAndTypeCorrespondsToSubclass() throws Exception {
119: final Collection<Triple> data = triplesForEntityT();
120:
121: final EntityManager em = getEntityManager(
122: "findLoadsSubclassWhenSuperclassIsPassedInAndTypeCorrespondsToSubclass",
123: false);
124: persistTestData(data, em);
125:
126: final OWLClassS result = em.find(OWLClassS.class, entityT.getUri());
127: assertNotNull(result);
128: assertTrue(result instanceof OWLClassT);
129: verifyEntityTAttributes((OWLClassT) result);
130: }
131:
132: private void verifyEntityTAttributes(OWLClassT result) {
133: assertEquals(entityT.getName(), result.getName());
134: assertEquals(entityT.getDescription(), result.getDescription());
135: assertEquals(entityT.getIntAttribute(), result.getIntAttribute());
136: assertEquals(entityT.getOwlClassA().getUri(), result.getOwlClassA().getUri());
137: }
138:
139: @Test
140: public void findLoadsSubclassOfAbstractParent() throws Exception {
141: final Collection<Triple> data = new ArrayList<>();
142: entityT.setUri(Generators.generateUri());
143: data.add(new Triple(entityT.getUri(), URI.create(RDF_TYPE), URI.create(Vocabulary.C_OWL_CLASS_S)));
144: data.add(new Triple(entityT.getUri(), URI.create(RDFS_LABEL), entityT.getName()));
145: data.add(new Triple(entityT.getUri(), URI.create(DC_DESCRIPTION), entityT.getDescription()));
146:
147: final EntityManager em = getEntityManager("findLoadsSubclassOfAbstractParent", false);
148: persistTestData(data, em);
149:
150: final OWLClassSParent result = em.find(OWLClassSParent.class, entityT.getUri());
151: assertNotNull(result);
152: assertTrue(result instanceof OWLClassS);
153: final OWLClassS sResult = (OWLClassS) result;
154: assertEquals(entityT.getName(), sResult.getName());
155: assertEquals(entityT.getDescription(), sResult.getDescription());
156: }
157:
158: @Test
159: public void findLoadsMostConcreteSubclassOfAbstractAncestor() throws Exception {
160: final Collection<Triple> data = triplesForEntityT();
161: data.add(new Triple(entityT.getUri(), URI.create(RDF_TYPE), URI.create(Vocabulary.C_OWL_CLASS_S)));
162:
163: final EntityManager em = getEntityManager("findLoadsMostConcreteSubclassOfAbstractAncestor", false);
164: persistTestData(data, em);
165:
166: final OWLClassSParent result = em.find(OWLClassSParent.class, entityT.getUri());
167: assertNotNull(result);
168: assertTrue(result instanceof OWLClassT);
169: verifyEntityTAttributes((OWLClassT) result);
170: }
171:
172: @Test
173: public void findReturnsNullWhenMatchingClassIsAbstract() throws Exception {
174: final Collection<Triple> data = triplesForEntityT();
175: data.remove(new Triple(entityT.getUri(), URI.create(RDF_TYPE), URI.create(Vocabulary.C_OWL_CLASS_T)));
176: data.add(new Triple(entityT.getUri(), URI.create(RDF_TYPE), URI.create(Vocabulary.C_OWL_CLASS_S_PARENT)));
177:
178: final EntityManager em = getEntityManager("findReturnsNullWhenMatchingClassIsAbstract", false);
179: persistTestData(data, em);
180:
181: assertNull(em.find(OWLClassSParent.class, entityT.getUri()));
182: }
183:
184: @Test
185: public void findReturnsMostSpecificSubtypeWhenReturnTypeIsAbstractAncestor() throws Exception {
186: final Collection<Triple> data = triplesForEntityT();
187: data.add(new Triple(entityT.getUri(), URI.create(RDF_TYPE), URI.create(Vocabulary.C_OWL_CLASS_S)));
188: data.add(new Triple(entityT.getUri(), URI.create(RDF_TYPE), URI.create(Vocabulary.C_OWL_CLASS_S_PARENT)));
189:
190: final EntityManager em = getEntityManager("findReturnsMostSpecificSubtypeWhenReturnTypeIsAbstractAncestor",
191: false);
192: persistTestData(data, em);
193:
194: final OWLClassSParent result = em.find(OWLClassSParent.class, entityT.getUri());
195: assertNotNull(result);
196: assertTrue(result instanceof OWLClassT);
197: verifyEntityTAttributes((OWLClassT) result);
198: }
199:
200: @Test
201: public void findLoadsMostSpecificSubclassFromCache() {
202: this.em = getEntityManager("findLoadsMostSpecificSubclassFromCache", true);
203: persist(entityT, entityA);
204:
205: final OWLClassSParent result = em.find(OWLClassSParent.class, entityT.getUri());
206: assertNotNull(result);
207: assertTrue(result instanceof OWLClassT);
208: verifyEntityTAttributes((OWLClassT) result);
209: }
210:
211: @Test
212: public void findLoadsInstanceOfSuperclassFromCacheWhenTypeMatchesAndIsSpecifiedAsReturnType() {
213: this.em = getEntityManager("findLoadsInstanceOfSuperclassWhenTypeMatchesAndIsSpecifiedAsReturnType", true);
214: persist(entityT, entityA);
215:
216: final OWLClassS result = em.find(OWLClassS.class, entityT.getUri());
217: assertNotNull(result);
218: assertEquals(entityT.getName(), result.getName());
219: assertEquals(entityT.getDescription(), result.getDescription());
220: }
221:
222: @Test
223: public void findThrowsAmbiguousTypeExceptionWhenIndividualHasMultipleMostSpecificTypes() throws Exception {
224: final Collection<Triple> data = triplesForEntityT();
225: data.add(new Triple(entityT.getUri(), URI.create(RDF_TYPE), URI.create(Vocabulary.C_OWL_CLASS_U)));
226: this.em = getEntityManager("findThrowsAmbiguousTypeExceptionWhenIndividualHasMultipleMostSpecificTypes", false);
227: persistTestData(data, em);
228: thrown.expect(AmbiguousEntityTypeException.class);
229:
230: em.find(OWLClassS.class, entityT.getUri());
231: }
232:
233: @Test
234: public void findThrowsAmbiguousTypeExceptionWhenIndividualHasMultipleMostSpecificTypesInCache() throws Exception {
235: this.em = getEntityManager("findThrowsAmbiguousTypeExceptionWhenIndividualHasMultipleMostSpecificTypesInCache",
236: true);
237: persist(entityT, entityA);
238: em.clear();
239: final OWLClassU sameIndividual = new OWLClassU();
240: sameIndividual.setUri(entityT.getUri());
241: sameIndividual.setName(entityT.getName());
242: persist(sameIndividual);
243: assertTrue(em.getEntityManagerFactory().getCache()
244: .contains(OWLClassT.class, entityT.getUri(), new EntityDescriptor()));
245: assertTrue(em.getEntityManagerFactory().getCache()
246: .contains(OWLClassU.class, sameIndividual.getUri(), new EntityDescriptor()));
247: thrown.expect(AmbiguousEntityTypeException.class);
248:
249: em.find(OWLClassS.class, entityT.getUri());
250: }
251:
252: @Test
253: public void loadingEntityLoadsExactMatchOfPolymorphicAttribute() {
254: this.em = getEntityManager("loadingEntityLoadsExactMatchOfPolymorphicAttribute", false);
255: final OWLClassS s = new OWLClassS();
256: s.setName("s");
257: s.setDescription("S - description");
258: entityU.setOwlClassS(s);
259: persist(entityU, s);
260:
261: final OWLClassU result = em.find(OWLClassU.class, entityU.getUri());
262: assertNotNull(result);
263: assertNotNull(result.getOwlClassS());
264: assertEquals(s.getUri(), result.getOwlClassS().getUri());
265: assertEquals(s.getName(), result.getOwlClassS().getName());
266: assertEquals(s.getDescription(), result.getOwlClassS().getDescription());
267: }
268:
269: @Test
270: public void loadingEntityLoadsCorrectSubtypeInPolymorphicAttribute() {
271: this.em = getEntityManager("loadingEntityLoadsCorrectSubtypeInPolymorphicAttribute", false);
272: persist(entityU, entityT, entityA);
273:
274: final OWLClassU result = em.find(OWLClassU.class, entityU.getUri());
275: assertNotNull(result.getOwlClassS());
276: assertTrue(result.getOwlClassS() instanceof OWLClassT);
277: final OWLClassT tResult = (OWLClassT) result.getOwlClassS();
278: verifyEntityTAttributes(tResult);
279: assertNotNull(tResult.getOwlClassA());
280: assertEquals(entityA.getUri(), tResult.getOwlClassA().getUri());
281: }
282: }