package nl.nn.adapterframework.util;

import static org.junit.Assert.assertEquals;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.concurrent.TimeoutException;

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

import nl.nn.adapterframework.align.AlignTestBase;
import nl.nn.adapterframework.configuration.ConfigurationException;
import nl.nn.adapterframework.core.IPipeLineSession;
import nl.nn.adapterframework.core.PipeLineSessionBase;

public class FileHandlerTest {

	public static String BASEDIR="/FileHandler/";

	private FileHandler handler;
	private IPipeLineSession session=new PipeLineSessionBase();
	
	@Before
	public void setup() throws ConfigurationException {
		handler = new FileHandler();
	}
	
	public URL getURL(String file) {
		return FileHandlerTest.class.getResource(BASEDIR+file);
	}
 
	protected String getTestFile(String file, String charset) throws IOException, TimeoutException {
		URL url=AlignTestBase.class.getResource(BASEDIR+file);
		if (url==null) {
			return null;
		}
        BufferedReader buf = new BufferedReader(new InputStreamReader(url.openStream(),charset));
        StringBuilder string = new StringBuilder();
        String line = buf.readLine();
        while (line != null) {
            string.append(line);
            line = buf.readLine();
            if (line!=null) {
            	string.append("\n");
            }
        }
        return string.toString();
    }
	
	public void testRead(String filename, String charset) throws Exception {
		URL fileURL=getURL(filename);
		String filepath=fileURL.getPath();
		
		handler.setActions("read");
		handler.setCharset(charset);
		handler.setFileName(filepath);
		handler.configure();
		
		String expectedContents=getTestFile(filename, charset);
		Object actualContents=handler.handle(null,session,null);
		
		assertEquals("file contents", expectedContents, actualContents);
	}
	
	@Test
	public void testRead() throws Exception {
		testRead("smiley.xml","utf-8");
		testRead("smiley.json","utf-8");
		testRead("smiley.txt","utf-8");
	}
	
}

@@ -1078,24 +1078,18 @@
 ll;%0A%09%09%7D%0A
-        
+%09%09
 Buffered
@@ -1170,24 +1170,18 @@
 rset));%0A
-        
+%09%09
 StringBu
@@ -1216,24 +1216,18 @@
 lder();%0A
-        
+%09%09
 String l
@@ -1248,24 +1248,18 @@
 Line();%0A
-        
+%09%09
 while (l
@@ -1273,28 +1273,19 @@
 null) %7B%0A
-            
+%09%09%09
 string.a
@@ -1297,28 +1297,19 @@
 (line);%0A
-            
+%09%09%09
 line = b
@@ -1323,28 +1323,19 @@
 Line();%0A
-            
+%09%09%09
 if (line
@@ -1344,28 +1344,19 @@
 null) %7B%0A
-            
+%09%09%09
 %09string.
@@ -1373,40 +1373,19 @@
 %22);%0A
-            %7D%0A        %7D%0A        
+%09%09%09%7D%0A%09%09%7D%0A%09%09
 retu
@@ -1410,15 +1410,11 @@
 ();%0A
-    
+%09
 %7D%0A
-%09
 %0A%09pu
@@ -1740,14 +1740,14 @@
 ;%0A%09%09
-Object
+String
  act
@@ -1757,17 +1757,28 @@
 Contents
-=
+ = (String) 
 handler.
@@ -1804,19 +1804,16 @@
 ,null);%0A
-%09%09%0A
 %09%09assert
@@ -1874,17 +1874,16 @@
 ts);%0A%09%7D%0A
-%09
 %0A%09@Test%0A
@@ -1903,16 +1903,19 @@
 testRead
+Xml
 () throw
@@ -1967,41 +1967,156 @@
 );%0A%09
-%09testRead(%22smiley.json%22,%22utf-8%22);
+%7D%0A%0A%09@Test%0A%09public void testReadJson() throws Exception %7B%0A%09%09testRead(%22smiley.json%22,%22utf-8%22);%0A%09%7D%0A%0A%09@Test%0A%09public void testReadTxt() throws Exception %7B
 %0A%09%09t
@@ -2149,12 +2149,10 @@
 8%22);%0A%09%7D%0A
-%09%0A
 %7D%0A

Change linedelimiters to unix