|
| 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 | +} |
0 commit comments