-
Notifications
You must be signed in to change notification settings - Fork 14
/
IndirectSetTest.java
227 lines (199 loc) · 7.19 KB
/
IndirectSetTest.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/**
* Copyright (C) 2019 Czech Technical University in Prague
*
* 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.
*
* 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.adapters;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.lang.reflect.Field;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import cz.cvut.kbss.jopa.environment.utils.Generators;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import cz.cvut.kbss.jopa.sessions.UnitOfWorkImpl;
import cz.cvut.kbss.jopa.environment.OWLClassA;
import cz.cvut.kbss.jopa.environment.OWLClassF;
public class IndirectSetTest {
private static Set<OWLClassA> set;
private static Set<OWLClassA> backupSet;
private static OWLClassF owner;
private static Field ownerField;
private IndirectSet<OWLClassA> target;
@Mock
private UnitOfWorkImpl uow;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
owner = new OWLClassF();
owner.setUri(URI.create("http://C"));
ownerField = OWLClassF.class.getDeclaredField("simpleSet");
backupSet = new HashSet<>(Generators.generateInstances(10));
set = new HashSet<>();
set.addAll(backupSet);
}
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
when(uow.isInTransaction()).thenReturn(Boolean.TRUE);
target = new IndirectSet<>(owner, ownerField, uow, set);
set.clear();
set.addAll(backupSet);
owner.setSimpleSet(target);
}
@Test(expected = NullPointerException.class)
public void testIndirectSetNullUoW() {
@SuppressWarnings("unused")
final IndirectSet<OWLClassA> res = new IndirectSet<>(owner, ownerField, null, set);
fail("This line should not have been reached.");
}
@Test(expected = NullPointerException.class)
public void testIndirectSetNullReferencedSet() {
@SuppressWarnings("unused")
final IndirectSet<OWLClassA> res = new IndirectSet<>(owner, ownerField, uow, null);
fail("This line should not have been reached.");
}
@Test
public void testContains() {
final OWLClassA elem = backupSet.iterator().next();
assertTrue(target.contains(elem));
}
@Test
public void testIteratorHasNext() {
final Iterator<OWLClassA> it = set.iterator();
final Iterator<OWLClassA> indIt = target.iterator();
while (it.hasNext()) {
it.next();
assertTrue(indIt.hasNext());
assertNotNull(indIt.next());
}
}
@Test
public void testIteratorNext() {
final Iterator<OWLClassA> it = set.iterator();
final Iterator<OWLClassA> indIt = target.iterator();
while (it.hasNext()) {
assertTrue(indIt.hasNext());
assertEquals(it.next(), indIt.next());
}
}
@Test
public void testIteratorRemove() {
final Iterator<OWLClassA> it = target.iterator();
assertTrue(it.hasNext());
it.next();
it.remove();
verify(uow).attributeChanged(owner, ownerField);
assertEquals(backupSet.size() - 1, set.size());
}
@Test
public void testAdd() {
final OWLClassA a = new OWLClassA();
a.setUri(URI.create("http://newA"));
a.setStringAttribute("testAttribute");
target.add(a);
verify(uow).attributeChanged(owner, ownerField);
assertEquals(backupSet.size() + 1, set.size());
}
@Test
public void addingExistingElementDoesNotTriggerAttributeChange() {
final OWLClassA toAdd = backupSet.iterator().next();
target.add(toAdd);
verify(uow, never()).attributeChanged(owner, ownerField);
}
@Test
public void testRemove() {
final OWLClassA toRemove = set.iterator().next();
target.remove(toRemove);
verify(uow).attributeChanged(owner, ownerField);
assertEquals(backupSet.size() - 1, set.size());
}
@Test
public void testAddAll() {
final List<OWLClassA> toAdd = new ArrayList<>();
for (int i = 0; i < 5; i++) {
final OWLClassA a = new OWLClassA();
a.setUri(URI.create("http://addAllA" + i));
toAdd.add(a);
}
target.addAll(toAdd);
verify(uow).attributeChanged(owner, ownerField);
assertEquals(backupSet.size() + toAdd.size(), set.size());
assertTrue(set.contains(toAdd.iterator().next()));
}
@Test
public void testRetainAll() {
Set<OWLClassA> toRetain = new HashSet<>();
Iterator<OWLClassA> it = backupSet.iterator();
for (int i = 0; i < 8; i++) {
assertTrue(it.hasNext());
toRetain.add(it.next());
}
target.retainAll(toRetain);
verify(uow).attributeChanged(owner, ownerField);
assertEquals(toRetain.size(), set.size());
assertEquals(toRetain.size(), target.size());
}
@Test
public void testRemoveAll() {
Set<OWLClassA> toRemove = new HashSet<>();
Iterator<OWLClassA> it = backupSet.iterator();
for (int i = 0; i < 8; i++) {
assertTrue(it.hasNext());
toRemove.add(it.next());
}
target.removeAll(toRemove);
verify(uow).attributeChanged(owner, ownerField);
assertEquals(backupSet.size() - toRemove.size(), set.size());
}
@Test(expected = NullPointerException.class)
public void testRemoveAllNull() {
target.removeAll(null);
fail("This line should not have been reached.");
}
@Test
public void testClear() {
target.clear();
verify(uow).attributeChanged(owner, ownerField);
assertTrue(set.isEmpty());
assertTrue(target.isEmpty());
}
@Test
public void equalsWorksForTwoIndirectSets() {
final IndirectSet<OWLClassA> other = new IndirectSet<>(owner, ownerField, uow, set);
assertEquals(target, other);
}
@Test
public void equalsWorksForRegularSetAndIndirectSet() {
assertEquals(backupSet, target);
}
@Test
public void equalsWorksForIndirectSetAndRegularSet() {
assertEquals(target, backupSet);
}
@Test
public void hashCodeReturnsInternalSetHashCode() {
assertEquals(backupSet.hashCode(), target.hashCode());
}
}