Skip to contentMethod: testPersistProperties()
1: /**
2: * Copyright (C) 2011 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.exceptions.OWLEntityExistsException;
18: import cz.cvut.kbss.jopa.exceptions.RollbackException;
19: import cz.cvut.kbss.jopa.test.*;
20: import cz.cvut.kbss.jopa.test.environment.Generators;
21: import cz.cvut.kbss.ontodriver.exception.PrimaryKeyNotSetException;
22: import org.junit.Test;
23:
24: import java.net.URI;
25: import java.util.Collections;
26: import java.util.HashMap;
27: import java.util.Map;
28: import java.util.Set;
29: import java.util.logging.Logger;
30:
31: import static org.junit.Assert.*;
32:
33: public abstract class CreateOperationsRunner extends BaseRunner {
34:
35: protected CreateOperationsRunner(Logger logger) {
36: super(logger);
37: }
38:
39: @Test
40: public void testPersistWithGenerated() {
41: logger.config("Test: persist into all contexts, also with generated id.");
42: this.em = getEntityManager("PersistWithGenerated", false);
43: persist(entityA, entityE);
44:
45: final OWLClassA resA1 = em.find(OWLClassA.class, entityA.getUri());
46: assertNotNull(resA1);
47: assertEquals(entityA.getStringAttribute(), resA1.getStringAttribute());
48: assertEquals(entityA.getTypes().size(), resA1.getTypes().size());
49: assertTrue(entityA.getTypes().containsAll(resA1.getTypes()));
50:
51: assertNotNull(entityE.getUri());
52: final OWLClassE resE = em.find(OWLClassE.class, entityE.getUri());
53: assertNotNull(resE);
54: assertEquals(entityE.getStringAttribute(), resE.getStringAttribute());
55: }
56:
57: @Test(expected = PrimaryKeyNotSetException.class)
58: public void testPersistWithoutId() {
59: logger.config("Test: persist without id. No ID generation specified.");
60: this.em = getEntityManager("PersistWithoutId", false);
61: final OWLClassB b = new OWLClassB();
62: b.setStringAttribute("someValue");
63: persist(b);
64: }
65:
66: @Test(expected = NullPointerException.class)
67: public void testPersistNull() {
68: logger.config("Test: persist null.");
69: this.em = getEntityManager("PersistNull", false);
70: em.getTransaction().begin();
71: em.persist(null);
72: }
73:
74: @Test
75: public void testPersistRollback() {
76: logger.config("Test: persist and then rollback the transaction.");
77: this.em = getEntityManager("PersistRollback", false);
78: em.getTransaction().begin();
79: em.persist(entityE);
80: assertTrue(em.contains(entityE));
81: em.getTransaction().rollback();
82:
83: assertFalse(em.contains(entityE));
84: assertNull(em.find(entityE.getClass(), entityE.getUri()));
85: }
86:
87: @Test(expected = RollbackException.class)
88: public void testPersistRollbackOnly() {
89: logger.config("Test: set transaction as rollback only and the try persisting an entity.");
90: this.em = getEntityManager("PersistRollbackOnly", false);
91: em.getTransaction().begin();
92: em.getTransaction().setRollbackOnly();
93: em.persist(entityE);
94: em.getTransaction().commit();
95: }
96:
97: @Test
98: public void testPersistCascade() {
99: logger.config("Test: persist with cascade over two relationships.");
100: this.em = getEntityManager("PersistWithCascade", false);
101: persist(entityG);
102:
103: final OWLClassA resA2 = em.find(OWLClassA.class, entityA.getUri());
104: assertNotNull(resA2);
105: final OWLClassH resH = em.find(OWLClassH.class, entityH.getUri());
106: assertNotNull(resH);
107: assertEquals(resH.getOwlClassA(), resA2);
108: final OWLClassG resG = em.find(OWLClassG.class, entityG.getUri());
109: assertNotNull(resG);
110: assertEquals(resG.getOwlClassH(), resH);
111: assertEquals(resG.getOwlClassH().getOwlClassA(), resA2);
112: }
113:
114: @Test(expected = OWLEntityExistsException.class)
115: public void testPersistTwiceInOne() {
116: logger.config("Test: persist twice into one context.");
117: this.em = getEntityManager("PersistTwice", false);
118: persist(entityB, entityB);
119: }
120:
121: @Test(expected = RollbackException.class)
122: public void testPersistWithoutCascade() {
123: logger.config("Test: try persisting relationship not marked as cascade.");
124: this.em = getEntityManager("PersistWithoutCascade", false);
125: persist(entityD);
126: }
127:
128: @Test(expected = OWLEntityExistsException.class)
129: public void testPersistDetached() {
130: logger.config("Test: persist detached entity. Should throw entity exists exception.");
131: this.em = getEntityManager("PersistDetached", false);
132: persist(entityA);
133:
134: final OWLClassA det = em.find(OWLClassA.class, entityA.getUri());
135: assertNotNull(det);
136: em.detach(det);
137: em.persist(det);
138: }
139:
140: @Test
141: public void testPersistSimpleList() {
142: logger.config("Test: persist entity with simple list.");
143: this.em = getEntityManager("PersistSimpleList", false);
144: entityC.setSimpleList(Generators.createSimpleList(10));
145: em.getTransaction().begin();
146: em.persist(entityC);
147: entityC.getSimpleList().forEach(em::persist);
148: em.getTransaction().commit();
149:
150: final OWLClassA a = em.find(OWLClassA.class, entityC.getSimpleList().get(1).getUri());
151: assertNotNull(a);
152: final OWLClassC c = em.find(OWLClassC.class, entityC.getUri());
153: assertNotNull(c);
154: assertNotNull(c.getSimpleList());
155: assertFalse(c.getSimpleList().isEmpty());
156: assertEquals(entityC.getSimpleList().size(), c.getSimpleList().size());
157: assertTrue(c.getSimpleList().contains(a));
158: }
159:
160: @Test(expected = RollbackException.class)
161: public void testPersistSimpleListNoCascade() {
162: logger.config("Test: persist entity with simple list, but don't persist the referenced entities.");
163: this.em = getEntityManager("PersistSimpleListNoCascade", false);
164: entityC.setSimpleList(Generators.createSimpleList(10));
165: persist(entityC);
166: }
167:
168: @Test
169: public void testPersistReferencedList() {
170: logger.config("Test: persist entity with referenced list.");
171: this.em = getEntityManager("PersistReferencedList", false);
172: entityC.setReferencedList(Generators.createReferencedList(5));
173: em.getTransaction().begin();
174: em.persist(entityC);
175: entityC.getReferencedList().forEach(em::persist);
176: assertTrue(em.contains(entityC));
177: assertTrue(em.contains(entityC.getReferencedList().get(0)));
178: em.getTransaction().commit();
179:
180: final OWLClassC c = em.find(OWLClassC.class, entityC.getUri());
181: assertNotNull(c);
182: assertNotNull(c.getReferencedList());
183: assertFalse(c.getReferencedList().isEmpty());
184: assertEquals(entityC.getReferencedList().size(), c.getReferencedList().size());
185: for (OWLClassA a : entityC.getReferencedList()) {
186: final OWLClassA resA = em.find(OWLClassA.class, a.getUri());
187: assertNotNull(resA);
188: assertEquals(a.getStringAttribute(), resA.getStringAttribute());
189: assertTrue(c.getReferencedList().contains(resA));
190: }
191: }
192:
193: @Test(expected = RollbackException.class)
194: public void testPersistReferencedListNoCascade() {
195: logger.config("Test: persist entity with referenced list. Don't persist the referenced entities.");
196: this.em = getEntityManager("PersistReferencedListNoCascade", false);
197: entityC.setReferencedList(Generators.createReferencedList(5));
198: persist(entityC);
199: }
200:
201: @Test
202: public void testPersistSimpleAndReferencedList() {
203: logger.config("Test: persist entity with both simple and referenced list.");
204: this.em = getEntityManager("PersistSimpleAndReferencedList", false);
205: entityC.setReferencedList(Generators.createReferencedList(5));
206: entityC.setSimpleList(Generators.createSimpleList(5));
207: em.getTransaction().begin();
208: em.persist(entityC);
209: entityC.getSimpleList().forEach(em::persist);
210: entityC.getReferencedList().forEach(em::persist);
211: em.getTransaction().commit();
212:
213: final OWLClassC c = em.find(OWLClassC.class, entityC.getUri());
214: assertNotNull(c);
215: assertNotNull(c.getSimpleList());
216: assertEquals(entityC.getSimpleList().size(), c.getSimpleList().size());
217: assertNotNull(c.getReferencedList());
218: assertEquals(entityC.getReferencedList().size(), c.getReferencedList().size());
219: for (OWLClassA a : entityC.getSimpleList()) {
220: final OWLClassA resA = em.find(OWLClassA.class, a.getUri());
221: assertNotNull(resA);
222: assertTrue(c.getSimpleList().contains(resA));
223: }
224: for (OWLClassA a : entityC.getReferencedList()) {
225: final OWLClassA resA = em.find(OWLClassA.class, a.getUri());
226: assertNotNull(resA);
227: assertTrue(c.getReferencedList().contains(resA));
228: }
229: }
230:
231: @Test
232: public void testPersistProperties() {
233: logger.config("Test: persist entity with properties.");
234: this.em = getEntityManager("PersistWithProperties", false);
235: final Map<String, Set<String>> props = new HashMap<>(3);
236: props.put("http://krizik.felk.cvut.cz/ontologies/jopa/attributes#propertyOne", Collections
237: .singleton("http://krizik.felk.cvut.cz/ontologies/jopa/tests/Individual10"));
238: props.put("http://krizik.felk.cvut.cz/ontologies/jopa/attributes#propertyTwo", Collections
239: .singleton("http://krizik.felk.cvut.cz/ontologies/jopa/tests/SomeEntity"));
240: props.put("http://krizik.felk.cvut.cz/ontologies/jopa/attributes#propertyThree",
241: Collections.singleton("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityG"));
242: final Map<String, Set<String>> expected = new HashMap<>(4);
243: expected.putAll(props);
244: entityB.setProperties(props);
245: persist(entityB);
246: em.clear();
247:
248: final OWLClassB res = em.find(OWLClassB.class, entityB.getUri());
249: assertNotNull(res);
250: assertEquals(entityB.getStringAttribute(), res.getStringAttribute());
251: assertNotNull(res.getProperties());
252: assertFalse(res.getProperties().isEmpty());
253: assertEquals(expected.size(), res.getProperties().size());
254:• for (Map.Entry<String, Set<String>> e : expected.entrySet()) {
255: assertTrue(res.getProperties().containsKey(e.getKey()));
256: final Set<String> s = e.getValue();
257: final Set<String> resS = res.getProperties().get(e.getKey());
258: assertNotNull(resS);
259: assertEquals(1, resS.size());
260: assertEquals(s.iterator().next(), resS.iterator().next());
261: }
262: }
263:
264: @Test
265: public void testPersistPropertiesEmpty() {
266: logger.config("Test: persist entity with properties. The properties will be an empty map.");
267: this.em = getEntityManager("PersistWithPropertiesEmpty", false);
268: entityB.setProperties(Collections.emptyMap());
269: em.getTransaction().begin();
270: em.persist(entityB);
271: assertTrue(em.contains(entityB));
272: em.getTransaction().commit();
273: em.clear();
274:
275: final OWLClassB b = em.find(OWLClassB.class, entityB.getUri());
276: assertNotNull(b);
277: assertEquals(entityB.getUri(), b.getUri());
278: assertEquals(entityB.getStringAttribute(), b.getStringAttribute());
279: assertNull(b.getProperties());
280: }
281:
282: @Test(expected = OWLEntityExistsException.class)
283: public void persistURITwiceInDifferentClasses() {
284: logger.config("Test: persist two different entities (of different types) with the same URI.");
285: this.em = getEntityManager("PersistURITwiceInDifferentClasses", false);
286: final URI pk = URI.create("http://krizik.felk.cvut.cz/jopa/onto/sameEntity");
287: final OWLClassA a = new OWLClassA();
288: a.setUri(pk);
289: final OWLClassB b = new OWLClassB();
290: b.setUri(pk);
291: em.getTransaction().begin();
292: em.persist(a);
293: em.persist(b);
294: em.getTransaction().commit();
295: }
296:
297: @Test
298: public void testPersistEntityWithBasicTypeAttributes() {
299: logger.config("Test: persist entity with attributes of basic types (Integer, Boolean etc.).");
300: this.em = getEntityManager("PersistEntityWithBasicTypeAttributes", false);
301: persist(entityM);
302: em.clear();
303:
304: final OWLClassM res = em.find(OWLClassM.class, entityM.getKey());
305: assertNotNull(res);
306: assertEquals(entityM.getKey(), res.getKey());
307: assertEquals(entityM.getBooleanAttribute(), res.getBooleanAttribute());
308: assertEquals(entityM.getIntAttribute(), res.getIntAttribute());
309: assertEquals(entityM.getLongAttribute(), res.getLongAttribute());
310: assertEquals(entityM.getDoubleAttribute(), res.getDoubleAttribute());
311: assertEquals(entityM.getDateAttribute(), res.getDateAttribute());
312: }
313:
314: @Test
315: public void testPersistAndUpdateAttributeBeforeCommit() {
316: logger.config("Test: persist entity, set attribute value and then commit.");
317: this.em = getEntityManager("PersistAndUpdateBeforeCommit", false);
318: final String updatedValue = "updatedStringAttributeValue";
319: em.getTransaction().begin();
320: em.persist(entityA);
321: entityA.setStringAttribute(updatedValue);
322: em.getTransaction().commit();
323: em.clear();
324:
325: final OWLClassA res = em.find(OWLClassA.class, entityA.getUri());
326: assertNotNull(res);
327: assertEquals(updatedValue, res.getStringAttribute());
328: }
329:
330: @Test
331: public void testPersistEntityWithEnumAttribute() {
332: logger.config("Test: persist entity with enum attribute.");
333: this.em = getEntityManager("PersistEntityWithEnum", false);
334: persist(entityM);
335:
336: final OWLClassM res = em.find(OWLClassM.class, entityM.getKey());
337: assertNotNull(res);
338: assertEquals(entityM.getEnumAttribute(), res.getEnumAttribute());
339: }
340: }