Skip to content
Permalink
Browse files
Tests should manage resources with try-with-resources blocks.
  • Loading branch information
garydgregory committed Oct 30, 2017
1 parent 7f8b028 commit 7a05892144434e3f8b8dc671171493038aed4384
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 77 deletions.
@@ -344,13 +344,12 @@ public void getQuadsQuery() throws Exception {
}

@Test
public void addBlankNodesFromMultipleDatasets() {
// Create two separate Dataset instances
final Dataset g1 = createDataset1();
final Dataset g2 = createDataset2();
public void addBlankNodesFromMultipleDatasets() throws Exception {
// Create two separate Dataset instances
try (final Dataset g1 = createDataset1();
final Dataset g2 = createDataset2();
final Dataset g3 = factory.createDataset()) {

// and add them to a new Dataset g3
final Dataset g3 = factory.createDataset();
addAllQuads(g1, g3);
addAllQuads(g2, g3);

@@ -408,6 +407,7 @@ public void addBlankNodesFromMultipleDatasets() {
// and these don't have any children (as far as we know)
assertFalse(g3.contains(null, b2Bob, hasChild, null));
assertFalse(g3.contains(null, b1Charlie, hasChild, null));
}
}

private void notEquals(final BlankNodeOrIRI node1, final BlankNodeOrIRI node2) {
@@ -537,37 +537,40 @@ public void getGraphNames() throws Exception {

@Test
public void getGraph() throws Exception {
final Graph defaultGraph = dataset.getGraph();
// TODO: Can we assume the default graph was empty before our new triples?
assertEquals(2, defaultGraph.size());
assertTrue(defaultGraph.contains(alice, isPrimaryTopicOf, graph1));
// NOTE: graph2 is a BlankNode
assertTrue(defaultGraph.contains(bob, isPrimaryTopicOf, null));
try (final Graph defaultGraph = dataset.getGraph()) {
// TODO: Can we assume the default graph was empty before our new triples?
assertEquals(2, defaultGraph.size());
assertTrue(defaultGraph.contains(alice, isPrimaryTopicOf, graph1));
// NOTE: graph2 is a BlankNode
assertTrue(defaultGraph.contains(bob, isPrimaryTopicOf, null));
}
}


@Test
public void getGraphNull() throws Exception {
// Default graph should be present
final Graph defaultGraph = dataset.getGraph(null).get();
// TODO: Can we assume the default graph was empty before our new triples?
assertEquals(2, defaultGraph.size());
assertTrue(defaultGraph.contains(alice, isPrimaryTopicOf, graph1));
// NOTE: wildcard as graph2 is a (potentially mapped) BlankNode
assertTrue(defaultGraph.contains(bob, isPrimaryTopicOf, null));
try (final Graph defaultGraph = dataset.getGraph(null).get()) {
// TODO: Can we assume the default graph was empty before our new triples?
assertEquals(2, defaultGraph.size());
assertTrue(defaultGraph.contains(alice, isPrimaryTopicOf, graph1));
// NOTE: wildcard as graph2 is a (potentially mapped) BlankNode
assertTrue(defaultGraph.contains(bob, isPrimaryTopicOf, null));
}
}


@Test
public void getGraph1() throws Exception {
// graph1 should be present
final Graph g1 = dataset.getGraph(graph1).get();
assertEquals(4, g1.size());
try (final Graph g1 = dataset.getGraph(graph1).get()) {
assertEquals(4, g1.size());

assertTrue(g1.contains(alice, name, aliceName));
assertTrue(g1.contains(alice, knows, bob));
assertTrue(g1.contains(alice, member, null));
assertTrue(g1.contains(null, name, secretClubName));
assertTrue(g1.contains(alice, name, aliceName));
assertTrue(g1.contains(alice, knows, bob));
assertTrue(g1.contains(alice, member, null));
assertTrue(g1.contains(null, name, secretClubName));
}
}

@Test
@@ -577,17 +580,17 @@ public void getGraph2() throws Exception {
final BlankNodeOrIRI graph2Name = (BlankNodeOrIRI) dataset.stream(Optional.empty(), bob, isPrimaryTopicOf, null)
.map(Quad::getObject).findAny().get();

final Graph g2 = dataset.getGraph(graph2Name).get();
assertEquals(4, g2.size());
final Triple bobNameTriple = bobNameQuad.asTriple();
assertTrue(g2.contains(bobNameTriple));
assertTrue(g2.contains(bob, member, bnode1));
assertTrue(g2.contains(bob, member, bnode2));
assertFalse(g2.contains(bnode1, name, secretClubName));
assertTrue(g2.contains(bnode2, name, companyName));
try (final Graph g2 = dataset.getGraph(graph2Name).get()) {
assertEquals(4, g2.size());
final Triple bobNameTriple = bobNameQuad.asTriple();
assertTrue(g2.contains(bobNameTriple));
assertTrue(g2.contains(bob, member, bnode1));
assertTrue(g2.contains(bob, member, bnode2));
assertFalse(g2.contains(bnode1, name, secretClubName));
assertTrue(g2.contains(bnode2, name, companyName));
}
}


@Test
public void containsLanguageTagsCaseInsensitive() {
// COMMONSRDF-51: Ensure we can add/contains/remove with any casing
@@ -316,15 +316,11 @@ public void getTriplesQuery() throws Exception {
}

@Test
public void addBlankNodesFromMultipleGraphs() {
public void addBlankNodesFromMultipleGraphs() throws Exception {

try {
// Create two separate Graph instances
final Graph g1 = createGraph1();
final Graph g2 = createGraph2();

// and add them to a new Graph g3
final Graph g3 = factory.createGraph();
// Create two separate Graph instances
// and add them to a new Graph g3
try (final Graph g1 = createGraph1(); final Graph g2 = createGraph2(); final Graph g3 = factory.createGraph()) {
addAllTriples(g1, g3);
addAllTriples(g2, g3);

@@ -387,7 +383,7 @@ public void addBlankNodesFromMultipleGraphs() {
}

@Test
public void containsLanguageTagsCaseInsensitive() {
public void containsLanguageTagsCaseInsensitive() throws Exception {
// COMMONSRDF-51: Ensure we can add/contains/remove with any casing
// of literal language tag
final Literal lower = factory.createLiteral("Hello", "en-gb");
@@ -397,35 +393,35 @@ public void containsLanguageTagsCaseInsensitive() {
final IRI example1 = factory.createIRI("http://example.com/s1");
final IRI greeting = factory.createIRI("http://example.com/greeting");

final Graph graph = factory.createGraph();
graph.add(example1, greeting, upper);
try (final Graph graph = factory.createGraph()) {
graph.add(example1, greeting, upper);

// any kind of Triple should match
assertTrue(graph.contains(factory.createTriple(example1, greeting, upper)));
assertTrue(graph.contains(factory.createTriple(example1, greeting, lower)));
assertTrue(graph.contains(factory.createTriple(example1, greeting, mixed)));
// any kind of Triple should match
assertTrue(graph.contains(factory.createTriple(example1, greeting, upper)));
assertTrue(graph.contains(factory.createTriple(example1, greeting, lower)));
assertTrue(graph.contains(factory.createTriple(example1, greeting, mixed)));

// or as patterns
assertTrue(graph.contains(null, null, upper));
assertTrue(graph.contains(null, null, lower));
assertTrue(graph.contains(null, null, mixed));
// or as patterns
assertTrue(graph.contains(null, null, upper));
assertTrue(graph.contains(null, null, lower));
assertTrue(graph.contains(null, null, mixed));
}
}

@Test
public void containsLanguageTagsCaseInsensitiveTurkish() {
public void containsLanguageTagsCaseInsensitiveTurkish() throws Exception {
// COMMONSRDF-51: Special test for Turkish issue where
// "i".toLowerCase() != "i"
// See also:
// https://garygregory.wordpress.com/2015/11/03/java-lowercase-conversion-turkey/

// This is similar to the test in AbstractRDFTest, but on a graph
final Locale defaultLocale = Locale.getDefault();
try {
try (final Graph g = factory.createGraph()) {
Locale.setDefault(Locale.ROOT);
final Literal lowerROOT = factory.createLiteral("moi", "fi");
final Literal upperROOT = factory.createLiteral("moi", "FI");
final Literal mixedROOT = factory.createLiteral("moi", "fI");
final Graph g = factory.createGraph();
final IRI exampleROOT = factory.createIRI("http://example.com/s1");
final IRI greeting = factory.createIRI("http://example.com/greeting");
g.add(exampleROOT, greeting, mixedROOT);
@@ -472,7 +468,7 @@ public void containsLanguageTagsCaseInsensitiveTurkish() {


@Test
public void removeLanguageTagsCaseInsensitive() {
public void removeLanguageTagsCaseInsensitive() throws Exception {
// COMMONSRDF-51: Ensure we can remove with any casing
// of literal language tag
final Literal lower = factory.createLiteral("Hello", "en-gb");
@@ -482,20 +478,21 @@ public void removeLanguageTagsCaseInsensitive() {
final IRI example1 = factory.createIRI("http://example.com/s1");
final IRI greeting = factory.createIRI("http://example.com/greeting");

final Graph graph = factory.createGraph();
graph.add(example1, greeting, upper);
try (final Graph graph = factory.createGraph()) {
graph.add(example1, greeting, upper);

// Remove should also honour any case
graph.remove(example1, null, mixed);
assertFalse(graph.contains(null, greeting, null));
// Remove should also honour any case
graph.remove(example1, null, mixed);
assertFalse(graph.contains(null, greeting, null));

graph.add(example1, greeting, lower);
graph.remove(example1, null, upper);
graph.add(example1, greeting, lower);
graph.remove(example1, null, upper);

// Check with Triple
graph.add(factory.createTriple(example1, greeting, mixed));
graph.remove(factory.createTriple(example1, greeting, upper));
assertFalse(graph.contains(null, greeting, null));
// Check with Triple
graph.add(factory.createTriple(example1, greeting, mixed));
graph.remove(factory.createTriple(example1, greeting, upper));
assertFalse(graph.contains(null, greeting, null));
}
}

private static Optional<? extends Triple> closableFindAny(final Stream<? extends Triple> stream) {
@@ -505,7 +502,7 @@ private static Optional<? extends Triple> closableFindAny(final Stream<? extends
}

@Test
public void streamLanguageTagsCaseInsensitive() {
public void streamLanguageTagsCaseInsensitive() throws Exception {
// COMMONSRDF-51: Ensure we can add/contains/remove with any casing
// of literal language tag
final Literal lower = factory.createLiteral("Hello", "en-gb");
@@ -515,17 +512,18 @@ public void streamLanguageTagsCaseInsensitive() {
final IRI example1 = factory.createIRI("http://example.com/s1");
final IRI greeting = factory.createIRI("http://example.com/greeting");

final Graph graph = factory.createGraph();
graph.add(example1, greeting, upper);
try (final Graph graph = factory.createGraph()) {
graph.add(example1, greeting, upper);

// or as patterns
assertTrue(closableFindAny(graph.stream(null, null, upper)).isPresent());
assertTrue(closableFindAny(graph.stream(null, null, lower)).isPresent());
assertTrue(closableFindAny(graph.stream(null, null, mixed)).isPresent());
// or as patterns
assertTrue(closableFindAny(graph.stream(null, null, upper)).isPresent());
assertTrue(closableFindAny(graph.stream(null, null, lower)).isPresent());
assertTrue(closableFindAny(graph.stream(null, null, mixed)).isPresent());

// Check the triples returned equal a new triple
final Triple t = closableFindAny(graph.stream(null, null, lower)).get();
assertEquals(t, factory.createTriple(example1, greeting, mixed));
// Check the triples returned equal a new triple
final Triple t = closableFindAny(graph.stream(null, null, lower)).get();
assertEquals(t, factory.createTriple(example1, greeting, mixed));
}
}

private void notEquals(final BlankNodeOrIRI node1, final BlankNodeOrIRI node2) {

0 comments on commit 7a05892

Please sign in to comment.