Skip to content

Commit e84e696

Browse files
committed
Added unit tests
1 parent 7988775 commit e84e696

File tree

3 files changed

+113419
-24
lines changed

3 files changed

+113419
-24
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package test.java.org.linkeddatafragments.datasource;
7+
8+
import com.google.gson.JsonObject;
9+
10+
import com.hp.hpl.jena.rdf.model.Model;
11+
import com.hp.hpl.jena.rdf.model.ModelFactory;
12+
import com.hp.hpl.jena.rdf.model.Property;
13+
import com.hp.hpl.jena.rdf.model.Resource;
14+
15+
import java.io.File;
16+
import java.io.InputStream;
17+
import java.nio.file.Files;
18+
19+
import org.junit.After;
20+
import org.junit.AfterClass;
21+
import org.junit.Assert;
22+
import org.junit.Before;
23+
import org.junit.BeforeClass;
24+
import org.junit.Test;
25+
26+
import org.linkeddatafragments.datasource.DataSourceFactory;
27+
import org.linkeddatafragments.datasource.IDataSource;
28+
import org.linkeddatafragments.datasource.TriplePatternFragment;
29+
30+
import org.rdfhdt.hdt.enums.RDFNotation;
31+
import org.rdfhdt.hdt.hdt.HDT;
32+
import org.rdfhdt.hdt.hdt.HDTManager;
33+
import org.rdfhdt.hdt.options.HDTSpecification;
34+
35+
/**
36+
*
37+
* @author Bart Hanssens <[email protected]>
38+
*/
39+
public class HdtDataSourceTest {
40+
private static IDataSource hdt;
41+
private static File hdtfile;
42+
43+
@BeforeClass
44+
public static void setUpClass() throws Exception {
45+
// HDT does not seem to support an InputReader, so write to temp file
46+
InputStream in = ClassLoader.getSystemResourceAsStream("demo.nt");
47+
String tmpdir = System.getProperty("java.io.tmpdir");
48+
File temp = new File(tmpdir, "ldf-jena-test-hdt.ttl");
49+
Files.copy(in, temp.toPath());
50+
51+
HDT mgr = HDTManager.generateHDT(temp.getAbsolutePath(), null,
52+
RDFNotation.NTRIPLES, new HDTSpecification(), null);
53+
hdtfile = new File(tmpdir, "ldf-hdt-test");
54+
mgr.saveToHDT(hdtfile.getAbsolutePath(), null);
55+
56+
temp.getAbsoluteFile().delete();
57+
58+
// Everything is in place, now create the LDF datasource
59+
60+
JsonObject config = new JsonObject();
61+
config.addProperty("title", "hdt test");
62+
config.addProperty("description", "hdt test");
63+
config.addProperty("type", DataSourceFactory.HDT);
64+
65+
JsonObject settings = new JsonObject();
66+
settings.addProperty("file", hdtfile.getAbsolutePath());
67+
config.add("settings", settings);
68+
69+
hdt = DataSourceFactory.create(config);
70+
}
71+
72+
@AfterClass
73+
public static void tearDownClass() throws Exception {
74+
if (hdtfile != null) {
75+
hdtfile.delete();
76+
}
77+
}
78+
79+
@Before
80+
public void setUp() throws Exception {
81+
82+
}
83+
84+
/**
85+
* Check if estimate makes sense
86+
*
87+
*/
88+
@Test
89+
public void testEstimate() {
90+
Model model = ModelFactory.createDefaultModel();
91+
92+
Resource subj = model.createResource("http://data.gov.be/catalog/ckanvl");
93+
Property pred = null;
94+
Resource obj = null;
95+
96+
long offset = 0;
97+
long limit = 50;
98+
99+
TriplePatternFragment fragment =
100+
hdt.getFragment(subj, pred, obj, offset, limit);
101+
long totalSize = fragment.getTotalSize();
102+
103+
Assert.assertTrue("Estimate is fake: " + totalSize, totalSize != 51);
104+
}
105+
106+
@After
107+
public void tearDown() throws Exception {
108+
}
109+
}

src/test/java/org/linkeddatafragments/datasource/JenaTDBDataSourceTest.java

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,27 @@
66
package test.java.org.linkeddatafragments.datasource;
77

88
import com.google.gson.JsonObject;
9+
910
import com.hp.hpl.jena.query.Dataset;
1011
import com.hp.hpl.jena.rdf.model.Model;
12+
import com.hp.hpl.jena.rdf.model.ModelFactory;
1113
import com.hp.hpl.jena.rdf.model.Property;
1214
import com.hp.hpl.jena.rdf.model.Resource;
1315
import com.hp.hpl.jena.tdb.TDBFactory;
16+
1417
import java.io.File;
18+
import java.io.InputStream;
19+
20+
import org.apache.jena.riot.Lang;
21+
import org.apache.jena.riot.RDFDataMgr;
22+
1523
import org.junit.After;
1624
import org.junit.AfterClass;
1725
import org.junit.Assert;
1826
import org.junit.Before;
1927
import org.junit.BeforeClass;
2028
import org.junit.Test;
29+
2130
import org.linkeddatafragments.datasource.DataSourceFactory;
2231
import org.linkeddatafragments.datasource.IDataSource;
2332
import org.linkeddatafragments.datasource.TriplePatternFragment;
@@ -28,10 +37,8 @@
2837
*/
2938
public class JenaTDBDataSourceTest {
3039
private static IDataSource tdb;
31-
private static Dataset dataset;
3240
private static File jena;
33-
34-
private final static String PREFIX = "http://test.ldf.org/";
41+
private static Dataset dataset;
3542

3643
@BeforeClass
3744
public static void setUpClass() throws Exception {
@@ -40,7 +47,14 @@ public static void setUpClass() throws Exception {
4047
jena.mkdir();
4148

4249
dataset = TDBFactory.createDataset(jena.getAbsolutePath());
50+
51+
Model model = dataset.getDefaultModel();
52+
InputStream in = ClassLoader.getSystemResourceAsStream("demo.nt");
53+
RDFDataMgr.read(model, in, Lang.NTRIPLES);
54+
model.commit();
4355

56+
// Everything is in place, now create the LDF datasource
57+
4458
JsonObject config = new JsonObject();
4559
config.addProperty("title", "jena test");
4660
config.addProperty("description", "jena tdb test");
@@ -65,25 +79,7 @@ public static void tearDownClass() throws Exception {
6579
}
6680

6781
@Before
68-
public void setUp() throws Exception {
69-
Model model = dataset.getDefaultModel();
70-
71-
// Generate a set of statements
72-
int subjs = 153;
73-
int preds = 29;
74-
int objs = 17;
75-
76-
for (int s = 0; s < subjs; s++) {
77-
Resource subj = model.createResource(PREFIX + "s/" + s);
78-
for (int p = 0; p < preds ; p++) {
79-
Property pred = model.createProperty(PREFIX + "p/" + p);
80-
for (int o = 0; o < objs ; o++) {
81-
Resource obj = model.createResource(PREFIX + "o/" + o);
82-
model.add(subj, pred, obj);
83-
}
84-
}
85-
}
86-
model.commit();
82+
public void setUp() throws Exception {
8783
}
8884

8985
/**
@@ -92,9 +88,9 @@ public void setUp() throws Exception {
9288
*/
9389
@Test
9490
public void testEstimate() {
95-
Model model = dataset.getDefaultModel();
91+
Model model = ModelFactory.createDefaultModel();
9692

97-
Resource subj = model.createResource(PREFIX + "s/1");
93+
Resource subj = model.createResource("http://data.gov.be/catalog/ckanvl");
9894
Property pred = null;
9995
Resource obj = null;
10096

0 commit comments

Comments
 (0)