/*
 * @author Ranides Atterwim <ranides@gmail.com>
 * @copyright Ranides Atterwim
 * @license WTFPL
 * @url http://ranides.net/projects/assira
 */
package net.ranides.assira.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import net.ranides.assira.text.Charsets;
import net.ranides.assira.text.IOStrings;
import org.junit.Test;
import static net.ranides.assira.junit.NewAssert.*;
import net.ranides.assira.system.HostSystem;
import net.ranides.assira.system.RuntimeUtils;
import org.junit.Assume;

/**
 *
 * @author Ranides Atterwim <ranides@gmail.com>
 */
public class FileUtilsTest {

	@Test
	public void testDeleteFile() throws IOException {
		File file = File.createTempFile("FileUtilsF1", ".txt");
		assertTrue( file.exists() );
		
		FileUtils.delete(file);
		assertFalse( file.exists() );
		
		FileUtils.delete(file);
		assertFalse( file.exists() );
	}
	
	@Test
	public void testDeleteFile_Error() throws IOException {
        Assume.assumeTrue(HostSystem.WINDOWS.detected());
        
		File file = File.createTempFile("FileUtilsF2", ".txt");
		assertTrue( file.exists() );
		
		try (InputStream istream = new FileInputStream(file)) {
			assertThrows(IOException.class, ()->{
				FileUtils.delete(file);
			});
			assertTrue( file.exists() );
		}
		FileUtils.delete(file);
		assertFalse( file.exists() );
	}
	
	@Test
	public void testDeleteDirectory() throws IOException {
		File root = File.createTempFile("FileUtilsD1", ".txt").getParentFile();
		File ndir = new File(root, "FileUtilsD2");
		assertFalse(ndir.exists());
		
		assertTrue(ndir.mkdir());
		File file = File.createTempFile("FileUtilsD3", ".txt", ndir);
		assertTrue(ndir.exists());
		assertTrue(file.exists());
		
		FileUtils.delete(ndir);
		assertFalse(ndir.exists());
		assertFalse(file.exists());
	}
	
	@Test
	public void testDeleteDirectory_Error() throws IOException {
        Assume.assumeTrue(HostSystem.WINDOWS.detected());
        
		File root = File.createTempFile("FileUtilsD4", ".txt").getParentFile();
		File ndir = new File(root, "FileUtilsD5");
		assertFalse(ndir.exists());
		
		assertTrue(ndir.mkdir());
		File file = File.createTempFile("FileUtilsD6", ".txt", ndir);
		assertTrue(ndir.exists());
		assertTrue(file.exists());
		
		try (InputStream istream = new FileInputStream(file)) {
			assertThrows(IOException.class, ()->{
				FileUtils.delete(ndir);
			});
			assertTrue(ndir.exists());
			assertTrue(file.exists());
		}
		
		FileUtils.delete(ndir);
		assertFalse(ndir.exists());
		assertFalse(file.exists());
		
	}
	
	@Test
	public void testReadContent() throws IOException {
		String text = "hello world";
		File file = File.createTempFile("FileUtilsC1", ".txt");
		IOStrings.write(file, text, Charsets.US_ASCII);
		
		assertArrayEquals(text.getBytes(Charsets.US_ASCII), FileUtils.content(file));
	}
	
	@Test
	public void testEquivalent() throws IOException {
		File file1 = File.createTempFile("FileUtilsE1", ".txt");
		File file2 = File.createTempFile("FileUtilsE2", ".txt");
		File file3 = File.createTempFile("FileUtilsE3", ".txt");
		File file4 = File.createTempFile("FileUtilsE3", ".txt");
		
		IOStrings.write(file1, "hello world", Charsets.US_ASCII);
		IOStrings.write(file2, "hello world", Charsets.US_ASCII);
		IOStrings.write(file3, "hello WORLD", Charsets.US_ASCII);
		IOStrings.write(file4, "hello ", Charsets.US_ASCII);
		
		assertTrue(FileUtils.equivalent(file1, file2));
		assertFalse(FileUtils.equivalent(file1, file3));
		assertFalse(FileUtils.equivalent(file1, file4));
		
		assertTrue(FileUtils.equivalent(file1, file1));
		assertTrue(FileUtils.equivalent(file1, new File(new File(file1, "com"),"..") ));
	}
	
}
