#6256: Do not create new Set/Map

This commit is contained in:
Antonio Alonzi 2020-03-30 09:17:07 +01:00
parent 76c9d38f57
commit f86529bbb9
2 changed files with 26 additions and 11 deletions

View file

@ -4,9 +4,12 @@ import static mage.constants.WatcherScope.GAME;
import static org.junit.Assert.assertEquals;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import mage.constants.WatcherScope;
import mage.game.Game;
import mage.game.events.GameEvent;
@ -18,24 +21,36 @@ public class WatcherTest {
@Test
public void test() {
// Given
Map<String, String> mapField = new HashMap<>();
mapField.put("mapFieldKey1", "mapFieldValue1");
mapField.put("mapFieldKey2", "mapFieldValue2");
TestWatcher testWatcher = new TestWatcher(GAME);
testWatcher.setStringField("stringField");
testWatcher.setSetField(ImmutableSet.of("setField1, setField2"));
testWatcher.setMapField(ImmutableMap.of("mapFieldKey1, mapFieldValue1", "mapFieldKey2, mapFieldValue2"));
testWatcher.setSetField(set("setField1", "setField2"));
testWatcher.setMapField(mapField);
// When
TestWatcher copy = testWatcher.copy();
// And
testWatcher.getSetField().add("setField3");
mapField.put("mapFieldKey3", "mapFieldValue3");
// Then
assertEquals(testWatcher.getStringField(), copy.getStringField());
assertEquals(testWatcher.getSetField(), copy.getSetField());
assertEquals(testWatcher.getMapField(), copy.getMapField());
assertEquals("stringField", copy.getStringField());
assertEquals(set("setField1", "setField2"), copy.getSetField());
assertEquals(ImmutableMap.of("mapFieldKey1", "mapFieldValue1", "mapFieldKey2", "mapFieldValue2"), copy.getMapField());
}
private Set<String> set(String... values) {
return Stream.of(values).collect(Collectors.toSet());
}
public static class TestWatcher extends Watcher {
private String stringField;
private Set<String> setField;
private Map<String, String> mapField;
private Set<String> setField = new HashSet<>();
private Map<String, String> mapField = new HashMap<>();
public TestWatcher(WatcherScope scope) {
super(scope);