Skip to content

Package: RetrieveOperationsRunner

RetrieveOperationsRunner

nameinstructionbranchcomplexitylinemethod
RetrieveOperationsRunner(Logger)
M: 0 C: 4
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%
findWithNullIdentifierThrowsNPX()
M: 7 C: 6
46%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 2 C: 1
33%
M: 0 C: 1
100%
lambda$0(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%
plainIdentifierAttributeIsAlwaysLoadedEagerly()
M: 0 C: 60
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: 41
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 8
100%
M: 0 C: 1
100%
refreshingNotManagedIsIllegal()
M: 5 C: 37
88%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 2 C: 7
78%
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: 64
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: 101
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 19
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: 118
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 22
100%
M: 0 C: 1
100%
testRefreshInstance()
M: 0 C: 67
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: 53
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 12
100%
M: 0 C: 1
100%
testRetrieveSimple()
M: 0 C: 56
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 10
100%
M: 0 C: 1
100%
testRetrieveWithGeneratedId()
M: 0 C: 85
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 18
100%
M: 0 C: 1
100%
testRetrieveWithLazyAttribute()
M: 0 C: 62
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 14
100%
M: 0 C: 1
100%

Coverage

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