public void testNefariousMapPutAll() { Map<String, Integer> map = Maps.newLinkedHashMap(); Map<String, Integer> constrained = MapConstraints.constrainedMap(map, TEST_CONSTRAINT); Map<String, Integer> onceIterable = onceIterableMap("foo", 1); constrained.putAll(onceIterable); assertEquals((Integer) 1, constrained.get("foo")); }
public void testConstrainedMapIllegal() { Map<String, Integer> map = Maps.newLinkedHashMap(); Map<String, Integer> constrained = MapConstraints.constrainedMap(map, TEST_CONSTRAINT); try { constrained.put(TEST_KEY, TEST_VALUE); fail("TestKeyException expected"); } catch (TestKeyException expected) { } try { constrained.put("baz", TEST_VALUE); fail("TestValueException expected"); } catch (TestValueException expected) { } try { constrained.put(TEST_KEY, 3); fail("TestKeyException expected"); } catch (TestKeyException expected) { } try { constrained.putAll(ImmutableMap.of("baz", 3, TEST_KEY, 4)); fail("TestKeyException expected"); } catch (TestKeyException expected) { } assertEquals(Collections.emptySet(), map.entrySet()); assertEquals(Collections.emptySet(), constrained.entrySet()); }
public void testConstrainedMapLegal() { Map<String, Integer> map = Maps.newLinkedHashMap(); Map<String, Integer> constrained = MapConstraints.constrainedMap(map, TEST_CONSTRAINT); map.put(TEST_KEY, TEST_VALUE); constrained.put("foo", 1); map.putAll(ImmutableMap.of("bar", 2)); constrained.putAll(ImmutableMap.of("baz", 3)); assertTrue(map.equals(constrained)); assertTrue(constrained.equals(map)); assertEquals(map.entrySet(), constrained.entrySet()); assertEquals(map.keySet(), constrained.keySet()); assertEquals(HashMultiset.create(map.values()), HashMultiset.create(constrained.values())); assertFalse(map.values() instanceof Serializable); assertEquals(map.toString(), constrained.toString()); assertEquals(map.hashCode(), constrained.hashCode()); ASSERT .that(map.entrySet()) .has() .allOf( Maps.immutableEntry(TEST_KEY, TEST_VALUE), Maps.immutableEntry("foo", 1), Maps.immutableEntry("bar", 2), Maps.immutableEntry("baz", 3)) .inOrder(); }
public void testMapEntrySetContainsNefariousEntry() { Map<String, Integer> map = Maps.newTreeMap(); Map<String, Integer> constrained = MapConstraints.constrainedMap(map, TEST_CONSTRAINT); map.put("foo", 1); Map.Entry<String, Integer> nefariousEntry = nefariousMapEntry(TEST_KEY, TEST_VALUE); Set<Map.Entry<String, Integer>> entries = constrained.entrySet(); assertFalse(entries.contains(nefariousEntry)); assertFalse(map.containsValue(TEST_VALUE)); assertFalse(entries.containsAll(Collections.singleton(nefariousEntry))); assertFalse(map.containsValue(TEST_VALUE)); }
public void testMapEntrySetToArray() { Map<String, Integer> map = Maps.newLinkedHashMap(); Map<String, Integer> constrained = MapConstraints.constrainedMap(map, TEST_CONSTRAINT); map.put("foo", 1); @SuppressWarnings("unchecked") Map.Entry<String, Integer> entry = (Map.Entry) constrained.entrySet().toArray()[0]; try { entry.setValue(TEST_VALUE); fail("TestValueException expected"); } catch (TestValueException expected) { } assertFalse(map.containsValue(TEST_VALUE)); }