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