-
Notifications
You must be signed in to change notification settings - Fork 14
/
OWL2JavaTransformerTest.java
204 lines (178 loc) · 8.82 KB
/
OWL2JavaTransformerTest.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
/**
* Copyright (C) 2016 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.owl2java;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLAxiom;
import org.semanticweb.owlapi.model.OWLDataFactory;
import uk.ac.manchester.cs.owl.owlapi.OWLDataFactoryImpl;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import static cz.cvut.kbss.jopa.owl2java.TestUtils.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public class OWL2JavaTransformerTest {
// Thing is always generated by OWL2Java
private static final List<String> KNOWN_CLASSES = Arrays
.asList("Agent", "Person", "Organization", "Answer", "Question", "Report", "Thing");
private String mappingFilePath;
private OWLDataFactory dataFactory;
private OWL2JavaTransformer transformer;
@Rule
public ExpectedException thrown = ExpectedException.none();
@Before
public void setUp() {
this.mappingFilePath = resolveMappingFilePath();
this.dataFactory = new OWLDataFactoryImpl();
this.transformer = new OWL2JavaTransformer();
}
private String resolveMappingFilePath() {
final File mf = new File(getClass().getClassLoader().getResource(MAPPING_FILE_NAME).getFile());
return mf.getAbsolutePath();
}
@Test
public void listContextsShowsContextsInICFile() {
transformer.setOntology(IC_ONTOLOGY_IRI, mappingFilePath, true);
final Collection<String> contexts = transformer.listContexts();
assertEquals(1, contexts.size());
assertEquals(CONTEXT, contexts.iterator().next());
}
@Test
public void transformGeneratesJavaClassesFromIntegrityConstraints() throws Exception {
final File targetDir = getTempDirectory();
assertEquals(0, targetDir.listFiles().length);
transformer.setOntology(IC_ONTOLOGY_IRI, mappingFilePath, true);
transformer.transform(CONTEXT, "", targetDir.getAbsolutePath(), true);
verifyGeneratedModel(targetDir);
}
@Test
public void transformGeneratesJavaClassesInPackage() throws Exception {
final String packageName = "cz.cvut.kbss.jopa.owl2java";
final File targetDir = getTempDirectory();
assertEquals(0, targetDir.listFiles().length);
transformer.setOntology(IC_ONTOLOGY_IRI, mappingFilePath, true);
transformer.transform(CONTEXT, packageName, targetDir.getAbsolutePath(), true);
verifyGeneratedTree(packageName, targetDir);
}
private void verifyGeneratedTree(String packageName, File parentDir) {
File currentDir = parentDir;
for (String p : packageName.split("\\.")) {
final List<String> files = Arrays.asList(currentDir.list());
assertTrue(files.contains(p));
currentDir = new File(currentDir.getAbsolutePath() + File.separator + p);
}
List<String> files = Arrays.asList(currentDir.list());
assertTrue(files.contains(VOCABULARY_FILE));
verifyGeneratedModel(currentDir);
}
private void verifyGeneratedModel(File currentDir) {
currentDir = new File(currentDir + File.separator + Constants.MODEL_PACKAGE);
final List<String> classNames = Arrays.asList(currentDir.list()).stream()
.map(fn -> fn.substring(0, fn.indexOf('.'))).collect(
Collectors.toList());
assertTrue(classNames.containsAll(KNOWN_CLASSES));
}
@Test
public void transformGeneratesVocabularyFile() throws Exception {
final File targetDir = getTempDirectory();
transformer.setOntology(IC_ONTOLOGY_IRI, mappingFilePath, true);
transformer.transform(CONTEXT, "", targetDir.getAbsolutePath(), true);
final List<String> fileNames = Arrays.asList(targetDir.list());
assertTrue(fileNames.contains(VOCABULARY_FILE));
}
@Test
public void transformGeneratesVocabularyFileForTheWholeFile() throws Exception {
final File targetDir = getTempDirectory();
transformer.setOntology(IC_ONTOLOGY_IRI, mappingFilePath, true);
transformer.transform(null, "", targetDir.getAbsolutePath(), true);
final List<String> fileNames = Arrays.asList(targetDir.list());
assertTrue(fileNames.contains(VOCABULARY_FILE));
}
@Test
public void transformThrowsIllegalArgumentForUnknownContext() throws Exception {
final String unknownContext = "someUnknownContext";
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Context " + unknownContext + " not found.");
final File targetDir = getTempDirectory();
transformer.setOntology(IC_ONTOLOGY_IRI, mappingFilePath, true);
transformer.transform(unknownContext, "", targetDir.getAbsolutePath(), true);
}
@Test
public void setUnknownOntologyIriThrowsIllegalArgumentException() throws Exception {
final String unknownOntoIri = "http://krizik.felk.cvut.cz/ontologies/an-unknown-ontology.owl";
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Unable to load ontology " + unknownOntoIri);
transformer.setOntology(unknownOntoIri, mappingFilePath, true);
}
@Test
public void setOntologyWithUnknownMappingFileThrowsIllegalArgument() throws Exception {
final String unknownMappingFile = "/tmp/unknown-mapping-file";
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Mapping file " + unknownMappingFile + " not found.");
transformer.setOntology(IC_ONTOLOGY_IRI, unknownMappingFile, true);
}
@Test
public void generateVocabularyGeneratesOnlyVocabularyFile() throws Exception {
final File targetDir = getTempDirectory();
transformer.setOntology(IC_ONTOLOGY_IRI, mappingFilePath, true);
transformer.generateVocabulary(CONTEXT, "", targetDir.getAbsolutePath(), false);
final List<String> fileNames = Arrays.asList(targetDir.list());
assertEquals(1, fileNames.size());
assertEquals(VOCABULARY_FILE, fileNames.get(0));
}
@Test
public void generateVocabularyGeneratesOnlyVocabularyFileForTheWholeFile() throws Exception {
final File targetDir = getTempDirectory();
transformer.setOntology(IC_ONTOLOGY_IRI, mappingFilePath, true);
transformer.generateVocabulary(null, "", targetDir.getAbsolutePath(), false);
final List<String> fileNames = Arrays.asList(targetDir.list());
assertEquals(1, fileNames.size());
assertEquals(VOCABULARY_FILE, fileNames.get(0));
}
@Test
public void generateVocabularyTransformsInvalidCharactersInIrisToValid() throws Exception {
final File targetDir = getTempDirectory();
transformer.setOntology(IC_ONTOLOGY_IRI, mappingFilePath, true);
// contains a ',', which will result in invalid Java identifier
final String invalidIri = "http://onto.fel.cvut.cz/ontologies/aviation-safety/accident,_incident_or_emergency";
final OWLAxiom axiom = dataFactory.getOWLDeclarationAxiom(dataFactory.getOWLClass(IRI.create(invalidIri)));
TestUtils.addAxiom(axiom, transformer);
transformer.generateVocabulary(null, "", targetDir.getAbsolutePath(), false);
final File vocabularyFile = targetDir.listFiles()[0];
final String fileContents = readFile(vocabularyFile);
assertFalse(fileContents.contains(invalidIri.substring(invalidIri.lastIndexOf('/') + 1) + " ="));
assertTrue(fileContents.contains(invalidIri.substring(invalidIri.lastIndexOf(',') + 1) + " ="));
}
private String readFile(File file) throws IOException {
final StringBuilder sb = new StringBuilder();
try (final BufferedReader in = new BufferedReader(new FileReader(file))) {
String line;
while ((line = in.readLine()) != null) {
sb.append(line).append('\n');
}
}
return sb.toString();
}
}