-
Notifications
You must be signed in to change notification settings - Fork 14
/
PendingAssertionsRegistryTest.java
98 lines (83 loc) · 4.73 KB
/
PendingAssertionsRegistryTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package cz.cvut.kbss.jopa.oom;
import cz.cvut.kbss.jopa.environment.utils.Generators;
import cz.cvut.kbss.ontodriver.model.Assertion;
import cz.cvut.kbss.ontodriver.model.NamedResource;
import org.junit.Test;
import java.lang.reflect.Field;
import java.net.URI;
import java.util.Map;
import java.util.Set;
import static org.junit.Assert.*;
public class PendingAssertionsRegistryTest {
private final NamedResource owner = NamedResource.create(Generators.createIndividualIdentifier());
private final Assertion assertion =
Assertion.createObjectPropertyAssertion(Generators.createPropertyIdentifier(), false);
private final Object object = new Object();
private PendingAssertionRegistry registry = new PendingAssertionRegistry();
@Test
public void addPendingAssertionAddsPendingAssertionToObjectsMap() throws Exception {
registry.addPendingAssertion(owner, assertion, object, null);
final Map<Object, Set<PendingAssertionRegistry.PendingAssertion>> assertions = getPendingAssertions();
assertTrue(assertions.containsKey(object));
final Set<PendingAssertionRegistry.PendingAssertion> pending = assertions.get(object);
assertEquals(1, pending.size());
final PendingAssertionRegistry.PendingAssertion pa = pending.iterator().next();
assertEquals(owner, pa.getOwner());
assertEquals(assertion, pa.getAssertion());
assertNull(pa.getContext());
}
@Test
public void addPendingAssertionAddsPendingAssertionToObjectsMapWithContext() throws Exception {
final URI context = Generators.createIndividualIdentifier();
registry.addPendingAssertion(owner, assertion, object, context);
final Map<Object, Set<PendingAssertionRegistry.PendingAssertion>> assertions = getPendingAssertions();
assertTrue(assertions.containsKey(object));
final Set<PendingAssertionRegistry.PendingAssertion> pending = assertions.get(object);
assertEquals(1, pending.size());
final PendingAssertionRegistry.PendingAssertion pa = pending.iterator().next();
assertEquals(owner, pa.getOwner());
assertEquals(assertion, pa.getAssertion());
assertEquals(context, pa.getContext());
}
private Map<Object, Set<PendingAssertionRegistry.PendingAssertion>> getPendingAssertions() throws Exception {
final Field paField = PendingAssertionRegistry.class.getDeclaredField("pendingAssertions");
paField.setAccessible(true);
return (Map<Object, Set<PendingAssertionRegistry.PendingAssertion>>) paField.get(registry);
}
@Test
public void removeAndGetRemovesPendingAssertionsForObjectAndReturnsThem() throws Exception {
registry.addPendingAssertion(owner, assertion, object, null);
final Map<Object, Set<PendingAssertionRegistry.PendingAssertion>> assertions = getPendingAssertions();
final Set<PendingAssertionRegistry.PendingAssertion> pending = assertions.get(object);
assertFalse(pending.isEmpty());
final Set<PendingAssertionRegistry.PendingAssertion> result =
registry.removeAndGetPendingAssertionsWith(object);
assertEquals(pending, result);
assertFalse(getPendingAssertions().containsKey(object));
}
@Test
public void removePendingAssertionsRemovesAssertionsRegardingSpecifiedSubject() throws Exception {
registry.addPendingAssertion(owner, assertion, object, null);
registry.addPendingAssertion(NamedResource.create(Generators.createIndividualIdentifier()), assertion, object,
null);
final Object anotherObject = new Object();
registry.addPendingAssertion(owner, assertion, anotherObject, null);
registry.removePendingAssertions(owner);
final Map<Object, Set<PendingAssertionRegistry.PendingAssertion>> result = getPendingAssertions();
assertEquals(1, result.get(object).size());
assertFalse(result.containsKey(anotherObject));
assertTrue(registry.getPendingResources().contains(object));
assertFalse(registry.getPendingResources().contains(anotherObject));
}
@Test
public void removePendingAssertionsRemovesAssertionsWithSubjectAndAssertion() throws Exception {
registry.addPendingAssertion(owner, assertion, object, null);
registry.addPendingAssertion(NamedResource.create(Generators.createIndividualIdentifier()), assertion, object,
null);
registry.removePendingAssertions(owner, assertion);
final Map<Object, Set<PendingAssertionRegistry.PendingAssertion>> result = getPendingAssertions();
assertEquals(1, result.get(object).size());
final PendingAssertionRegistry.PendingAssertion pa = result.get(object).iterator().next();
assertNotEquals(owner, pa.getOwner());
}
}