/**
 * 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 com.github.commonsrdf.dummyimpl;

import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.stream.Stream;

import org.junit.Before;
import org.junit.Test;

import com.github.commonsrdf.api.BlankNode;
import com.github.commonsrdf.api.IRI;

public class TestWritingGraph {

	private GraphImpl graph;

	@Before
	public void createGraph() throws Exception {
		graph = new GraphImpl();
		BlankNode subject = new BlankNodeImpl("subj");
		IRI predicate = new IRIImpl("pred");
		// 200k triples should do
		for (int i=0; i<200000; i++) {
			graph.add(subject, predicate, 
					new LiteralImpl("Example " + i, "en"));		
		}		
	}
	
	@Test
	public void writeGraphFromStream() throws Exception {
		Path graphFile = Files.createTempFile("graph", ".nt");
		System.out.println("From stream: " + graphFile);		
		Stream<CharSequence> stream = graph.getTriples().unordered().parallel().
				map(Object::toString);
		Files.write(graphFile, 
				stream::iterator,
				Charset.forName("UTF-8"));
	}

	@Test
	public void writeGraphFromStreamFiltered() throws Exception {
		Path graphFile = Files.createTempFile("graph", ".nt");		
		System.out.println("Filtered stream: " + graphFile);
		BlankNode subject = new BlankNodeImpl("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"));
		
	}

}
 
@@ -884,16 +884,150 @@
 raph %7B%0A%0A
+%09/** Run tests with -Dkeepfiles=true to inspect /tmp files **/%0A%09private static boolean KEEP_FILES = Boolean.getBoolean(%22keepfiles%22);%0A%0A
 %09private
@@ -1259,14 +1259,18 @@
 nt i
-=
+ = 
 0; i
-%3C
+ %3C 
 2000
@@ -1317,14 +1317,8 @@
 te, 
-%0A%09%09%09%09%09
 new 
@@ -1356,21 +1356,16 @@
 %22));
-%09%09
 %0A%09%09%7D
-%09%09
 %0A%09%7D%0A
-%09
 %0A%09@T
@@ -1480,16 +1480,37 @@
 %22.nt%22);%0A
+%09%09if (KEEP_FILES) %7B%0A%09
 %09%09System
@@ -1551,18 +1551,70 @@
 phFile);
+%0A
 %09%09
+%7D else %7B%0A%09%09%09graphFile.toFile().deleteOnExit();%0A%09%09%7D%0A
 %0A%09%09Strea
@@ -1679,22 +1679,22 @@
 rallel()
-.
 %0A%09%09%09%09
+.
 map(Obje
@@ -1725,37 +1725,32 @@
 rite(graphFile, 
-%0A%09%09%09%09
 stream::iterator
@@ -1742,37 +1742,33 @@
 tream::iterator,
-%0A%09%09%09%09
+ 
 Charset.forName(
@@ -1908,19 +1908,38 @@
  %22.nt%22);
+%0A
 %09%09
-%0A
+if (KEEP_FILES) %7B%0A%09
 %09%09System
@@ -1985,16 +1985,72 @@
 hFile);%0A
+%09%09%7D else %7B%0A%09%09%09graphFile.toFile().deleteOnExit();%0A%09%09%7D%0A%09%09%0A
 %09%09BlankN
@@ -2089,17 +2089,16 @@
 %22subj%22);
- 
 %0A%09%09IRI p
@@ -2158,32 +2158,37 @@
 %3E stream = graph
+%0A%09%09%09%09
 .getTriples(subj
@@ -2209,21 +2209,16 @@
 , null).
-%0A%09%09%09%09
 map(Obje
@@ -2257,21 +2257,16 @@
 phFile, 
-%0A%09%09%09%09
 stream::
@@ -2274,21 +2274,17 @@
 terator,
-%0A%09%09%09%09
+ 
 Charset.
@@ -2306,14 +2306,11 @@
 ));%0A
-%09%09
 %0A%09%7D%0A%0A%7D%0A
- 

Support -Dkeepfiles=true