-
Notifications
You must be signed in to change notification settings - Fork 14
/
StorageTest.java
102 lines (91 loc) · 4.42 KB
/
StorageTest.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
/**
* Copyright (C) 2019 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.ontodriver.jena.connector;
import cz.cvut.kbss.ontodriver.config.DriverConfiguration;
import cz.cvut.kbss.ontodriver.exception.OntoDriverInitializationException;
import cz.cvut.kbss.ontodriver.jena.config.JenaConfigParam;
import cz.cvut.kbss.ontodriver.jena.config.JenaOntoDriverProperties;
import cz.cvut.kbss.ontodriver.jena.environment.Generator;
import org.apache.jena.query.DatasetFactory;
import org.apache.jena.rdf.model.ResourceFactory;
import org.apache.jena.vocabulary.RDF;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import java.io.File;
import java.nio.file.Files;
import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.*;
public class StorageTest extends StorageTestUtil {
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void createInitializesMemoryStorageForMemoryConfiguration() {
final DriverConfiguration config = createConfiguration("test:uri");
config.setProperty(JenaConfigParam.STORAGE_TYPE, JenaOntoDriverProperties.IN_MEMORY);
final Storage result = Storage.create(config);
assertNotNull(result);
assertTrue(result instanceof MemoryStorage);
assertNotNull(result.getDataset());
}
@Test
public void createInitializesFileStorageForFileConfiguration() throws Exception {
final File file = Files.createTempFile("jena-onto", ".ttl").toFile();
file.deleteOnExit();
final DriverConfiguration config = createConfiguration(file.getAbsolutePath());
config.setProperty(JenaConfigParam.STORAGE_TYPE, JenaOntoDriverProperties.FILE);
final Storage result = Storage.create(config);
assertNotNull(result);
assertTrue(result instanceof FileStorage);
}
@Test
public void createThrowsInitializationExceptionForUnknownStorageType() {
thrown.expect(OntoDriverInitializationException.class);
thrown.expectMessage(containsString("Unsupported storage type"));
final DriverConfiguration config = createConfiguration("test:uri");
config.setProperty(JenaConfigParam.STORAGE_TYPE, "dontKnowThisOne");
Storage.create(config);
}
@Test
public void createCreatesInMemoryByDefault() {
final DriverConfiguration config = createConfiguration("test:uri");
final Storage result = Storage.create(config);
assertNotNull(result);
assertTrue(result instanceof MemoryStorage);
}
@Test
public void getDefaultGraphReturnsUnionModelWhenConfiguredTo() {
final DriverConfiguration config = createConfiguration("test:uri");
config.setProperty(JenaConfigParam.TREAT_DEFAULT_GRAPH_AS_UNION, Boolean.toString(true));
final Storage result = Storage.create(config);
final String ctx = "test";
result.dataset.getNamedModel(ctx).add(ResourceFactory
.createStatement(ResourceFactory.createResource(), RDF.type, ResourceFactory.createResource(
Generator.generateUri().toString())));
assertFalse(result.getNamedGraph(ctx).isEmpty());
assertFalse(result.getDefaultGraph().isEmpty());
}
@Test
public void setDatasetThrowsUnsupportedOperationException() throws Exception {
final File file = Files.createTempFile("jena-onto", ".ttl").toFile();
file.deleteOnExit();
final DriverConfiguration config = createConfiguration(file.getAbsolutePath());
config.setProperty(JenaConfigParam.STORAGE_TYPE, JenaOntoDriverProperties.FILE);
final Storage storage = Storage.create(config);
assertNotNull(storage);
thrown.expect(UnsupportedOperationException.class);
storage.setDataset(DatasetFactory.create());
}
}