-
Notifications
You must be signed in to change notification settings - Fork 14
/
OWL2JavaTransformerTest.java
120 lines (100 loc) · 4.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
package cz.cvut.kbss.jopa.owl2java;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class OWL2JavaTransformerTest {
private static final String MAPPING_FILE_NAME = "mapping";
private static final String IC_ONTOLOGY_IRI = "http://krizik.felk.cvut.cz/ontologies/owl2java-ics.owl";
private static final String CONTEXT = "owl2java-ic";
private static final String VOCABULARY_FILE = "Vocabulary.java";
// 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 OWL2JavaTransformer transformer;
@Rule
public ExpectedException thrown = ExpectedException.none();
@Before
public void setUp() {
this.mappingFilePath = resolveMappingFilePath();
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);
final File generatedModel = new File(targetDir.getAbsolutePath() + File.separator + "model");
assertTrue(generatedModel.listFiles().length > 0);
for (String fileName : generatedModel.list()) {
final String className = fileName.substring(0, fileName.indexOf('.')); // Strip the suffix
assertTrue(KNOWN_CLASSES.contains(className));
}
}
private File getTempDirectory() throws IOException {
final File targetDir = Files.createTempDirectory("owl2java-test").toFile();
targetDir.deleteOnExit();
return targetDir;
}
@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 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));
}
}