Child: [97ae4a] (diff)

Download this file

AlternativesBuilderTest.java    214 lines (181 with data), 13.0 kB

  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
/**
* Copyright (c) 2013/2014 Verein zur Foerderung der IT-Sicherheit in Oesterreich (SBA).
* The work has been developed in the TIMBUS Project and the above-mentioned are Members of the TIMBUS Consortium.
* TIMBUS is supported by the European Union under the 7th Framework Programme for research and technological
* development and demonstration activities (FP7/2007-2013) under grant agreement no. 269940.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at: http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including without
* limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTIBITLY, or FITNESS FOR A PARTICULAR
* PURPOSE. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise,
* unless required by applicable law or agreed to in writing, shall any Contributor be liable for damages, including
* any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this
* License or out of the use or inability to use the Work.
* See the License for the specific language governing permissions and limitation under the License.
*/
package org.sbaresearch.owl;
import junitparams.JUnitParamsRunner;
import junitparams.Parameters;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.semanticweb.owlapi.model.*;
import java.io.ByteArrayInputStream;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertTrue;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.junit.Assert.assertThat;
@RunWith(JUnitParamsRunner.class)
public class AlternativesBuilderTest {
@Test
@Parameters({"IndivD, http://localhost/test_ab3.owl#IndivD"})
public void testFindIndividual_indivInBaseOntology_shouldReturnIRI(String indivName, String expectedIRI) throws OWLOntologyCreationException, URISyntaxException, OwlElementNotFoundException {
AlternativesBuilder alternativesBuilder = createDefaultAlternativesBuilder();
IRI iri = alternativesBuilder.findIndividualByFragmentAndLabel(indivName);
assertThat(iri.toString(), equalTo(expectedIRI));
}
@Test
@Parameters({
"IndivA, http://localhost/test_ab1.owl#IndivA",
"IndivB, http://localhost/test_ab2.owl#IndivB"
})
public void testFindIndividual_indivInImportedOntology_shouldReturnIRI(String indivName, String expectedIRI) throws OWLOntologyCreationException, URISyntaxException, OwlElementNotFoundException {
AlternativesBuilder alternativesBuilder = createDefaultAlternativesBuilder();
IRI iri = alternativesBuilder.findIndividualByFragmentAndLabel(indivName);
assertThat(iri.toString(), equalTo(expectedIRI));
}
@Test
@Parameters({
"IndivE, http://localhost/test_ab4_usingLabel.owl#bf342340921",
"IndivA, http://localhost/test_ab1_usingLabel.owl#ab1232323"
})
public void testFindIndividual_byLabel_shouldReturnIRI(String indivName, String expectedIRI) throws OWLOntologyCreationException, URISyntaxException, OwlElementNotFoundException {
AlternativesBuilder alternativesBuilder = createAlternativesBuilderModelUsingLabel();
IRI actualIRI = alternativesBuilder.findIndividualByFragmentAndLabel(indivName);
assertThat(actualIRI.toString(), equalTo(expectedIRI));
}
@Test
public void testCopyOntology_validSource_shouldContainSameNumberOfAxioms() throws OWLOntologyCreationException, URISyntaxException {
AlternativesBuilder alternativesBuilder = createDefaultAlternativesBuilder();
OWLOntology actual = alternativesBuilder.copyOntology(IRI.create("http://localhost/abtest1"));
assertThat(actual.getAxiomCount(), is(alternativesBuilder.getModel().getOntology().getAxiomCount()));
}
@Test
public void testCopyOntology_validSource_shouldHaveNewIRI() throws OWLOntologyCreationException, URISyntaxException {
AlternativesBuilder alternativesBuilder = createDefaultAlternativesBuilder();
IRI targetIRI = IRI.create("http://localhost/abtest1");
OWLOntology actual = alternativesBuilder.copyOntology(targetIRI);
assertThat(actual.getOntologyID().getOntologyIRI(), equalTo(targetIRI));
}
@Test
public void testCopyOntology_validSourceExistingManager_shouldNotCreateNewManager() throws OWLOntologyCreationException, URISyntaxException {
AlternativesBuilder alternativesBuilder = createDefaultAlternativesBuilder();
IRI targetIRI = IRI.create("http://localhost/abtest1");
alternativesBuilder.copyOntology(targetIRI, alternativesBuilder.getModel().getOntology().getOWLOntologyManager());
assertTrue(alternativesBuilder.getModel().getOntology().containsIndividualInSignature(IRI.create("http://localhost/abtest1#IndivD")));
}
@Test
public void testCopyOntology_validSource_shouldCreateNewManager() throws OWLOntologyCreationException, URISyntaxException {
AlternativesBuilder alternativesBuilder = createDefaultAlternativesBuilder();
IRI targetIRI = IRI.create("http://localhost/abtest1");
alternativesBuilder.copyOntology(targetIRI);
assertFalse(alternativesBuilder.getModel().getOntology().containsIndividualInSignature(IRI.create("http://localhost/abtest1#IndivD")));
}
@Test
public void testCopyOntology_validSource_shouldRenameIndivs() throws OWLOntologyCreationException, URISyntaxException, OwlElementNotFoundException {
AlternativesBuilder alternativesBuilder = createDefaultAlternativesBuilder();
final OwlApiFacade newOntology = new OwlApiFacade(alternativesBuilder.copyOntology(IRI.create("http://localhost/abtest1.owl")));
Set<OWLNamedIndividual> actualIndivs = newOntology.getOntology().getIndividualsInSignature();
final Set<OWLNamedIndividual> expectedIndivs = new HashSet<OWLNamedIndividual>() {{
add(newOntology.getDataFactory().getOWLNamedIndividual(IRI.create("http://localhost/abtest1.owl#IndivD")));
}};
assertThat(expectedIndivs, containsInAnyOrder(actualIndivs.toArray()));
}
@Test
public void testCopyOntology_validSource_shouldRenameClasses() throws OWLOntologyCreationException, URISyntaxException, OwlElementNotFoundException {
AlternativesBuilder alternativesBuilder = createDefaultAlternativesBuilder();
final OwlApiFacade newOntology = new OwlApiFacade(alternativesBuilder.copyOntology(IRI.create("http://localhost/abtest1.owl")));
Set<OWLClass> actualClasses = newOntology.getOntology().getClassesInSignature();
final Set<OWLClass> expectedClasses = new HashSet<OWLClass>() {{
add(newOntology.getDataFactory().getOWLClass(IRI.create("http://localhost/abtest1.owl#ClassD")));
}};
assertThat(expectedClasses, containsInAnyOrder(actualClasses.toArray()));
}
@Test
public void testCopyOntology_validSourceWithoutImports_shouldContainNoImports() throws OWLOntologyCreationException, URISyntaxException, OwlElementNotFoundException {
OwlApiFacade model = TestUtils.createDefaultOwlApiFacade();
model.load(TestUtils.getUri("/test_ab1.owl"));
AlternativesBuilder alternativesBuilder = new AlternativesBuilder(model);
OwlApiFacade newOntology = new OwlApiFacade(alternativesBuilder.copyOntology(IRI.create("http://localhost/abtest1.owl")));
Set<IRI> actualImports = newOntology.getOntology().getDirectImportsDocuments();
assertThat(actualImports.size(), is(0));
}
@Test
public void testCopyOntology_validSourceWithImports_shouldContainAllImports() throws OWLOntologyCreationException, URISyntaxException, OwlElementNotFoundException {
AlternativesBuilder alternativesBuilder = createDefaultAlternativesBuilder();
Set<IRI> expectedImports = alternativesBuilder.getModel().getOntology().getDirectImportsDocuments();
OwlApiFacade newOntology = new OwlApiFacade(alternativesBuilder.copyOntology(IRI.create("http://localhost/abtest1.owl")));
Set<IRI> actualImports = newOntology.getOntology().getDirectImportsDocuments();
assertThat(actualImports, containsInAnyOrder(expectedImports.toArray()));
}
@Test
public void testRenameIndividualByFragment_validFragment_shouldRenameIndividual() throws OWLOntologyCreationException, URISyntaxException, OwlElementNotFoundException {
AlternativesBuilder alternativesBuilder = createDefaultAlternativesBuilder();
alternativesBuilder.renameIndividualByIRI(alternativesBuilder.getModel().createIri("IndivD"), "xIndivD");
assertThat(alternativesBuilder.getModel().getIndividualByUnqualifiedName("xIndivD", false).getIRI().toString(), equalTo("http://localhost/test_ab3.owl#xIndivD"));
}
@Test (expected = OwlElementNotFoundException.class)
public void testRenameIndividualByFragment_inexistingFragment_shouldThrowException() throws OWLOntologyCreationException, URISyntaxException, OwlElementNotFoundException {
AlternativesBuilder alternativesBuilder = createDefaultAlternativesBuilder();
alternativesBuilder.renameIndividualByIRI(alternativesBuilder.getModel().createIri("yIndivD"), "xIndivD");
}
@Test
public void testRenameIndividualByFragment_indivHasLabel_shouldRenameLabelAlso() throws OWLOntologyCreationException, URISyntaxException, OwlElementNotFoundException {
AlternativesBuilder alternativesBuilder = createAlternativesBuilderModelUsingLabel();
alternativesBuilder.renameIndividualByIRI(alternativesBuilder.getModel().createIri("bf342340921"), "xIndivD");
assertThat(alternativesBuilder.getModel().getIndividualByLabel("xIndivD").getIRI().toString(), equalTo("http://localhost/test_ab4_usingLabel.owl#xIndivD"));
}
@Test
public void testRenameIndividualByLabel_validLabel_shouldRenameLabelButNotTheFragment() throws OWLOntologyCreationException, URISyntaxException, OwlElementNotFoundException {
AlternativesBuilder alternativesBuilder = createAlternativesBuilderModelUsingLabel();
alternativesBuilder.renameIndividualByLabel("IndivE", "xIndivE");
assertThat(alternativesBuilder.getModel().getIndividualByLabel("xIndivE").getIRI().toString(), equalTo("http://localhost/test_ab4_usingLabel.owl#bf342340921"));
}
@Test (expected = OwlElementNotFoundException.class)
public void testRenameIndividualByLabel_inexistingLabel_shouldThrowException() throws OWLOntologyCreationException, URISyntaxException, OwlElementNotFoundException {
AlternativesBuilder alternativesBuilder = createDefaultAlternativesBuilder();
alternativesBuilder.renameIndividualByLabel("yIndivD", "xIndivD");
}
@Test
public void testToXmlString_validOntology_shouldReturnXmlString() throws OWLOntologyCreationException, URISyntaxException {
OwlApiFacade source = TestUtils.createDefaultOwlApiFacade();
source.load(TestUtils.getUri("/test_ab1.owl"));
String actual = AlternativesBuilder.toXMLString(source.getOntology());
OwlApiFacade ontology = TestUtils.createDefaultOwlApiFacade();
ontology.load(new ByteArrayInputStream(actual.getBytes()));
assertThat(ontology.getOntology().getOntologyID().getOntologyIRI().toString(), equalTo("http://localhost/test_ab1.owl"));
}
private AlternativesBuilder createDefaultAlternativesBuilder() throws URISyntaxException, OWLOntologyCreationException {
OwlApiFacade model = TestUtils.createDefaultOwlApiFacade();
model.load(TestUtils.getUri("/test_ab3.owl"), new HashMap<String, String>() {{
put("http://localhost/test_ab1.owl", TestUtils.getUri("/test_ab1.owl"));
put("http://localhost/test_ab2.owl", TestUtils.getUri("/test_ab2.owl"));
}});
return new AlternativesBuilder(model);
}
private AlternativesBuilder createAlternativesBuilderModelUsingLabel() throws URISyntaxException, OWLOntologyCreationException {
OwlApiFacade model = TestUtils.createDefaultOwlApiFacade();
model.load(TestUtils.getUri("/test_ab4_usingLabel.owl"), new HashMap<String, String>() {{
put("http://localhost/test_ab1_usingLabel.owl", TestUtils.getUri("/test_ab1_usingLabel.owl"));
put("http://localhost/test_ab2.owl", TestUtils.getUri("/test_ab2.owl"));
}});
return new AlternativesBuilder(model);
}
}