mirror of
https://github.com/correl/mage.git
synced 2025-04-14 01:01:08 -09:00
Tests: added additional watcher test for #10092
This commit is contained in:
parent
407369ea83
commit
e1c7139425
1 changed files with 213 additions and 179 deletions
|
@ -1,217 +1,251 @@
|
||||||
package mage;
|
package mage;
|
||||||
|
|
||||||
import static mage.constants.WatcherScope.GAME;
|
|
||||||
import static org.junit.Assert.*;
|
|
||||||
|
|
||||||
import com.google.common.collect.ImmutableList;
|
import com.google.common.collect.ImmutableList;
|
||||||
import com.google.common.collect.ImmutableMap;
|
import com.google.common.collect.ImmutableMap;
|
||||||
import com.google.common.collect.ImmutableSet;
|
import com.google.common.collect.ImmutableSet;
|
||||||
import java.util.HashMap;
|
import com.google.common.collect.ImmutableSortedSet;
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Set;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.stream.Collectors;
|
|
||||||
import java.util.stream.Stream;
|
|
||||||
import mage.constants.WatcherScope;
|
import mage.constants.WatcherScope;
|
||||||
import mage.game.Game;
|
import mage.game.Game;
|
||||||
import mage.game.events.GameEvent;
|
import mage.game.events.GameEvent;
|
||||||
import mage.watchers.Watcher;
|
import mage.watchers.Watcher;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
import static mage.constants.WatcherScope.GAME;
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
public class WatcherTest {
|
public class WatcherTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testShallowCopy() {
|
public void testShallowCopy() {
|
||||||
// Given
|
// Given
|
||||||
Map<String, String> mapField = new HashMap<>();
|
Map<String, String> mapField = new HashMap<>();
|
||||||
mapField.put("mapFieldKey1", "mapFieldValue1");
|
mapField.put("mapFieldKey1", "mapFieldValue1");
|
||||||
mapField.put("mapFieldKey2", "mapFieldValue2");
|
mapField.put("mapFieldKey2", "mapFieldValue2");
|
||||||
|
|
||||||
TestWatcher testWatcher = new TestWatcher(GAME);
|
TestWatcher testWatcher = new TestWatcher(GAME);
|
||||||
testWatcher.setStringField("stringField");
|
testWatcher.setStringField("stringField");
|
||||||
testWatcher.setSetField(set("setField1", "setField2"));
|
testWatcher.setSetField(set("setField1", "setField2"));
|
||||||
testWatcher.setMapField(mapField);
|
testWatcher.setMapField(mapField);
|
||||||
|
|
||||||
// When
|
// When
|
||||||
TestWatcher copy = testWatcher.copy();
|
TestWatcher copy = testWatcher.copy();
|
||||||
|
|
||||||
// And
|
// And
|
||||||
testWatcher.getSetField().add("setField3");
|
testWatcher.getSetField().add("setField3");
|
||||||
mapField.put("mapFieldKey3", "mapFieldValue3");
|
mapField.put("mapFieldKey3", "mapFieldValue3");
|
||||||
|
|
||||||
// Then
|
// Then
|
||||||
assertEquals("stringField", copy.getStringField());
|
assertEquals("stringField", copy.getStringField());
|
||||||
assertEquals(set("setField1", "setField2"), copy.getSetField());
|
assertEquals(set("setField1", "setField2"), copy.getSetField());
|
||||||
assertEquals(ImmutableMap.of("mapFieldKey1", "mapFieldValue1", "mapFieldKey2", "mapFieldValue2"), copy.getMapField());
|
assertEquals(ImmutableMap.of("mapFieldKey1", "mapFieldValue1", "mapFieldKey2", "mapFieldValue2"), copy.getMapField());
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testDeepCopyMapOfList() {
|
|
||||||
// Given
|
|
||||||
Map<String, List<String>> listInMapField = new HashMap<>();
|
|
||||||
List<String> list1 = new ArrayList<>();
|
|
||||||
list1.add("v1");
|
|
||||||
list1.add("v1.1");
|
|
||||||
List<String> list2 = new ArrayList<>();
|
|
||||||
list2.add("v2");
|
|
||||||
listInMapField.put("k1", list1);
|
|
||||||
listInMapField.put("k2", list2);
|
|
||||||
|
|
||||||
TestWatcher testWatcher = new TestWatcher(GAME);
|
|
||||||
testWatcher.setListInMapField(listInMapField);
|
|
||||||
|
|
||||||
// When
|
|
||||||
TestWatcher copy = testWatcher.copy();
|
|
||||||
|
|
||||||
// And
|
|
||||||
testWatcher.getListInMapField().get("k1").add("v1.2");
|
|
||||||
List<String> list3 = new ArrayList<>();
|
|
||||||
list3.add("v5");
|
|
||||||
testWatcher.getListInMapField().put("k5", list3);
|
|
||||||
|
|
||||||
// Then
|
|
||||||
Map<String, List<String>> copyListInMap = copy.getListInMapField();
|
|
||||||
assertEquals(2, copyListInMap.size());
|
|
||||||
assertTrue(copyListInMap.containsKey("k1"));
|
|
||||||
assertEquals(ImmutableList.of("v1", "v1.1"), copyListInMap.get("k1"));
|
|
||||||
assertTrue(copyListInMap.containsKey("k2"));
|
|
||||||
assertEquals(ImmutableList.of("v2"), copyListInMap.get("k2"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testDeepCopyMapOfSet() {
|
|
||||||
// Given
|
|
||||||
Map<String, Set<String>> setInMapField = new HashMap<>();
|
|
||||||
Set<String> set1 = new HashSet<>();
|
|
||||||
set1.add("v3");
|
|
||||||
Set<String> set2 = new HashSet<>();
|
|
||||||
set2.add("v4");
|
|
||||||
set2.add("v4.1");
|
|
||||||
setInMapField.put("k3", set1);
|
|
||||||
setInMapField.put("k4", set2);
|
|
||||||
setInMapField.put("k", new HashSet<String>());
|
|
||||||
|
|
||||||
TestWatcher testWatcher = new TestWatcher(GAME);
|
|
||||||
testWatcher.setSetInMapField(setInMapField);
|
|
||||||
|
|
||||||
// When
|
|
||||||
TestWatcher copy = testWatcher.copy();
|
|
||||||
|
|
||||||
// And
|
|
||||||
testWatcher.getSetInMapField().get("k3").add("v3.1");
|
|
||||||
|
|
||||||
// Then
|
|
||||||
Map<String, Set<String>> copySetInMap = copy.getSetInMapField();
|
|
||||||
assertEquals(3, copySetInMap.size());
|
|
||||||
assertTrue(copySetInMap.containsKey("k3"));
|
|
||||||
assertEquals(ImmutableSet.of("v3"), copySetInMap.get("k3"));
|
|
||||||
assertTrue(copySetInMap.containsKey("k4"));
|
|
||||||
assertEquals(ImmutableSet.of("v4", "v4.1"), copySetInMap.get("k4"));
|
|
||||||
assertTrue(copySetInMap.containsKey("k"));
|
|
||||||
assertEquals(ImmutableSet.of(), copySetInMap.get("k"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testDeepCopyMapOfMap() {
|
|
||||||
// Given
|
|
||||||
Map<String, Map<String, String>> mapInMapField = new HashMap<>();
|
|
||||||
Map<String, String> map1 = new HashMap<>();
|
|
||||||
map1.put("k1.1", "v1.1");
|
|
||||||
map1.put("k1.2", "v1.2");
|
|
||||||
Map<String, String> map2 = new HashMap<>();
|
|
||||||
map2.put("k2.1", "v2.1");
|
|
||||||
mapInMapField.put("k1", map1);
|
|
||||||
mapInMapField.put("k2", map2);
|
|
||||||
|
|
||||||
TestWatcher testWatcher = new TestWatcher(GAME);
|
|
||||||
testWatcher.setMapInMapField(mapInMapField);
|
|
||||||
|
|
||||||
// When
|
|
||||||
TestWatcher copy = testWatcher.copy();
|
|
||||||
|
|
||||||
// And
|
|
||||||
testWatcher.getMapInMapField().get("k2").put("k2.2", "v2.2");
|
|
||||||
testWatcher.getMapInMapField().put("k3", new HashMap<>());
|
|
||||||
|
|
||||||
// Then
|
|
||||||
Map<String, Map<String, String>> copyMapInMap = copy.getMapInMapField();
|
|
||||||
assertEquals(2, copyMapInMap.size());
|
|
||||||
assertTrue(copyMapInMap.containsKey("k1"));
|
|
||||||
assertEquals(ImmutableMap.of("k1.1", "v1.1", "k1.2", "v1.2"), copyMapInMap.get("k1"));
|
|
||||||
assertTrue(copyMapInMap.containsKey("k2"));
|
|
||||||
assertEquals(ImmutableMap.of("k2.1", "v2.1"), copyMapInMap.get("k2"));
|
|
||||||
assertFalse(copyMapInMap.containsKey("k3"));
|
|
||||||
}
|
|
||||||
|
|
||||||
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 = new HashSet<>();
|
|
||||||
private Map<String, String> mapField = new HashMap<>();
|
|
||||||
private Map<String, List<String>> listInMapField = new HashMap<>();
|
|
||||||
private Map<String, Set<String>> setInMapField = new HashMap<>();
|
|
||||||
private Map<String, Map<String, String>> mapInMapField = new HashMap<>();
|
|
||||||
|
|
||||||
public TestWatcher(WatcherScope scope) {
|
|
||||||
super(scope);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Test
|
||||||
public void watch(GameEvent event, Game game) {
|
public void testDeepCopyMapOfList() {
|
||||||
System.out.println("watch");
|
// Given
|
||||||
|
Map<String, List<String>> listInMapField = new HashMap<>();
|
||||||
|
List<String> list1 = new ArrayList<>();
|
||||||
|
list1.add("v1");
|
||||||
|
list1.add("v1.1");
|
||||||
|
List<String> list2 = new ArrayList<>();
|
||||||
|
list2.add("v2");
|
||||||
|
listInMapField.put("k1", list1);
|
||||||
|
listInMapField.put("k2", list2);
|
||||||
|
|
||||||
|
TestWatcher testWatcher = new TestWatcher(GAME);
|
||||||
|
testWatcher.setListInMapField(listInMapField);
|
||||||
|
|
||||||
|
// When
|
||||||
|
TestWatcher copy = testWatcher.copy();
|
||||||
|
|
||||||
|
// And
|
||||||
|
testWatcher.getListInMapField().get("k1").add("v1.2");
|
||||||
|
List<String> list3 = new ArrayList<>();
|
||||||
|
list3.add("v5");
|
||||||
|
testWatcher.getListInMapField().put("k5", list3);
|
||||||
|
|
||||||
|
// Then
|
||||||
|
Map<String, List<String>> copyListInMap = copy.getListInMapField();
|
||||||
|
assertEquals(2, copyListInMap.size());
|
||||||
|
assertTrue(copyListInMap.containsKey("k1"));
|
||||||
|
assertEquals(ImmutableList.of("v1", "v1.1"), copyListInMap.get("k1"));
|
||||||
|
assertTrue(copyListInMap.containsKey("k2"));
|
||||||
|
assertEquals(ImmutableList.of("v2"), copyListInMap.get("k2"));
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getStringField() {
|
@Test
|
||||||
return stringField;
|
public void testDeepCopyMapOfSet() {
|
||||||
|
// Given
|
||||||
|
Map<String, Set<String>> setInMapField = new HashMap<>();
|
||||||
|
Set<String> set1 = new HashSet<>();
|
||||||
|
set1.add("v3");
|
||||||
|
Set<String> set2 = new HashSet<>();
|
||||||
|
set2.add("v4");
|
||||||
|
set2.add("v4.1");
|
||||||
|
setInMapField.put("k3", set1);
|
||||||
|
setInMapField.put("k4", set2);
|
||||||
|
setInMapField.put("k", new HashSet<String>());
|
||||||
|
|
||||||
|
TestWatcher testWatcher = new TestWatcher(GAME);
|
||||||
|
testWatcher.setSetInMapField(setInMapField);
|
||||||
|
|
||||||
|
// When
|
||||||
|
TestWatcher copy = testWatcher.copy();
|
||||||
|
|
||||||
|
// And
|
||||||
|
testWatcher.getSetInMapField().get("k3").add("v3.1");
|
||||||
|
|
||||||
|
// Then
|
||||||
|
Map<String, Set<String>> copySetInMap = copy.getSetInMapField();
|
||||||
|
assertEquals(3, copySetInMap.size());
|
||||||
|
assertTrue(copySetInMap.containsKey("k3"));
|
||||||
|
assertEquals(ImmutableSet.of("v3"), copySetInMap.get("k3"));
|
||||||
|
assertTrue(copySetInMap.containsKey("k4"));
|
||||||
|
assertEquals(ImmutableSet.of("v4", "v4.1"), copySetInMap.get("k4"));
|
||||||
|
assertTrue(copySetInMap.containsKey("k"));
|
||||||
|
assertEquals(ImmutableSet.of(), copySetInMap.get("k"));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setStringField(String stringField) {
|
@Test
|
||||||
this.stringField = stringField;
|
public void testDeepCopyMapOfMap() {
|
||||||
|
// Given
|
||||||
|
Map<String, Map<String, String>> mapInMapField = new HashMap<>();
|
||||||
|
Map<String, String> map1 = new HashMap<>();
|
||||||
|
map1.put("k1.1", "v1.1");
|
||||||
|
map1.put("k1.2", "v1.2");
|
||||||
|
Map<String, String> map2 = new HashMap<>();
|
||||||
|
map2.put("k2.1", "v2.1");
|
||||||
|
mapInMapField.put("k1", map1);
|
||||||
|
mapInMapField.put("k2", map2);
|
||||||
|
|
||||||
|
TestWatcher testWatcher = new TestWatcher(GAME);
|
||||||
|
testWatcher.setMapInMapField(mapInMapField);
|
||||||
|
|
||||||
|
// When
|
||||||
|
TestWatcher copy = testWatcher.copy();
|
||||||
|
|
||||||
|
// And
|
||||||
|
testWatcher.getMapInMapField().get("k2").put("k2.2", "v2.2");
|
||||||
|
testWatcher.getMapInMapField().put("k3", new HashMap<>());
|
||||||
|
|
||||||
|
// Then
|
||||||
|
Map<String, Map<String, String>> copyMapInMap = copy.getMapInMapField();
|
||||||
|
assertEquals(2, copyMapInMap.size());
|
||||||
|
assertTrue(copyMapInMap.containsKey("k1"));
|
||||||
|
assertEquals(ImmutableMap.of("k1.1", "v1.1", "k1.2", "v1.2"), copyMapInMap.get("k1"));
|
||||||
|
assertTrue(copyMapInMap.containsKey("k2"));
|
||||||
|
assertEquals(ImmutableMap.of("k2.1", "v2.1"), copyMapInMap.get("k2"));
|
||||||
|
assertFalse(copyMapInMap.containsKey("k3"));
|
||||||
}
|
}
|
||||||
|
|
||||||
public Set<String> getSetField() {
|
@Test
|
||||||
return setField;
|
public void testDeepCopyMapOfSortedSet() {
|
||||||
|
// Given
|
||||||
|
Map<String, SortedSet<String>> sortedSetInMapField = new HashMap<>();
|
||||||
|
sortedSetInMapField.put("k1", new TreeSet<>(Arrays.asList("v1_1", "v1_2")));
|
||||||
|
sortedSetInMapField.put("k2", new TreeSet<>(Arrays.asList("v2_1", "v2_2")));
|
||||||
|
|
||||||
|
TestWatcher testWatcher = new TestWatcher(GAME);
|
||||||
|
testWatcher.setSortedSetInMapField(sortedSetInMapField);
|
||||||
|
|
||||||
|
// When
|
||||||
|
TestWatcher copy = testWatcher.copy();
|
||||||
|
|
||||||
|
// And
|
||||||
|
testWatcher.getSortedSetInMapField().get("k2").add("v2_3");
|
||||||
|
testWatcher.getSortedSetInMapField().put("k3", new TreeSet<>());
|
||||||
|
|
||||||
|
// Then
|
||||||
|
Map<String, SortedSet<String>> copySortedSetInMapField = copy.getSortedSetInMapField();
|
||||||
|
assertEquals(2, copySortedSetInMapField.size());
|
||||||
|
assertTrue(copySortedSetInMapField.containsKey("k1"));
|
||||||
|
assertEquals(ImmutableSortedSet.of("v1_1", "v1_2"), copySortedSetInMapField.get("k1"));
|
||||||
|
assertTrue(copySortedSetInMapField.containsKey("k2"));
|
||||||
|
assertEquals(ImmutableSortedSet.of("v2_1", "v2_2"), copySortedSetInMapField.get("k2"));
|
||||||
|
assertFalse(copySortedSetInMapField.containsKey("k3"));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setSetField(Set<String> setField) {
|
private Set<String> set(String... values) {
|
||||||
this.setField = setField;
|
return Stream.of(values).collect(Collectors.toSet());
|
||||||
}
|
}
|
||||||
|
|
||||||
public Map<String, String> getMapField() {
|
public static class TestWatcher extends Watcher {
|
||||||
return mapField;
|
private String stringField;
|
||||||
}
|
private Set<String> setField = new HashSet<>();
|
||||||
|
private Map<String, String> mapField = new HashMap<>();
|
||||||
|
private Map<String, List<String>> listInMapField = new HashMap<>();
|
||||||
|
private Map<String, Set<String>> setInMapField = new HashMap<>();
|
||||||
|
private Map<String, Map<String, String>> mapInMapField = new HashMap<>();
|
||||||
|
|
||||||
public void setMapField(Map<String, String> mapField) {
|
private Map<String, SortedSet<String>> sortedSetInMapField = new HashMap<>();
|
||||||
this.mapField = mapField;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Map<String, List<String>> getListInMapField() {
|
public TestWatcher(WatcherScope scope) {
|
||||||
return listInMapField;
|
super(scope);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setListInMapField(Map<String, List<String>> listInMapField) {
|
@Override
|
||||||
this.listInMapField = listInMapField;
|
public void watch(GameEvent event, Game game) {
|
||||||
}
|
System.out.println("watch");
|
||||||
|
}
|
||||||
|
|
||||||
public Map<String, Set<String>> getSetInMapField() {
|
public String getStringField() {
|
||||||
return setInMapField;
|
return stringField;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setSetInMapField(Map<String, Set<String>> setInMapField) {
|
public void setStringField(String stringField) {
|
||||||
this.setInMapField = setInMapField;
|
this.stringField = stringField;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Map<String, Map<String, String>> getMapInMapField() {
|
public Set<String> getSetField() {
|
||||||
return mapInMapField;
|
return setField;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setMapInMapField(Map<String, Map<String, String>> mapInMapField) {
|
public void setSetField(Set<String> setField) {
|
||||||
this.mapInMapField = mapInMapField;
|
this.setField = setField;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<String, String> getMapField() {
|
||||||
|
return mapField;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMapField(Map<String, String> mapField) {
|
||||||
|
this.mapField = mapField;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<String, List<String>> getListInMapField() {
|
||||||
|
return listInMapField;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setListInMapField(Map<String, List<String>> listInMapField) {
|
||||||
|
this.listInMapField = listInMapField;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<String, Set<String>> getSetInMapField() {
|
||||||
|
return setInMapField;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSetInMapField(Map<String, Set<String>> setInMapField) {
|
||||||
|
this.setInMapField = setInMapField;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<String, Map<String, String>> getMapInMapField() {
|
||||||
|
return mapInMapField;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMapInMapField(Map<String, Map<String, String>> mapInMapField) {
|
||||||
|
this.mapInMapField = mapInMapField;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<String, SortedSet<String>> getSortedSetInMapField() {
|
||||||
|
return sortedSetInMapField;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSortedSetInMapField(Map<String, SortedSet<String>> sortedSetInMapField) {
|
||||||
|
this.sortedSetInMapField = sortedSetInMapField;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue