package com.approximatrix.draco.application.preferences;

import static org.junit.Assert.*;

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

import com.google.gson.Gson;

public class PluginPreferencesTest {
	
	private PluginPreferences prefs = null;
	
	@Before
	public void setUp() throws Exception {
		prefs = new PluginPreferences();
		prefs.addDirectory("one");
		prefs.addDirectory("two");
	}

	@After
	public void tearDown() throws Exception {
		prefs = null;
	}

	@Test
	public void testTag() {
		assertEquals("plugins",PluginPreferences.TAG);
	}

	@Test
	public void testSetThreadLimit() {
		prefs.setThreadLimit(-1);
		assertTrue(prefs.getThreadLimit()>0);
	}

	@Test
	public void testSetParallelInverseEnabled() {
		prefs.setParallelInverseEnabled(false);
		assertFalse(prefs.isParallelInverseEnabled());
		prefs.setParallelInverseEnabled(true);
		assertTrue(prefs.isParallelInverseEnabled());
	}

	@Test
	public void testSetParallelMultiplyEnabled() {
		prefs.setParallelMultiplyEnabled(false);
		assertFalse(prefs.isParallelMultiplyEnabled());
		prefs.setParallelMultiplyEnabled(true);
		assertTrue(prefs.isParallelMultiplyEnabled());
	}

	@Test
	public void testGetDirectories() {
		assertNotNull(prefs.getDirectories());
		assertEquals(2,prefs.getDirectories().size());
		assertEquals("one",prefs.getDirectories().get(0));
		assertEquals("two",prefs.getDirectories().get(1));
	}

	@Test
	public void testAddDirectory() {
		prefs.addDirectory("three");
		assertEquals(3,prefs.getDirectories().size());
		assertEquals("three",prefs.getDirectories().get(2));
		
		// Should not add a duplicate
		prefs.addDirectory("one");
		assertEquals(3,prefs.getDirectories().size());
	}

	@Test
	public void testRemoveDirectory() {
		// Shouldn't be present
		prefs.removeDirectory("three");
		assertEquals(2,prefs.getDirectories().size());
		
		prefs.removeDirectory("two");
		assertEquals(1,prefs.getDirectories().size());
		assertEquals("one",prefs.getDirectories().get(0));
	}

	@Test
	public void testJSONable() {
		Gson gson = new Gson();
		prefs.addDirectory("three");
		prefs.setParallelInverseEnabled(true);
		prefs.setThreadLimit(92);
		String prefs_string = gson.toJson(prefs);
		
		assertNotNull(prefs_string);
		
		prefs = gson.fromJson(prefs_string, PluginPreferences.class);
		
		assertEquals(3,prefs.getDirectories().size());
		assertEquals("three",prefs.getDirectories().get(2));
		assertEquals(92,prefs.getThreadLimit());
		assertTrue(prefs.isParallelInverseEnabled());
	}
}
