commonsrdf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From st...@apache.org
Subject incubator-commonsrdf git commit: Ensure tests use try autoclose with graph.stream()
Date Thu, 06 Oct 2016 14:47:29 GMT
Repository: incubator-commonsrdf
Updated Branches:
  refs/heads/rdf4j d058f6672 -> 95cda336c


Ensure tests use try autoclose with graph.stream()

otherwise rdf4j shutdown will take 20 seconds per test!

NativeStoreGraphTest now tests with brand new repository


Project: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/commit/95cda336
Tree: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/tree/95cda336
Diff: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/diff/95cda336

Branch: refs/heads/rdf4j
Commit: 95cda336cffa92809408a6a02ac99a8e6f8a53cb
Parents: d058f66
Author: Stian Soiland-Reyes <stain@apache.org>
Authored: Tue Oct 4 18:55:17 2016 +0100
Committer: Stian Soiland-Reyes <stain@apache.org>
Committed: Tue Oct 4 18:57:01 2016 +0100

----------------------------------------------------------------------
 .../commons/rdf/api/AbstractGraphTest.java      | 38 ++++++---
 .../commons/rdf/rdf4j/NativeStoreGraphTest.java | 85 ++++++++++----------
 2 files changed, 69 insertions(+), 54 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/95cda336/api/src/test/java/org/apache/commons/rdf/api/AbstractGraphTest.java
----------------------------------------------------------------------
diff --git a/api/src/test/java/org/apache/commons/rdf/api/AbstractGraphTest.java b/api/src/test/java/org/apache/commons/rdf/api/AbstractGraphTest.java
index 4dba5cb..cb78c05 100644
--- a/api/src/test/java/org/apache/commons/rdf/api/AbstractGraphTest.java
+++ b/api/src/test/java/org/apache/commons/rdf/api/AbstractGraphTest.java
@@ -30,6 +30,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.Optional;
 import java.util.concurrent.ConcurrentHashMap;
+import java.util.stream.Stream;
 
 import org.junit.Assume;
 import org.junit.Before;
@@ -184,11 +185,13 @@ public abstract class AbstractGraphTest {
 
         assertTrue(graph.contains(alice, knows, bob));
 
-        Optional<? extends Triple> first = graph.stream().skip(4)
-                .findFirst();
-        Assume.assumeTrue(first.isPresent());
-        Triple existingTriple = first.get();
-        assertTrue(graph.contains(existingTriple));
+        try (Stream<? extends Triple> stream = graph.stream()) {
+			Optional<? extends Triple> first = stream.skip(4)
+	                .findFirst();			
+	        Assume.assumeTrue(first.isPresent());
+	        Triple existingTriple = first.get();
+	        assertTrue(graph.contains(existingTriple));
+        }
 
         Triple nonExistingTriple = factory.createTriple(bob, knows, alice);
         assertFalse(graph.contains(nonExistingTriple));
@@ -225,10 +228,13 @@ public abstract class AbstractGraphTest {
         graph.remove(alice, knows, bob);
         assertEquals(shrunkSize, graph.size());
 
-        Optional<? extends Triple> anyTriple = graph.stream().findAny();
-        Assume.assumeTrue(anyTriple.isPresent());
+        Triple otherTriple;
+        try (Stream<? extends Triple> stream = graph.stream()) {
+        	Optional<? extends Triple> anyTriple = stream.findAny();
+        	Assume.assumeTrue(anyTriple.isPresent());
+			otherTriple = anyTriple.get();
+        }
 
-        Triple otherTriple = anyTriple.get();
         graph.remove(otherTriple);
         assertEquals(shrunkSize - 1, graph.size());
         graph.remove(otherTriple);
@@ -252,9 +258,16 @@ public abstract class AbstractGraphTest {
 
     @Test
     public void getTriples() throws Exception {
-        long tripleCount = graph.stream().count();
+    	long tripleCount;
+        try (Stream<? extends Triple> stream = graph.stream()) {
+			tripleCount = stream.count();
+        }
         assertTrue(tripleCount > 0);
-        assertTrue(graph.stream().allMatch(t -> graph.contains(t)));
+
+        try (Stream<? extends Triple> stream = graph.stream()) {
+        	assertTrue(stream.allMatch(t -> graph.contains(t)));
+        }
+        
         // Check exact count
         Assume.assumeNotNull(bnode1, bnode2, aliceName, bobName, secretClubName,
                 companyName, bobNameTriple);
@@ -365,7 +378,10 @@ public abstract class AbstractGraphTest {
 
         // unordered() as we don't need to preserve triple order
         // sequential() as we don't (currently) require target Graph to be thread-safe
-        source.stream().unordered().sequential().forEach(t -> target.add(t));
+        
+    	try (Stream<? extends Triple> stream = source.stream()) {
+    		stream.unordered().sequential().forEach(t -> target.add(t));
+    	}
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/95cda336/rdf4j/src/test/java/org/apache/commons/rdf/rdf4j/NativeStoreGraphTest.java
----------------------------------------------------------------------
diff --git a/rdf4j/src/test/java/org/apache/commons/rdf/rdf4j/NativeStoreGraphTest.java b/rdf4j/src/test/java/org/apache/commons/rdf/rdf4j/NativeStoreGraphTest.java
index bac936f..392d63d 100644
--- a/rdf4j/src/test/java/org/apache/commons/rdf/rdf4j/NativeStoreGraphTest.java
+++ b/rdf4j/src/test/java/org/apache/commons/rdf/rdf4j/NativeStoreGraphTest.java
@@ -18,13 +18,7 @@
 package org.apache.commons.rdf.rdf4j;
 
 import java.io.IOException;
-import java.nio.file.FileVisitResult;
-import java.nio.file.Files;
-import java.nio.file.Path;
-import java.nio.file.SimpleFileVisitor;
-import java.nio.file.attribute.BasicFileAttributes;
-import java.util.Collections;
-import java.util.Set;
+import java.io.UncheckedIOException;
 
 import org.apache.commons.rdf.api.AbstractGraphTest;
 import org.apache.commons.rdf.api.BlankNodeOrIRI;
@@ -37,8 +31,9 @@ import org.eclipse.rdf4j.repository.RepositoryResult;
 import org.eclipse.rdf4j.repository.sail.SailRepository;
 import org.eclipse.rdf4j.sail.Sail;
 import org.eclipse.rdf4j.sail.nativerdf.NativeStore;
-import org.junit.AfterClass;
-import org.junit.BeforeClass;
+import org.junit.After;
+import org.junit.Rule;
+import org.junit.rules.TemporaryFolder;
 
 
 /**
@@ -57,13 +52,11 @@ public class NativeStoreGraphTest extends AbstractGraphTest {
 
 	public final class NativeStoreFactory implements RDFTermFactory {
 
-		RDF4JTermFactory rdf4jFactory = new RDF4JTermFactory(repository.getValueFactory());
+		RDF4JTermFactory rdf4jFactory = new RDF4JTermFactory(getRepository().getValueFactory());
 
 		@Override
 		public RDF4JGraph createGraph() {
-			// We re-use the repository connection, but use a different context every time
-			Set<RDF4JBlankNode> context = Collections.singleton(rdf4jFactory.createBlankNode());
-			return rdf4jFactory.asRDFTermGraph(repository, context);
+			return rdf4jFactory.asRDFTermGraph(getRepository());
 		}
 
 		// Delegate methods 
@@ -90,49 +83,55 @@ public class NativeStoreGraphTest extends AbstractGraphTest {
 		}
 	}
 
-	private static SailRepository repository;
-	private static Path tempDir;
+
+	@Rule
+	public TemporaryFolder tempDir = new TemporaryFolder();
+
+	private SailRepository repository;
 	
-	@BeforeClass
-	public static void createRepository() {
-		try {
-			tempDir = Files.createTempDirectory("test-commonsrdf-rdf4j");
-		} catch (IOException e) {
-			throw new RuntimeException(e);
-		}
-		Sail sail = new NativeStore(tempDir.toFile());
+	public void createRepository() throws IOException {
+		System.out.println("Oh");
+		Sail sail = new NativeStore(tempDir.newFolder());
 		repository = new SailRepository(sail);
 		repository.initialize();
 	}
 
-	@AfterClass
-	public static void shutdownAndDelete() throws IOException {
+	public synchronized SailRepository getRepository() {
+		if (repository == null) {
+			try {
+				createRepository();
+			} catch (IOException e) {
+				throw new UncheckedIOException(e);
+			}
+		}
+		return repository;
+	}
+	
+	@After
+	public void shutdownAndDelete() throws IOException {
 		// must shutdown before we delete
 		if (repository != null) {
 			System.out.println("Shutting down rdf4j repository " + repository);
 			repository.shutDown();
 			System.out.println("rdf4j repository shut down.");
 		}
-		if (tempDir != null) {
-			deleteAll(tempDir);
-		}
 	}
 	
-	private static void deleteAll(Path dir) throws IOException {
-		Files.walkFileTree(dir, new SimpleFileVisitor<Path>(){
-			@Override
-			public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException
{
-				Files.delete(file);
-				return FileVisitResult.CONTINUE;
-			}
-			@Override
-			public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException
{
-				FileVisitResult r = super.postVisitDirectory(dir, exc);
-				Files.delete(dir);
-				return r;
-			}
-		});
-	}
+//	private static void deleteAll(Path dir) throws IOException {
+//		Files.walkFileTree(dir, new SimpleFileVisitor<Path>(){
+//			@Override
+//			public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException
{
+//				Files.delete(file);
+//				return FileVisitResult.CONTINUE;
+//			}
+//			@Override
+//			public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException
{
+//				FileVisitResult r = super.postVisitDirectory(dir, exc);
+//				Files.delete(dir);
+//				return r;
+//			}
+//		});
+//	}
 
 	@Override
 	public RDFTermFactory createFactory() {


Mime
View raw message