-
Notifications
You must be signed in to change notification settings - Fork 14
/
OWL2JavaTest.java
170 lines (153 loc) · 7.1 KB
/
OWL2JavaTest.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
/**
* Copyright (C) 2016 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 org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.PrintStream;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static cz.cvut.kbss.jopa.owl2java.TestUtils.MAPPING_FILE_NAME;
import static cz.cvut.kbss.jopa.owl2java.TestUtils.VOCABULARY_FILE;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class OWL2JavaTest {
private String mappingFilePath;
private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
@Rule
public ExpectedException thrown = ExpectedException.none();
private File targetDir;
@Before
public void setUp() {
this.mappingFilePath = resolveMappingFilePath();
System.setOut(new PrintStream(outContent));
System.setErr(new PrintStream(errContent));
}
private String resolveMappingFilePath() {
final File mf = new File(getClass().getClassLoader().getResource(MAPPING_FILE_NAME).getFile());
return mf.getAbsolutePath();
}
@After
public void tearDown() throws Exception {
System.setOut(null);
System.setErr(null);
if (targetDir != null) {
TestUtils.recursivelyDeleteDirectory(targetDir);
}
}
@Test
public void vocabularyCommandGeneratesVocabularyFromICs() throws Exception {
this.targetDir = TestUtils.getTempDirectory();
final String packageName = "";
final String[] args = new String[]{"vocabulary", TestUtils.IC_ONTOLOGY_IRI,
"-m",
mappingFilePath,
"-p",
packageName,
"-c",
TestUtils.CONTEXT,
"-d",
targetDir.getAbsolutePath()};
OWL2Java.main(args);
final List<String> fileNames = Arrays.asList(targetDir.list());
assertEquals(1, fileNames.size());
assertEquals(VOCABULARY_FILE, fileNames.get(0));
}
@Test
public void transformGeneratesClassesAndVocabularyFromICs() throws Exception {
this.targetDir = TestUtils.getTempDirectory();
final String packageName = "cz.cvut.kbss.jopa.owl2java";
final String[] args = new String[]{"transform", TestUtils.IC_ONTOLOGY_IRI,
"-m",
mappingFilePath,
"-p",
packageName,
"-c",
TestUtils.CONTEXT,
"-d",
targetDir.getAbsolutePath()};
OWL2Java.main(args);
assertTrue(targetDir.list().length > 0);
}
@Test
public void transformPrintsErrorAndQuitsWhenContextIsMissing() throws Exception {
this.targetDir = TestUtils.getTempDirectory();
final String packageName = "cz.cvut.kbss.jopa.owl2java";
final String[] args = new String[]{"transform", TestUtils.IC_ONTOLOGY_IRI,
"-m",
mappingFilePath,
"-p",
packageName,
"-d",
targetDir.getAbsolutePath()};
OWL2Java.main(args);
assertEquals(0, targetDir.list().length);
verifyErrorContent("The parameter '-[a-z]' is obligatory");
}
private void verifyErrorContent(String regexp) {
final String err = errContent.toString();
final Pattern p = Pattern.compile(regexp);
final Matcher m = p.matcher(err);
assertTrue(m.find());
}
@Test
public void transformPrintsErrorAndQuitesWhenOntologyIriIsMissing() throws Exception {
this.targetDir = TestUtils.getTempDirectory();
final String packageName = "cz.cvut.kbss.jopa.owl2java";
final String[] args = new String[]{"transform",
"-m",
mappingFilePath,
"-p",
packageName,
"-c",
TestUtils.CONTEXT,
"-d",
targetDir.getAbsolutePath()};
OWL2Java.main(args);
assertEquals(0, targetDir.list().length);
verifyErrorContent("Exactly one ontology IRI has to be specified, got 0");
}
@Test
public void transformUsesCustomJavaClassNameSpecifiedByCustomAnnotation() throws Exception {
this.targetDir = TestUtils.getTempDirectory();
final String packageName = "";
final String classNameAnnotation = "http://krizik.felk.cvut.cz/ontologies/2009/ic.owl#JavaClassName";
final String className = "Company.java";
final String[] args = new String[]{"transform", TestUtils.IC_ONTOLOGY_IRI,
"-m",
mappingFilePath,
"-p",
packageName,
"-c",
TestUtils.CONTEXT,
"-jca",
classNameAnnotation,
"-d",
targetDir.getAbsolutePath()};
OWL2Java.main(args);
final File modelDir = new File(targetDir.getAbsolutePath() + File.separator + "model");
final Set<String> fileNames = new HashSet<>(Arrays.asList(modelDir.list()));
assertTrue(fileNames.contains(className));
}
}