You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commonsrdf.apache.org by wi...@apache.org on 2015/03/27 19:44:46 UTC

[4/7] incubator-commonsrdf git commit: COMMONSRDF-2: refactorized simple impl java packages

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/f3a452ea/simple/src/test/java/org/apache/commons/rdf/simple/TestWritingGraph.java
----------------------------------------------------------------------
diff --git a/simple/src/test/java/org/apache/commons/rdf/simple/TestWritingGraph.java b/simple/src/test/java/org/apache/commons/rdf/simple/TestWritingGraph.java
new file mode 100644
index 0000000..c434d45
--- /dev/null
+++ b/simple/src/test/java/org/apache/commons/rdf/simple/TestWritingGraph.java
@@ -0,0 +1,96 @@
+/**
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.rdf.simple;
+
+import java.nio.charset.Charset;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.Optional;
+import java.util.stream.Stream;
+
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import org.apache.commons.rdf.api.BlankNode;
+import org.apache.commons.rdf.api.IRI;
+
+public class TestWritingGraph {
+
+	/*
+	 * 200k triples should do - about 7 MB on disk. Override with
+	 * -Dtriples=20000000 to exercise your memory banks.
+	 */
+	private static final int TRIPLES = Integer.getInteger("triples", 200000);
+
+	/** Run tests with -Dkeepfiles=true to inspect /tmp files **/
+	private static boolean KEEP_FILES = Boolean.getBoolean("keepfiles");
+
+	private static GraphImpl graph;
+
+	@BeforeClass
+	public static void createGraph() throws Exception {
+		graph = new GraphImpl();
+		BlankNode subject = new BlankNodeImpl(Optional.of(graph), "subj");
+		IRI predicate = new IRIImpl("pred");
+		for (int i = 0; i < TRIPLES; i++) {
+			graph.add(subject, predicate, new LiteralImpl("Example " + i, "en"));
+		}
+	}
+
+	@Test
+	public void createGraphTiming() throws Exception {
+		createGraph();
+	}
+	
+	@Test
+	public void countQuery() {
+		BlankNode subject = new BlankNodeImpl(Optional.of(graph), "subj");
+		IRI predicate = new IRIImpl("pred");
+		long count = graph.getTriples(subject, predicate, null).unordered()
+				.parallel().count();
+		System.out.println("Counted - " + count);
+	}
+
+	@Test
+	public void writeGraphFromStream() throws Exception {
+		Path graphFile = Files.createTempFile("graph", ".nt");
+		if (KEEP_FILES) {
+			System.out.println("From stream: " + graphFile);
+		} else {
+			graphFile.toFile().deleteOnExit();
+		}
+		
+		Stream<CharSequence> stream = graph.getTriples().unordered()
+				.map(Object::toString);
+		Files.write(graphFile, stream::iterator, Charset.forName("UTF-8"));
+	}
+
+	@Test
+	public void writeGraphFromStreamFiltered() throws Exception {
+		Path graphFile = Files.createTempFile("graph", ".nt");
+		if (KEEP_FILES) {
+			System.out.println("Filtered stream: " + graphFile);
+		} else {
+			graphFile.toFile().deleteOnExit();
+		}
+
+		BlankNode subject = new BlankNodeImpl(Optional.of(graph), "subj");
+		IRI predicate = new IRIImpl("pred");
+		Stream<CharSequence> stream = graph
+				.getTriples(subject, predicate, null).map(Object::toString);
+		Files.write(graphFile, stream::iterator, Charset.forName("UTF-8"));
+
+	}
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/f3a452ea/simple/src/test/java/org/apache/commons/rdf/simple/TypesTest.java
----------------------------------------------------------------------
diff --git a/simple/src/test/java/org/apache/commons/rdf/simple/TypesTest.java b/simple/src/test/java/org/apache/commons/rdf/simple/TypesTest.java
new file mode 100644
index 0000000..25c118c
--- /dev/null
+++ b/simple/src/test/java/org/apache/commons/rdf/simple/TypesTest.java
@@ -0,0 +1,67 @@
+/**
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.rdf.simple;
+
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+
+/**
+ * Tests for the {@link Types} enumeration.
+ * 
+ * @author Peter Ansell p_ansell@yahoo.com
+ */
+public class TypesTest {
+
+	/**
+	 * Test method for {@link Types#getIRIString()}
+	 * .
+	 */
+	@Test
+	public final void testGetIRIString() {
+		assertEquals("http://www.w3.org/1999/02/22-rdf-syntax-ns#langString",
+				Types.RDF_LANGSTRING.getIRIString());
+	}
+
+	/**
+	 * Test method for
+	 * {@link Types#ntriplesString()}.
+	 */
+	@Test
+	public final void testNtriplesString() {
+		assertEquals("<http://www.w3.org/1999/02/22-rdf-syntax-ns#langString>",
+				Types.RDF_LANGSTRING.ntriplesString());
+	}
+
+	/**
+	 * Test method for
+	 * {@link Types#get(org.apache.commons.rdf.api.IRI)}
+	 * .
+	 */
+	@Test
+	public final void testGet() {
+		assertTrue(Types.get(
+				new IRIImpl("http://www.w3.org/2001/XMLSchema#boolean"))
+				.isPresent());
+		assertEquals(
+				"http://www.w3.org/2001/XMLSchema#boolean",
+				Types.get(
+						new IRIImpl("http://www.w3.org/2001/XMLSchema#boolean"))
+						.get().getIRIString());
+		assertFalse(Types.get(
+				new IRIImpl("http://www.w3.org/2001/XMLSchema#nonExistent"))
+				.isPresent());
+	}
+
+}