Skip to content

Method: triplesForEntityT()

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