Skip to content

Package: RetrieveOperationsRunner

RetrieveOperationsRunner

nameinstructionbranchcomplexitylinemethod
RetrieveOperationsRunner(Logger, PersistenceFactory, DataAccessor)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
findByUnknownIdReturnsNull()
M: 0 C: 18
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
findOfEntityWithExistingIdButDifferentTypeReturnsNull()
M: 0 C: 27
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
getReferenceRetrievesReferenceToInstanceWhenCacheIsEnabled()
M: 0 C: 39
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
getReferenceRetrievesReferenceToInstanceWithDataPropertiesWhoseAttributesAreLoadedLazily()
M: 0 C: 76
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 16
100%
M: 0 C: 1
100%
getReferenceRetrievesReferenceToInstanceWithObjectPropertiesWhoseAttributesAreLoadedLazily()
M: 0 C: 57
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 11
100%
M: 0 C: 1
100%
lambda$0(OWLClassA)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
lambda$3(Set, Thing)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
loadEntitySupportsCollectionAttribute()
M: 0 C: 85
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 11
100%
M: 0 C: 1
100%
loadEntityWithSimpleLiteralLoadsAlsoLanguageTaggedValue()
M: 0 C: 56
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 8
100%
M: 0 C: 1
100%
loadingEntityWithLexicalFormAttributeLoadsLexicalFormOfLiteral()
M: 0 C: 47
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 9
100%
M: 0 C: 1
100%
loadingEntityWithSimpleLiteralLoadsSimpleLiteralValue()
M: 0 C: 56
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 9
100%
M: 0 C: 1
100%
plainIdentifierAttributeIsAlwaysLoadedEagerly()
M: 0 C: 59
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 12
100%
M: 0 C: 1
100%
readingIndividualWithStringIdTwiceInPersistenceContextReturnsSameInstance()
M: 0 C: 35
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
refreshingNotManagedIsIllegal()
M: 0 C: 40
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 8
100%
M: 0 C: 1
100%
reloadAllowsToReloadFileStorageContent()
M: 0 C: 100
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 23
100%
M: 0 C: 1
100%
replaceFileContents(File)
M: 0 C: 22
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
retrieveAllowsToOverridePULevelLanguageSpecification()
M: 0 C: 73
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 14
100%
M: 0 C: 1
100%
retrieveGetsStringAttributeWithCorrectLanguageWhenItIsSpecifiedInDescriptor()
M: 0 C: 65
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 14
100%
M: 0 C: 1
100%
retrieveGetsStringAttributesWithDifferentLanguageTagsSpecifiedInDescriptor()
M: 0 C: 111
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 22
100%
M: 0 C: 1
100%
retrieveLoadsStringLiteralWithCorrectLanguageTagWhenCachedValueHasDifferentLanguageTag()
M: 0 C: 72
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 14
100%
M: 0 C: 1
100%
retrieveLoadsUnmappedPropertiesTogetherWithObjectPropertyValues()
M: 0 C: 116
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 21
100%
M: 0 C: 1
100%
testRefreshInstance()
M: 0 C: 65
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 12
100%
M: 0 C: 1
100%
testRefreshInstanceWithUnmappedProperties()
M: 0 C: 50
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 11
100%
M: 0 C: 1
100%
testRetrieveEntityWithManagedTypeQueryAttr()
M: 0 C: 52
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 8
100%
M: 0 C: 1
100%
testRetrieveEntityWithQueryAttr()
M: 0 C: 52
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 9
100%
M: 0 C: 1
100%
testRetrieveSimple()
M: 0 C: 53
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 9
100%
M: 0 C: 1
100%
testRetrieveWithGeneratedId()
M: 0 C: 82
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 17
100%
M: 0 C: 1
100%
testRetrieveWithLazyAttribute()
M: 0 C: 59
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 13
100%
M: 0 C: 1
100%
testRetrieveWithLazyQueryAttribute()
M: 0 C: 76
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 16
100%
M: 0 C: 1
100%

Coverage

1: /**
2: * Copyright (C) 2020 Czech Technical University in Prague
3: * <p>
4: * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
5: * the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
6: * <p>
7: * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
8: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a
9: * copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
10: */
11: package cz.cvut.kbss.jopa.test.runner;
12:
13: import cz.cvut.kbss.jopa.model.JOPAPersistenceProperties;
14: import cz.cvut.kbss.jopa.model.descriptors.Descriptor;
15: import cz.cvut.kbss.jopa.model.descriptors.EntityDescriptor;
16: import cz.cvut.kbss.jopa.model.query.TypedQuery;
17: import cz.cvut.kbss.jopa.test.*;
18: import cz.cvut.kbss.jopa.test.environment.DataAccessor;
19: import cz.cvut.kbss.jopa.test.environment.Generators;
20: import cz.cvut.kbss.jopa.test.environment.PersistenceFactory;
21: import cz.cvut.kbss.jopa.test.environment.Quad;
22: import cz.cvut.kbss.jopa.vocabulary.RDF;
23: import cz.cvut.kbss.ontodriver.ReloadableDataSource;
24: import cz.cvut.kbss.ontodriver.config.OntoDriverProperties;
25: import org.junit.jupiter.api.Test;
26: import org.slf4j.Logger;
27:
28: import java.io.File;
29: import java.io.IOException;
30: import java.io.InputStream;
31: import java.lang.reflect.Field;
32: import java.net.URI;
33: import java.net.URL;
34: import java.nio.file.Files;
35: import java.nio.file.StandardCopyOption;
36: import java.util.*;
37: import java.util.stream.Collectors;
38:
39: import static org.hamcrest.CoreMatchers.hasItems;
40: import static org.hamcrest.MatcherAssert.assertThat;
41: import static org.junit.jupiter.api.Assertions.*;
42:
43:
44: public abstract class RetrieveOperationsRunner extends BaseRunner {
45:
46: public RetrieveOperationsRunner(Logger logger, PersistenceFactory persistenceFactory, DataAccessor dataAccessor) {
47: super(logger, persistenceFactory, dataAccessor);
48: }
49:
50: @Test
51: void testRetrieveSimple() {
52: this.em = getEntityManager("RetrieveSimple", false);
53: persist(entityA);
54:
55: em.getEntityManagerFactory().getCache().evictAll();
56: final OWLClassA res = findRequired(OWLClassA.class, entityA.getUri());
57: assertEquals(entityA.getUri(), res.getUri());
58: assertEquals(entityA.getStringAttribute(), res.getStringAttribute());
59: assertTrue(entityA.getTypes().containsAll(res.getTypes()));
60: assertTrue(em.contains(res));
61: }
62:
63: @Test
64: void testRetrieveWithLazyAttribute() throws Exception {
65: this.em = getEntityManager("RetrieveLazy", false);
66: persist(entityI);
67:
68: final OWLClassI resI = findRequired(OWLClassI.class, entityI.getUri());
69: final Field f = OWLClassI.class.getDeclaredField("owlClassA");
70: f.setAccessible(true);
71: Object value = f.get(resI);
72: assertNull(value);
73: assertNotNull(resI.getOwlClassA());
74: value = f.get(resI);
75: assertNotNull(value);
76: assertEquals(entityA.getUri(), resI.getOwlClassA().getUri());
77: assertTrue(em.contains(resI.getOwlClassA()));
78: }
79:
80: @Test
81: void testRetrieveWithGeneratedId() {
82: this.em = getEntityManager("RetrieveGenerated", false);
83: em.getTransaction().begin();
84: final int size = 10;
85: final List<OWLClassE> lst = new ArrayList<>(size);
86:• for (int i = 0; i < size; i++) {
87: final OWLClassE e = new OWLClassE();
88: e.setStringAttribute("blablabla" + i);
89: assertNull(e.getUri());
90: em.persist(e);
91: assertNotNull(e.getUri());
92: lst.add(e);
93: }
94: em.getTransaction().commit();
95:
96: em.clear();
97:• for (OWLClassE e : lst) {
98: final OWLClassE res = findRequired(OWLClassE.class, e.getUri());
99: assertEquals(e.getStringAttribute(), res.getStringAttribute());
100: }
101: }
102:
103: @Test
104: void findByUnknownIdReturnsNull() {
105: this.em = getEntityManager("RetrieveNotExisting", false);
106: final OWLClassB res = em.find(OWLClassB.class, entityB.getUri());
107: assertNull(res);
108: }
109:
110: @Test
111: void testRefreshInstance() {
112: this.em = getEntityManager("Refresh", false);
113: persist(entityD, entityA);
114:
115: final OWLClassA newA = new OWLClassA();
116: newA.setUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityA"));
117: newA.setStringAttribute("newA");
118: final OWLClassD d = findRequired(OWLClassD.class, entityD.getUri());
119: final OWLClassA a = findRequired(OWLClassA.class, entityA.getUri());
120: assertEquals(d.getOwlClassA(), a);
121: d.setOwlClassA(newA);
122: em.refresh(d);
123: assertEquals(a.getUri(), d.getOwlClassA().getUri());
124: }
125:
126: @Test
127: void refreshingNotManagedIsIllegal() {
128: this.em = getEntityManager("RefreshNotManaged", false);
129: persist(entityA);
130:
131: findRequired(OWLClassA.class, entityA.getUri());
132: final OWLClassA newA = new OWLClassA();
133: newA.setUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityA"));
134: newA.setStringAttribute("newA");
135: assertThrows(IllegalArgumentException.class, () -> em.refresh(newA));
136: }
137:
138: @Test
139: void findOfEntityWithExistingIdButDifferentTypeReturnsNull() {
140: this.em = getEntityManager("RetrieveDifferentType", false);
141: persist(entityA);
142:
143: final OWLClassB res = em.find(OWLClassB.class, entityA.getUri());
144: assertNull(res);
145: }
146:
147: @Test
148: void testRefreshInstanceWithUnmappedProperties() {
149: this.em = getEntityManager("RefreshEntityWithProperties", false);
150: final Map<URI, Set<Object>> properties = Generators.createTypedProperties();
151: entityP.setProperties(properties);
152: persist(entityP);
153:
154: final OWLClassP p = findRequired(OWLClassP.class, entityP.getUri());
155: p.getProperties().put(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#addedProperty"),
156: Collections.singleton("Test"));
157: assertNotEquals(properties, p.getProperties());
158: em.refresh(p);
159: assertEquals(properties, p.getProperties());
160: }
161:
162: @Test
163: void plainIdentifierAttributeIsAlwaysLoadedEagerly() throws Exception {
164: this.em = getEntityManager("PlainIdentifiersAreLoadedEagerly", false);
165: entityP.setIndividualUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#plainIdentifier"));
166: entityP.setIndividuals(Collections.singleton(new URL("http://krizik.felk.cvut.cz/ontologies/jopa#url")));
167: persist(entityP);
168:
169: final OWLClassP res = findRequired(OWLClassP.class, entityP.getUri());
170: final Field singularField = OWLClassP.class.getDeclaredField("individualUri");
171: singularField.setAccessible(true);
172: assertNotNull(singularField.get(res));
173: final Field pluralField = OWLClassP.class.getDeclaredField("individuals");
174: pluralField.setAccessible(true);
175: assertNotNull(pluralField.get(res));
176: }
177:
178: @Test
179: void readingIndividualWithStringIdTwiceInPersistenceContextReturnsSameInstance() {
180: this.em = getEntityManager("readingIndividualWithStringIdTwiceInPersistenceContextReturnsSameInstance", true);
181: persist(entityN);
182:
183: final OWLClassN resultOne = findRequired(OWLClassN.class, entityN.getId());
184: final OWLClassN resultTwo = findRequired(OWLClassN.class, entityN.getId());
185: assertSame(resultOne, resultTwo);
186: }
187:
188: @Test
189: void retrieveLoadsUnmappedPropertiesTogetherWithObjectPropertyValues() {
190: this.em = getEntityManager("retrieveLoadsUnmappedPropertiesTogetherWithObjectPropertyValues", false);
191: final OWLClassV v = new OWLClassV();
192: v.setProperties(Generators.createProperties());
193: v.setThings(new HashSet<>());
194:• for (int i = 0; i < Generators.randomPositiveInt(5, 10); i++) {
195: final Thing thing = new Thing();
196: thing.setName("thing" + i);
197: thing.setDescription("description of a thing. Number " + i);
198: thing.setTypes(Collections.singleton(Vocabulary.C_OWL_CLASS_A));
199: v.getThings().add(thing);
200: }
201: em.getTransaction().begin();
202: em.persist(v);
203: v.getThings().forEach(em::persist);
204: em.getTransaction().commit();
205: em.clear();
206:
207: final OWLClassV result = findRequired(OWLClassV.class, v.getUri());
208: assertEquals(v.getProperties(), result.getProperties());
209: final Set<String> expectedUris = v.getThings().stream().map(Thing::getUri).collect(Collectors.toSet());
210: assertEquals(v.getThings().size(), result.getThings().size());
211: result.getThings().forEach(t -> assertTrue(expectedUris.contains(t.getUri())));
212: }
213:
214: @Test
215: void retrieveGetsStringAttributeWithCorrectLanguageWhenItIsSpecifiedInDescriptor() throws Exception {
216: this.em = getEntityManager("retrieveGetsStringAttributeWithCorrectLanguageWhenItIsSpecifiedInDescriptor",
217: false);
218: persist(entityA);
219: final String value = "v cestine";
220: final String lang = "cs";
221: persistTestData(Collections
222: .singleton(new Quad(entityA.getUri(), URI.create(Vocabulary.P_A_STRING_ATTRIBUTE), value, lang)), em);
223:
224: final Descriptor descriptor = new EntityDescriptor();
225: descriptor.setLanguage(lang);
226:
227: final OWLClassA result = em.find(OWLClassA.class, entityA.getUri(), descriptor);
228: assertNotNull(result);
229: assertEquals(value, result.getStringAttribute());
230: assertEquals(entityA.getTypes(), result.getTypes());
231: }
232:
233: @Test
234: void retrieveGetsStringAttributesWithDifferentLanguageTagsSpecifiedInDescriptor() throws Exception {
235: this.em = getEntityManager("retrieveGetsStringAttributesWithDifferentLanguageTagsSpecifiedInDescriptor", false);
236: entityN.setAnnotationProperty("english annotation");
237: entityN.setStringAttribute("english string");
238: persist(entityN);
239: final String csAnnotation = "anotace cesky";
240: final String csString = "retezec cesky";
241: final Set<Quad> testData = new HashSet<>();
242: testData.add(new Quad(URI.create(entityN.getId()), URI.create(Vocabulary.P_N_STR_ANNOTATION_PROPERTY),
243: csAnnotation, "cs"));
244: testData.add(
245: new Quad(URI.create(entityN.getId()), URI.create(Vocabulary.P_N_STRING_ATTRIBUTE), csString, "cs"));
246: persistTestData(testData, em);
247:
248: final Descriptor descriptor = new EntityDescriptor();
249: descriptor.setAttributeLanguage(
250: em.getMetamodel().entity(OWLClassN.class).getDeclaredAttribute("annotationProperty"), "en");
251: descriptor
252: .setAttributeLanguage(em.getMetamodel().entity(OWLClassN.class).getDeclaredAttribute("stringAttribute"),
253: "cs");
254: final OWLClassN result = em.find(OWLClassN.class, entityN.getId(), descriptor);
255: assertEquals(entityN.getAnnotationProperty(), result.getAnnotationProperty());
256: assertEquals(csString, result.getStringAttribute());
257: }
258:
259: @Test
260: void retrieveAllowsToOverridePULevelLanguageSpecification() throws Exception {
261: this.em = getEntityManager("retrieveAllowsToOverridePULevelLanguageSpecification", false);
262: entityA.setStringAttribute(null);
263: // PU-level language is en
264: persist(entityA);
265: final String value = "cestina";
266: persistTestData(Collections
267: .singleton(new Quad(entityA.getUri(), URI.create(Vocabulary.P_A_STRING_ATTRIBUTE), value, "cs")), em);
268:
269: final OWLClassA resOne = findRequired(OWLClassA.class, entityA.getUri());
270: assertNull(resOne.getStringAttribute());
271: em.clear();
272:
273: final Descriptor descriptor = new EntityDescriptor();
274: descriptor.setLanguage("cs");
275: final OWLClassA resTwo = em.find(OWLClassA.class, entityA.getUri(), descriptor);
276: assertEquals(value, resTwo.getStringAttribute());
277: }
278:
279: @Test
280: void retrieveLoadsStringLiteralWithCorrectLanguageTagWhenCachedValueHasDifferentLanguageTag()
281: throws Exception {
282: this.em = getEntityManager(
283: "retrieveLoadsStringLiteralWithCorrectLanguageTagWhenCachedValueHasDifferentLanguageTag", true);
284: persist(entityA); // persisted with @en
285: final String value = "cestina";
286: persistTestData(Collections
287: .singleton(new Quad(entityA.getUri(), URI.create(Vocabulary.P_A_STRING_ATTRIBUTE), value, "cs")), em);
288:
289: final OWLClassA resOne = findRequired(OWLClassA.class, entityA.getUri());
290: assertEquals(entityA.getStringAttribute(), resOne.getStringAttribute());
291: em.clear();
292:
293: final Descriptor descriptor = new EntityDescriptor();
294: descriptor.setLanguage("cs");
295: final OWLClassA resTwo = em.find(OWLClassA.class, entityA.getUri(), descriptor);
296: assertEquals(value, resTwo.getStringAttribute());
297: }
298:
299: @Test
300: public void reloadAllowsToReloadFileStorageContent() throws Exception {
301: final Map<String, String> props = new HashMap<>();
302: final File storage = Files.createTempFile("reload-driver-test", ".owl").toFile();
303: storage.deleteOnExit();
304: final String initialContent = "<?xml version=\"1.0\"?>\n" +
305: "<rdf:RDF\n" +
306: " xmlns:owl = \"http://www.w3.org/2002/07/owl#\"\n" +
307: " xmlns:rdf = \"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">" +
308: "<owl:Ontology rdf:about=\"http://www.w3.org/TR/2003/PR-owl-guide-20031209/wine\"></owl:Ontology>" +
309: "</rdf:RDF>";
310: Files.write(storage.toPath(), initialContent.getBytes());
311: props.put(JOPAPersistenceProperties.ONTOLOGY_PHYSICAL_URI_KEY, storage.toURI().toString());
312: props.put(JOPAPersistenceProperties.ONTOLOGY_URI_KEY, "http://www.w3.org/TR/2003/PR-owl-guide-20031209/wine");
313: props.put(OntoDriverProperties.USE_TRANSACTIONAL_ONTOLOGY, Boolean.toString(false));
314: addFileStorageProperties(props);
315: this.em = getEntityManager("reloadAllowsToReloadFileStorageContent", false, props);
316: final String subject = "http://www.w3.org/TR/2003/PR-owl-guide-20031209/wine#CaliforniaRegion";
317: final String type = "http://www.w3.org/TR/2003/PR-owl-guide-20031209/wine#Region";
318: em.getTransaction().begin();
319: final TypedQuery<Boolean> query =
320: em.createNativeQuery("ASK { ?x a ?y . }", Boolean.class).setParameter("x", URI.create(subject))
321: .setParameter("y", URI.create(type));
322: assertFalse(query.getSingleResult());
323: replaceFileContents(storage);
324:
325: final ReloadableDataSource ds = em.getEntityManagerFactory().unwrap(ReloadableDataSource.class);
326: ds.reload();
327: // Need to force OWLAPI driver to open a new ontology snapshot with the reloaded data
328: em.getTransaction().commit();
329: assertTrue(query.getSingleResult());
330: }
331:
332: private void replaceFileContents(File target) throws IOException {
333: try (final InputStream is = new URL("https://www.w3.org/TR/2003/PR-owl-guide-20031209/wine").openStream()) {
334: Files.copy(is, target.toPath(), StandardCopyOption.REPLACE_EXISTING);
335: }
336: }
337:
338: protected abstract void addFileStorageProperties(Map<String, String> properties);
339:
340: @Test
341: void getReferenceRetrievesReferenceToInstanceWithDataPropertiesWhoseAttributesAreLoadedLazily()
342: throws Exception {
343: this.em = getEntityManager(
344: "getReferenceRetrievesReferenceToInstanceWithDataPropertiesWhoseAttributesAreLoadedLazily", false);
345: persist(entityM);
346: final OWLClassM result = em.getReference(OWLClassM.class, entityM.getKey());
347: assertNotNull(result);
348: assertTrue(em.contains(result));
349: final Field intAttField = OWLClassM.class.getDeclaredField("intAttribute");
350: intAttField.setAccessible(true);
351: assertNull(intAttField.get(result));
352: assertNotNull(result.getIntAttribute());
353: assertEquals(entityM.getBooleanAttribute(), result.getBooleanAttribute());
354: assertEquals(entityM.getIntAttribute(), result.getIntAttribute());
355: assertEquals(entityM.getIntegerSet(), result.getIntegerSet());
356: assertEquals(entityM.getDateAttribute(), result.getDateAttribute());
357: assertEquals(entityM.getEnumAttribute(), result.getEnumAttribute());
358: }
359:
360: @Test
361: void getReferenceRetrievesReferenceToInstanceWithObjectPropertiesWhoseAttributesAreLoadedLazily() {
362: this.em = getEntityManager(
363: "getReferenceRetrievesReferenceToInstanceWithObjectPropertiesWhoseAttributesAreLoadedLazily", false);
364: persist(entityG);
365: final OWLClassG gResult = em.getReference(OWLClassG.class, entityG.getUri());
366: assertNotNull(gResult);
367: assertNotNull(gResult.getOwlClassH());
368: assertEquals(entityH.getUri(), gResult.getOwlClassH().getUri());
369: assertNotNull(gResult.getOwlClassH().getOwlClassA());
370: assertEquals(entityA.getUri(), gResult.getOwlClassH().getOwlClassA().getUri());
371: assertEquals(entityA.getStringAttribute(), gResult.getOwlClassH().getOwlClassA().getStringAttribute());
372: }
373:
374: @Test
375: void getReferenceRetrievesReferenceToInstanceWhenCacheIsEnabled() {
376: this.em = getEntityManager("getReferenceRetrievesReferenceToInstanceWhenCacheIsEnabled", true);
377: persist(entityA);
378:
379: final OWLClassA result = em.getReference(OWLClassA.class, entityA.getUri());
380: assertNotNull(result);
381: assertEquals(entityA.getUri(), result.getUri());
382: assertEquals(entityA.getStringAttribute(), result.getStringAttribute());
383: }
384:
385: @Test
386: void loadingEntityWithLexicalFormAttributeLoadsLexicalFormOfLiteral() throws Exception {
387: this.em = getEntityManager("loadingEntityWithLexicalFormAttributeLoadsLexicalFormOfLiteral", false);
388: persist(entityM);
389: final Integer value = 117;
390: persistTestData(Collections
391: .singleton(new Quad(URI.create(entityM.getKey()), URI.create(Vocabulary.p_m_lexicalForm), value)),
392: em);
393: final OWLClassM result = findRequired(OWLClassM.class, entityM.getKey());
394: assertEquals(value.toString(), result.getLexicalForm());
395: }
396:
397: @Test
398: void loadingEntityWithSimpleLiteralLoadsSimpleLiteralValue() throws Exception {
399: this.em = getEntityManager("loadingEntityWithSimpleLiteralLoadsSimpleLiteralValue", false);
400: final String value = "test";
401: persistTestData(Arrays.asList(
402: new Quad(URI.create(entityM.getKey()), URI.create(RDF.TYPE), URI.create(Vocabulary.C_OWL_CLASS_M)),
403: new Quad(URI.create(entityM.getKey()), URI.create(Vocabulary.p_m_simpleLiteral), value, (String) null)),
404: em);
405:
406: final OWLClassM result = findRequired(OWLClassM.class, entityM.getKey());
407: assertEquals(value, result.getSimpleLiteral());
408: }
409:
410: @Test
411: void loadEntityWithSimpleLiteralLoadsAlsoLanguageTaggedValue() throws Exception {
412: this.em = getEntityManager("loadEntityWithSimpleLiteralLoadsAlsoLanguageTaggedValue", false);
413: final String value = "test";
414: persistTestData(Arrays.asList(
415: new Quad(URI.create(entityM.getKey()), URI.create(RDF.TYPE), URI.create(Vocabulary.C_OWL_CLASS_M)),
416: new Quad(URI.create(entityM.getKey()), URI.create(Vocabulary.p_m_simpleLiteral), value, "en")), em);
417:
418: final OWLClassM result = findRequired(OWLClassM.class, entityM.getKey());
419: assertEquals(value, result.getSimpleLiteral());
420: }
421:
422: @Test
423: void loadEntitySupportsCollectionAttribute() throws Exception {
424: this.em = getEntityManager("loadEntitySupportsCollectionAttribute", false);
425: persistTestData(Arrays.asList(
426: new Quad(URI.create(entityM.getKey()), URI.create(RDF.TYPE), URI.create(Vocabulary.C_OWL_CLASS_M)),
427: new Quad(URI.create(entityM.getKey()), URI.create(Vocabulary.p_m_StringCollection), "value", "en"),
428: new Quad(URI.create(entityM.getKey()), URI.create(Vocabulary.p_m_StringCollection), "valueTwo", "en")),
429: em);
430:
431: final OWLClassM result = findRequired(OWLClassM.class, entityM.getKey());
432: assertNotNull(result.getStringCollection());
433: assertFalse(result.getStringCollection().isEmpty());
434: assertThat(result.getStringCollection(), hasItems("value", "valueTwo"));
435: }
436:
437: @Test
438: void testRetrieveEntityWithQueryAttr() {
439: this.em = getEntityManager("RetrieveWithQueryAttr", false);
440:
441: persist(entityWithQueryAttr);
442:
443: em.getEntityManagerFactory().getCache().evictAll();
444: final OWLClassWithQueryAttr res = findRequired(OWLClassWithQueryAttr.class, entityWithQueryAttr.getUri());
445: assertEquals(entityWithQueryAttr.getUri(), res.getUri());
446: assertEquals(entityWithQueryAttr.getStringAttribute(), res.getStringAttribute());
447: assertEquals(entityWithQueryAttr.getStringAttribute(), res.getStringQueryAttribute());
448: assertTrue(em.contains(res));
449: }
450:
451: @Test
452: void testRetrieveEntityWithManagedTypeQueryAttr() {
453: this.em = getEntityManager("RetrieveWithManagedTypeQueryAttr", false);
454:
455: persist(entityWithQueryAttr2, entityA);
456:
457: final OWLClassWithQueryAttr2 res = findRequired(OWLClassWithQueryAttr2.class, entityWithQueryAttr2.getUri());
458: assertEquals(entityWithQueryAttr2.getUri(), res.getUri());
459: assertEquals(entityWithQueryAttr2.getEntityAttribute(), res.getEntityAttribute());
460: assertEquals(entityWithQueryAttr2.getEntityAttribute(), res.getEntityQueryAttribute());
461: assertTrue(em.contains(res));
462: }
463:
464: @Test
465: void testRetrieveWithLazyQueryAttribute() throws Exception {
466: this.em = getEntityManager("RetrieveLazyQueryAttr", false);
467:
468: Set<OWLClassA> simpleSet = Generators.createSimpleSet(20);
469: entityWithQueryAttr6.setPluralAttribute(simpleSet);
470:
471: persist(entityWithQueryAttr6);
472:
473: final OWLClassWithQueryAttr6 res = findRequired(OWLClassWithQueryAttr6.class, entityWithQueryAttr6.getUri());
474: final Field f = OWLClassWithQueryAttr6.class.getDeclaredField("pluralQueryAttribute");
475: f.setAccessible(true);
476: Object value = f.get(res);
477: assertNull(value);
478: assertNotNull(res.getPluralQueryAttribute());
479: value = f.get(res);
480: assertNotNull(value);
481: assertEquals(entityWithQueryAttr6.getPluralAttribute(), res.getPluralQueryAttribute());
482:• for (OWLClassA classA : res.getPluralQueryAttribute()) {
483: assertTrue(em.contains(classA));
484: }
485: }
486: }