-
Notifications
You must be signed in to change notification settings - Fork 14
/
JavaTransformerTest.java
286 lines (262 loc) · 15.3 KB
/
JavaTransformerTest.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
279
280
281
282
283
284
285
286
/**
* Copyright (C) 2023 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.owl2java;
import com.sun.codemodel.JDefinedClass;
import com.sun.codemodel.JFieldVar;
import com.sun.codemodel.JType;
import cz.cvut.kbss.jopa.model.MultilingualString;
import cz.cvut.kbss.jopa.owl2java.config.TransformationConfiguration;
import cz.cvut.kbss.jopa.vocabulary.RDFS;
import cz.cvut.kbss.jopa.vocabulary.SKOS;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLClass;
import org.semanticweb.owlapi.model.OWLDataFactory;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyManager;
import org.semanticweb.owlapi.vocab.OWL2Datatype;
import uk.ac.manchester.cs.owl.owlapi.OWLDataFactoryImpl;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasKey;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
class JavaTransformerTest {
private static final String PREFIX = "http://onto.fel.cvut.cz/ontologies/jopa/";
private static final String ONTOLOGY_IRI = "http://onto.fel.cvut.cz/ontologies/owl2java/java-transformer-test";
private OWLOntology ontology;
private OWLDataFactory dataFactory;
private JavaTransformer sut;
@BeforeEach
void setUp() throws Exception {
final OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
this.ontology = manager.createOntology(IRI.create(ONTOLOGY_IRI));
this.dataFactory = new OWLDataFactoryImpl();
this.sut = new JavaTransformer(TransformationConfiguration.builder().packageName("").build());
}
@Test
void generatesValidJavaIdentifiersForIrisWithNonAsciiCharacters() {
final IRI iri = IRI.create("http://onto.fel.cvut.cz/ontologies/slovník/agendový/popis-dat/pojem/navržený-term");
ontology.add(dataFactory.getOWLDeclarationAxiom(dataFactory.getOWLClass(iri)));
final ContextDefinition context = new ContextDefinition();
context.add(dataFactory.getOWLClass(iri));
final ObjectModel result = sut.generateVocabulary(ontology, context);
final JDefinedClass vocabClass = result.getCodeModel()._getClass(Constants.VOCABULARY_CLASS);
assertNotNull(vocabClass);
final Map<String, JFieldVar> fields = vocabClass.fields();
assertTrue(fields.keySet().stream().anyMatch(n -> n.endsWith("navrzeny_term")));
}
@Test
void generateModelCreatesToStringMethodForGeneratedModelClasses() {
final String className = "TestClass";
final IRI iri = IRI.create(PREFIX + className);
ontology.add(dataFactory.getOWLDeclarationAxiom(dataFactory.getOWLClass(iri)));
final ContextDefinition context = new ContextDefinition();
context.add(dataFactory.getOWLClass(iri));
context.parse();
final ObjectModel result = sut.generateModel(ontology, context);
final JDefinedClass resultClass =
result.getCodeModel()._getClass(Constants.MODEL_PACKAGE + Constants.PACKAGE_SEPARATOR + className);
assertNotNull(resultClass);
assertNotNull(resultClass.getMethod("toString", new JType[0]));
}
@Test
void shouldGenerateIdAndTypesFields() {
final String className = "TestClass";
final IRI iri = IRI.create(PREFIX + className);
ontology.add(dataFactory.getOWLDeclarationAxiom(dataFactory.getOWLClass(iri)));
final ContextDefinition context = new ContextDefinition();
context.add(dataFactory.getOWLClass(iri));
context.parse();
final ObjectModel result = sut.generateModel(ontology, context);
final JDefinedClass resultClass =
result.getCodeModel()._getClass(Constants.MODEL_PACKAGE + Constants.PACKAGE_SEPARATOR + className);
//Check the presence of fields with @Id and @Type annotation
checkHasFieldWithName(resultClass, "id");
checkHasFieldWithName(resultClass, "types");
}
private void checkHasFieldWithName(final JDefinedClass cls, final String name) {
for (final JFieldVar f : cls.fields().values()) {if (f.name().equals(name)) {return;}}
fail();
}
@Test
void shouldNotGenerateIdAndTypesFieldsInSubclasses() {
final String className1 = "TestClass1";
final IRI iri1 = IRI.create(PREFIX + className1);
final String className2 = "TestClass2";
final IRI iri2 = IRI.create(PREFIX + className2);
ontology.add(dataFactory.getOWLDeclarationAxiom(dataFactory.getOWLClass(iri1)));
ontology.add(dataFactory.getOWLDeclarationAxiom(dataFactory.getOWLClass(iri2)));
final ContextDefinition context = new ContextDefinition();
final OWLClass owlClass1 = dataFactory.getOWLClass(iri1);
final OWLClass owlClass2 = dataFactory.getOWLClass(iri2);
context.add(owlClass1);
context.add(owlClass2);
context.addAxiom(dataFactory.getOWLSubClassOfAxiom(owlClass2, owlClass1));
context.parse();
final ObjectModel result = sut.generateModel(ontology, context);
final JDefinedClass resultClass1 =
result.getCodeModel()._getClass(Constants.MODEL_PACKAGE + Constants.PACKAGE_SEPARATOR + className1);
//Check the presence of fields with @Id and @Type annotation
checkHasFieldWithName(resultClass1, "id");
checkHasFieldWithName(resultClass1, "types");
final JDefinedClass resultClass2 =
result.getCodeModel()._getClass(Constants.MODEL_PACKAGE + Constants.PACKAGE_SEPARATOR + className2);
// Check the absence of fields with @Id and @Type annotation
checkHasNoFieldWithName(resultClass2, "id");
checkHasNoFieldWithName(resultClass2, "types");
}
private void checkHasNoFieldWithName(final JDefinedClass cls, final String name) {
for (final JFieldVar f : cls.fields().values()) {assertNotEquals(f.name(), name);}
}
@Test
void generateModelGeneratesFieldOfTypeMultilingualStringForLangStringRangeWhenConfiguredToPreferMultilingualStrings() {
final String className = "TestClass1";
final String fieldName = "multilingualString";
final ContextDefinition context = generateAxiomsForLangStrings(className, fieldName);
final ObjectModel result = sut.generateModel(ontology, context);
final JDefinedClass resultClass =
result.getCodeModel()._getClass(Constants.MODEL_PACKAGE + Constants.PACKAGE_SEPARATOR + className);
assertNotNull(resultClass);
final JFieldVar resultField = resultClass.fields().get(fieldName);
assertNotNull(resultField);
assertEquals(MultilingualString.class.getCanonicalName(), resultField.type().fullName());
}
private ContextDefinition generateAxiomsForLangStrings(String className, String fieldName) {
final IRI classIri = IRI.create(PREFIX + className);
ontology.add(dataFactory.getOWLDeclarationAxiom(dataFactory.getOWLClass(classIri)));
final IRI propertyIri = IRI.create(PREFIX + fieldName);
ontology.add(dataFactory.getOWLDeclarationAxiom(dataFactory.getOWLDataProperty(propertyIri)));
ontology.add(dataFactory.getOWLDataPropertyRangeAxiom(dataFactory.getOWLDataProperty(propertyIri),
OWL2Datatype.RDF_LANG_STRING));
final ContextDefinition context = new ContextDefinition();
context.add(dataFactory.getOWLClass(classIri));
context.add(dataFactory.getOWLDataProperty(propertyIri));
context.addAxiom(dataFactory.getOWLSubClassOfAxiom(dataFactory.getOWLClass(classIri), dataFactory
.getOWLDataAllValuesFrom(dataFactory.getOWLDataProperty(propertyIri), OWL2Datatype.RDF_LANG_STRING)));
context.addAxiom(dataFactory.getOWLSubClassOfAxiom(dataFactory.getOWLClass(classIri), dataFactory
.getOWLDataMaxCardinality(1, dataFactory.getOWLDataProperty(propertyIri))));
context.parse();
return context;
}
@Test
void generateModelGeneratesFieldOfTypeStringForLangStringRangeWhenConfiguredNotToPreferMultilingualStrings() {
this.sut = new JavaTransformer(TransformationConfiguration.builder().preferMultilingualStrings(false)
.packageName("").build());
final String className = "TestClass";
final String fieldName = "multilingualString";
final ContextDefinition context = generateAxiomsForLangStrings(className, fieldName);
final ObjectModel result = sut.generateModel(ontology, context);
final JDefinedClass resultClass =
result.getCodeModel()._getClass(Constants.MODEL_PACKAGE + Constants.PACKAGE_SEPARATOR + className);
assertNotNull(resultClass);
final JFieldVar resultField = resultClass.fields().get(fieldName);
assertNotNull(resultField);
assertEquals(String.class.getCanonicalName(), resultField.type().fullName());
}
@Test
void generateModelGeneratesJavadocWithCommentInDefaultLanguage() {
final String className = "TestClass";
final IRI iri = IRI.create(PREFIX + className);
final OWLClass owlClass = dataFactory.getOWLClass(iri);
ontology.add(dataFactory.getOWLDeclarationAxiom(owlClass));
final String expectedComment = "Comment in default language";
ontology.add(dataFactory.getOWLAnnotationAssertionAxiom(dataFactory.getOWLAnnotationProperty(RDFS.COMMENT), owlClass.getIRI(), dataFactory.getOWLLiteral("Český komentář", "cs")));
ontology.add(dataFactory.getOWLAnnotationAssertionAxiom(dataFactory.getOWLAnnotationProperty(RDFS.COMMENT), owlClass.getIRI(), dataFactory.getOWLLiteral(expectedComment, Constants.LANGUAGE)));
final ContextDefinition context = new ContextDefinition();
context.add(dataFactory.getOWLClass(iri));
context.parse();
final ObjectModel result = sut.generateModel(ontology, context);
final JDefinedClass resultClass =
result.getCodeModel()._getClass(Constants.MODEL_PACKAGE + Constants.PACKAGE_SEPARATOR + className);
assertThat(resultClass.javadoc().toString(), containsString(expectedComment));
}
@Test
void generateModelGeneratesJavadocWithCommentUsingLanguageLessAnnotationValue() {
final String className = "TestClass";
final IRI iri = IRI.create(PREFIX + className);
final OWLClass owlClass = dataFactory.getOWLClass(iri);
ontology.add(dataFactory.getOWLDeclarationAxiom(owlClass));
final String expectedComment = "Comment in default language";
ontology.add(dataFactory.getOWLAnnotationAssertionAxiom(dataFactory.getOWLAnnotationProperty(RDFS.COMMENT), owlClass.getIRI(), dataFactory.getOWLLiteral(expectedComment)));
final ContextDefinition context = new ContextDefinition();
context.add(dataFactory.getOWLClass(iri));
context.parse();
final ObjectModel result = sut.generateModel(ontology, context);
final JDefinedClass resultClass =
result.getCodeModel()._getClass(Constants.MODEL_PACKAGE + Constants.PACKAGE_SEPARATOR + className);
assertThat(resultClass.javadoc().toString(), containsString(expectedComment));
}
@Test
void generateModelDoesNotGenerateLabelAndDescriptionFieldsWhenDisabled() {
this.sut = new JavaTransformer(TransformationConfiguration.builder().packageName("")
.generateAnnotationFields(false).build());
final String className = "TestClass";
final IRI iri = IRI.create(PREFIX + className);
final OWLClass owlClass = dataFactory.getOWLClass(iri);
ontology.add(dataFactory.getOWLDeclarationAxiom(owlClass));
final ContextDefinition context = new ContextDefinition();
context.add(dataFactory.getOWLClass(iri));
context.parse();
final ObjectModel result = sut.generateModel(ontology, context);
final JDefinedClass resultClass =
result.getCodeModel()._getClass(Constants.MODEL_PACKAGE + Constants.PACKAGE_SEPARATOR + className);
final Map<String, JFieldVar> fields = resultClass.fields();
assertThat(fields, not(hasKey(Constants.LABEL_FIELD_NAME)));
assertThat(fields, not(hasKey(Constants.DESCRIPTION_FIELD_NAME)));
}
@Test
void generateModelDoesNotGenerateThingEntityClassWhenDisabled() {
this.sut = new JavaTransformer(TransformationConfiguration.builder().packageName("").generateThing(false)
.build());
final ContextDefinition context = new ContextDefinition();
context.parse();
final ObjectModel result = sut.generateModel(ontology, context);
final JDefinedClass resultClass =
result.getCodeModel()._getClass(Constants.MODEL_PACKAGE + Constants.PACKAGE_SEPARATOR + "Thing");
assertNull(resultClass);
}
@Test
void generateModelDisambiguateClassesWithIdenticalJavaName() {
this.sut = new JavaTransformer(TransformationConfiguration.builder().packageName("").generateThing(false)
.build());
final String className = "Concept";
final IRI iriOne = IRI.create(PREFIX + className);
final IRI iriTwo = IRI.create(SKOS.CONCEPT);
ontology.add(dataFactory.getOWLDeclarationAxiom(dataFactory.getOWLClass(iriOne)));
ontology.add(dataFactory.getOWLDeclarationAxiom(dataFactory.getOWLClass(iriTwo)));
final ContextDefinition context = new ContextDefinition();
context.add(dataFactory.getOWLClass(iriOne));
context.add(dataFactory.getOWLClass(iriTwo));
context.parse();
final ObjectModel result = sut.generateModel(ontology, context);
final Iterator<JDefinedClass> classIterator = result.getCodeModel()._package(Constants.MODEL_PACKAGE).classes();
final List<JDefinedClass> classes = new ArrayList<>();
while (classIterator.hasNext()) {
classes.add(classIterator.next());
}
assertEquals(2, classes.size());
assertEquals(2, classes.stream().filter(cls -> cls.name().startsWith(className)).count());
}
}