Skip to contentMethod: retrieveLoadsUnmappedPropertiesTogetherWithObjectPropertyValues()
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.DataAccessor;
21: import cz.cvut.kbss.jopa.test.environment.Generators;
22: import cz.cvut.kbss.jopa.test.environment.PersistenceFactory;
23: import cz.cvut.kbss.jopa.test.environment.Triple;
24: import org.junit.Test;
25: import org.slf4j.Logger;
26:
27: import java.lang.reflect.Field;
28: import java.net.URI;
29: import java.net.URL;
30: import java.util.*;
31: import java.util.stream.Collectors;
32:
33: import static org.junit.Assert.*;
34:
35: public abstract class RetrieveOperationsRunner extends BaseRunner {
36:
37: public RetrieveOperationsRunner(Logger logger, PersistenceFactory persistenceFactory, DataAccessor dataAccessor) {
38: super(logger, persistenceFactory, dataAccessor);
39: }
40:
41: @Test
42: public void testRetrieveSimple() {
43: this.em = getEntityManager("RetrieveSimple", false);
44: persist(entityA);
45:
46: em.getEntityManagerFactory().getCache().evictAll();
47: final OWLClassA res = em.find(OWLClassA.class, entityA.getUri());
48: assertNotNull(res);
49: assertEquals(entityA.getUri(), res.getUri());
50: assertEquals(entityA.getStringAttribute(), res.getStringAttribute());
51: assertTrue(entityA.getTypes().containsAll(res.getTypes()));
52: assertTrue(em.contains(res));
53: }
54:
55: @Test(expected = NullPointerException.class)
56: public void findWithNullIdentifierThrowsNPX() {
57: this.em = getEntityManager("RetrieveNull", false);
58: em.find(OWLClassA.class, null);
59: }
60:
61: @Test
62: public void testRetrieveWithLazyAttribute() throws Exception {
63: this.em = getEntityManager("RetrieveLazy", false);
64: persist(entityI);
65:
66: final OWLClassI resI = em.find(OWLClassI.class, entityI.getUri());
67: assertNotNull(resI);
68: final Field f = OWLClassI.class.getDeclaredField("owlClassA");
69: f.setAccessible(true);
70: Object value = f.get(resI);
71: assertNull(value);
72: assertNotNull(resI.getOwlClassA());
73: value = f.get(resI);
74: assertNotNull(value);
75: assertEquals(entityA.getUri(), resI.getOwlClassA().getUri());
76: assertTrue(em.contains(resI.getOwlClassA()));
77: }
78:
79: @Test
80: public void testRetrieveWithGeneratedId() {
81: this.em = getEntityManager("RetrieveGenerated", false);
82: em.getTransaction().begin();
83: final int size = 10;
84: final List<OWLClassE> lst = new ArrayList<>(size);
85: for (int i = 0; i < size; i++) {
86: final OWLClassE e = new OWLClassE();
87: e.setStringAttribute("blablabla" + i);
88: assertNull(e.getUri());
89: em.persist(e);
90: assertNotNull(e.getUri());
91: lst.add(e);
92: }
93: em.getTransaction().commit();
94:
95: em.clear();
96: for (OWLClassE e : lst) {
97: final OWLClassE res = em.find(OWLClassE.class, e.getUri());
98: assertNotNull(res);
99: assertEquals(e.getStringAttribute(), res.getStringAttribute());
100: }
101: }
102:
103: @Test
104: public 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: public 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 = em.find(OWLClassD.class, entityD.getUri());
119: final OWLClassA a = em.find(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(expected = IllegalArgumentException.class)
127: public void refreshingNotManagedIsIllegal() {
128: this.em = getEntityManager("RefreshNotManaged", false);
129: persist(entityA);
130:
131: final OWLClassA a = em.find(OWLClassA.class, entityA.getUri());
132: assertNotNull(a);
133: final OWLClassA newA = new OWLClassA();
134: newA.setUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityA"));
135: newA.setStringAttribute("newA");
136: em.refresh(newA);
137: }
138:
139: @Test
140: public void findOfEntityWithExistingIdButDifferentTypeReturnsNull() {
141: this.em = getEntityManager("RetrieveDifferentType", false);
142: persist(entityA);
143:
144: final OWLClassB res = em.find(OWLClassB.class, entityA.getUri());
145: assertNull(res);
146: }
147:
148: @Test
149: public void testRefreshInstanceWithUnmappedProperties() {
150: this.em = getEntityManager("RefreshEntityWithProperties", false);
151: final Map<URI, Set<Object>> properties = Generators.createTypedProperties();
152: entityP.setProperties(properties);
153: persist(entityP);
154:
155: final OWLClassP p = em.find(OWLClassP.class, entityP.getUri());
156: assertNotNull(p);
157: p.getProperties().put(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#addedProperty"),
158: Collections.singleton("Test"));
159: assertNotEquals(properties, p.getProperties());
160: em.refresh(p);
161: assertEquals(properties, p.getProperties());
162: }
163:
164: @Test
165: public void plainIdentifierAttributeIsAlwaysLoadedEagerly() throws Exception {
166: this.em = getEntityManager("PlainIdentifiersAreLoadedEagerly", false);
167: entityP.setIndividualUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#plainIdentifier"));
168: entityP.setIndividuals(Collections.singleton(new URL("http://krizik.felk.cvut.cz/ontologies/jopa#url")));
169: persist(entityP);
170:
171: final OWLClassP res = em.find(OWLClassP.class, entityP.getUri());
172: final Field singularField = OWLClassP.class.getDeclaredField("individualUri");
173: singularField.setAccessible(true);
174: assertNotNull(singularField.get(res));
175: final Field pluralField = OWLClassP.class.getDeclaredField("individuals");
176: pluralField.setAccessible(true);
177: assertNotNull(pluralField.get(res));
178: }
179:
180: @Test
181: public void readingIndividualWithStringIdTwiceInPersistenceContextReturnsSameInstance() {
182: this.em = getEntityManager("readingIndividualWithStringIdTwiceInPersistenceContextReturnsSameInstance", true);
183: persist(entityN);
184:
185: final OWLClassN resultOne = em.find(OWLClassN.class, entityN.getId());
186: final OWLClassN resultTwo = em.find(OWLClassN.class, entityN.getId());
187: assertNotNull(resultOne);
188: assertNotNull(resultTwo);
189: assertSame(resultOne, resultTwo);
190: }
191:
192: @Test
193: public void retrieveLoadsUnmappedPropertiesTogetherWithObjectPropertyValues() {
194: this.em = getEntityManager("retrieveLoadsUnmappedPropertiesTogetherWithObjectPropertyValues", false);
195: final OWLClassV v = new OWLClassV();
196: v.setProperties(Generators.createProperties());
197: v.setThings(new HashSet<>());
198:• for (int i = 0; i < Generators.randomPositiveInt(5, 10); i++) {
199: final Thing thing = new Thing();
200: thing.setName("thing" + i);
201: thing.setDescription("description of a thing. Number " + i);
202: thing.setTypes(Collections.singleton(Vocabulary.C_OWL_CLASS_A));
203: v.getThings().add(thing);
204: }
205: em.getTransaction().begin();
206: em.persist(v);
207: v.getThings().forEach(em::persist);
208: em.getTransaction().commit();
209: em.clear();
210:
211: final OWLClassV result = em.find(OWLClassV.class, v.getUri());
212: assertNotNull(result);
213: assertEquals(v.getProperties(), result.getProperties());
214: final Set<String> expectedUris = v.getThings().stream().map(Thing::getUri).collect(Collectors.toSet());
215: assertEquals(v.getThings().size(), result.getThings().size());
216: result.getThings().forEach(t -> assertTrue(expectedUris.contains(t.getUri())));
217: }
218:
219: @Test
220: public void retrieveGetsStringAttributeWithCorrectLanguageWhenItIsSpecifiedInDescriptor() throws Exception {
221: this.em = getEntityManager("retrieveGetsStringAttributeWithCorrectLanguageWhenItIsSpecifiedInDescriptor",
222: false);
223: persist(entityA);
224: final String value = "v cestine";
225: final String lang = "cs";
226: persistTestData(Collections
227: .singleton(new Triple(entityA.getUri(), URI.create(Vocabulary.P_A_STRING_ATTRIBUTE), value, lang)), em);
228:
229: final Descriptor descriptor = new EntityDescriptor();
230: descriptor.setLanguage(lang);
231:
232: final OWLClassA result = em.find(OWLClassA.class, entityA.getUri(), descriptor);
233: assertNotNull(result);
234: assertEquals(value, result.getStringAttribute());
235: assertEquals(entityA.getTypes(), result.getTypes());
236: }
237:
238: @Test
239: public void retrieveGetsStringAttributesWithDifferentLanguageTagsSpecifiedInDescriptor() throws Exception {
240: this.em = getEntityManager("retrieveGetsStringAttributesWithDifferentLanguageTagsSpecifiedInDescriptor", false);
241: entityN.setAnnotationProperty("english annotation");
242: entityN.setStringAttribute("english string");
243: persist(entityN);
244: final String csAnnotation = "anotace cesky";
245: final String csString = "retezec cesky";
246: final Set<Triple> testData = new HashSet<>();
247: testData.add(new Triple(URI.create(entityN.getId()), URI.create(Vocabulary.P_N_STR_ANNOTATION_PROPERTY),
248: csAnnotation, "cs"));
249: testData.add(
250: new Triple(URI.create(entityN.getId()), URI.create(Vocabulary.P_N_STRING_ATTRIBUTE), csString, "cs"));
251: persistTestData(testData, em);
252:
253: final Descriptor descriptor = new EntityDescriptor();
254: descriptor.setAttributeLanguage(OWLClassN.class.getDeclaredField("annotationProperty"), "en");
255: descriptor.setAttributeLanguage(OWLClassN.class.getDeclaredField("stringAttribute"), "cs");
256: final OWLClassN result = em.find(OWLClassN.class, entityN.getId(), descriptor);
257: assertEquals(entityN.getAnnotationProperty(), result.getAnnotationProperty());
258: assertEquals(csString, result.getStringAttribute());
259: }
260:
261: @Test
262: public void retrieveAllowsToOverridePULevelLanguageSpecification() throws Exception {
263: this.em = getEntityManager("retrieveAllowsToOverridePULevelLanguageSpecification", false);
264: entityA.setStringAttribute(null);
265: // PU-level language is en
266: persist(entityA);
267: final String value = "cestina";
268: persistTestData(Collections
269: .singleton(new Triple(entityA.getUri(), URI.create(Vocabulary.P_A_STRING_ATTRIBUTE), value, "cs")), em);
270:
271: final OWLClassA resOne = em.find(OWLClassA.class, entityA.getUri());
272: assertNull(resOne.getStringAttribute());
273: em.clear();
274:
275: final Descriptor descriptor = new EntityDescriptor();
276: descriptor.setLanguage("cs");
277: final OWLClassA resTwo = em.find(OWLClassA.class, entityA.getUri(), descriptor);
278: assertEquals(value, resTwo.getStringAttribute());
279: }
280:
281: @Test
282: public void retrieveLoadsStringLiteralWithCorrectLanguageTagWhenCachedValueHasDifferentLanguageTag()
283: throws Exception {
284: this.em = getEntityManager(
285: "retrieveLoadsStringLiteralWithCorrectLanguageTagWhenCachedValueHasDifferentLanguageTag", true);
286: persist(entityA); // persisted with @en
287: final String value = "cestina";
288: persistTestData(Collections
289: .singleton(new Triple(entityA.getUri(), URI.create(Vocabulary.P_A_STRING_ATTRIBUTE), value, "cs")), em);
290:
291: final OWLClassA resOne = em.find(OWLClassA.class, entityA.getUri());
292: assertEquals(entityA.getStringAttribute(), resOne.getStringAttribute());
293: em.clear();
294:
295: final Descriptor descriptor = new EntityDescriptor();
296: descriptor.setLanguage("cs");
297: final OWLClassA resTwo = em.find(OWLClassA.class, entityA.getUri(), descriptor);
298: assertEquals(value, resTwo.getStringAttribute());
299: }
300: }