Skip to content

Method: testRetrieveWithLazyAttribute()

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.descriptors.Descriptor;
18: import cz.cvut.kbss.jopa.model.descriptors.EntityDescriptor;
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.lang.reflect.Field;
26: import java.net.URI;
27: import java.net.URL;
28: import java.util.*;
29: import java.util.stream.Collectors;
30:
31: import static org.junit.Assert.*;
32:
33: public abstract class RetrieveOperationsRunner extends BaseRunner {
34:
35: public RetrieveOperationsRunner(Logger logger) {
36: super(logger);
37: }
38:
39: @Test
40: public void testRetrieveSimple() {
41: this.em = getEntityManager("RetrieveSimple", false);
42: persist(entityA);
43:
44: em.getEntityManagerFactory().getCache().evictAll();
45: final OWLClassA res = em.find(OWLClassA.class, entityA.getUri());
46: assertNotNull(res);
47: assertEquals(entityA.getUri(), res.getUri());
48: assertEquals(entityA.getStringAttribute(), res.getStringAttribute());
49: assertTrue(entityA.getTypes().containsAll(res.getTypes()));
50: assertTrue(em.contains(res));
51: }
52:
53: @Test(expected = NullPointerException.class)
54: public void findWithNullIdentifierThrowsNPX() {
55: this.em = getEntityManager("RetrieveNull", false);
56: em.find(OWLClassA.class, null);
57: }
58:
59: @Test
60: public void testRetrieveWithLazyAttribute() throws Exception {
61: this.em = getEntityManager("RetrieveLazy", false);
62: persist(entityI);
63:
64: final OWLClassI resI = em.find(OWLClassI.class, entityI.getUri());
65: assertNotNull(resI);
66: final Field f = OWLClassI.class.getDeclaredField("owlClassA");
67: f.setAccessible(true);
68: Object value = f.get(resI);
69: assertNull(value);
70: assertNotNull(resI.getOwlClassA());
71: value = f.get(resI);
72: assertNotNull(value);
73: assertEquals(entityA.getUri(), resI.getOwlClassA().getUri());
74: assertTrue(em.contains(resI.getOwlClassA()));
75: }
76:
77: @Test
78: public void testRetrieveWithGeneratedId() {
79: this.em = getEntityManager("RetrieveGenerated", false);
80: em.getTransaction().begin();
81: final int size = 10;
82: final List<OWLClassE> lst = new ArrayList<>(size);
83: for (int i = 0; i < size; i++) {
84: final OWLClassE e = new OWLClassE();
85: e.setStringAttribute("blablabla" + i);
86: assertNull(e.getUri());
87: em.persist(e);
88: assertNotNull(e.getUri());
89: lst.add(e);
90: }
91: em.getTransaction().commit();
92:
93: em.clear();
94: for (OWLClassE e : lst) {
95: final OWLClassE res = em.find(OWLClassE.class, e.getUri());
96: assertNotNull(res);
97: assertEquals(e.getStringAttribute(), res.getStringAttribute());
98: }
99: }
100:
101: @Test
102: public void findByUnknownIdReturnsNull() {
103: this.em = getEntityManager("RetrieveNotExisting", false);
104: final OWLClassB res = em.find(OWLClassB.class, entityB.getUri());
105: assertNull(res);
106: }
107:
108: @Test
109: public void testRefreshInstance() {
110: this.em = getEntityManager("Refresh", false);
111: persist(entityD, entityA);
112:
113: final OWLClassA newA = new OWLClassA();
114: newA.setUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityA"));
115: newA.setStringAttribute("newA");
116: final OWLClassD d = em.find(OWLClassD.class, entityD.getUri());
117: final OWLClassA a = em.find(OWLClassA.class, entityA.getUri());
118: assertEquals(d.getOwlClassA(), a);
119: d.setOwlClassA(newA);
120: em.refresh(d);
121: assertEquals(a.getUri(), d.getOwlClassA().getUri());
122: }
123:
124: @Test(expected = IllegalArgumentException.class)
125: public void refreshingNotManagedIsIllegal() {
126: this.em = getEntityManager("RefreshNotManaged", false);
127: persist(entityA);
128:
129: final OWLClassA a = em.find(OWLClassA.class, entityA.getUri());
130: assertNotNull(a);
131: final OWLClassA newA = new OWLClassA();
132: newA.setUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityA"));
133: newA.setStringAttribute("newA");
134: em.refresh(newA);
135: }
136:
137: @Test
138: public void findOfEntityWithExistingIdButDifferentTypeReturnsNull() {
139: this.em = getEntityManager("RetrieveDifferentType", false);
140: persist(entityA);
141:
142: final OWLClassB res = em.find(OWLClassB.class, entityA.getUri());
143: assertNull(res);
144: }
145:
146: @Test
147: public void testRefreshInstanceWithUnmappedProperties() {
148: this.em = getEntityManager("RefreshEntityWithProperties", false);
149: final Map<URI, Set<Object>> properties = Generators.createTypedProperties();
150: entityP.setProperties(properties);
151: persist(entityP);
152:
153: final OWLClassP p = em.find(OWLClassP.class, entityP.getUri());
154: assertNotNull(p);
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: public 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 = em.find(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: public void readingIndividualWithStringIdTwiceInPersistenceContextReturnsSameInstance() {
180: this.em = getEntityManager("readingIndividualWithStringIdTwiceInPersistenceContextReturnsSameInstance", true);
181: persist(entityN);
182:
183: final OWLClassN resultOne = em.find(OWLClassN.class, entityN.getId());
184: final OWLClassN resultTwo = em.find(OWLClassN.class, entityN.getId());
185: assertNotNull(resultOne);
186: assertNotNull(resultTwo);
187: assertSame(resultOne, resultTwo);
188: }
189:
190: @Test
191: public void retrieveLoadsUnmappedPropertiesTogetherWithObjectPropertyValues() {
192: this.em = getEntityManager("retrieveLoadsUnmappedPropertiesTogetherWithObjectPropertyValues", false);
193: final OWLClassV v = new OWLClassV();
194: v.setProperties(Generators.createProperties());
195: v.setThings(new HashSet<>());
196: for (int i = 0; i < Generators.randomPositiveInt(5, 10); i++) {
197: final Thing thing = new Thing();
198: thing.setName("thing" + i);
199: thing.setDescription("description of a thing. Number " + i);
200: thing.setTypes(Collections.singleton(Vocabulary.C_OWL_CLASS_A));
201: v.getThings().add(thing);
202: }
203: em.getTransaction().begin();
204: em.persist(v);
205: v.getThings().forEach(em::persist);
206: em.getTransaction().commit();
207: em.clear();
208:
209: final OWLClassV result = em.find(OWLClassV.class, v.getUri());
210: assertNotNull(result);
211: assertEquals(v.getProperties(), result.getProperties());
212: final Set<String> expectedUris = v.getThings().stream().map(Thing::getUri).collect(Collectors.toSet());
213: assertEquals(v.getThings().size(), result.getThings().size());
214: result.getThings().forEach(t -> assertTrue(expectedUris.contains(t.getUri())));
215: }
216:
217: @Test
218: public void retrieveGetsStringAttributeWithCorrectLanguageWhenItIsSpecifiedInDescriptor() throws Exception {
219: this.em = getEntityManager("retrieveGetsStringAttributeWithCorrectLanguageWhenItIsSpecifiedInDescriptor",
220: false);
221: persist(entityA);
222: final String value = "v cestine";
223: final String lang = "cs";
224: persistTestData(Collections
225: .singleton(new Triple(entityA.getUri(), URI.create(Vocabulary.P_A_STRING_ATTRIBUTE), value, lang)), em);
226:
227: final Descriptor descriptor = new EntityDescriptor();
228: descriptor.setLanguage(lang);
229:
230: final OWLClassA result = em.find(OWLClassA.class, entityA.getUri(), descriptor);
231: assertNotNull(result);
232: assertEquals(value, result.getStringAttribute());
233: assertEquals(entityA.getTypes(), result.getTypes());
234: }
235:
236: @Test
237: public void retrieveGetsStringAttributesWithDifferentLanguageTagsSpecifiedInDescriptor() throws Exception {
238: this.em = getEntityManager("retrieveGetsStringAttributesWithDifferentLanguageTagsSpecifiedInDescriptor", false);
239: entityN.setAnnotationProperty("english annotation");
240: entityN.setStringAttribute("english string");
241: persist(entityN);
242: final String csAnnotation = "anotace cesky";
243: final String csString = "retezec cesky";
244: final Set<Triple> testData = new HashSet<>();
245: testData.add(new Triple(URI.create(entityN.getId()), URI.create(Vocabulary.P_N_STR_ANNOTATION_PROPERTY),
246: csAnnotation, "cs"));
247: testData.add(
248: new Triple(URI.create(entityN.getId()), URI.create(Vocabulary.P_N_STRING_ATTRIBUTE), csString, "cs"));
249: persistTestData(testData, em);
250:
251: final Descriptor descriptor = new EntityDescriptor();
252: descriptor.setAttributeLanguage(OWLClassN.class.getDeclaredField("annotationProperty"), "en");
253: descriptor.setAttributeLanguage(OWLClassN.class.getDeclaredField("stringAttribute"), "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: public 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 Triple(entityA.getUri(), URI.create(Vocabulary.P_A_STRING_ATTRIBUTE), value, "cs")), em);
268:
269: final OWLClassA resOne = em.find(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: public 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 Triple(entityA.getUri(), URI.create(Vocabulary.P_A_STRING_ATTRIBUTE), value, "cs")), em);
288:
289: final OWLClassA resOne = em.find(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: }