package me.coley.recaf;

import me.coley.recaf.util.EmptyResource;
import me.coley.recaf.workspace.JavaResource;
import me.coley.recaf.workspace.Workspace;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.*;

import static org.junit.jupiter.api.Assertions.assertTrue;

/**
 * Tests the listening map used in {@link me.coley.recaf.workspace.JavaResource}.
 *
 * @author Matt
 */
public class ResourceUpdateTest extends Base {
	private JavaResource resource;
	private Workspace workspace;

	@BeforeEach
	public void setup() {
		resource = new EmptyResource();
		workspace = new Workspace(resource);
	}

	@Test
	public void testClassPut() {
		String valueToPut = "Test";
		// Record values put in the map
		Set<String> putted = new HashSet<>();
		resource.getClasses().getPutListeners().add((name, code) -> putted.add(name));
		// Put value in map
		resource.getClasses().put(valueToPut, new byte[0]);
		// Assert value listener has been fired
		assertTrue(putted.contains(valueToPut));
	}

	@Test
	public void testResourcePut() {
		String valueToPut = "Test";
		// Record values put in the map
		Set<String> putted = new HashSet<>();
		resource.getResources().getPutListeners().add((name, code) -> putted.add(name));
		// Put value in map
		resource.getResources().put(valueToPut, new byte[0]);
		// Assert value listener has been fired
		assertTrue(putted.contains(valueToPut));
	}

	@Test
	public void testClassPutAll() {
		String valueToPut1 = "Test1";
		String valueToPut2 = "Test2";
		Map<String, byte[]> map = new HashMap<>();
		map.put(valueToPut1, new byte[0]);
		map.put(valueToPut2, new byte[0]);
		// Record values put in the map
		Set<String> putted = new HashSet<>();
		resource.getClasses().getPutListeners().add((name, code) -> putted.add(name));
		// Put value in map
		resource.getClasses().putAll(map);
		// Assert value listener has been fired
		assertTrue(putted.contains(valueToPut1));
		assertTrue(putted.contains(valueToPut2));
	}

	@Test
	public void testResourcePutAll() {
		String valueToPut1 = "Test1";
		String valueToPut2 = "Test2";
		Map<String, byte[]> map = new HashMap<>();
		map.put(valueToPut1, new byte[0]);
		map.put(valueToPut2, new byte[0]);
		// Record values put in the map
		Set<String> putted = new HashSet<>();
		resource.getResources().getPutListeners().add((name, code) -> putted.add(name));
		// Put value in map
		resource.getResources().putAll(map);
		// Assert value listener has been fired
		assertTrue(putted.contains(valueToPut1));
		assertTrue(putted.contains(valueToPut2));
	}

	@Test
	public void testClassRemove() {
		String valueToRemove = "Test";
		// Record values removed from the map
		Set<Object> removed = new HashSet<>();
		resource.getClasses().getRemoveListeners().add(key -> removed.add(key));
		// Remove value from map
		resource.getClasses().remove(valueToRemove);
		// Assert value listener has been fired
		assertTrue(removed.contains(valueToRemove));
	}

	@Test
	public void testResourceRemove() {
		String valueToRemove = "Test";
		// Record values removed from the map
		Set<Object> removed = new HashSet<>();
		resource.getResources().getRemoveListeners().add(key -> removed.add(key));
		// Remove value from map
		resource.getResources().remove(valueToRemove);
		// Assert value listener has been fired
		assertTrue(removed.contains(valueToRemove));
	}
}

@@ -1014,35 +1014,98 @@
 s(valueToPut));%0A
+%09%09assertTrue(resource.getDirtyClasses().contains(valueToPut));%0A
 %09%7D%0A
-
 %0A%09@Test%0A%09public 
@@ -1471,32 +1471,97 @@
 s(valueToPut));%0A
+%09%09assertTrue(resource.getDirtyResources().contains(valueToPut));%0A
 %09%7D%0A%0A%09@Test%0A%09publ
@@ -2107,32 +2107,161 @@
 (valueToPut2));%0A
+%09%09assertTrue(resource.getDirtyClasses().contains(valueToPut1));%0A%09%09assertTrue(resource.getDirtyClasses().contains(valueToPut2));%0A%0A
 %09%7D%0A%0A%09@Test%0A%09publ
@@ -2770,32 +2770,32 @@
 (valueToPut1));%0A
-
 %09%09assertTrue(put
@@ -2818,24 +2818,156 @@
 ueToPut2));%0A
+%09%09assertTrue(resource.getDirtyResources().contains(valueToPut1));%0A%09%09assertTrue(resource.getDirtyResources().contains(valueToPut2));%0A
 %09%7D%0A%0A%09@Test%0A%09

Add dirty checks to ResourceUpdateTest