-
Notifications
You must be signed in to change notification settings - Fork 14
/
AbstractIdentifiableTypeTest.java
245 lines (218 loc) · 10.3 KB
/
AbstractIdentifiableTypeTest.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package cz.cvut.kbss.jopa.model.metamodel;
import cz.cvut.kbss.jopa.environment.OWLClassA;
import cz.cvut.kbss.jopa.environment.OWLClassB;
import cz.cvut.kbss.jopa.model.IRI;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import java.util.Collections;
import java.util.Set;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.*;
public class AbstractIdentifiableTypeTest {
private static Class<OWLClassA> cls;
private static IRI classIri;
private static String className;
@Rule
public ExpectedException thrown = ExpectedException.none();
private EntityTypeImpl<OWLClassA> et;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
cls = OWLClassA.class;
classIri = IRI.create(OWLClassA.getClassIri());
className = OWLClassA.class.getName();
}
@Before
public void setUp() throws Exception {
this.et = new EntityTypeImpl<>(className, cls, classIri);
}
@Test
public void hasSingleIdAttributeReturnsAlwaysTrue() {
assertTrue(et.hasSingleIdAttribute());
}
@Test
public void getAttributesReturnsDeclaredAttributesPlusInheritedAttributes() {
final AbstractIdentifiableType<? super OWLClassA> supertype = mock(AbstractIdentifiableType.class);
et.setSupertype(supertype);
et.getAttributes();
verify(supertype).getAttributes();
}
@Test
public void getAttributesReturnsDeclaredAttributesWhenThereIsNoSupertype() {
final Attribute<OWLClassA, ?> att = mock(Attribute.class);
et.addDeclaredAttribute("test", att);
final Set<Attribute<? super OWLClassA, ?>> result = et.getAttributes();
assertEquals(1, result.size());
assertTrue(result.contains(att));
}
@Test
public void getAttributeReturnsInheritedAttribute() {
final AbstractIdentifiableType<? super OWLClassA> supertype = mock(AbstractIdentifiableType.class);
et.setSupertype(supertype);
final String attName = "test";
final Attribute att = mock(Attribute.class);
when(supertype.getAttribute(attName)).thenReturn(att);
assertEquals(att, et.getAttribute(attName));
verify(supertype).getAttribute(attName);
}
@Test
public void getAttributeThrowsIllegalArgumentWhenAttributeIsNotFoundInDeclaredAndInheritedAttributes() {
final String attName = "unknownAttribute";
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Attribute " + attName + " is not present in type " + et.toString());
et.getAttribute(attName);
}
@Test
public void getCollectionThrowsIllegalArgumentWhenElementTypeDoesNotMatch() {
final CollectionAttribute att = mock(CollectionAttribute.class);
final String attName = "test";
when(att.getBindableJavaType()).thenReturn(OWLClassA.class);
et.addDeclaredAttribute(attName, att);
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Collection attribute " + attName + " with element type " + OWLClassB.class +
" is not present in type " + et.toString());
et.getCollection(attName, OWLClassB.class);
}
@Test
public void getListReturnsListAttribute() {
final ListAttribute att = mock(ListAttribute.class);
final String attName = "test";
when(att.getBindableJavaType()).thenReturn(OWLClassA.class);
et.addDeclaredAttribute(attName, att);
final ListAttribute result = et.getList(attName, OWLClassA.class);
assertEquals(att, result);
}
@Test
public void getSetThrowsIllegalArgumentWhenAttributeIsNotSet() {
final CollectionAttribute att = mock(CollectionAttribute.class);
final String attName = "test";
when(att.getBindableJavaType()).thenReturn(OWLClassA.class);
et.addDeclaredAttribute(attName, att);
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Set attribute " + attName + " with element type " + OWLClassA.class +
" is not present in type " + et.toString());
et.getSet(attName, OWLClassA.class);
}
@Test
public void getPluralAttributesGetsAlsoInheritedPluralAttributes() {
final CollectionAttribute att = mock(CollectionAttribute.class);
when(att.isCollection()).thenReturn(true);
final ListAttribute listAtt = mock(ListAttribute.class);
when(listAtt.isCollection()).thenReturn(true);
final AbstractIdentifiableType<? super OWLClassA> supertype = mock(AbstractIdentifiableType.class);
et.setSupertype(supertype);
et.addDeclaredAttribute("test", listAtt);
when(supertype.getPluralAttributes()).thenReturn(Collections.singleton(att));
final Set<PluralAttribute<? super OWLClassA, ?, ?>> result = et.getPluralAttributes();
assertTrue(result.contains(att));
assertTrue(result.contains(listAtt));
}
@Test
public void getSingularAttributesGetsAlsoInheritedSingularAttributes() {
final SingularAttribute attOne = mock(SingularAttribute.class);
when(attOne.isCollection()).thenReturn(false);
final SingularAttribute attTwo = mock(SingularAttribute.class);
when(attTwo.isCollection()).thenReturn(false);
final AbstractIdentifiableType<? super OWLClassA> supertype = mock(AbstractIdentifiableType.class);
et.setSupertype(supertype);
et.addDeclaredAttribute("test", attTwo);
when(supertype.getSingularAttributes()).thenReturn(Collections.singleton(attOne));
final Set<SingularAttribute<? super OWLClassA, ?>> result = et.getSingularAttributes();
assertTrue(result.contains(attOne));
assertTrue(result.contains(attTwo));
}
@Test
public void getSingularAttributeReturnsAttributeWithMatchingNameAndType() {
final SingularAttribute attOne = mock(SingularAttribute.class);
when(attOne.isCollection()).thenReturn(false);
final String attName = "test";
when(attOne.getJavaType()).thenReturn(OWLClassA.class);
et.addDeclaredAttribute(attName, attOne);
final SingularAttribute result = et.getSingularAttribute(attName, OWLClassA.class);
assertEquals(attOne, result);
}
@Test
public void getSingularAttributeThrowsIllegalARgumentWhenAttributeIsNotSingular() {
final ListAttribute att = mock(ListAttribute.class);
final String attName = "test";
when(att.getBindableJavaType()).thenReturn(OWLClassA.class);
when(att.isCollection()).thenReturn(true);
et.addDeclaredAttribute(attName, att);
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage(
"Singular attribute " + attName + " of type " + OWLClassB.class + " is not present in type " +
et.toString());
et.getSingularAttribute(attName, OWLClassB.class);
}
@Test
public void getDeclaredAttributeReturnsAttributeDeclaredInType() {
final SingularAttribute attOne = mock(SingularAttribute.class);
when(attOne.isCollection()).thenReturn(false);
final String attName = "test";
when(attOne.getJavaType()).thenReturn(OWLClassA.class);
et.addDeclaredAttribute(attName, attOne);
final Attribute result = et.getDeclaredAttribute(attName);
assertEquals(attOne, result);
}
@Test
public void getDeclaredCollectionReturnsDeclaredCollectionAttribute() {
final CollectionAttribute att = mock(CollectionAttribute.class);
final String attName = "test";
when(att.getBindableJavaType()).thenReturn(OWLClassA.class);
et.addDeclaredAttribute(attName, att);
final CollectionAttribute result = et.getDeclaredCollection(attName);
assertEquals(att, result);
}
@Test
public void getDeclaredListThrowsIllegalArgumentWhenListElementTypeDoesNotMatch() {
final ListAttribute att = mock(ListAttribute.class);
final String attName = "test";
when(att.getBindableJavaType()).thenReturn(OWLClassA.class);
when(att.isCollection()).thenReturn(true);
et.addDeclaredAttribute(attName, att);
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage(
"List attribute " + attName + " with element type " + OWLClassB.class + " is not declared in type " +
et.toString());
et.getDeclaredList(attName, OWLClassB.class);
}
@Test
public void getDeclaredSetThrowsIllegalArgumentWhenAttributeIsNotSet() {
final ListAttribute att = mock(ListAttribute.class);
final String attName = "test";
when(att.getBindableJavaType()).thenReturn(OWLClassA.class);
when(att.isCollection()).thenReturn(true);
et.addDeclaredAttribute(attName, att);
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage(
"Set attribute " + attName + " with element type " + OWLClassB.class + " is not declared in type " +
et.toString());
et.getDeclaredSet(attName, OWLClassB.class);
}
@Test
public void getDeclaredSingularAttributeReturnsDeclaredSingularAttribute() {
final SingularAttribute att = mock(SingularAttribute.class);
final String attName = "test";
when(att.isCollection()).thenReturn(false);
when(att.getJavaType()).thenReturn(OWLClassA.class);
et.addDeclaredAttribute(attName, att);
final SingularAttribute result = et.getDeclaredSingularAttribute(attName);
assertEquals(att, result);
}
@Test
public void getDeclaredSingularAttributeThrowsIllegalArgumentForInvalidType() {
final SingularAttribute att = mock(SingularAttribute.class);
final String attName = "test";
when(att.isCollection()).thenReturn(false);
when(att.getJavaType()).thenReturn(OWLClassA.class);
et.addDeclaredAttribute(attName, att);
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage(
"Singular attribute " + attName + " of type " + OWLClassB.class + " is not declared in type " +
et.toString());
et.getDeclaredSingularAttribute(attName, OWLClassB.class);
}
}