-
Notifications
You must be signed in to change notification settings - Fork 14
/
RepositoryMapTest.java
183 lines (156 loc) · 6.58 KB
/
RepositoryMapTest.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/**
* Copyright (C) 2020 Czech Technical University in Prague
* <p>
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
* version.
* <p>
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details. You should have received a copy of the GNU General Public License along with this program. If not, see
* <http://www.gnu.org/licenses/>.
*/
package cz.cvut.kbss.jopa.sessions;
import cz.cvut.kbss.jopa.environment.OWLClassA;
import cz.cvut.kbss.jopa.environment.OWLClassB;
import cz.cvut.kbss.jopa.environment.OWLClassE;
import cz.cvut.kbss.jopa.model.descriptors.Descriptor;
import cz.cvut.kbss.jopa.model.descriptors.EntityDescriptor;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.lang.reflect.Field;
import java.net.URI;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.*;
public class RepositoryMapTest {
private static final URI CONTEXT = URI.create("http://repositoryMapTestContext");
private static OWLClassA entityA;
private static OWLClassA cloneA;
private static OWLClassB entityB;
private static OWLClassB cloneB;
private static OWLClassE entityE;
private static OWLClassE cloneE;
private static Descriptor descriptor;
private RepositoryMap repoMap;
private Map<URI, Map<Object, Object>> theMap;
private Map<Object, Descriptor> descriptors;
@BeforeAll
public static void setUpBeforeClass() {
entityA = new OWLClassA();
entityA.setStringAttribute("SomeString");
cloneA = new OWLClassA();
cloneA.setStringAttribute(entityA.getStringAttribute());
entityB = new OWLClassB();
entityB.setStringAttribute("otherString");
cloneB = new OWLClassB();
cloneB.setStringAttribute(entityB.getStringAttribute());
entityE = new OWLClassE();
cloneE = new OWLClassE();
descriptor = new EntityDescriptor(CONTEXT);
}
@SuppressWarnings("unchecked")
@BeforeEach
public void setUp() throws Exception {
initData();
this.repoMap = new RepositoryMap();
repoMap.initDescriptors();
final Field mapField = RepositoryMap.class.getDeclaredField("origsToClones");
mapField.setAccessible(true);
this.theMap = (Map<URI, Map<Object, Object>>) mapField.get(repoMap);
final Field descriptorsField = RepositoryMap.class.getDeclaredField("entityDescriptors");
descriptorsField.setAccessible(true);
this.descriptors = (Map<Object, Descriptor>) descriptorsField.get(repoMap);
}
private void initData() {
entityA.setUri(URI.create("http://krizik.felk.cvut.cz/jopa/ontology/entityA"));
cloneA.setUri(URI.create(entityA.getUri().toString()));
entityB.setUri(URI.create("http://krizik.felk.cvut.cz/jopa/ontology/entityB"));
cloneB.setUri(URI.create(entityB.getUri().toString()));
entityE.setUri(URI.create("http://krizik.felk.cvut.cz/jopa/ontology/entityE"));
cloneE.setUri(URI.create(entityE.getUri().toString()));
}
@Test
public void testAdd() {
repoMap.add(descriptor, entityA, cloneA);
assertTrue(repoMap.contains(descriptor, entityA));
assertTrue(theMap.containsKey(CONTEXT));
assertTrue(theMap.get(CONTEXT).containsKey(entityA));
}
@Test
public void testRemove() {
repoMap.add(descriptor, entityA, cloneA);
repoMap.add(descriptor, entityB, cloneB);
assertTrue(repoMap.contains(descriptor, entityA));
assertTrue(repoMap.contains(descriptor, entityB));
repoMap.remove(descriptor, entityB);
assertTrue(repoMap.contains(descriptor, entityA));
assertFalse(repoMap.contains(descriptor, entityB));
}
@Test
public void testGet() {
repoMap.add(descriptor, entityA, cloneA);
repoMap.add(descriptor, entityB, cloneB);
final Object res = repoMap.get(descriptor, entityA);
assertNotNull(res);
assertSame(cloneA, res);
}
@Test
public void testGetUnknown() {
final Object res = repoMap.get(descriptor, entityA);
assertNull(res);
}
@Test
public void testContains() {
repoMap.add(descriptor, entityA, cloneA);
repoMap.add(descriptor, entityB, cloneB);
assertTrue(repoMap.contains(descriptor, entityA));
assertTrue(repoMap.contains(descriptor, entityB));
assertFalse(repoMap.contains(descriptor, entityE));
final EntityDescriptor desc = new EntityDescriptor();
assertFalse(repoMap.contains(desc, entityA));
}
@Test
public void testAddEntityToRepository() {
repoMap.addEntityToRepository(entityA, descriptor);
assertTrue(descriptors.containsKey(entityA));
assertSame(descriptor, descriptors.get(entityA));
}
@Test
public void testRemoveEntityToRepository() {
repoMap.addEntityToRepository(entityA, descriptor);
assertTrue(descriptors.containsKey(entityA));
repoMap.removeEntityToRepository(entityA);
assertFalse(descriptors.containsKey(entityA));
}
@Test
public void testGetEntityDescriptor() {
repoMap.addEntityToRepository(entityA, descriptor);
assertTrue(descriptors.containsKey(entityA));
final Descriptor res = repoMap.getEntityDescriptor(entityA);
assertNotNull(res);
assertSame(descriptor, res);
}
@Test
public void testClear() {
repoMap.add(descriptor, entityA, cloneA);
repoMap.add(descriptor, entityB, cloneB);
repoMap.addEntityToRepository(entityA, descriptor);
repoMap.clear();
assertFalse(repoMap.contains(descriptor, entityA));
assertFalse(repoMap.contains(descriptor, entityB));
assertNull(repoMap.getEntityDescriptor(entityA));
}
@Test
public void addingTwoObjectsWithNullIdentifierIsPossible() {
entityA.setUri(null);
cloneA.setUri(null);
entityB.setUri(null);
cloneB.setUri(null);
repoMap.add(descriptor, entityA, cloneA);
assertTrue(theMap.get(CONTEXT).containsValue(cloneA));
repoMap.add(descriptor, entityB, cloneB);
assertTrue(theMap.get(CONTEXT).containsValue(cloneA));
assertTrue(theMap.get(CONTEXT).containsValue(cloneB));
}
}