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%
testRefresh()
M: 0 C: 71
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 13
100%
M: 0 C: 1
100%
testRefreshInstanceWithUnmappedProperties()
M: 0 C: 57
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 13
100%
M: 0 C: 1
100%
testRefreshNotManaged()
M: 5 C: 41
89%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 2 C: 8
80%
M: 0 C: 1
100%
testRetrieveDifferentType()
M: 0 C: 31
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
testRetrieveGenerated()
M: 0 C: 89
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 19
100%
M: 0 C: 1
100%
testRetrieveLazy()
M: 0 C: 66
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 15
100%
M: 0 C: 1
100%
testRetrieveNotExisting()
M: 0 C: 22
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
testRetrieveNull()
M: 7 C: 10
59%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 2 C: 2
50%
M: 0 C: 1
100%
testRetrieveSimple()
M: 0 C: 60
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 11
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 the terms of the GNU General Public
5: * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
6: * version.
7: * <p>
8: * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
9: * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
10: * details. You should have received a copy of the GNU General Public License along with this program. If not, see
11: * <http://www.gnu.org/licenses/>.
12: */
13: package cz.cvut.kbss.jopa.test.runner;
14:
15: import cz.cvut.kbss.jopa.test.*;
16: import cz.cvut.kbss.jopa.test.environment.Generators;
17: import org.junit.Test;
18: import org.slf4j.Logger;
19:
20: import java.lang.reflect.Field;
21: import java.net.URI;
22: import java.util.*;
23:
24: import static org.junit.Assert.*;
25:
26: public abstract class RetrieveOperationsRunner extends BaseRunner {
27:
28: public RetrieveOperationsRunner(Logger logger) {
29: super(logger);
30: }
31:
32: @Test
33: public void testRetrieveSimple() {
34: logger.debug("Test: retrieve a simple entity.");
35: this.em = getEntityManager("RetrieveSimple", false);
36: persist(entityA);
37:
38: em.getEntityManagerFactory().getCache().evictAll();
39: final OWLClassA res = em.find(OWLClassA.class, entityA.getUri());
40: assertNotNull(res);
41: assertEquals(entityA.getUri(), res.getUri());
42: assertEquals(entityA.getStringAttribute(), res.getStringAttribute());
43: assertTrue(entityA.getTypes().containsAll(res.getTypes()));
44: assertTrue(em.contains(res));
45: }
46:
47: @Test(expected = NullPointerException.class)
48: public void testRetrieveNull() {
49: logger.debug("Test: retrieve null.");
50: this.em = getEntityManager("RetrieveNull", false);
51: em.find(OWLClassA.class, null);
52: }
53:
54: @Test
55: public void testRetrieveLazy() throws Exception {
56: logger.debug("Test: retrieve entity with lazy loaded attribute.");
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 testRetrieveGenerated() throws Exception {
75: logger.debug("Test: persist and retrieve several entities with generated identifiers.");
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 testRetrieveNotExisting() {
100: logger.debug("Test: retrieve entity which does not exist in the specified context.");
101: this.em = getEntityManager("RetrieveNotExisting", false);
102: final OWLClassB res = em.find(OWLClassB.class, entityB.getUri());
103: assertNull(res);
104: }
105:
106: @Test
107: public void testRefresh() {
108: logger.debug("Test: refresh entity.");
109: this.em = getEntityManager("Refresh", false);
110: persist(entityD, entityA);
111:
112: final OWLClassA newA = new OWLClassA();
113: newA.setUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityA"));
114: newA.setStringAttribute("newA");
115: final OWLClassD d = em.find(OWLClassD.class, entityD.getUri());
116: final OWLClassA a = em.find(OWLClassA.class, entityA.getUri());
117: assertEquals(d.getOwlClassA(), a);
118: d.setOwlClassA(newA);
119: em.refresh(d);
120: assertEquals(a.getUri(), d.getOwlClassA().getUri());
121: }
122:
123: @Test(expected = IllegalArgumentException.class)
124: public void testRefreshNotManaged() {
125: logger.debug("Test: refresh entity which is not managed.");
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 testRetrieveDifferentType() {
139: logger.debug("Test: persist entity but try to retrieve it as a different type.");
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: logger.debug("Test: refresh entity with @Properties field.");
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: }