Skip to content

Commit 1a52743

Browse files
committed
Improved test cases
1 parent e84e696 commit 1a52743

File tree

4 files changed

+173
-71
lines changed

4 files changed

+173
-71
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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;
7+
8+
import org.junit.runner.RunWith;
9+
import org.junit.runners.Suite;
10+
11+
import test.java.org.linkeddatafragments.datasource.HdtDataSourceTest;
12+
import test.java.org.linkeddatafragments.datasource.JenaTDBDataSourceTest;
13+
14+
/**
15+
*
16+
* @author Bart Hanssens <[email protected]>
17+
*/
18+
@RunWith(Suite.class)
19+
@Suite.SuiteClasses({
20+
HdtDataSourceTest.class,
21+
JenaTDBDataSourceTest.class
22+
})
23+
public class TestSuite {
24+
25+
}
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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+
import com.hp.hpl.jena.rdf.model.Model;
10+
import com.hp.hpl.jena.rdf.model.ModelFactory;
11+
import com.hp.hpl.jena.rdf.model.Property;
12+
import com.hp.hpl.jena.rdf.model.Resource;
13+
14+
import java.io.File;
15+
import java.io.IOException;
16+
import java.io.InputStream;
17+
18+
import java.nio.file.Files;
19+
import java.nio.file.StandardCopyOption;
20+
21+
import org.junit.Assert;
22+
import org.junit.Test;
23+
24+
import org.linkeddatafragments.datasource.IDataSource;
25+
import org.linkeddatafragments.datasource.TriplePatternFragment;
26+
27+
28+
/**
29+
*
30+
* @author Bart Hanssens <[email protected]>
31+
*/
32+
public abstract class DataSourceTest {
33+
private static IDataSource ds;
34+
35+
/**
36+
* Get data source
37+
*
38+
* @return data source interface
39+
*/
40+
public static IDataSource getDatasource() {
41+
return ds;
42+
}
43+
44+
/**
45+
* Set the data source
46+
*
47+
* @param ds data source
48+
*/
49+
public static void setDatasource(IDataSource ds) {
50+
DataSourceTest.ds = ds;
51+
}
52+
53+
/**
54+
* Copy the demo triple in the jar to a temp file.
55+
*
56+
* @return temp file
57+
* @throws IOException
58+
*/
59+
public static File getResourceAsFile() throws IOException {
60+
File temp = File.createTempFile("ldf-test-hdt", ".ttl");
61+
temp.deleteOnExit();
62+
63+
InputStream in = ClassLoader.getSystemResourceAsStream("demo.nt");
64+
Files.copy(in, temp.toPath(), StandardCopyOption.REPLACE_EXISTING);
65+
66+
return temp;
67+
}
68+
69+
/**
70+
* Generate a basic Json configuration
71+
*
72+
* @param title
73+
* @param desc
74+
* @param type
75+
* @return JSON object
76+
*/
77+
public static JsonObject createConfig(String title, String desc, String type) {
78+
JsonObject config = new JsonObject();
79+
config.addProperty("title", title);
80+
config.addProperty("description", desc);
81+
config.addProperty("type", type);
82+
83+
return config;
84+
}
85+
86+
87+
/**
88+
* Test total size of empty TPF
89+
*
90+
*/
91+
@Test
92+
public void testEmpty() {
93+
Model model = ModelFactory.createDefaultModel();
94+
95+
Resource subj = model.createResource("http://nothing.ldf.org");
96+
Property pred = null;
97+
Resource obj = null;
98+
99+
long offset = 0;
100+
long limit = 50;
101+
102+
TriplePatternFragment fragment =
103+
getDatasource().getFragment(subj, pred, obj, offset, limit);
104+
long totalSize = fragment.getTotalSize();
105+
106+
Assert.assertTrue("Estimate is too big : " + totalSize, totalSize == 0);
107+
108+
}
109+
110+
/**
111+
* Test if estimate seems reasonable.
112+
*/
113+
@Test
114+
public void testEstimate() {
115+
Model model = ModelFactory.createDefaultModel();
116+
117+
Resource subj = model.createResource("http://data.gov.be/catalog/ckanvl");
118+
Property pred = null;
119+
Resource obj = null;
120+
121+
long offset = 0;
122+
long limit = 50;
123+
124+
TriplePatternFragment fragment =
125+
getDatasource().getFragment(subj, pred, obj, offset, limit);
126+
long totalSize = fragment.getTotalSize();
127+
128+
Assert.assertTrue("Estimate is too small : " + totalSize, totalSize > 100);
129+
}
130+
}

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

Lines changed: 13 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313
import com.hp.hpl.jena.rdf.model.Resource;
1414

1515
import java.io.File;
16+
import java.io.IOException;
1617
import java.io.InputStream;
1718
import java.nio.file.Files;
19+
import java.nio.file.StandardCopyOption;
1820

1921
import org.junit.After;
2022
import org.junit.AfterClass;
@@ -36,37 +38,32 @@
3638
*
3739
* @author Bart Hanssens <[email protected]>
3840
*/
39-
public class HdtDataSourceTest {
40-
private static IDataSource hdt;
41+
public class HdtDataSourceTest extends DataSourceTest {
42+
4143
private static File hdtfile;
42-
44+
45+
4346
@BeforeClass
4447
public static void setUpClass() throws Exception {
4548
// 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,
49+
File temp = getResourceAsFile();
50+
51+
HDT mgr = HDTManager.generateHDT(temp.getAbsolutePath(),
52+
"http://linkeddatafragments.org",
5253
RDFNotation.NTRIPLES, new HDTSpecification(), null);
53-
hdtfile = new File(tmpdir, "ldf-hdt-test");
54+
hdtfile = File.createTempFile("ldf-hdt-test", ".hdt");
5455
mgr.saveToHDT(hdtfile.getAbsolutePath(), null);
5556

5657
temp.getAbsoluteFile().delete();
5758

5859
// Everything is in place, now create the LDF datasource
5960

60-
JsonObject config = new JsonObject();
61-
config.addProperty("title", "hdt test");
62-
config.addProperty("description", "hdt test");
63-
config.addProperty("type", DataSourceFactory.HDT);
64-
61+
JsonObject config = createConfig("hdt test", "hdt test", DataSourceFactory.HDT);
6562
JsonObject settings = new JsonObject();
6663
settings.addProperty("file", hdtfile.getAbsolutePath());
6764
config.add("settings", settings);
6865

69-
hdt = DataSourceFactory.create(config);
66+
setDatasource(DataSourceFactory.create(config));
7067
}
7168

7269
@AfterClass
@@ -80,28 +77,6 @@ public static void tearDownClass() throws Exception {
8077
public void setUp() throws Exception {
8178

8279
}
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-
}
10580

10681
@After
10782
public void tearDown() throws Exception {

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

Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,17 @@
2222

2323
import org.junit.After;
2424
import org.junit.AfterClass;
25-
import org.junit.Assert;
25+
2626
import org.junit.Before;
2727
import org.junit.BeforeClass;
28-
import org.junit.Test;
2928

3029
import org.linkeddatafragments.datasource.DataSourceFactory;
31-
import org.linkeddatafragments.datasource.IDataSource;
32-
import org.linkeddatafragments.datasource.TriplePatternFragment;
3330

3431
/**
3532
*
3633
* @author Bart Hanssens <[email protected]>
3734
*/
38-
public class JenaTDBDataSourceTest {
39-
private static IDataSource tdb;
35+
public class JenaTDBDataSourceTest extends DataSourceTest {
4036
private static File jena;
4137
private static Dataset dataset;
4238

@@ -55,16 +51,14 @@ public static void setUpClass() throws Exception {
5551

5652
// Everything is in place, now create the LDF datasource
5753

58-
JsonObject config = new JsonObject();
59-
config.addProperty("title", "jena test");
60-
config.addProperty("description", "jena tdb test");
61-
config.addProperty("type", DataSourceFactory.JENA_TDB);
54+
JsonObject config = createConfig("jena tdb test", "jena tdb test",
55+
DataSourceFactory.JENA_TDB);
6256

6357
JsonObject settings = new JsonObject();
6458
settings.addProperty("directory", jena.getAbsolutePath());
6559
config.add("settings", settings);
6660

67-
tdb = DataSourceFactory.create(config);
61+
setDatasource(DataSourceFactory.create(config));
6862
}
6963

7064
@AfterClass
@@ -81,28 +75,6 @@ public static void tearDownClass() throws Exception {
8175
@Before
8276
public void setUp() throws Exception {
8377
}
84-
85-
/**
86-
* Check if estimate is based on jena query plan, or just a fake one.
87-
*
88-
*/
89-
@Test
90-
public void testEstimate() {
91-
Model model = ModelFactory.createDefaultModel();
92-
93-
Resource subj = model.createResource("http://data.gov.be/catalog/ckanvl");
94-
Property pred = null;
95-
Resource obj = null;
96-
97-
long offset = 0;
98-
long limit = 50;
99-
100-
TriplePatternFragment fragment =
101-
tdb.getFragment(subj, pred, obj, offset, limit);
102-
long totalSize = fragment.getTotalSize();
103-
104-
Assert.assertTrue("Estimate is fake: " + totalSize, totalSize != 51);
105-
}
10678

10779
@After
10880
public void tearDown() throws Exception {

0 commit comments

Comments
 (0)