org.eclipse.rdf4j.rio.helpers.StatementCollector Java Examples

The following examples show how to use org.eclipse.rdf4j.rio.helpers.StatementCollector. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RDFWriterTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testWriteCommentBNodeContextBNodeWithNamespace() throws Exception {
	ByteArrayOutputStream outputWriter = new ByteArrayOutputStream();
	RDFWriter rdfWriter = rdfWriterFactory.getWriter(outputWriter);
	setupWriterConfig(rdfWriter.getWriterConfig());
	rdfWriter.startRDF();
	rdfWriter.handleNamespace("ex", exNs);
	rdfWriter.handleStatement(vf.createStatement(uri1, uri1, uri1, bnode));
	rdfWriter.handleComment("This comment should not screw up parsing");
	rdfWriter.handleStatement(vf.createStatement(uri1, uri1, uri2, bnode));
	rdfWriter.endRDF();
	ByteArrayInputStream inputReader = new ByteArrayInputStream(outputWriter.toByteArray());
	RDFParser rdfParser = rdfParserFactory.getParser();
	setupParserConfig(rdfParser.getParserConfig());
	Model parsedOutput = new LinkedHashModel();
	rdfParser.setRDFHandler(new StatementCollector(parsedOutput));
	rdfParser.parse(inputReader, "");
	assertEquals(2, parsedOutput.size());
	assertTrue(parsedOutput.contains(uri1, uri1, uri1));
	assertTrue(parsedOutput.contains(uri1, uri1, uri2));
	assertEquals(1, parsedOutput.contexts().size());
}
 
Example #2
Source File: SPARQLQueryComplianceTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private final Set<Statement> readExpectedGraphQueryResult() throws Exception {
	RDFFormat rdfFormat = Rio.getParserFormatForFileName(resultFileURL)
			.orElseThrow(Rio.unsupportedFormat(resultFileURL));

	RDFParser parser = Rio.createParser(rdfFormat);
	parser.setDatatypeHandling(DatatypeHandling.IGNORE);
	parser.setPreserveBNodeIDs(true);
	parser.setValueFactory(getDataRepository().getValueFactory());

	Set<Statement> result = new LinkedHashSet<>();
	parser.setRDFHandler(new StatementCollector(result));

	InputStream in = new URL(resultFileURL).openStream();
	try {
		parser.parse(in, resultFileURL);
	} finally {
		in.close();
	}

	return result;
}
 
Example #3
Source File: FedXBaseTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Read the expected graph query result from the specified resource
 *
 * @param resultFile
 * @return
 * @throws Exception
 */
protected Set<Statement> readExpectedGraphQueryResult(String resultFile) throws Exception {
	RDFFormat rdfFormat = Rio.getParserFormatForFileName(resultFile).get();

	if (rdfFormat != null) {
		RDFParser parser = Rio.createParser(rdfFormat);
		parser.setPreserveBNodeIDs(true);
		parser.setValueFactory(SimpleValueFactory.getInstance());

		Set<Statement> result = new LinkedHashSet<>();
		parser.setRDFHandler(new StatementCollector(result));

		InputStream in = SPARQLBaseTest.class.getResourceAsStream(resultFile);
		try {
			parser.parse(in, resultFile);
		} finally {
			in.close();
		}

		return result;
	} else {
		throw new RuntimeException("Unable to determine file type of results file");
	}
}
 
Example #4
Source File: SpinRendererTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private static String toRDF(StatementCollector stmts) throws RDFHandlerException {
	WriterConfig config = new WriterConfig();
	config.set(BasicWriterSettings.PRETTY_PRINT, false);
	StringBuilderWriter writer = new StringBuilderWriter();
	final RDFWriter rdfWriter = Rio.createWriter(RDFFormat.TURTLE, writer);
	rdfWriter.setWriterConfig(config);

	rdfWriter.startRDF();
	for (Map.Entry<String, String> entry : stmts.getNamespaces().entrySet()) {
		rdfWriter.handleNamespace(entry.getKey(), entry.getValue());
	}
	for (final Statement st : stmts.getStatements()) {
		rdfWriter.handleStatement(st);
	}
	rdfWriter.endRDF();

	writer.close();
	return writer.toString();
}
 
Example #5
Source File: DAWGTestResultSetUtil.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static Model toGraph(TupleQueryResult tqr) throws QueryEvaluationException {
	Model graph = new TreeModel();
	DAWGTestResultSetWriter writer = new DAWGTestResultSetWriter(new StatementCollector(graph));

	try {
		writer.startQueryResult(tqr.getBindingNames());
		while (tqr.hasNext()) {
			writer.handleSolution(tqr.next());
		}
		writer.endQueryResult();
	} catch (TupleQueryResultHandlerException e) {
		// No exceptions expected from DAWGTestResultSetWriter or
		// StatementCollector, foud a bug?
		throw new RuntimeException(e);
	}

	return graph;
}
 
Example #6
Source File: RemoteRepositoryManager.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public RepositoryConfig getRepositoryConfig(String id) throws RepositoryException {
	Model model = getModelFactory().createEmptyModel();
	try (RDF4JProtocolSession protocolSession = getSharedHttpClientSessionManager()
			.createRDF4JProtocolSession(serverURL)) {
		protocolSession.setUsernameAndPassword(username, password);

		int serverProtocolVersion = Integer.parseInt(protocolSession.getServerProtocol());
		if (serverProtocolVersion < 10) { // explicit per-repo config endpoint was introduced in Protocol version 10
			protocolSession.setRepository(Protocol.getRepositoryLocation(serverURL, SystemRepository.ID));
			protocolSession.getStatements(null, null, null, true, new StatementCollector(model));
		} else {
			protocolSession.setRepository(Protocol.getRepositoryLocation(serverURL, id));
			protocolSession.getRepositoryConfig(new StatementCollector(model));
		}

	} catch (IOException | QueryEvaluationException | UnauthorizedException ue) {
		throw new RepositoryException(ue);
	}
	return RepositoryConfigUtil.getRepositoryConfig(model, id);
}
 
Example #7
Source File: AbstractNTriplesParserUnitTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testBlankNodeIdentifiersWithDotAsFirstCahracter() throws Exception {
	// The character . may appear anywhere except the first or last character.
	RDFParser ntriplesParser = new NTriplesParser();
	Model model = new LinkedHashModel();
	ntriplesParser.setRDFHandler(new StatementCollector(model));
	try {
		ntriplesParser.parse(new StringReader("_:123 <urn:test:predicate> _:.456 ."), NTRIPLES_TEST_URL);
		fail("Should have failed to parse invalid N-Triples bnode with '.' at the begining of the bnode label");
	} catch (Exception e) {
	}

	assertEquals(0, model.size());
	assertEquals(0, model.subjects().size());
	assertEquals(0, model.predicates().size());
	assertEquals(0, model.objects().size());
}
 
Example #8
Source File: AbstractNTriplesParserUnitTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testExceptionHandlingWithDefaultSettings() throws Exception {
	String data = "invalid nt";

	RDFParser ntriplesParser = createRDFParser();
	ntriplesParser.setDatatypeHandling(RDFParser.DatatypeHandling.IGNORE);
	Model model = new LinkedHashModel();
	ntriplesParser.setRDFHandler(new StatementCollector(model));

	try {
		ntriplesParser.parse(new StringReader(data), NTRIPLES_TEST_URL);
		fail("expected RDFParseException due to invalid data");
	} catch (RDFParseException expected) {
		assertEquals(expected.getLineNumber(), 1);
	}
}
 
Example #9
Source File: RDFWriterTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private void testSES2030BNodeCollisionsInternal(boolean preserveBNodeIDs) throws Exception {
		ByteArrayOutputStream output = new ByteArrayOutputStream();
		RDFWriter rdfWriter = rdfWriterFactory.getWriter(output);
		setupWriterConfig(rdfWriter.getWriterConfig());
		rdfWriter.startRDF();
		int count = 18;
		for (int i = 0; i < count; i++) {
			BNode bNode2 = vf.createBNode("a" + Integer.toHexString(i).toUpperCase());
			// System.out.println(bNode2.getID());
			rdfWriter.handleStatement(vf.createStatement(uri1, uri2, bNode2));
		}
		rdfWriter.endRDF();
		RDFParser rdfParser = rdfParserFactory.getParser();
		setupParserConfig(rdfParser.getParserConfig());
		if (preserveBNodeIDs) {
			rdfParser.getParserConfig().set(BasicParserSettings.PRESERVE_BNODE_IDS, true);
		}
		Model parsedModel = new LinkedHashModel();
		rdfParser.setRDFHandler(new StatementCollector(parsedModel));
		rdfParser.parse(new ByteArrayInputStream(output.toByteArray()), "");
//		if (count != parsedModel.size()) {
//			Rio.write(parsedModel, System.out, RDFFormat.NQUADS);
//		}
		assertEquals(count, parsedModel.size());
	}
 
Example #10
Source File: AbstractNTriplesParserUnitTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testUriWithSpaceShouldFailToParse() throws Exception {
	RDFParser ntriplesParser = createRDFParser();
	Model model = new LinkedHashModel();
	ntriplesParser.setRDFHandler(new StatementCollector(model));

	String nt = "<http://example/ space> <http://example/p> <http://example/o> .";

	try {
		ntriplesParser.parse(new StringReader(nt), NTRIPLES_TEST_URL);
		fail("Should have failed to parse invalid N-Triples uri with space");
	} catch (RDFParseException ignored) {
	}

	assertEquals(0, model.size());
	assertEquals(0, model.subjects().size());
	assertEquals(0, model.predicates().size());
	assertEquals(0, model.objects().size());
}
 
Example #11
Source File: RDFWriterTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testWriteCommentURIContextURI() throws Exception {
	ByteArrayOutputStream outputWriter = new ByteArrayOutputStream();
	RDFWriter rdfWriter = rdfWriterFactory.getWriter(outputWriter);
	setupWriterConfig(rdfWriter.getWriterConfig());
	rdfWriter.startRDF();
	rdfWriter.handleStatement(vf.createStatement(uri1, uri1, uri1, uri1));
	rdfWriter.handleComment("This comment should not screw up parsing");
	rdfWriter.handleStatement(vf.createStatement(uri1, uri1, uri2, uri1));
	rdfWriter.endRDF();
	logger.debug(outputWriter.toString());
	ByteArrayInputStream inputReader = new ByteArrayInputStream(outputWriter.toByteArray());
	RDFParser rdfParser = rdfParserFactory.getParser();
	setupParserConfig(rdfParser.getParserConfig());
	Model parsedOutput = new LinkedHashModel();
	rdfParser.setRDFHandler(new StatementCollector(parsedOutput));
	rdfParser.parse(inputReader, "");
	assertEquals(2, parsedOutput.size());
	if (rdfWriterFactory.getRDFFormat().supportsContexts()) {
		assertTrue(parsedOutput.contains(uri1, uri1, uri1, uri1));
		assertTrue(parsedOutput.contains(uri1, uri1, uri2, uri1));
	} else {
		assertTrue(parsedOutput.contains(uri1, uri1, uri1));
		assertTrue(parsedOutput.contains(uri1, uri1, uri2));
	}
}
 
Example #12
Source File: RDFWriterTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testWriteCommentBNodeContext() throws Exception {
	ByteArrayOutputStream outputWriter = new ByteArrayOutputStream();
	RDFWriter rdfWriter = rdfWriterFactory.getWriter(outputWriter);
	setupWriterConfig(rdfWriter.getWriterConfig());
	rdfWriter.startRDF();
	rdfWriter.handleStatement(vf.createStatement(uri1, uri1, uri1, bnode));
	rdfWriter.handleComment("This comment should not screw up parsing");
	rdfWriter.endRDF();
	ByteArrayInputStream inputReader = new ByteArrayInputStream(outputWriter.toByteArray());
	RDFParser rdfParser = rdfParserFactory.getParser();
	setupParserConfig(rdfParser.getParserConfig());
	Model parsedOutput = new LinkedHashModel();
	rdfParser.setRDFHandler(new StatementCollector(parsedOutput));
	rdfParser.parse(inputReader, "");
	assertEquals(1, parsedOutput.size());
	assertTrue(parsedOutput.contains(uri1, uri1, uri1));
}
 
Example #13
Source File: RDFWriterTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testWriteCommentURIContextWithNamespace() throws Exception {
	ByteArrayOutputStream outputWriter = new ByteArrayOutputStream();
	RDFWriter rdfWriter = rdfWriterFactory.getWriter(outputWriter);
	setupWriterConfig(rdfWriter.getWriterConfig());
	rdfWriter.startRDF();
	rdfWriter.handleNamespace("ex", exNs);
	rdfWriter.handleStatement(vf.createStatement(uri1, uri1, uri1, uri1));
	rdfWriter.handleComment("This comment should not screw up parsing");
	rdfWriter.endRDF();
	ByteArrayInputStream inputReader = new ByteArrayInputStream(outputWriter.toByteArray());
	RDFParser rdfParser = rdfParserFactory.getParser();
	setupParserConfig(rdfParser.getParserConfig());
	Model parsedOutput = new LinkedHashModel();
	rdfParser.setRDFHandler(new StatementCollector(parsedOutput));
	rdfParser.parse(inputReader, "");
	assertEquals(1, parsedOutput.size());
	if (rdfWriterFactory.getRDFFormat().supportsContexts()) {
		assertTrue(parsedOutput.contains(uri1, uri1, uri1, uri1));
	} else {
		assertTrue(parsedOutput.contains(uri1, uri1, uri1));
	}
}
 
Example #14
Source File: RDFWriterTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testWriteCommentURIContextURIWithNamespace() throws Exception {
	ByteArrayOutputStream outputWriter = new ByteArrayOutputStream();
	RDFWriter rdfWriter = rdfWriterFactory.getWriter(outputWriter);
	setupWriterConfig(rdfWriter.getWriterConfig());
	rdfWriter.startRDF();
	rdfWriter.handleNamespace("ex", exNs);
	rdfWriter.handleStatement(vf.createStatement(uri1, uri1, uri1, uri1));
	rdfWriter.handleComment("This comment should not screw up parsing");
	rdfWriter.handleStatement(vf.createStatement(uri1, uri1, uri2, uri1));
	rdfWriter.endRDF();
	ByteArrayInputStream inputReader = new ByteArrayInputStream(outputWriter.toByteArray());
	RDFParser rdfParser = rdfParserFactory.getParser();
	setupParserConfig(rdfParser.getParserConfig());
	Model parsedOutput = new LinkedHashModel();
	rdfParser.setRDFHandler(new StatementCollector(parsedOutput));
	rdfParser.parse(inputReader, "");
	assertEquals(2, parsedOutput.size());
	if (rdfWriterFactory.getRDFFormat().supportsContexts()) {
		assertTrue(parsedOutput.contains(uri1, uri1, uri1, uri1));
		assertTrue(parsedOutput.contains(uri1, uri1, uri2, uri1));
	} else {
		assertTrue(parsedOutput.contains(uri1, uri1, uri1));
		assertTrue(parsedOutput.contains(uri1, uri1, uri2));
	}
}
 
Example #15
Source File: RDFWriterTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testWriteCommentBNodeContextWithNamespace() throws Exception {
	ByteArrayOutputStream outputWriter = new ByteArrayOutputStream();
	RDFWriter rdfWriter = rdfWriterFactory.getWriter(outputWriter);
	setupWriterConfig(rdfWriter.getWriterConfig());
	rdfWriter.startRDF();
	rdfWriter.handleNamespace("ex", exNs);
	rdfWriter.handleStatement(vf.createStatement(uri1, uri1, uri1, bnode));
	rdfWriter.handleComment("This comment should not screw up parsing");
	rdfWriter.endRDF();
	ByteArrayInputStream inputReader = new ByteArrayInputStream(outputWriter.toByteArray());
	RDFParser rdfParser = rdfParserFactory.getParser();
	setupParserConfig(rdfParser.getParserConfig());
	Model parsedOutput = new LinkedHashModel();
	rdfParser.setRDFHandler(new StatementCollector(parsedOutput));
	rdfParser.parse(inputReader, "");
	assertEquals(1, parsedOutput.size());
	assertTrue(parsedOutput.contains(uri1, uri1, uri1));
}
 
Example #16
Source File: SPARQLServiceEvaluationTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Read the expected graph query result from the specified resource
 *
 * @param resultFile
 * @return
 * @throws Exception
 */
private Set<Statement> readExpectedGraphQueryResult(String resultFile) throws Exception {
	RDFFormat rdfFormat = Rio.getParserFormatForFileName(resultFile).orElseThrow(Rio.unsupportedFormat(resultFile));

	RDFParser parser = Rio.createParser(rdfFormat);
	parser.setDatatypeHandling(DatatypeHandling.IGNORE);
	parser.setPreserveBNodeIDs(true);
	parser.setValueFactory(SimpleValueFactory.getInstance());

	Set<Statement> result = new LinkedHashSet<>();
	parser.setRDFHandler(new StatementCollector(result));

	InputStream in = SPARQLServiceEvaluationTest.class.getResourceAsStream(resultFile);
	try {
		parser.parse(in, null); // TODO check
	} finally {
		in.close();
	}

	return result;
}
 
Example #17
Source File: RDFWriterTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testWriteCommentURIContextBeforeNamespace() throws Exception {
	ByteArrayOutputStream outputWriter = new ByteArrayOutputStream();
	RDFWriter rdfWriter = rdfWriterFactory.getWriter(outputWriter);
	setupWriterConfig(rdfWriter.getWriterConfig());
	rdfWriter.startRDF();
	rdfWriter.handleStatement(vf.createStatement(uri1, uri1, uri1, uri1));
	rdfWriter.handleComment("This comment should not screw up parsing");
	rdfWriter.handleNamespace("ex1", exNs);
	rdfWriter.endRDF();
	ByteArrayInputStream inputReader = new ByteArrayInputStream(outputWriter.toByteArray());
	RDFParser rdfParser = rdfParserFactory.getParser();
	setupParserConfig(rdfParser.getParserConfig());
	Model parsedOutput = new LinkedHashModel();
	rdfParser.setRDFHandler(new StatementCollector(parsedOutput));
	rdfParser.parse(inputReader, "");
	assertEquals(1, parsedOutput.size());
	if (rdfWriterFactory.getRDFFormat().supportsContexts()) {
		assertTrue(parsedOutput.contains(uri1, uri1, uri1, uri1));
	} else {
		assertTrue(parsedOutput.contains(uri1, uri1, uri1));
	}
}
 
Example #18
Source File: RDFWriterTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testWriteCommentBNodeContextBeforeNamespace() throws Exception {
	ByteArrayOutputStream outputWriter = new ByteArrayOutputStream();
	RDFWriter rdfWriter = rdfWriterFactory.getWriter(outputWriter);
	setupWriterConfig(rdfWriter.getWriterConfig());
	rdfWriter.startRDF();
	rdfWriter.handleStatement(vf.createStatement(uri1, uri1, uri1, bnode));
	rdfWriter.handleComment("This comment should not screw up parsing");
	rdfWriter.handleNamespace("ex1", exNs);
	rdfWriter.endRDF();
	ByteArrayInputStream inputReader = new ByteArrayInputStream(outputWriter.toByteArray());
	RDFParser rdfParser = rdfParserFactory.getParser();
	setupParserConfig(rdfParser.getParserConfig());
	Model parsedOutput = new LinkedHashModel();
	rdfParser.setRDFHandler(new StatementCollector(parsedOutput));
	rdfParser.parse(inputReader, "");
	assertEquals(1, parsedOutput.size());
	assertTrue(parsedOutput.contains(uri1, uri1, uri1));
}
 
Example #19
Source File: RDFWriterTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testWriteCommentBNodeContextBNodeBeforeNamespace() throws Exception {
	ByteArrayOutputStream outputWriter = new ByteArrayOutputStream();
	RDFWriter rdfWriter = rdfWriterFactory.getWriter(outputWriter);
	setupWriterConfig(rdfWriter.getWriterConfig());
	rdfWriter.startRDF();
	rdfWriter.handleStatement(vf.createStatement(uri1, uri1, uri1, bnode));
	rdfWriter.handleComment("This comment should not screw up parsing");
	rdfWriter.handleNamespace("ex1", exNs);
	rdfWriter.handleStatement(vf.createStatement(uri1, uri1, uri2, bnode));
	rdfWriter.endRDF();
	ByteArrayInputStream inputReader = new ByteArrayInputStream(outputWriter.toByteArray());
	RDFParser rdfParser = rdfParserFactory.getParser();
	setupParserConfig(rdfParser.getParserConfig());
	Model parsedOutput = new LinkedHashModel();
	rdfParser.setRDFHandler(new StatementCollector(parsedOutput));
	rdfParser.parse(inputReader, "");
	assertEquals(2, parsedOutput.size());
	assertTrue(parsedOutput.contains(uri1, uri1, uri1));
	assertTrue(parsedOutput.contains(uri1, uri1, uri2));
	assertEquals(1, parsedOutput.contexts().size());
}
 
Example #20
Source File: RDFWriterTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testWriteCommentURIContextWithNamespaceBeforeNamespace() throws Exception {
	ByteArrayOutputStream outputWriter = new ByteArrayOutputStream();
	RDFWriter rdfWriter = rdfWriterFactory.getWriter(outputWriter);
	setupWriterConfig(rdfWriter.getWriterConfig());
	rdfWriter.startRDF();
	rdfWriter.handleNamespace("ex", exNs);
	rdfWriter.handleStatement(vf.createStatement(uri1, uri1, uri1, uri1));
	rdfWriter.handleComment("This comment should not screw up parsing");
	rdfWriter.handleNamespace("ex1", exNs);
	rdfWriter.endRDF();
	ByteArrayInputStream inputReader = new ByteArrayInputStream(outputWriter.toByteArray());
	RDFParser rdfParser = rdfParserFactory.getParser();
	setupParserConfig(rdfParser.getParserConfig());
	Model parsedOutput = new LinkedHashModel();
	rdfParser.setRDFHandler(new StatementCollector(parsedOutput));
	rdfParser.parse(inputReader, "");
	assertEquals(1, parsedOutput.size());
	if (rdfWriterFactory.getRDFFormat().supportsContexts()) {
		assertTrue(parsedOutput.contains(uri1, uri1, uri1, uri1));
	} else {
		assertTrue(parsedOutput.contains(uri1, uri1, uri1));
	}
}
 
Example #21
Source File: RDFXMLParserCustomTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Test with Secure processing setting off.
 * <p>
 * IMPORTANT: Only turn this on to verify it is still working, as there is no way to safely perform this test.
 * <p>
 * WARNING: This test will cause an OutOfMemoryException when it eventually fails, as it will eventually fail.
 *
 * @throws Exception
 */
@Ignore
@Test(timeout = 10000)
public void testEntityExpansionNoSecureProcessing() throws Exception {
	final Model aGraph = new LinkedHashModel();
	ParseErrorCollector errorCollector = new ParseErrorCollector();
	RDFParser aParser = Rio.createParser(RDFFormat.RDFXML)
			.setRDFHandler(new StatementCollector(aGraph))
			.set(XMLParserSettings.SECURE_PROCESSING, false)
			.setParseErrorListener(errorCollector);

	try {
		// IMPORTANT: This will not use the entity limit
		aParser.parse(
				this.getClass().getResourceAsStream("/testcases/rdfxml/openrdf/bad-entity-expansion-limit.rdf"),
				"http://example.org");
		fail("Parser did not throw an exception");
	} catch (RDFParseException e) {
		// assertTrue(e.getMessage().contains(
		// "The parser has encountered more than \"64,000\" entity expansions in this document; this is the limit
		// imposed by the"));
	}
	assertEquals(0, errorCollector.getWarnings().size());
	assertEquals(0, errorCollector.getErrors().size());
	assertEquals(1, errorCollector.getFatalErrors().size());
}
 
Example #22
Source File: RDFWriterTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testWriteCommentBNodeContextWithNamespaceBeforeNamespace() throws Exception {
	ByteArrayOutputStream outputWriter = new ByteArrayOutputStream();
	RDFWriter rdfWriter = rdfWriterFactory.getWriter(outputWriter);
	setupWriterConfig(rdfWriter.getWriterConfig());
	rdfWriter.startRDF();
	rdfWriter.handleNamespace("ex", exNs);
	rdfWriter.handleStatement(vf.createStatement(uri1, uri1, uri1, bnode));
	rdfWriter.handleComment("This comment should not screw up parsing");
	rdfWriter.handleNamespace("ex1", exNs);
	rdfWriter.endRDF();
	ByteArrayInputStream inputReader = new ByteArrayInputStream(outputWriter.toByteArray());
	RDFParser rdfParser = rdfParserFactory.getParser();
	setupParserConfig(rdfParser.getParserConfig());
	Model parsedOutput = new LinkedHashModel();
	rdfParser.setRDFHandler(new StatementCollector(parsedOutput));
	rdfParser.parse(inputReader, "");
	assertEquals(1, parsedOutput.size());
	assertTrue(parsedOutput.contains(uri1, uri1, uri1));
}
 
Example #23
Source File: RDFWriterTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testWriteCommentBNodeContextBNodeWithNamespaceBeforeNamespace() throws Exception {
	ByteArrayOutputStream outputWriter = new ByteArrayOutputStream();
	RDFWriter rdfWriter = rdfWriterFactory.getWriter(outputWriter);
	setupWriterConfig(rdfWriter.getWriterConfig());
	rdfWriter.startRDF();
	rdfWriter.handleNamespace("ex", exNs);
	rdfWriter.handleStatement(vf.createStatement(uri1, uri1, uri1, bnode));
	rdfWriter.handleComment("This comment should not screw up parsing");
	rdfWriter.handleNamespace("ex1", exNs);
	rdfWriter.handleStatement(vf.createStatement(uri1, uri1, uri2, bnode));
	rdfWriter.endRDF();
	ByteArrayInputStream inputReader = new ByteArrayInputStream(outputWriter.toByteArray());
	RDFParser rdfParser = rdfParserFactory.getParser();
	setupParserConfig(rdfParser.getParserConfig());
	Model parsedOutput = new LinkedHashModel();
	rdfParser.setRDFHandler(new StatementCollector(parsedOutput));
	rdfParser.parse(inputReader, "");
	assertEquals(2, parsedOutput.size());
	assertTrue(parsedOutput.contains(uri1, uri1, uri1));
	assertTrue(parsedOutput.contains(uri1, uri1, uri2));
	assertEquals(1, parsedOutput.contexts().size());
}
 
Example #24
Source File: RDFWriterTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testSuccessBNodeParsesAreDistinct() throws Exception {
	ByteArrayOutputStream outputWriter = new ByteArrayOutputStream();
	RDFWriter rdfWriter = rdfWriterFactory.getWriter(outputWriter);
	setupWriterConfig(rdfWriter.getWriterConfig());
	rdfWriter.startRDF();
	rdfWriter.handleStatement(vf.createStatement(uri1, uri1, bnode));
	rdfWriter.endRDF();
	ByteArrayInputStream inputReader = new ByteArrayInputStream(outputWriter.toByteArray());
	RDFParser rdfParser = rdfParserFactory.getParser();
	setupParserConfig(rdfParser.getParserConfig());
	Model parsedOutput = new LinkedHashModel();
	rdfParser.setRDFHandler(new StatementCollector(parsedOutput));
	rdfParser.parse(inputReader, "");
	assertEquals(1, parsedOutput.size());
	ByteArrayInputStream inputReader2 = new ByteArrayInputStream(outputWriter.toByteArray());
	rdfParser.parse(inputReader2, "");
	assertEquals(2, parsedOutput.size());
}
 
Example #25
Source File: RDFXMLParserCustomTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Test with Secure processing setting on.
 *
 * @throws Exception
 */
@Test
public void testEntityExpansionSecureProcessing() throws Exception {
	final Model aGraph = new LinkedHashModel();
	ParseErrorCollector errorCollector = new ParseErrorCollector();
	RDFParser aParser = Rio.createParser(RDFFormat.RDFXML)
			.setRDFHandler(new StatementCollector(aGraph))
			.set(XMLParserSettings.SECURE_PROCESSING, true)
			.setParseErrorListener(errorCollector);

	try {
		// this should trigger a SAX parse exception that will blow up at the
		// 64k entity limit rather than OOMing
		aParser.parse(
				this.getClass().getResourceAsStream("/testcases/rdfxml/openrdf/bad-entity-expansion-limit.rdf"),
				"http://example.org");
		fail("Parser did not throw an exception");
	} catch (RDFParseException e) {
		// assertTrue(e.getMessage().contains(
		// "The parser has encountered more than \"64,000\" entity expansions in this document; this is the limit
		// imposed by the "));
	}
	assertEquals(0, errorCollector.getWarnings().size());
	assertEquals(0, errorCollector.getErrors().size());
	assertEquals(1, errorCollector.getFatalErrors().size());
}
 
Example #26
Source File: AbstractNTriplesParserUnitTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test(expected = RDFParseException.class)
public void testBlankNodeIdentifiersWithDotAsLastCahracter() throws Exception {
	// The character . may appear anywhere except the first or last character.
	RDFParser ntriplesParser = new NTriplesParser();
	Model model = new LinkedHashModel();
	ntriplesParser.setRDFHandler(new StatementCollector(model));
	try {
		ntriplesParser.parse(new StringReader("_:123 <urn:test:predicate> _:456. ."), NTRIPLES_TEST_URL);
	} catch (RDFParseException e) {
		assertEquals(0, model.size());
		assertEquals(0, model.subjects().size());
		assertEquals(0, model.predicates().size());
		assertEquals(0, model.objects().size());
		throw e;
	}
	fail("Should have failed to parse invalid N-Triples bnode with '.' at the end of the bnode label");
}
 
Example #27
Source File: RDFXMLParserCustomTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Test with the default ParserConfig settings. Ie, setParserConfig is not called.
 *
 * @throws Exception
 */
@Test
public void testEntityExpansionDefaultSettings() throws Exception {
	final Model aGraph = new LinkedHashModel();
	ParseErrorCollector errorCollector = new ParseErrorCollector();
	RDFParser aParser = Rio.createParser(RDFFormat.RDFXML)
			.setRDFHandler(new StatementCollector(aGraph))
			.setParseErrorListener(errorCollector);

	try {
		// this should trigger a SAX parse exception that will blow up at the
		// 64k entity limit rather than OOMing
		aParser.parse(
				this.getClass().getResourceAsStream("/testcases/rdfxml/openrdf/bad-entity-expansion-limit.rdf"),
				"http://example.org");
		fail("Parser did not throw an exception");
	} catch (RDFParseException e) {
		// assertTrue(e.getMessage().contains(
		// "The parser has encountered more than \"64,000\" entity expansions in this document; this is the limit
		// imposed by the "));
	}
	assertEquals(0, errorCollector.getWarnings().size());
	assertEquals(0, errorCollector.getErrors().size());
	assertEquals(1, errorCollector.getFatalErrors().size());
}
 
Example #28
Source File: RDFWriterTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testListWithObject() throws Exception {
	Model input = new LinkedHashModel();
	Resource _1 = vf.createBNode();
	input.add(uri1, uri2, _1);
	input.add(_1, RDF.FIRST, bnode);
	input.add(bnode, RDF.TYPE, RDFS.RESOURCE);
	input.add(_1, RDF.REST, RDF.NIL);
	ByteArrayOutputStream outputWriter = new ByteArrayOutputStream();
	RDFWriter rdfWriter = rdfWriterFactory.getWriter(outputWriter);
	setupWriterConfig(rdfWriter.getWriterConfig());
	rdfWriter.startRDF();
	for (Statement st : input) {
		rdfWriter.handleStatement(st);
	}
	rdfWriter.endRDF();
	logger.debug(new String(outputWriter.toByteArray()));
	ByteArrayInputStream inputReader = new ByteArrayInputStream(outputWriter.toByteArray());
	RDFParser rdfParser = rdfParserFactory.getParser();
	setupParserConfig(rdfParser.getParserConfig());
	Model parsedOutput = new LinkedHashModel();
	rdfParser.setRDFHandler(new StatementCollector(parsedOutput));
	rdfParser.parse(inputReader, "");
	assertSameModel(input, parsedOutput);
}
 
Example #29
Source File: RDFWriterTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testBogusIRICharacters() throws Exception {
	Model model = new LinkedHashModel();
	String illegal = " <>^|\t\n\r\"`";
	for (int i = 0; i < illegal.length(); i++) {
		model.add(vf.createIRI("urn:test:char" + illegal.charAt(i)), RDF.TYPE, RDFS.RESOURCE);
	}
	ByteArrayOutputStream outputWriter = new ByteArrayOutputStream();
	RDFWriter rdfWriter = rdfWriterFactory.getWriter(outputWriter);
	setupWriterConfig(rdfWriter.getWriterConfig());
	rdfWriter.startRDF();
	model.forEach(st -> rdfWriter.handleStatement(st));
	rdfWriter.endRDF();
	ByteArrayInputStream inputReader = new ByteArrayInputStream(outputWriter.toByteArray());
	RDFParser rdfParser = rdfParserFactory.getParser();
	setupParserConfig(rdfParser.getParserConfig().set(BasicParserSettings.VERIFY_URI_SYNTAX, false));
	Model parsedOutput = new LinkedHashModel();
	rdfParser.setRDFHandler(new StatementCollector(parsedOutput));
	rdfParser.parse(inputReader, "");
	Assert.assertEquals(model.size(), parsedOutput.size());
	ByteArrayInputStream inputReader2 = new ByteArrayInputStream(outputWriter.toByteArray());
	rdfParser.parse(inputReader2, "");
	Assert.assertEquals(model.size(), parsedOutput.size());
}
 
Example #30
Source File: AbstractNQuadsWriterTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testReadWrite() throws RDFHandlerException, IOException, RDFParseException {
	StatementCollector statementCollector = new StatementCollector();
	parser.setRDFHandler(statementCollector);
	parser.parse(this.getClass().getResourceAsStream("/testcases/nquads/test2.nq"), "http://test.base.uri");

	Assert.assertEquals(400, statementCollector.getStatements().size());

	ByteArrayOutputStream baos = new ByteArrayOutputStream();
	writer = rdfWriterFactory.getWriter(baos);
	writer.startRDF();
	for (Statement nextStatement : statementCollector.getStatements()) {
		writer.handleStatement(nextStatement);
	}
	writer.endRDF();

	Assert.assertEquals("Unexpected number of lines.", 400, baos.toString().split("\n").length);
}