package com.alexrnl.subtitlecorrector.io.subrip;

import static org.junit.Assert.assertArrayEquals;

import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

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

import com.alexrnl.subtitlecorrector.common.SubtitleFile;

/**
 * Test suite for the {@link SubRipWriter} class.
 * @author Alex
 */
public class SubRipWriterTest {
	/** The writer to use for the test */
	private SubRipWriter	writer;

	/**
	 * Set up test attributes.
	 */
	@Before
	public void setUp () {
		writer = new SubRipWriter();
	}
	
	/**
	 * Test method for {@link com.alexrnl.subtitlecorrector.io.SubtitleWriter#writeFile(com.alexrnl.subtitlecorrector.common.SubtitleFile, java.nio.file.Path)}.
	 * @throws URISyntaxException
	 *         if the path to the file is badly formatted.
	 * @throws IOException
	 *         if and IO error occurs.
	 */
	@Test
	public void testWriteFile () throws IOException, URISyntaxException {
		final Path original = Paths.get(getClass().getResource("/Suits.S03E01.srt").toURI());
		final Path output = Files.createTempFile("subtitle", ".srt");
		
		final SubRipReader reader = new SubRipReader();
		final SubtitleFile subtitle = reader.readFile(original);
		
		writer.writeFile(subtitle, output);
		
		assertArrayEquals(Files.readAllBytes(original), Files.readAllBytes(output));
		
		Files.delete(output);
	}
	
	/**
	 * Test method for {@link com.alexrnl.subtitlecorrector.io.SubtitleWriter#writeFile(com.alexrnl.subtitlecorrector.common.SubtitleFile, java.nio.file.Path)}.
	 * @throws IOException
	 *         if and IO error occurs.
	 */
	@Test(expected = IllegalArgumentException.class)
	public void testWriteFileDirectory () throws IOException {
		final Path temporaryDirectory = Files.createTempDirectory("tmpdir");
		temporaryDirectory.toFile().deleteOnExit();
		writer.writeFile(new SubtitleFile(null), temporaryDirectory);
	}
	

	
	/**
	 * Test method for {@link com.alexrnl.subtitlecorrector.io.SubtitleWriter#writeFile(com.alexrnl.subtitlecorrector.common.SubtitleFile, java.nio.file.Path)}.
	 * @throws IOException
	 *         if and IO error occurs.
	 */
	@Test(expected = IllegalArgumentException.class)
	public void testWriteFileNotWritable () throws IOException {
		final Path temporaryFile = Files.createTempFile("subtitle", ".srt");
		temporaryFile.toFile().setWritable(false);
		writer.writeFile(new SubtitleFile(null), temporaryFile);
	}
}

@@ -1178,16 +1178,50 @@
 .srt%22);%0A
+%09%09output.toFile().deleteOnExit();%0A
 %09%09%0A%09%09fin
@@ -1454,32 +1454,8 @@
 %0A%09%09%0A
-%09%09Files.delete(output);%0A
 %09%7D%0A%09
@@ -2438,16 +2438,57 @@
 false);%0A
+%09%09temporaryFile.toFile().deleteOnExit();%0A
 %09%09writer

Mark test files to be deleted when the JVM exits