Skip to content

Commit

Permalink
GH-4990: adjust cloning for context var + unit test coverage
Browse files Browse the repository at this point in the history
Mage sure to have null-safe clone also for the context variable. Covered
the finding with a unit test
  • Loading branch information
aschwarte10 committed May 31, 2024
1 parent a173941 commit 4d903e4
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ protected Set<Endpoint> performSourceSelection(FedXArbitraryLengthPath pathExpr,
identifiedMembers = new HashSet<>(members);
} else {
StatementPattern checkStmt = new StatementPattern(stmt.getScope(), new Var("subject"),
stmt.getPredicateVar().clone(), new Var("object"), stmt.getContextVar());
clone(stmt.getPredicateVar()), new Var("object"), clone(stmt.getContextVar()));
@SuppressWarnings("unused") // only used as artificial parent
HolderNode holderParent = new HolderNode(checkStmt);

Expand Down Expand Up @@ -364,6 +364,13 @@ protected Set<Endpoint> performSourceSelection(FedXArbitraryLengthPath pathExpr,
return identifiedMembers;
}

private Var clone(Var var) {
if (var == null) {
return null;
}
return var.clone();
}

protected void optimizeJoinOrder(TupleExpr query, QueryInfo queryInfo, GenericInfoOptimizer info) {
// optimize statement groups and join order
new StatementGroupAndJoinOptimizer(queryInfo, DefaultFedXCostModel.INSTANCE).optimize(query);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,4 +301,52 @@ public void testZeroLengthPath_length2_crossRepository() throws Exception {

}

@Test
public void testPropertyPath_sourceSelection_crossRepository() throws Exception {

prepareTest(Arrays.asList("/tests/basic/data_emptyStore.ttl", "/tests/basic/data_emptyStore.ttl"));

Repository repo1 = getRepository(1);
Repository repo2 = getRepository(2);

try (RepositoryConnection con = repo1.getConnection()) {
con.add(Values.iri("http://example.org/A"), RDFS.SUBCLASSOF, Values.iri("http://example.org/B"),
Values.iri("http://example.org/graph1"));
}

try (RepositoryConnection con = repo2.getConnection()) {
con.add(Values.iri("http://example.org/B"), RDFS.SUBCLASSOF, Values.iri("http://example.org/C"),
Values.iri("http://example.org/graph2"));
}

Repository fedxRepo = fedxRule.getRepository();

// 1a: bound (matching) object
try (RepositoryConnection con = fedxRepo.getConnection()) {
TupleQuery tupleQuery = con.prepareTupleQuery(
"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> "
+ "SELECT * WHERE { "
+ " ?subClass (rdfs:subClassOf+) <http://example.org/C> . "
+ " } "
);

Assertions.assertEquals(2, QueryResults.asSet(tupleQuery.evaluate()).size());
}

// 1b: with named graph
try (RepositoryConnection con = fedxRepo.getConnection()) {
TupleQuery tupleQuery = con.prepareTupleQuery(
"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> "
+ "SELECT * WHERE { "
+ " GRAPH <http://example.org/graph2> {"
+ " ?subClass (rdfs:subClassOf+) <http://example.org/C> . "
+ " }"
+ "} "
);

Assertions.assertEquals(1, QueryResults.asSet(tupleQuery.evaluate()).size());
}

}

}

0 comments on commit 4d903e4

Please sign in to comment.