package com.alexrnl.subtitlecorrector.io;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

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

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

/**
 * Test suite for the {@link Dictionary} class.
 * @author Alex
 */
public class DictionaryTest {
	/** The file for the dictionary. */
	private static Path	dictionaryFile;
	/** The file for the copy of the dictionary */
	private static Path	dictionaryCopy;
	
	/** A read-only dictionary */
	private Dictionary dictionary;
	/** An editable dictionary */
	private Dictionary editableDictionary;
	

	/**
	 * Copy the dictionary to a temporary file so it can be safely edited.
	 * @throws IOException
	 *         if there is a problem when loading the file.
	 * @throws URISyntaxException
	 *         if the the path is badly formatted.
	 */
	@BeforeClass
	public static void setUpBeforeClass () throws IOException, URISyntaxException {
		dictionaryFile = Paths.get(Dictionary.class.getResource("/dico.fr.txt").toURI());
		dictionaryCopy = Files.createTempFile("dictionary", ".txt");
		dictionaryCopy.toFile().deleteOnExit();
		Files.copy(dictionaryFile, dictionaryCopy, StandardCopyOption.REPLACE_EXISTING);
	}
	
	/**
	 * Set up test attributes.
	 * @throws IOException
	 *         if there is a problem when loading the file.
	 * @throws URISyntaxException
	 *         if the the path is badly formatted.
	 */
	@Before
	public void setUp () throws IOException, URISyntaxException {
		dictionary = new Dictionary(dictionaryFile);
		editableDictionary = new Dictionary(dictionaryCopy, true);
	}
	
	/**
	 * Test method for {@link com.alexrnl.subtitlecorrector.io.Dictionary#Dictionary(java.nio.file.Path)}.
	 * @throws IOException
	 *         if there is a problem when loading the file.
	 */
	@Test(expected = NullPointerException.class)
	public void testDictionaryNPEPath () throws IOException {
		new Dictionary(null);
	}
	
	/**
	 * Test method for {@link com.alexrnl.subtitlecorrector.io.Dictionary#Dictionary(java.nio.file.Path)}.
	 * @throws IOException
	 *         if there is a problem when loading the file.
	 */
	@Test(expected = NullPointerException.class)
	public void testDictionaryNPECharSet () throws IOException {
		new Dictionary(Paths.get("dummy", "path"), null, true);
	}
	
	/**
	 * Test method for {@link com.alexrnl.subtitlecorrector.io.Dictionary#Dictionary(java.nio.file.Path)}.
	 * @throws IOException
	 *         if there is a problem when loading the file.
	 */
	@Test(expected = IllegalArgumentException.class)
	public void testDictionaryIAEPathNotExists () throws IOException {
		new Dictionary(Paths.get("dummy", "path"));
	}
	
	/**
	 * Test method for {@link com.alexrnl.subtitlecorrector.io.Dictionary#Dictionary(java.nio.file.Path)}.
	 * @throws IOException
	 *         if there is a problem when loading the file.
	 */
	@Test(expected = IllegalArgumentException.class)
	public void testDictionaryIAENotWritable () throws IOException {
		final Path temporaryFile = Files.createTempFile("dictionary", ".txt");
		temporaryFile.toFile().setWritable(false);
		temporaryFile.toFile().deleteOnExit();
		new Dictionary(temporaryFile, StandardCharsets.UTF_8, true);
	}
	
	/**
	 * Test method for {@link com.alexrnl.subtitlecorrector.io.Dictionary#save()}.
	 * @throws IOException
	 *         if there is a problem when writing the file.
	 */
	@Test
	public void testSave () throws IOException {
		assertFalse(editableDictionary.contains("zedzfrgtlermforopfz"));
		assertTrue(editableDictionary.addWord("zedzfrgtlermforopfz"));
		editableDictionary.save();
		final Dictionary savedDictionary = new Dictionary(dictionaryCopy);
		assertTrue(savedDictionary.contains("zedzfrgtlermforopfz"));
	}
	
	/**
	 * Test method for {@link com.alexrnl.subtitlecorrector.io.Dictionary#save()}.
	 * @throws IOException
	 *         if there is a problem when writing the file.
	 */
	@Test(expected = IllegalStateException.class)
	public void testSaveIllegalStateExcetion () throws IOException {
		dictionary.save();
	}
	
	/**
	 * Test method for {@link com.alexrnl.subtitlecorrector.io.Dictionary#isEditable()}.
	 */
	@Test
	public void testIsEditable () {
		assertFalse(dictionary.isEditable());
		assertTrue(editableDictionary.isEditable());
	}
	
	/**
	 * Test method for {@link com.alexrnl.subtitlecorrector.io.Dictionary#contains(java.lang.String)}.
	 */
	@Test
	public void testContains () {
		assertFalse(dictionary.contains("zedzfrgtlermforopfz"));
		assertFalse(dictionary.contains(null));
		assertFalse(dictionary.contains(""));
		assertTrue(dictionary.contains("mot"));
	}
	
	/**
	 * Test method for {@link com.alexrnl.subtitlecorrector.io.Dictionary#addWord(java.lang.String)}.
	 */
	@Test
	public void testAddWord () {
		assertFalse(dictionary.addWord("mot"));
		assertFalse(dictionary.contains("zedzfrgtlermforopfz"));
		assertTrue(dictionary.addWord("zedzfrgtlermforopfz"));
		assertTrue(dictionary.contains("zedzfrgtlermforopfz"));
	}
}

@@ -4485,32 +4485,610 @@
 e());%0A%09%7D%0A%09%0A%09/**%0A
+%09 * Test method for %7B@link com.alexrnl.subtitlecorrector.io.Dictionary#isUpdated()%7D.%0A%09 * @throws IOException%0A%09 *         if a save action fails.%0A%09 */%0A%09@Test%0A%09public void testIsUpdated () throws IOException %7B%0A%09%09assertFalse(dictionary.isUpdated());%0A%09%09assertTrue(dictionary.addWord(%22zedzfrgtforopfz%22));%0A%09%09assertTrue(dictionary.isUpdated());%0A%0A%09%09assertFalse(editableDictionary.isUpdated());%0A%09%09assertTrue(editableDictionary.addWord(%22zedgtlermforop%22));%0A%09%09assertTrue(editableDictionary.isUpdated());%0A%09%09editableDictionary.save();%0A%09%09assertFalse(editableDictionary.isUpdated());%0A%09%7D%0A%09%0A%09/**%0A
 %09 * Test method 
@@ -5257,35 +5257,32 @@
 zedzfrgtlermforo
-pfz
 %22));%0A%09%09assertFal

Add test for the updated flag in Dictionary class