-
Notifications
You must be signed in to change notification settings - Fork 14
/
StaticMetamodelInitializerTest.java
278 lines (232 loc) · 12.3 KB
/
StaticMetamodelInitializerTest.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package cz.cvut.kbss.jopa.model.metamodel;
import cz.cvut.kbss.jopa.environment.*;
import cz.cvut.kbss.jopa.environment.utils.MetamodelMocks;
import cz.cvut.kbss.jopa.exception.StaticMetamodelInitializationException;
import cz.cvut.kbss.jopa.model.annotations.*;
import cz.cvut.kbss.jopa.model.annotations.Properties;
import cz.cvut.kbss.jopa.vocabulary.RDFS;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import java.net.URI;
import java.util.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
class StaticMetamodelInitializerTest {
@Mock
private Metamodel metamodel;
private MetamodelMocks metamodelMocks;
private StaticMetamodelInitializer sut;
@BeforeEach
void setUp() throws Exception {
MockitoAnnotations.openMocks(this);
this.metamodelMocks = new MetamodelMocks();
metamodelMocks.setMocks(metamodel);
this.sut = new StaticMetamodelInitializer(metamodel);
}
@Test
void initializeStaticMetamodelInitializesFieldsOfSimpleEntityStaticMetamodel() {
when(metamodel.getEntities()).thenReturn(Collections.singleton(metamodelMocks.forOwlClassA().entityType()));
sut.initializeStaticMetamodel();
assertEquals(metamodelMocks.forOwlClassA().identifier(), OWLClassA_.uri);
assertEquals(metamodelMocks.forOwlClassA().stringAttribute(), OWLClassA_.stringAttribute);
assertEquals(metamodelMocks.forOwlClassA().typesSpec(), OWLClassA_.types);
}
@Test
void initializeStaticMetamodelInitializesPropertiesFieldInStaticMetamodelClass() {
when(metamodel.getEntities()).thenReturn(Collections.singleton(metamodelMocks.forOwlClassB().entityType()));
sut.initializeStaticMetamodel();
assertEquals(metamodelMocks.forOwlClassB().identifier(), OWLClassB_.uri);
assertEquals(metamodelMocks.forOwlClassB().stringAttribute(), OWLClassB_.stringAttribute);
assertEquals(metamodelMocks.forOwlClassB().propertiesSpec(), OWLClassB_.properties);
}
@Test
void initializeStaticMetamodelInitializesPluralAttributesInStaticMetamodelClass() {
when(metamodel.getEntities()).thenReturn(Collections.singleton(metamodelMocks.forOwlClassC().entityType()));
sut.initializeStaticMetamodel();
assertEquals(metamodelMocks.forOwlClassC().identifier(), OWLClassC_.uri);
assertEquals(metamodelMocks.forOwlClassC().referencedListAtt(), OWLClassC_.referencedList);
assertEquals(metamodelMocks.forOwlClassC().simpleListAtt(), OWLClassC_.simpleList);
}
@Test
void initializeStaticMetamodelThrowsStaticMetamodelInitializationExceptionWhenStaticMetamodelFieldHasNoMetamodelCounterpart()
throws Exception {
final EntityType<NoMatching> et = mock(EntityType.class);
final Identifier id = mock(Identifier.class);
when(id.getJavaField()).thenReturn(NoMatching.class.getDeclaredField("uri"));
when(et.getIdentifier()).thenReturn(id);
when(et.getDeclaredAttribute(any())).thenThrow(IllegalArgumentException.class);
when(et.getJavaType()).thenReturn(NoMatching.class);
when(metamodel.getEntities()).thenReturn(Collections.singleton(et));
assertThrows(StaticMetamodelInitializationException.class, () -> sut.initializeStaticMetamodel());
}
@OWLClass(iri = Vocabulary.CLASS_BASE + "NoMatching")
public static class NoMatching {
@Id
private URI uri;
@Types
private Set<String> types;
@Properties
private Map<String, Set<String>> properties;
}
@StaticMetamodel(NoMatching.class)
public static class NoMatching_ {
public static volatile Identifier<NoMatching, URI> uri;
public static volatile Attribute<NoMatching, String> unknown;
}
@Test
void initializeStaticMetamodelIgnoresClassWithoutMatchingStaticMetamodelAnnotation() throws Exception {
final EntityType<NoStaticMetamodelAnnotation> et = mock(EntityType.class);
final Identifier id = mock(Identifier.class);
when(id.getJavaField()).thenReturn(NoStaticMetamodelAnnotation.class.getDeclaredField("uri"));
when(et.getIdentifier()).thenReturn(id);
when(et.getDeclaredAttribute(any())).thenThrow(IllegalArgumentException.class);
when(et.getJavaType()).thenReturn(NoStaticMetamodelAnnotation.class);
when(metamodel.getEntities()).thenReturn(Collections.singleton(et));
assertNull(NoStaticMetamodelAnnotation_.uri);
sut.initializeStaticMetamodel();
assertNull(NoStaticMetamodelAnnotation_.uri);
}
@OWLClass(iri = Vocabulary.CLASS_BASE + "NoStaticMetamodelAnnotation")
public static class NoStaticMetamodelAnnotation {
@Id
private URI uri;
}
public static class NoStaticMetamodelAnnotation_ {
public static volatile Identifier<NoMatching, URI> uri;
}
@Test
void initializeStaticMetamodelIgnoresEntityIdentifierWhenItIsInheritedFromSuperclass() throws Exception {
final EntityType<WithInheritedIdentifier> et = mock(EntityType.class);
final Identifier id = mock(Identifier.class);
when(id.getJavaField()).thenReturn(NoMatching.class.getDeclaredField("uri"));
when(et.getIdentifier()).thenReturn(id);
when(et.getDeclaredAttribute(any())).thenThrow(IllegalArgumentException.class);
when(et.getJavaType()).thenReturn(WithInheritedIdentifier.class);
when(metamodel.getEntities()).thenReturn(Collections.singleton(et));
assertThrows(StaticMetamodelInitializationException.class, () -> sut.initializeStaticMetamodel());
assertNull(WithInheritedIdentifier_.uri);
}
@OWLClass(iri = Vocabulary.CLASS_BASE + "WithInheritedIdentifier")
public static class WithInheritedIdentifier {
}
@StaticMetamodel(WithInheritedIdentifier.class)
public static class WithInheritedIdentifier_ {
public static volatile Identifier<WithInheritedIdentifier, URI> uri;
}
@Test
void initializeStaticMetamodelIgnoresTypesSpecificationWhenItIsInheritedFromSuperclass() throws Exception {
final EntityType<WithInheritedTypes> et = mock(EntityType.class);
final Identifier id = mock(Identifier.class);
when(id.getJavaField()).thenReturn(NoMatching.class.getDeclaredField("uri"));
when(et.getIdentifier()).thenReturn(id);
final TypesSpecification types = mock(TypesSpecification.class);
when(types.getJavaField()).thenReturn(NoMatching.class.getDeclaredField("types"));
when(et.getTypes()).thenReturn(types);
when(et.getDeclaredAttribute(any())).thenThrow(IllegalArgumentException.class);
when(et.getJavaType()).thenReturn(WithInheritedTypes.class);
when(metamodel.getEntities()).thenReturn(Collections.singleton(et));
assertThrows(StaticMetamodelInitializationException.class, () -> sut.initializeStaticMetamodel());
assertNull(WithInheritedTypes_.types);
}
@OWLClass(iri = Vocabulary.CLASS_BASE + "WithInheritedTypes")
public static class WithInheritedTypes {
}
@StaticMetamodel(WithInheritedTypes.class)
public static class WithInheritedTypes_ {
public static volatile TypesSpecification<WithInheritedTypes, String> types;
}
@Test
void initializeStaticMetamodelIgnoresPropertiesSpecificationWhenItIsInheritedFromSuperclass() throws Exception {
final EntityType<WithInheritedProperties> et = mock(EntityType.class);
final Identifier id = mock(Identifier.class);
when(id.getJavaField()).thenReturn(NoMatching.class.getDeclaredField("uri"));
when(et.getIdentifier()).thenReturn(id);
final PropertiesSpecification types = mock(PropertiesSpecification.class);
when(types.getJavaField()).thenReturn(NoMatching.class.getDeclaredField("properties"));
when(et.getProperties()).thenReturn(types);
when(et.getDeclaredAttribute(any())).thenThrow(IllegalArgumentException.class);
when(et.getJavaType()).thenReturn(WithInheritedProperties.class);
when(metamodel.getEntities()).thenReturn(Collections.singleton(et));
assertThrows(StaticMetamodelInitializationException.class, () -> sut.initializeStaticMetamodel());
assertNull(WithInheritedProperties_.properties);
}
@OWLClass(iri = Vocabulary.CLASS_BASE + "WithInheritedProperties")
public static class WithInheritedProperties {
}
@StaticMetamodel(WithInheritedProperties.class)
public static class WithInheritedProperties_ {
public static volatile PropertiesSpecification<WithInheritedProperties, Set<String>, String, String> properties;
}
@Test
void initializeStaticMetamodelInitializesMetamodelForMappedSuperclasses() {
when(metamodel.getEntities()).thenReturn(Collections.singleton(metamodelMocks.forOwlClassQ().entityType()));
doReturn(new HashSet<>(Arrays.asList(metamodelMocks.forOwlClassQ().entityType(),
metamodelMocks.forOwlClassQ().entityType().getSupertype()))).when(metamodel).getManagedTypes();
sut.initializeStaticMetamodel();
assertEquals(metamodelMocks.forOwlClassQ().identifier(), QMappedSuperclass_.uri);
assertEquals(metamodelMocks.forOwlClassQ().qLabelAtt(), QMappedSuperclass_.label);
assertEquals(metamodelMocks.forOwlClassQ().qParentStringAtt(), QMappedSuperclass_.parentString);
assertEquals(metamodelMocks.forOwlClassQ().qOwlClassAAtt(), QMappedSuperclass_.owlClassA);
assertEquals(metamodelMocks.forOwlClassQ().qStringAtt(), OWLClassQ_.stringAttribute);
}
@Test
void initializeStaticMetamodelChecksThatModelSubclassHasDeclaredSuperclassInStaticMetamodelAsWell()
throws Exception {
final EntityType<SubClass> et = mock(EntityType.class);
final SingularAttribute labelAtt = mock(SingularAttribute.class);
when(labelAtt.getJavaField()).thenReturn(SubClass.class.getDeclaredField("label"));
when(labelAtt.getDeclaringType()).thenReturn(et);
when(et.getDeclaredAttribute("label")).thenReturn(labelAtt);
when(et.getJavaType()).thenReturn(SubClass.class);
final Identifier id = mock(Identifier.class);
when(id.getJavaField()).thenReturn(SuperClass.class.getDeclaredField("uri"));
when(et.getIdentifier()).thenReturn(id);
final MappedSuperclassType<SuperClass> superType = mock(MappedSuperclassType.class);
when(superType.getIdentifier()).thenReturn(id);
when(superType.getJavaType()).thenReturn(SuperClass.class);
when(et.getSupertype()).thenReturn((IdentifiableType) superType);
when(metamodel.getEntities()).thenReturn(Collections.singleton(et));
doReturn(new HashSet<>(Arrays.asList(et, superType))).when(metamodel).getManagedTypes();
assertThrows(StaticMetamodelInitializationException.class, () -> sut.initializeStaticMetamodel());
}
@MappedSuperclass
public static class SuperClass {
@Id
private URI uri;
}
@OWLClass(iri = Vocabulary.CLASS_BASE + "SubClass")
public static class SubClass extends SuperClass {
@OWLAnnotationProperty(iri = RDFS.LABEL)
private String label;
}
@StaticMetamodel(SubClass.class)
public static class SubClass_ {
public static volatile SingularAttribute<SubClass, String> label;
}
@Test
void initializeStaticMetamodelSkipsFieldsWhichAreNotPublicStaticVolatile() throws Exception {
final EntityType<SimpleClass> et = mock(EntityType.class);
final Identifier id = mock(Identifier.class);
when(id.getJavaField()).thenReturn(SimpleClass.class.getDeclaredField("uri"));
when(et.getIdentifier()).thenReturn(id);
when(et.getJavaType()).thenReturn(SimpleClass.class);
when(et.getDeclaredAttribute(anyString())).thenThrow(IllegalArgumentException.class);
when(metamodel.getEntities()).thenReturn(Collections.singleton(et));
sut.initializeStaticMetamodel();
assertEquals(id, SimpleClass_.uri);
assertNull(SimpleClass_.test);
}
@OWLClass(iri = Vocabulary.CLASS_BASE + "SimpleClass")
public static class SimpleClass {
@Id
private URI uri;
}
@StaticMetamodel(SimpleClass.class)
public static class SimpleClass_ {
public static volatile Identifier<SimpleClass, URI> uri;
private static String test;
}
}