Skip to content

Package: CreateOperationsRunner

CreateOperationsRunner

nameinstructionbranchcomplexitylinemethod
CreateOperationsRunner(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%
lambda$0(int)
M: 0 C: 15
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
lambda$1(OWLClassE)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
lambda$2(OWLClassE)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
lambda$3(int)
M: 0 C: 15
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
lambda$4(OWLClassE)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
lambda$5(OWLClassE)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
persistAllowsOverridingGlobalLanguageWithLocalEmptyTag()
M: 0 C: 65
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 13
100%
M: 0 C: 1
100%
persistAllowsToSpecifyLanguageTagPerEntityAndOverrideItOnAttributeLevel()
M: 0 C: 106
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 19
100%
M: 0 C: 1
100%
persistEntityAndReferenceWithNonNullGeneratedIdentifiersDoesNotRewriteThem()
M: 0 C: 81
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 20
100%
M: 0 C: 1
100%
persistEntityWithDatatypePropertyCollectionPersistsAllValues()
M: 0 C: 46
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 10
100%
M: 0 C: 1
100%
persistEntityWithNonNullGeneratedIdentifierDoesNotRewriteIdentifier()
M: 0 C: 51
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 12
100%
M: 0 C: 1
100%
persistNullThrowsNPX()
M: 5 C: 10
67%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 2 C: 2
50%
M: 0 C: 1
100%
persistSavesInstanceWithReferenceToExistingInstance()
M: 0 C: 46
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 10
100%
M: 0 C: 1
100%
persistSetsStringLiteralLanguageTagAccordingToDescriptor()
M: 0 C: 56
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 11
100%
M: 0 C: 1
100%
persistSetsStringLiteralLanguageTagToGloballyConfiguredValueWhenDescriptorDoesNotSpecifyIt()
M: 0 C: 45
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 10
100%
M: 0 C: 1
100%
persistTwoInstancesOfDifferentClassesWithSameUriIntoDifferentPersistenceContextsIsLegal()
M: 0 C: 64
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 16
100%
M: 0 C: 1
100%
persistTwoInstancesOfDifferentClassesWithSameUriIntoTheSamePersistenceContextIsIllegal()
M: 9 C: 31
78%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 3 C: 8
73%
M: 0 C: 1
100%
persistWithReferencedListSavesListReferenceWhenAllItemsArePersisted()
M: 0 C: 63
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 14
100%
M: 0 C: 1
100%
persistWithSimpleListSavesListReferenceWhenAllItemsArePersisted()
M: 0 C: 63
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 14
100%
M: 0 C: 1
100%
persistingDetachedEntityIsIllegal()
M: 9 C: 34
79%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 3 C: 6
67%
M: 0 C: 1
100%
persistingEntityWithReferencedListWithoutCascadeIsIllegal()
M: 10 C: 11
52%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 2 C: 2
50%
M: 0 C: 1
100%
persistingEntityWithSimpleListWithoutCascadeIsIllegal()
M: 10 C: 11
52%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 2 C: 2
50%
M: 0 C: 1
100%
persistingEntityWithoutIdAndWithoutGeneratedIdThrowsException()
M: 9 C: 13
59%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 2 C: 3
60%
M: 0 C: 1
100%
persistingInRollbackOnlyThrowsExceptionOnCommit()
M: 5 C: 19
79%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 2 C: 4
67%
M: 0 C: 1
100%
persistingOnlyOnePartOfRelationWithoutCascadeThrowsRollbackException()
M: 10 C: 6
38%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 2 C: 1
33%
M: 0 C: 1
100%
testPersistAndRollbackChanges()
M: 0 C: 42
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 8
100%
M: 0 C: 1
100%
testPersistAndUpdateAttributeBeforeCommit()
M: 0 C: 44
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 11
100%
M: 0 C: 1
100%
testPersistEntityWithBasicTypeAttributes()
M: 0 C: 66
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 12
100%
M: 0 C: 1
100%
testPersistEntityWithEnumAttribute()
M: 0 C: 33
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
testPersistEntityWithUriTypes()
M: 0 C: 53
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 10
100%
M: 0 C: 1
100%
testPersistInstanceWithAnnotationProperties()
M: 0 C: 57
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 14
100%
M: 0 C: 1
100%
testPersistInstanceWithPlainIdentifierObjectPropertyValue()
M: 0 C: 45
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 11
100%
M: 0 C: 1
100%
testPersistInstanceWithPluralObjectPropertyAttributeRepresentedByUrls()
M: 0 C: 44
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 11
100%
M: 0 C: 1
100%
testPersistInstanceWithReferencedListOfIdentifiers()
M: 0 C: 44
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 10
100%
M: 0 C: 1
100%
testPersistInstanceWithSimpleListOfIdentifiers()
M: 0 C: 44
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 10
100%
M: 0 C: 1
100%
testPersistSimpleAndReferencedList()
M: 0 C: 137
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 23
100%
M: 0 C: 1
100%
testPersistTypedProperties()
M: 0 C: 44
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 10
100%
M: 0 C: 1
100%
testPersistWithCascade()
M: 0 C: 62
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 12
100%
M: 0 C: 1
100%
testPersistWithEmptyProperties()
M: 0 C: 59
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 13
100%
M: 0 C: 1
100%
testPersistWithGeneratedId()
M: 0 C: 76
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 12
100%
M: 0 C: 1
100%
testPersistWithProperties()
M: 0 C: 129
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 27
100%
M: 0 C: 1
100%
testPersistWithReferencedList()
M: 0 C: 108
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 19
100%
M: 0 C: 1
100%
testPersistWithSimpleList()
M: 0 C: 80
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 15
100%
M: 0 C: 1
100%
verifyLists(List, List)
M: 0 C: 38
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 5
100%
M: 0 C: 1
100%

Coverage

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.exceptions.OWLEntityExistsException;
18: import cz.cvut.kbss.jopa.exceptions.RollbackException;
19: import cz.cvut.kbss.jopa.model.EntityManager;
20: import cz.cvut.kbss.jopa.model.descriptors.Descriptor;
21: import cz.cvut.kbss.jopa.model.descriptors.EntityDescriptor;
22: import cz.cvut.kbss.jopa.test.*;
23: import cz.cvut.kbss.jopa.test.environment.Generators;
24: import cz.cvut.kbss.jopa.test.environment.Triple;
25: import cz.cvut.kbss.ontodriver.exception.PrimaryKeyNotSetException;
26: import org.junit.Test;
27: import org.slf4j.Logger;
28:
29: import java.net.URI;
30: import java.net.URL;
31: import java.util.*;
32: import java.util.stream.Collectors;
33: import java.util.stream.IntStream;
34:
35: import static org.junit.Assert.*;
36:
37: public abstract class CreateOperationsRunner extends BaseRunner {
38:
39: protected CreateOperationsRunner(Logger logger) {
40: super(logger);
41: }
42:
43: @Test
44: public void testPersistWithGeneratedId() {
45: this.em = getEntityManager("PersistWithGenerated", false);
46: persist(entityA, entityE);
47:
48: final OWLClassA resA1 = em.find(OWLClassA.class, entityA.getUri());
49: assertNotNull(resA1);
50: assertEquals(entityA.getStringAttribute(), resA1.getStringAttribute());
51: assertEquals(entityA.getTypes().size(), resA1.getTypes().size());
52: assertTrue(entityA.getTypes().containsAll(resA1.getTypes()));
53:
54: assertNotNull(entityE.getUri());
55: final OWLClassE resE = em.find(OWLClassE.class, entityE.getUri());
56: assertNotNull(resE);
57: assertEquals(entityE.getStringAttribute(), resE.getStringAttribute());
58: }
59:
60: @Test(expected = PrimaryKeyNotSetException.class)
61: public void persistingEntityWithoutIdAndWithoutGeneratedIdThrowsException() {
62: this.em = getEntityManager("PersistWithoutId", false);
63: final OWLClassB b = new OWLClassB();
64: b.setStringAttribute("someValue");
65: persist(b);
66: }
67:
68: @Test(expected = NullPointerException.class)
69: public void persistNullThrowsNPX() {
70: this.em = getEntityManager("PersistNull", false);
71: em.getTransaction().begin();
72: em.persist(null);
73: }
74:
75: @Test
76: public void testPersistAndRollbackChanges() {
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 persistingInRollbackOnlyThrowsExceptionOnCommit() {
89: this.em = getEntityManager("PersistRollbackOnly", false);
90: em.getTransaction().begin();
91: em.getTransaction().setRollbackOnly();
92: em.persist(entityE);
93: em.getTransaction().commit();
94: }
95:
96: @Test
97: public void testPersistWithCascade() {
98: this.em = getEntityManager("PersistWithCascade", false);
99: persist(entityG);
100:
101: final OWLClassA resA2 = em.find(OWLClassA.class, entityA.getUri());
102: assertNotNull(resA2);
103: final OWLClassH resH = em.find(OWLClassH.class, entityH.getUri());
104: assertNotNull(resH);
105: assertEquals(resH.getOwlClassA(), resA2);
106: final OWLClassG resG = em.find(OWLClassG.class, entityG.getUri());
107: assertNotNull(resG);
108: assertEquals(resG.getOwlClassH(), resH);
109: assertEquals(resG.getOwlClassH().getOwlClassA(), resA2);
110: }
111:
112: @Test(expected = RollbackException.class)
113: public void persistingOnlyOnePartOfRelationWithoutCascadeThrowsRollbackException() {
114: this.em = getEntityManager("PersistWithoutCascade", false);
115: persist(entityD);
116: }
117:
118: @Test(expected = OWLEntityExistsException.class)
119: public void persistingDetachedEntityIsIllegal() {
120: this.em = getEntityManager("PersistDetached", false);
121: persist(entityA);
122:
123: final OWLClassA det = em.find(OWLClassA.class, entityA.getUri());
124: assertNotNull(det);
125: em.getTransaction().begin();
126: em.detach(det);
127: em.persist(det);
128: em.getTransaction().commit();
129: }
130:
131: @Test
132: public void testPersistWithSimpleList() {
133: this.em = getEntityManager("PersistSimpleList", false);
134: entityC.setSimpleList(Generators.createSimpleList(10));
135: em.getTransaction().begin();
136: em.persist(entityC);
137: entityC.getSimpleList().forEach(em::persist);
138: em.getTransaction().commit();
139:
140: final OWLClassA a = em.find(OWLClassA.class, entityC.getSimpleList().get(1).getUri());
141: assertNotNull(a);
142: final OWLClassC c = em.find(OWLClassC.class, entityC.getUri());
143: assertNotNull(c);
144: assertNotNull(c.getSimpleList());
145: assertFalse(c.getSimpleList().isEmpty());
146: assertEquals(entityC.getSimpleList().size(), c.getSimpleList().size());
147: assertTrue(c.getSimpleList().contains(a));
148: }
149:
150: @Test(expected = RollbackException.class)
151: public void persistingEntityWithSimpleListWithoutCascadeIsIllegal() {
152: this.em = getEntityManager("PersistSimpleListNoCascade", false);
153: entityC.setSimpleList(Generators.createSimpleList(10));
154: persist(entityC);
155: }
156:
157: @Test
158: public void persistWithSimpleListSavesListReferenceWhenAllItemsArePersisted() {
159: this.em = getEntityManager("persistWithSimpleListSavesListReferenceWhenAllItemsArePersisted", false);
160: final OWLClassK entityK = new OWLClassK();
161: entityK.setSimpleList(IntStream.range(0, 5).mapToObj(i -> {
162: final OWLClassE item = new OWLClassE();
163: item.setStringAttribute("item" + i);
164: return item;
165: }).collect(Collectors.toList()));
166: em.getTransaction().begin();
167: em.persist(entityK);
168: entityK.getSimpleList().forEach(item -> assertNull(item.getUri()));
169: entityK.getSimpleList().forEach(em::persist);
170: entityK.getSimpleList().forEach(item -> assertNotNull(item.getUri()));
171: em.getTransaction().commit();
172:
173: final OWLClassK result = em.find(OWLClassK.class, entityK.getUri());
174: assertNotNull(result);
175: verifyLists(entityK.getSimpleList(), result.getSimpleList());
176: }
177:
178: private void verifyLists(List<OWLClassE> expected, List<OWLClassE> actual) {
179: assertEquals(expected.size(), actual.size());
180:• for (int i = 0; i < expected.size(); i++) {
181: assertEquals(expected.get(i).getUri(), actual.get(i).getUri());
182: assertEquals(expected.get(i).getStringAttribute(), actual.get(i).getStringAttribute());
183: }
184: }
185:
186: @Test
187: public void testPersistWithReferencedList() {
188: this.em = getEntityManager("PersistReferencedList", false);
189: entityC.setReferencedList(Generators.createReferencedList(5));
190: em.getTransaction().begin();
191: em.persist(entityC);
192: entityC.getReferencedList().forEach(em::persist);
193: assertTrue(em.contains(entityC));
194: assertTrue(em.contains(entityC.getReferencedList().get(0)));
195: em.getTransaction().commit();
196:
197: final OWLClassC c = em.find(OWLClassC.class, entityC.getUri());
198: assertNotNull(c);
199: assertNotNull(c.getReferencedList());
200: assertFalse(c.getReferencedList().isEmpty());
201: assertEquals(entityC.getReferencedList().size(), c.getReferencedList().size());
202:• for (OWLClassA a : entityC.getReferencedList()) {
203: final OWLClassA resA = em.find(OWLClassA.class, a.getUri());
204: assertNotNull(resA);
205: assertEquals(a.getStringAttribute(), resA.getStringAttribute());
206: assertTrue(c.getReferencedList().contains(resA));
207: }
208: }
209:
210:
211: @Test(expected = RollbackException.class)
212: public void persistingEntityWithReferencedListWithoutCascadeIsIllegal() {
213: this.em = getEntityManager("PersistReferencedListNoCascade", false);
214: entityC.setReferencedList(Generators.createReferencedList(5));
215: persist(entityC);
216: }
217:
218: @Test
219: public void persistWithReferencedListSavesListReferenceWhenAllItemsArePersisted() {
220: this.em = getEntityManager("persistWithReferencedListSavesListReferenceWhenAllItemsArePersisted", false);
221: final OWLClassK entityK = new OWLClassK();
222: entityK.setReferencedList(IntStream.range(0, 5).mapToObj(i -> {
223: final OWLClassE item = new OWLClassE();
224: item.setStringAttribute("item" + i);
225: return item;
226: }).collect(Collectors.toList()));
227: em.getTransaction().begin();
228: em.persist(entityK);
229: entityK.getReferencedList().forEach(item -> assertNull(item.getUri()));
230: entityK.getReferencedList().forEach(em::persist);
231: entityK.getReferencedList().forEach(item -> assertNotNull(item.getUri()));
232: em.getTransaction().commit();
233:
234: final OWLClassK result = em.find(OWLClassK.class, entityK.getUri());
235: assertNotNull(result);
236: verifyLists(entityK.getReferencedList(), result.getReferencedList());
237: }
238:
239: @Test
240: public void testPersistSimpleAndReferencedList() {
241: this.em = getEntityManager("PersistSimpleAndReferencedList", false);
242: entityC.setReferencedList(Generators.createReferencedList(5));
243: entityC.setSimpleList(Generators.createSimpleList(5));
244: em.getTransaction().begin();
245: em.persist(entityC);
246: entityC.getSimpleList().forEach(em::persist);
247: entityC.getReferencedList().forEach(em::persist);
248: em.getTransaction().commit();
249:
250: final OWLClassC c = em.find(OWLClassC.class, entityC.getUri());
251: assertNotNull(c);
252: assertNotNull(c.getSimpleList());
253: assertEquals(entityC.getSimpleList().size(), c.getSimpleList().size());
254: assertNotNull(c.getReferencedList());
255: assertEquals(entityC.getReferencedList().size(), c.getReferencedList().size());
256:• for (OWLClassA a : entityC.getSimpleList()) {
257: final OWLClassA resA = em.find(OWLClassA.class, a.getUri());
258: assertNotNull(resA);
259: assertTrue(c.getSimpleList().contains(resA));
260: }
261:• for (OWLClassA a : entityC.getReferencedList()) {
262: final OWLClassA resA = em.find(OWLClassA.class, a.getUri());
263: assertNotNull(resA);
264: assertTrue(c.getReferencedList().contains(resA));
265: }
266: }
267:
268: @Test
269: public void testPersistWithProperties() {
270: this.em = getEntityManager("PersistWithProperties", false);
271: final Map<String, Set<String>> props = new HashMap<>(3);
272: props.put("http://krizik.felk.cvut.cz/ontologies/jopa/attributes#propertyOne", Collections
273: .singleton("http://krizik.felk.cvut.cz/ontologies/jopa/tests/Individual10"));
274: props.put("http://krizik.felk.cvut.cz/ontologies/jopa/attributes#propertyTwo", Collections
275: .singleton("http://krizik.felk.cvut.cz/ontologies/jopa/tests/SomeEntity"));
276: props.put("http://krizik.felk.cvut.cz/ontologies/jopa/attributes#propertyThree",
277: Collections.singleton("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityG"));
278: final Map<String, Set<String>> expected = new HashMap<>(4);
279: expected.putAll(props);
280: entityB.setProperties(props);
281: persist(entityB);
282: em.clear();
283:
284: final OWLClassB res = em.find(OWLClassB.class, entityB.getUri());
285: assertNotNull(res);
286: assertEquals(entityB.getStringAttribute(), res.getStringAttribute());
287: assertNotNull(res.getProperties());
288: assertFalse(res.getProperties().isEmpty());
289: assertEquals(expected.size(), res.getProperties().size());
290:• for (Map.Entry<String, Set<String>> e : expected.entrySet()) {
291: assertTrue(res.getProperties().containsKey(e.getKey()));
292: final Set<String> s = e.getValue();
293: final Set<String> resS = res.getProperties().get(e.getKey());
294: assertNotNull(resS);
295: assertEquals(1, resS.size());
296: assertEquals(s.iterator().next(), resS.iterator().next());
297: }
298: }
299:
300: @Test
301: public void testPersistWithEmptyProperties() {
302: this.em = getEntityManager("PersistWithPropertiesEmpty", false);
303: entityB.setProperties(Collections.emptyMap());
304: em.getTransaction().begin();
305: em.persist(entityB);
306: assertTrue(em.contains(entityB));
307: em.getTransaction().commit();
308: em.clear();
309:
310: final OWLClassB b = em.find(OWLClassB.class, entityB.getUri());
311: assertNotNull(b);
312: assertEquals(entityB.getUri(), b.getUri());
313: assertEquals(entityB.getStringAttribute(), b.getStringAttribute());
314: assertNull(b.getProperties());
315: }
316:
317: @Test(expected = OWLEntityExistsException.class)
318: public void persistTwoInstancesOfDifferentClassesWithSameUriIntoTheSamePersistenceContextIsIllegal() {
319: this.em = getEntityManager("PersistURITwiceInDifferentClassesSamePC", false);
320: final URI pk = URI.create("http://krizik.felk.cvut.cz/jopa/onto/sameEntity");
321: final OWLClassA a = new OWLClassA();
322: a.setUri(pk);
323: final OWLClassB b = new OWLClassB();
324: b.setUri(pk);
325: em.getTransaction().begin();
326: em.persist(a);
327: em.persist(b);
328: em.getTransaction().commit();
329: }
330:
331: @Test
332: public void persistTwoInstancesOfDifferentClassesWithSameUriIntoDifferentPersistenceContextsIsLegal() {
333: this.em = getEntityManager("PersistURITwiceInDifferentClassesDifferentPCs", false);
334: final URI uri = URI.create("http://krizik.felk.cvut.cz/jopa/onto/sameEntity");
335: entityA.setUri(uri);
336: entityB.setUri(uri);
337: em.getTransaction().begin();
338: em.persist(entityA);
339: em.getTransaction().commit();
340: final EntityManager emTwo = em.getEntityManagerFactory().createEntityManager();
341: try {
342: emTwo.getTransaction().begin();
343: emTwo.persist(entityB);
344: emTwo.getTransaction().commit();
345:
346: assertNotNull(emTwo.find(OWLClassA.class, entityA.getUri()));
347: assertNotNull(em.find(OWLClassB.class, entityB.getUri()));
348: } finally {
349: emTwo.close();
350: }
351: }
352:
353: @Test
354: public void testPersistEntityWithBasicTypeAttributes() {
355: this.em = getEntityManager("PersistEntityWithBasicTypeAttributes", false);
356: persist(entityM);
357: em.clear();
358:
359: final OWLClassM res = em.find(OWLClassM.class, entityM.getKey());
360: assertNotNull(res);
361: assertEquals(entityM.getKey(), res.getKey());
362: assertEquals(entityM.getBooleanAttribute(), res.getBooleanAttribute());
363: assertEquals(entityM.getIntAttribute(), res.getIntAttribute());
364: assertEquals(entityM.getLongAttribute(), res.getLongAttribute());
365: assertEquals(entityM.getDoubleAttribute(), res.getDoubleAttribute());
366: assertEquals(entityM.getDateAttribute(), res.getDateAttribute());
367: }
368:
369: @Test
370: public void testPersistAndUpdateAttributeBeforeCommit() {
371: this.em = getEntityManager("PersistAndUpdateBeforeCommit", false);
372: final String updatedValue = "updatedStringAttributeValue";
373: em.getTransaction().begin();
374: em.persist(entityA);
375: entityA.setStringAttribute(updatedValue);
376: em.getTransaction().commit();
377: em.clear();
378:
379: final OWLClassA res = em.find(OWLClassA.class, entityA.getUri());
380: assertNotNull(res);
381: assertEquals(updatedValue, res.getStringAttribute());
382: }
383:
384: @Test
385: public void testPersistEntityWithEnumAttribute() {
386: this.em = getEntityManager("PersistEntityWithEnum", false);
387: persist(entityM);
388:
389: final OWLClassM res = em.find(OWLClassM.class, entityM.getKey());
390: assertNotNull(res);
391: assertEquals(entityM.getEnumAttribute(), res.getEnumAttribute());
392: }
393:
394: @Test
395: public void testPersistTypedProperties() {
396: this.em = getEntityManager("PersistTypedProperties", false);
397: entityP.setProperties(Generators.createTypedProperties());
398: em.getTransaction().begin();
399: em.persist(entityP);
400: em.getTransaction().commit();
401: em.clear();
402:
403: final OWLClassP res = em.find(OWLClassP.class, entityP.getUri());
404: assertNotNull(res);
405: assertEquals(entityP.getProperties(), res.getProperties());
406: }
407:
408: @Test
409: public void testPersistInstanceWithPlainIdentifierObjectPropertyValue() {
410: this.em = getEntityManager("PersistInstanceWithIdentifierObjectPropertyValue", false);
411: final URI value = URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#individualAAA");
412: entityP.setIndividualUri(value);
413: em.getTransaction().begin();
414: em.persist(entityP);
415: em.getTransaction().commit();
416: em.clear();
417:
418: final OWLClassP res = em.find(OWLClassP.class, entityP.getUri());
419: assertNotNull(res);
420: assertEquals(value, res.getIndividualUri());
421: }
422:
423: @Test
424: public void testPersistInstanceWithPluralObjectPropertyAttributeRepresentedByUrls() {
425: this.em = getEntityManager("PersistInstanceWithPluralIdentifierObjectPropertyValue", false);
426: final Set<URL> urls = Generators.createUrls();
427: entityP.setIndividuals(urls);
428: em.getTransaction().begin();
429: em.persist(entityP);
430: em.getTransaction().commit();
431: em.clear();
432:
433: final OWLClassP res = em.find(OWLClassP.class, entityP.getUri());
434: assertNotNull(res);
435: assertEquals(urls, res.getIndividuals());
436: }
437:
438: @Test
439: public void testPersistInstanceWithSimpleListOfIdentifiers() {
440: this.em = getEntityManager("PersistInstanceWithSimpleListOfIdentifiers", false);
441: entityP.setSimpleList(Generators.createListOfIdentifiers());
442: em.getTransaction().begin();
443: em.persist(entityP);
444: em.getTransaction().commit();
445: em.clear();
446:
447: final OWLClassP res = em.find(OWLClassP.class, entityP.getUri());
448: assertNotNull(res);
449: assertEquals(entityP.getSimpleList(), res.getSimpleList());
450: }
451:
452: @Test
453: public void testPersistInstanceWithReferencedListOfIdentifiers() {
454: this.em = getEntityManager("PersistInstanceWithReferencedListOfIdentifiers", false);
455: entityP.setReferencedList(Generators.createListOfIdentifiers());
456: em.getTransaction().begin();
457: em.persist(entityP);
458: em.getTransaction().commit();
459: em.clear();
460:
461: final OWLClassP res = em.find(OWLClassP.class, entityP.getUri());
462: assertNotNull(res);
463: assertEquals(entityP.getReferencedList(), res.getReferencedList());
464: }
465:
466: @Test
467: public void testPersistInstanceWithAnnotationProperties() {
468: this.em = getEntityManager("PersistInstanceWithAnnotationPropertyValues", false);
469: final String apValue = "annotationPropertyValue";
470: final URI apUriValue = URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#annotationPropertyValue");
471: entityN.setAnnotationProperty(apValue);
472: entityN.setAnnotationUri(apUriValue);
473: em.getTransaction().begin();
474: em.persist(entityN);
475: em.getTransaction().commit();
476: em.clear();
477: assertNotNull(entityN.getId());
478:
479: final OWLClassN res = em.find(OWLClassN.class, entityN.getId());
480: assertEquals(apValue, res.getAnnotationProperty());
481: assertEquals(apUriValue, res.getAnnotationUri());
482: }
483:
484: @Test
485: public void persistEntityWithNonNullGeneratedIdentifierDoesNotRewriteIdentifier() {
486: this.em = getEntityManager("PersistEntityWithNonNullGeneratedIdentifiersDoesNotRewriteIdentifier", false);
487: final URI u = URI.create("http://krizik.felk.cvut.cz/ontolgoies/jopa#EntityELives");
488: entityE.setUri(u);
489: em.getTransaction().begin();
490: em.persist(entityE);
491: em.getTransaction().commit();
492:
493: assertEquals(u, entityE.getUri());
494: final OWLClassE res = em.find(OWLClassE.class, u);
495: assertNotNull(res);
496: assertEquals(u, res.getUri());
497: assertEquals(entityE.getStringAttribute(), res.getStringAttribute());
498: }
499:
500: @Test
501: public void persistEntityAndReferenceWithNonNullGeneratedIdentifiersDoesNotRewriteThem() {
502: this.em = getEntityManager("PersistEntityAndReferenceWithNonNullGeneratedIdentifiersDoesNotRewriteThem", false);
503: final URI uK = URI.create("http://krizik.felk.cvut.cz/ontolgoies/jopa#EntityKLives");
504: final URI uE = URI.create("http://krizik.felk.cvut.cz/ontolgoies/jopa#EntityELives");
505: final OWLClassK entityK = new OWLClassK();
506: entityK.setUri(uK);
507: entityK.setOwlClassE(entityE);
508: entityE.setUri(uE);
509: em.getTransaction().begin();
510: em.persist(entityK);
511: em.persist(entityE);
512: em.getTransaction().commit();
513:
514: assertEquals(uK, entityK.getUri());
515: assertEquals(uE, entityE.getUri());
516: final OWLClassK resK = em.find(OWLClassK.class, uK);
517: assertNotNull(resK);
518: assertEquals(uE, resK.getOwlClassE().getUri());
519: final OWLClassE resE = em.find(OWLClassE.class, uE);
520: assertNotNull(resE);
521: assertEquals(uE, resE.getUri());
522: }
523:
524: @Test
525: public void testPersistEntityWithUriTypes() {
526: this.em = getEntityManager("PersistEntityWithUriTypes", false);
527: entityP.setTypes(Generators.createUriTypes());
528: em.getTransaction().begin();
529: em.persist(entityP);
530: em.getTransaction().commit();
531: em.clear();
532:
533: final OWLClassP result = em.find(OWLClassP.class, entityP.getUri());
534: assertEquals(entityP.getTypes().size(), result.getTypes().size());
535: assertTrue(entityP.getTypes().containsAll(result.getTypes()));
536: }
537:
538: @Test
539: public void persistEntityWithDatatypePropertyCollectionPersistsAllValues() {
540: assertFalse(entityM.getIntegerSet().isEmpty());
541: this.em = getEntityManager("PersistEntityWithDatatypePropertyCollection", false);
542: em.getTransaction().begin();
543: em.persist(entityM);
544: em.getTransaction().commit();
545:
546: assertNotNull(entityM.getKey());
547: final OWLClassM result = em.find(OWLClassM.class, entityM.getKey());
548: assertNotNull(result);
549: assertEquals(entityM.getIntegerSet(), result.getIntegerSet());
550: }
551:
552: @Test
553: public void persistSetsStringLiteralLanguageTagAccordingToDescriptor() throws Exception {
554: this.em = getEntityManager("persistSetsStringLiteralLanguageTagAccordingToDescriptor", false);
555: em.getTransaction().begin();
556: final Descriptor descriptor = new EntityDescriptor();
557: descriptor.setAttributeLanguage(OWLClassA.class.getDeclaredField("stringAttribute"), "cs");
558: em.persist(entityA, descriptor);
559: em.getTransaction().commit();
560:
561: verifyStatementsPresent(Collections.singleton(
562: new Triple(entityA.getUri(), URI.create(Vocabulary.P_A_STRING_ATTRIBUTE), entityA.getStringAttribute(),
563: "cs")), em);
564: assertNotNull(em.find(OWLClassA.class, entityA.getUri()));
565: }
566:
567: @Test
568: public void persistSetsStringLiteralLanguageTagToGloballyConfiguredValueWhenDescriptorDoesNotSpecifyIt()
569: throws Exception {
570: this.em = getEntityManager(
571: "persistSetsStringLiteralLanguageTagToGloballyConfiguredValueWhenDescriptorDoesNotSpecifyIt", false);
572: em.getTransaction().begin();
573: em.persist(entityA);
574: em.getTransaction().commit();
575:
576: verifyStatementsPresent(Collections.singleton(
577: new Triple(entityA.getUri(), URI.create(Vocabulary.P_A_STRING_ATTRIBUTE), entityA.getStringAttribute(),
578: "en")), em);
579: assertNotNull(em.find(OWLClassA.class, entityA.getUri()));
580: }
581:
582: @Test
583: public void persistAllowsOverridingGlobalLanguageWithLocalEmptyTag() throws Exception {
584: this.em = getEntityManager("persistAllowsOverridingGlobalLanguageWithLocalEmptyTag", false);
585: em.getTransaction().begin();
586: final Descriptor descriptor = new EntityDescriptor();
587: descriptor.setAttributeLanguage(OWLClassA.class.getDeclaredField("stringAttribute"), null);
588: em.persist(entityA, descriptor);
589: em.getTransaction().commit();
590:
591: verifyStatementsPresent(Collections.singleton(
592: new Triple(entityA.getUri(), URI.create(Vocabulary.P_A_STRING_ATTRIBUTE), entityA.getStringAttribute(),
593: null)), em);
594: final OWLClassA result = em.find(OWLClassA.class, entityA.getUri());
595: assertNotNull(result);
596: // The string attribute should be loaded even though PU language is set to en, because the persisted value has no lang tag
597: assertEquals(entityA.getStringAttribute(), result.getStringAttribute());
598: }
599:
600: @Test
601: public void persistAllowsToSpecifyLanguageTagPerEntityAndOverrideItOnAttributeLevel() throws Exception {
602: this.em = getEntityManager("persistAllowsToSpecifyLanguageTagPerEntityAndOverrideItOnAttributeLevel", false);
603: entityN.setStringAttribute("retezec v cestine");
604: entityN.setAnnotationProperty("entity descriptor ist in Deutsch");
605: final Descriptor descriptor = new EntityDescriptor();
606: descriptor.setLanguage("de");
607: descriptor.setAttributeLanguage(OWLClassN.class.getDeclaredField("stringAttribute"), "cs");
608:
609: em.getTransaction().begin();
610: em.persist(entityN, descriptor);
611: em.getTransaction().commit();
612:
613: final Set<Triple> statements = new HashSet<>(4);
614: statements.add(new Triple(URI.create(entityN.getId()), URI.create(Vocabulary.P_N_STR_ANNOTATION_PROPERTY),
615: entityN.getAnnotationProperty(), "de"));
616: statements.add(new Triple(URI.create(entityN.getId()), URI.create(Vocabulary.P_N_STRING_ATTRIBUTE),
617: entityN.getStringAttribute(), "cs"));
618: verifyStatementsPresent(statements, em);
619:
620: final OWLClassN result = em.find(OWLClassN.class, entityN.getId(), descriptor);
621: assertEquals(entityN.getAnnotationProperty(), result.getAnnotationProperty());
622: assertEquals(entityN.getStringAttribute(), result.getStringAttribute());
623: }
624:
625: @Test
626: public void persistSavesInstanceWithReferenceToExistingInstance() {
627: this.em = getEntityManager("persistSavesInstanceWithReferenceToExistingInstance", false);
628: persist(entityA);
629:
630: em.clear();
631: em.getTransaction().begin();
632: em.persist(entityD);
633: em.getTransaction().commit();
634:
635: final OWLClassD resultD = em.find(OWLClassD.class, entityD.getUri());
636: assertNotNull(resultD);
637: assertNotNull(resultD.getOwlClassA());
638: }
639: }