Skip to contentMethod: testRetrieveGenerated()
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.test.*;
18: import org.junit.Test;
19:
20: import java.lang.reflect.Field;
21: import java.net.URI;
22: import java.util.ArrayList;
23: import java.util.List;
24: import java.util.logging.Logger;
25:
26: import static org.junit.Assert.*;
27:
28: public abstract class RetrieveOperationsRunner extends BaseRunner {
29:
30: public RetrieveOperationsRunner(Logger logger) {
31: super(logger);
32: }
33:
34: @Test
35: public void testRetrieveSimple() {
36: logger.config("Test: retrieve a simple entity.");
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 testRetrieveNull() {
51: logger.config("Test: retrieve null.");
52: this.em = getEntityManager("RetrieveNull", false);
53: em.find(OWLClassA.class, null);
54: }
55:
56: @Test
57: public void testRetrieveLazy() throws Exception {
58: logger.config("Test: retrieve entity with lazy loaded attribute.");
59: this.em = getEntityManager("RetrieveLazy", false);
60: persist(entityI);
61:
62: final OWLClassI resI = em.find(OWLClassI.class, entityI.getUri());
63: assertNotNull(resI);
64: final Field f = OWLClassI.class.getDeclaredField("owlClassA");
65: f.setAccessible(true);
66: Object value = f.get(resI);
67: assertNull(value);
68: assertNotNull(resI.getOwlClassA());
69: value = f.get(resI);
70: assertNotNull(value);
71: assertEquals(entityA.getUri(), resI.getOwlClassA().getUri());
72: assertTrue(em.contains(resI.getOwlClassA()));
73: }
74:
75: @Test
76: public void testRetrieveGenerated() throws Exception {
77: logger.config("Test: persist and retrieve several entities with generated identifiers.");
78: this.em = getEntityManager("RetrieveGenerated", false);
79: em.getTransaction().begin();
80: final int size = 10;
81: final List<OWLClassE> lst = new ArrayList<>(size);
82:• for (int i = 0; i < size; i++) {
83: final OWLClassE e = new OWLClassE();
84: e.setStringAttribute("blablabla" + i);
85: assertNull(e.getUri());
86: em.persist(e);
87: assertNotNull(e.getUri());
88: lst.add(e);
89: }
90: em.getTransaction().commit();
91:
92: em.clear();
93:• for (OWLClassE e : lst) {
94: final OWLClassE res = em.find(OWLClassE.class, e.getUri());
95: assertNotNull(res);
96: assertEquals(e.getStringAttribute(), res.getStringAttribute());
97: }
98: }
99:
100: @Test
101: public void testRetrieveNotExisting() {
102: logger.config("Test: retrieve entity which does not exist in the specified context.");
103: this.em = getEntityManager("RetrieveNotExisting", false);
104: final OWLClassB res = em.find(OWLClassB.class, entityB.getUri());
105: assertNull(res);
106: }
107:
108: @Test
109: public void testRefresh() {
110: logger.config("Test: refresh entity.");
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 testRefreshNotManaged() {
127: logger.config("Test: refresh entity which is not managed.");
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 testRetrieveDifferentType() {
141: logger.config("Test: persist entity but try to retrieve it as a different type.");
142: this.em = getEntityManager("RetrieveDifferentType", false);
143: persist(entityA);
144:
145: final OWLClassB res = em.find(OWLClassB.class, entityA.getUri());
146: assertNull(res);
147: }
148: }