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%
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.test.*;
18: import cz.cvut.kbss.jopa.test.environment.Generators;
19: import org.junit.Test;
20: import org.slf4j.Logger;
21:
22: import java.lang.reflect.Field;
23: import java.net.URI;
24: import java.net.URL;
25: import java.util.*;
26: import java.util.stream.Collectors;
27:
28: import static org.junit.Assert.*;
29:
30: public abstract class RetrieveOperationsRunner extends BaseRunner {
31:
32: public RetrieveOperationsRunner(Logger logger) {
33: super(logger);
34: }
35:
36: @Test
37: public void testRetrieveSimple() {
38: this.em = getEntityManager("RetrieveSimple", false);
39: persist(entityA);
40:
41: em.getEntityManagerFactory().getCache().evictAll();
42: final OWLClassA res = em.find(OWLClassA.class, entityA.getUri());
43: assertNotNull(res);
44: assertEquals(entityA.getUri(), res.getUri());
45: assertEquals(entityA.getStringAttribute(), res.getStringAttribute());
46: assertTrue(entityA.getTypes().containsAll(res.getTypes()));
47: assertTrue(em.contains(res));
48: }
49:
50: @Test(expected = NullPointerException.class)
51: public void findWithNullIdentifierThrowsNPX() {
52: this.em = getEntityManager("RetrieveNull", false);
53: em.find(OWLClassA.class, null);
54: }
55:
56: @Test
57: public void testRetrieveWithLazyAttribute() throws Exception {
58: this.em = getEntityManager("RetrieveLazy", false);
59: persist(entityI);
60:
61: final OWLClassI resI = em.find(OWLClassI.class, entityI.getUri());
62: assertNotNull(resI);
63: final Field f = OWLClassI.class.getDeclaredField("owlClassA");
64: f.setAccessible(true);
65: Object value = f.get(resI);
66: assertNull(value);
67: assertNotNull(resI.getOwlClassA());
68: value = f.get(resI);
69: assertNotNull(value);
70: assertEquals(entityA.getUri(), resI.getOwlClassA().getUri());
71: assertTrue(em.contains(resI.getOwlClassA()));
72: }
73:
74: @Test
75: public void testRetrieveWithGeneratedId() {
76: this.em = getEntityManager("RetrieveGenerated", false);
77: em.getTransaction().begin();
78: final int size = 10;
79: final List<OWLClassE> lst = new ArrayList<>(size);
80:• for (int i = 0; i < size; i++) {
81: final OWLClassE e = new OWLClassE();
82: e.setStringAttribute("blablabla" + i);
83: assertNull(e.getUri());
84: em.persist(e);
85: assertNotNull(e.getUri());
86: lst.add(e);
87: }
88: em.getTransaction().commit();
89:
90: em.clear();
91:• for (OWLClassE e : lst) {
92: final OWLClassE res = em.find(OWLClassE.class, e.getUri());
93: assertNotNull(res);
94: assertEquals(e.getStringAttribute(), res.getStringAttribute());
95: }
96: }
97:
98: @Test
99: public void findByUnknownIdReturnsNull() {
100: this.em = getEntityManager("RetrieveNotExisting", false);
101: final OWLClassB res = em.find(OWLClassB.class, entityB.getUri());
102: assertNull(res);
103: }
104:
105: @Test
106: public void testRefreshInstance() {
107: this.em = getEntityManager("Refresh", false);
108: persist(entityD, entityA);
109:
110: final OWLClassA newA = new OWLClassA();
111: newA.setUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityA"));
112: newA.setStringAttribute("newA");
113: final OWLClassD d = em.find(OWLClassD.class, entityD.getUri());
114: final OWLClassA a = em.find(OWLClassA.class, entityA.getUri());
115: assertEquals(d.getOwlClassA(), a);
116: d.setOwlClassA(newA);
117: em.refresh(d);
118: assertEquals(a.getUri(), d.getOwlClassA().getUri());
119: }
120:
121: @Test(expected = IllegalArgumentException.class)
122: public void refreshingNotManagedIsIllegal() {
123: this.em = getEntityManager("RefreshNotManaged", false);
124: persist(entityA);
125:
126: final OWLClassA a = em.find(OWLClassA.class, entityA.getUri());
127: assertNotNull(a);
128: final OWLClassA newA = new OWLClassA();
129: newA.setUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityA"));
130: newA.setStringAttribute("newA");
131: em.refresh(newA);
132: }
133:
134: @Test
135: public void findOfEntityWithExistingIdButDifferentTypeReturnsNull() {
136: this.em = getEntityManager("RetrieveDifferentType", false);
137: persist(entityA);
138:
139: final OWLClassB res = em.find(OWLClassB.class, entityA.getUri());
140: assertNull(res);
141: }
142:
143: @Test
144: public void testRefreshInstanceWithUnmappedProperties() {
145: this.em = getEntityManager("RefreshEntityWithProperties", false);
146: final Map<URI, Set<Object>> properties = Generators.createTypedProperties();
147: entityP.setProperties(properties);
148: persist(entityP);
149:
150: final OWLClassP p = em.find(OWLClassP.class, entityP.getUri());
151: assertNotNull(p);
152: p.getProperties().put(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#addedProperty"),
153: Collections.singleton("Test"));
154: assertNotEquals(properties, p.getProperties());
155: em.refresh(p);
156: assertEquals(properties, p.getProperties());
157: }
158:
159: @Test
160: public void plainIdentifierAttributeIsAlwaysLoadedEagerly() throws Exception {
161: this.em = getEntityManager("PlainIdentifiersAreLoadedEagerly", false);
162: entityP.setIndividualUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#plainIdentifier"));
163: entityP.setIndividuals(Collections.singleton(new URL("http://krizik.felk.cvut.cz/ontologies/jopa#url")));
164: persist(entityP);
165:
166: final OWLClassP res = em.find(OWLClassP.class, entityP.getUri());
167: final Field singularField = OWLClassP.class.getDeclaredField("individualUri");
168: singularField.setAccessible(true);
169: assertNotNull(singularField.get(res));
170: final Field pluralField = OWLClassP.class.getDeclaredField("individuals");
171: pluralField.setAccessible(true);
172: assertNotNull(pluralField.get(res));
173: }
174:
175: @Test
176: public void readingIndividualWithStringIdTwiceInPersistenceContextReturnsSameInstance() {
177: this.em = getEntityManager("readingIndividualWithStringIdTwiceInPersistenceContextReturnsSameInstance", true);
178: persist(entityN);
179:
180: final OWLClassN resultOne = em.find(OWLClassN.class, entityN.getId());
181: final OWLClassN resultTwo = em.find(OWLClassN.class, entityN.getId());
182: assertNotNull(resultOne);
183: assertNotNull(resultTwo);
184: assertSame(resultOne, resultTwo);
185: }
186:
187: @Test
188: public void retrieveLoadsUnmappedPropertiesTogetherWithObjectPropertyValues() {
189: this.em = getEntityManager("retrieveLoadsUnmappedPropertiesTogetherWithObjectPropertyValues", false);
190: final OWLClassV v = new OWLClassV();
191: v.setProperties(Generators.createProperties());
192: v.setThings(new HashSet<>());
193:• for (int i = 0; i < Generators.randomPositiveInt(5, 10); i++) {
194: final Thing thing = new Thing();
195: thing.setName("thing" + i);
196: thing.setDescription("description of a thing. Number " + i);
197: thing.setTypes(Collections.singleton(Vocabulary.C_OWL_CLASS_A));
198: v.getThings().add(thing);
199: }
200: em.getTransaction().begin();
201: em.persist(v);
202: v.getThings().forEach(em::persist);
203: em.getTransaction().commit();
204: em.clear();
205:
206: final OWLClassV result = em.find(OWLClassV.class, v.getUri());
207: assertNotNull(result);
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: }