package me.coley.recaf;

import me.coley.recaf.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));
		assertTrue(resource.getDirtyClasses().contains(valueToPut));
	}

	@Test
	public void testResourcePut() {
		String valueToPut = "Test";
		// Record values put in the map
		Set<String> putted = new HashSet<>();
		resource.getFiles().getPutListeners().add((name, data) -> putted.add(name));
		// Put value in map
		resource.getFiles().put(valueToPut, new byte[0]);
		// Assert value listener has been fired
		assertTrue(putted.contains(valueToPut));
		assertTrue(resource.getDirtyFiles().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));
		assertTrue(resource.getDirtyClasses().contains(valueToPut1));
		assertTrue(resource.getDirtyClasses().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.getFiles().getPutListeners().add((name, data) -> putted.add(name));
		// Put value in map
		resource.getFiles().putAll(map);
		// Assert value listener has been fired
		assertTrue(putted.contains(valueToPut1));
		assertTrue(putted.contains(valueToPut2));
		assertTrue(resource.getDirtyFiles().contains(valueToPut1));
		assertTrue(resource.getDirtyFiles().contains(valueToPut2));
	}

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

@@ -130,16 +130,44 @@
 .Test;%0A%0A
+import java.io.IOException;%0A
 import j
@@ -515,12 +515,12 @@
 new 
-Empt
+Dumm
 yRes
@@ -3629,11 +3629,354 @@
 ve));%0A%09%7D
+%0A%0A%09/**%0A%09 * Empty resource that allows items to be added.%0A%09 */%0A%09private static class DummyResource extends EmptyResource %7B%0A%09%09@Override%0A%09%09protected Map%3CString, byte%5B%5D%3E loadClasses() throws IOException %7B%0A%09%09%09return new HashMap%3C%3E();%0A%09%09%7D%0A%0A%09%09@Override%0A%09%09protected Map%3CString, byte%5B%5D%3E loadFiles() throws IOException %7B%0A%09%09%09return new HashMap%3C%3E();%0A%09%09%7D%0A%09%7D
 %0A%7D%0A

Fix failing tests due to EmptyResource changes