-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3abe032
commit f0e51c0
Showing
4 changed files
with
260 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
156 changes: 156 additions & 0 deletions
156
jena/src/test/scala/eu/ostrzyciel/jelly/convert/jena/riot/JenaNamespaceDeclarationSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
package eu.ostrzyciel.jelly.convert.jena.riot | ||
|
||
import eu.ostrzyciel.jelly.convert.jena.traits.JenaTest | ||
import eu.ostrzyciel.jelly.core.proto.v1.{RdfStreamFrame, RdfStreamRow} | ||
import org.apache.jena.graph.NodeFactory | ||
import org.apache.jena.rdf.model.ModelFactory | ||
import org.apache.jena.riot.system.StreamRDFWriter | ||
import org.apache.jena.riot.{RDFDataMgr, RDFWriter, RIOT} | ||
import org.apache.jena.sparql.core.DatasetGraphFactory | ||
import org.scalatest.matchers.should.Matchers | ||
import org.scalatest.wordspec.AnyWordSpec | ||
|
||
import java.io.{ByteArrayInputStream, ByteArrayOutputStream} | ||
|
||
/** | ||
* Round-trip tests for namespace declarations. | ||
*/ | ||
class JenaNamespaceDeclarationSpec extends AnyWordSpec, Matchers, JenaTest: | ||
// Prepare data | ||
val m = ModelFactory.createDefaultModel() | ||
m.add( | ||
m.createResource("http://example.com/s"), | ||
m.createProperty("http://example.com/p"), | ||
m.createResource("http://example.com/o") | ||
) | ||
m.setNsPrefix("ex", "http://example.com/") | ||
m.setNsPrefix("ex2", "http://example2.com/") | ||
|
||
val ds = DatasetGraphFactory.create() | ||
ds.addGraph( | ||
NodeFactory.createURI("http://example2.com/g"), | ||
m.getGraph | ||
) | ||
ds.prefixes().putAll(m.getNsPrefixMap) | ||
|
||
private def checkDeclarations(out: ByteArrayOutputStream, shouldBeThere: Boolean) = | ||
val rows: Seq[RdfStreamRow] = RdfStreamFrame.parseDelimitedFrom(ByteArrayInputStream(out.toByteArray)).get.rows | ||
val nsDecls = rows.filter(_.row.isNamespace).map(_.row.namespace) | ||
if shouldBeThere then | ||
nsDecls.size should be (2) | ||
nsDecls.map(_.nsName) should contain allOf ("ex", "ex2") | ||
else | ||
nsDecls.size should be (0) | ||
|
||
"JellyGraphWriter" should { | ||
"preserve namespace declarations" in { | ||
val out = new ByteArrayOutputStream() | ||
RDFWriter | ||
.source(m) | ||
.lang(JellyLanguage.JELLY) | ||
.set(JellyLanguage.SYMBOL_ENABLE_NAMESPACE_DECLARATIONS, true) | ||
.output(out) | ||
|
||
checkDeclarations(out, true) | ||
val m2 = ModelFactory.createDefaultModel() | ||
RDFDataMgr.read(m2, ByteArrayInputStream(out.toByteArray), JellyLanguage.JELLY) | ||
m2.getNsPrefixMap should be (m.getNsPrefixMap) | ||
} | ||
|
||
"not preserve namespace declarations if disabled" in { | ||
val out = new ByteArrayOutputStream() | ||
RDFWriter | ||
.source(m) | ||
.lang(JellyLanguage.JELLY) | ||
// Default is false | ||
// .set(JellyLanguage.SYMBOL_ENABLE_NAMESPACE_DECLARATIONS, false) | ||
.output(out) | ||
|
||
checkDeclarations(out, false) | ||
val m2 = ModelFactory.createDefaultModel() | ||
RDFDataMgr.read(m2, ByteArrayInputStream(out.toByteArray), JellyLanguage.JELLY) | ||
m2.getNsPrefixMap should be (java.util.Map.of()) | ||
} | ||
} | ||
|
||
"JellyDatasetWriter" should { | ||
"preserve namespace declarations" in { | ||
val out = new ByteArrayOutputStream() | ||
RDFWriter | ||
.source(ds) | ||
.lang(JellyLanguage.JELLY) | ||
.set(JellyLanguage.SYMBOL_ENABLE_NAMESPACE_DECLARATIONS, true) | ||
.output(out) | ||
|
||
checkDeclarations(out, true) | ||
val ds2 = DatasetGraphFactory.create() | ||
RDFDataMgr.read(ds2, ByteArrayInputStream(out.toByteArray), JellyLanguage.JELLY) | ||
ds2.prefixes().getMapping should be (ds.prefixes().getMapping) | ||
} | ||
|
||
"not preserve namespace declarations if disabled" in { | ||
val out = new ByteArrayOutputStream() | ||
RDFWriter | ||
.source(ds) | ||
.lang(JellyLanguage.JELLY) | ||
// Default is false | ||
// .set(JellyLanguage.SYMBOL_ENABLE_NAMESPACE_DECLARATIONS, false) | ||
.output(out) | ||
|
||
checkDeclarations(out, false) | ||
val ds2 = DatasetGraphFactory.create() | ||
RDFDataMgr.read(ds2, ByteArrayInputStream(out.toByteArray), JellyLanguage.JELLY) | ||
ds2.prefixes().getMapping should be (java.util.Map.of()) | ||
} | ||
} | ||
|
||
"JellyStreamWriterAutodetectType" should { | ||
"preserve namespace declarations (prefixes before triples)" in { | ||
val out = new ByteArrayOutputStream() | ||
val writer = StreamRDFWriter.getWriterStream( | ||
out, | ||
JellyLanguage.JELLY, | ||
RIOT.getContext.copy().set(JellyLanguage.SYMBOL_ENABLE_NAMESPACE_DECLARATIONS, true) | ||
) | ||
writer.start() | ||
writer.prefix("ex", "http://example.com") | ||
writer.prefix("ex2", "http://example2.com") | ||
writer.triple(m.getGraph.find().next()) | ||
writer.finish() | ||
|
||
checkDeclarations(out, true) | ||
} | ||
|
||
"preserve namespace declarations (triples before prefixes)" in { | ||
val out = new ByteArrayOutputStream() | ||
val writer = StreamRDFWriter.getWriterStream( | ||
out, | ||
JellyLanguage.JELLY, | ||
RIOT.getContext.copy().set(JellyLanguage.SYMBOL_ENABLE_NAMESPACE_DECLARATIONS, true) | ||
) | ||
writer.start() | ||
writer.triple(m.getGraph.find().next()) | ||
writer.prefix("ex", "http://example.com") | ||
writer.prefix("ex2", "http://example2.com") | ||
writer.finish() | ||
|
||
checkDeclarations(out, true) | ||
} | ||
|
||
"not preserve namespace declarations if disabled" in { | ||
val out = new ByteArrayOutputStream() | ||
val writer = StreamRDFWriter.getWriterStream( | ||
out, | ||
JellyLanguage.JELLY, | ||
// default is false | ||
RIOT.getContext.copy() // .set(JellyLanguage.SYMBOL_ENABLE_NAMESPACE_DECLARATIONS, false) | ||
) | ||
writer.start() | ||
writer.prefix("ex", "http://example.com") | ||
writer.prefix("ex2", "http://example2.com") | ||
writer.triple(m.getGraph.find().next()) | ||
writer.finish() | ||
|
||
checkDeclarations(out, false) | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
.../src/test/scala/eu/ostrzyciel/jelly/convert/rdf4j/rio/Rdf4jNamespaceDeclarationSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package eu.ostrzyciel.jelly.convert.rdf4j.rio | ||
|
||
import eu.ostrzyciel.jelly.core.proto.v1.* | ||
import org.eclipse.rdf4j.model.impl.SimpleValueFactory | ||
import org.eclipse.rdf4j.rio.helpers.AbstractRDFHandler | ||
import org.scalatest.matchers.should.Matchers | ||
import org.scalatest.wordspec.AnyWordSpec | ||
|
||
import java.io.{ByteArrayInputStream, ByteArrayOutputStream} | ||
|
||
/** | ||
* Round-trip tests for namespace declarations. | ||
*/ | ||
class Rdf4jNamespaceDeclarationSpec extends AnyWordSpec, Matchers: | ||
private def checkDeclarations(out: ByteArrayOutputStream, shouldBeThere: Boolean) = | ||
val rows: Seq[RdfStreamRow] = RdfStreamFrame.parseDelimitedFrom(ByteArrayInputStream(out.toByteArray)).get.rows | ||
val nsDecls = rows.filter(_.row.isNamespace).map(_.row.namespace) | ||
|
||
val parser = JellyParserFactory().getParser() | ||
val namespaces = new collection.mutable.HashMap[String, String]() | ||
parser.setRDFHandler(new AbstractRDFHandler() { | ||
override def handleNamespace(prefix: String, uri: String): Unit = { | ||
namespaces.put(prefix, uri) | ||
} | ||
}) | ||
parser.parse(new ByteArrayInputStream(out.toByteArray), "") | ||
|
||
if shouldBeThere then | ||
nsDecls.size should be(2) | ||
nsDecls.map(_.nsName) should contain allOf("ex", "ex2") | ||
namespaces should be (Map("ex" -> "http://example.com/", "ex2" -> "http://example2.com/")) | ||
else | ||
nsDecls.size should be(0) | ||
namespaces should be (Map.empty) | ||
|
||
val vf = SimpleValueFactory.getInstance() | ||
val triple = vf.createStatement( | ||
vf.createIRI("http://example2.com/s"), | ||
vf.createIRI("http://example.com/p"), | ||
vf.createIRI("http://example.com/o") | ||
) | ||
|
||
"JellyWriter and JellyReader" should { | ||
"preserve namespace declarations (prefixes before triples)" in { | ||
val out = new ByteArrayOutputStream() | ||
val writer = JellyWriterFactory().getWriter(out) | ||
writer.set(JellyWriterSettings.ENABLE_NAMESPACE_DECLARATIONS, true) | ||
|
||
writer.startRDF() | ||
writer.handleNamespace("ex", "http://example.com/") | ||
writer.handleNamespace("ex2", "http://example2.com/") | ||
writer.handleStatement(triple) | ||
writer.endRDF() | ||
|
||
checkDeclarations(out, shouldBeThere = true) | ||
} | ||
|
||
"preserve namespace declarations (triples before prefixes)" in { | ||
val out = new ByteArrayOutputStream() | ||
val writer = JellyWriterFactory().getWriter(out) | ||
writer.set(JellyWriterSettings.ENABLE_NAMESPACE_DECLARATIONS, true) | ||
|
||
writer.startRDF() | ||
writer.handleStatement(triple) | ||
writer.handleNamespace("ex", "http://example.com/") | ||
writer.handleNamespace("ex2", "http://example2.com/") | ||
writer.endRDF() | ||
|
||
checkDeclarations(out, shouldBeThere = true) | ||
} | ||
|
||
"not preserve namespace declarations if disabled" in { | ||
val out = new ByteArrayOutputStream() | ||
val writer = JellyWriterFactory().getWriter(out) | ||
// Default is false | ||
// writer.set(JellyWriterSettings.ENABLE_NAMESPACE_DECLARATIONS, false) | ||
|
||
writer.startRDF() | ||
writer.handleNamespace("ex", "http://example.com/") | ||
writer.handleNamespace("ex2", "http://example2.com/") | ||
writer.handleStatement(triple) | ||
writer.endRDF() | ||
|
||
checkDeclarations(out, shouldBeThere = false) | ||
} | ||
} |