Пример #1
0
 public void testConstrainedMultimapQueue() {
   Multimap<String, Integer> multimap =
       Multimaps.newMultimap(new HashMap<String, Collection<Integer>>(), new QueueSupplier());
   Multimap<String, Integer> constrained =
       MapConstraints.constrainedMultimap(multimap, TEST_CONSTRAINT);
   constrained.put("foo", 1);
   assertTrue(constrained.get("foo").contains(1));
   assertTrue(multimap.get("foo").contains(1));
   try {
     constrained.put(TEST_KEY, 1);
     fail("TestKeyException expected");
   } catch (TestKeyException expected) {
   }
   try {
     constrained.put("foo", TEST_VALUE);
     fail("TestValueException expected");
   } catch (TestValueException expected) {
   }
   try {
     constrained.get("foo").add(TEST_VALUE);
     fail("TestKeyException expected");
   } catch (TestValueException expected) {
   }
   try {
     constrained.get(TEST_KEY).add(1);
     fail("TestValueException expected");
   } catch (TestKeyException expected) {
   }
   assertEquals(1, constrained.size());
   assertEquals(1, multimap.size());
 }
Пример #2
0
 public void testNefariousMultimapGetAddAll() {
   Multimap<String, Integer> multimap = LinkedListMultimap.create();
   Multimap<String, Integer> constrained =
       MapConstraints.constrainedMultimap(multimap, TEST_CONSTRAINT);
   Collection<Integer> onceIterable = ConstraintsTest.onceIterableCollection(1);
   constrained.get("foo").addAll(onceIterable);
   assertEquals(ImmutableList.of(1), constrained.get("foo"));
 }
Пример #3
0
 public void testNefariousMultimapPutAllMultimap() {
   Multimap<String, Integer> multimap = LinkedListMultimap.create();
   Multimap<String, Integer> constrained =
       MapConstraints.constrainedMultimap(multimap, TEST_CONSTRAINT);
   Multimap<String, Integer> onceIterable = Multimaps.forMap(onceIterableMap("foo", 1));
   constrained.putAll(onceIterable);
   assertEquals(ImmutableList.of(1), constrained.get("foo"));
 }
Пример #4
0
 public void testMultimapEntriesRemoveNefariousEntry() {
   Multimap<String, Integer> multimap = LinkedListMultimap.create();
   Multimap<String, Integer> constrained =
       MapConstraints.constrainedMultimap(multimap, TEST_CONSTRAINT);
   multimap.put("foo", 1);
   Map.Entry<String, Integer> nefariousEntry = nefariousMapEntry(TEST_KEY, TEST_VALUE);
   Collection<Map.Entry<String, Integer>> entries = constrained.entries();
   assertFalse(entries.remove(nefariousEntry));
   assertFalse(multimap.containsValue(TEST_VALUE));
   assertFalse(entries.removeAll(Collections.singleton(nefariousEntry)));
   assertFalse(multimap.containsValue(TEST_VALUE));
 }
Пример #5
0
 public void testConstrainedMultimapLegal() {
   Multimap<String, Integer> multimap = LinkedListMultimap.create();
   Multimap<String, Integer> constrained =
       MapConstraints.constrainedMultimap(multimap, TEST_CONSTRAINT);
   multimap.put(TEST_KEY, TEST_VALUE);
   constrained.put("foo", 1);
   multimap.get("bar").add(2);
   constrained.get("baz").add(3);
   multimap.get("qux").addAll(Arrays.asList(4));
   constrained.get("zig").addAll(Arrays.asList(5));
   multimap.putAll("zag", Arrays.asList(6));
   constrained.putAll("bee", Arrays.asList(7));
   multimap.putAll(new ImmutableMultimap.Builder<String, Integer>().put("bim", 8).build());
   constrained.putAll(new ImmutableMultimap.Builder<String, Integer>().put("bop", 9).build());
   multimap.putAll(new ImmutableMultimap.Builder<String, Integer>().put("dig", 10).build());
   constrained.putAll(new ImmutableMultimap.Builder<String, Integer>().put("dag", 11).build());
   assertTrue(multimap.equals(constrained));
   assertTrue(constrained.equals(multimap));
   ASSERT
       .that(ImmutableList.copyOf(multimap.entries()))
       .is(ImmutableList.copyOf(constrained.entries()));
   ASSERT.that(constrained.asMap().get("foo")).has().item(1);
   assertNull(constrained.asMap().get("missing"));
   assertEquals(multimap.asMap(), constrained.asMap());
   assertEquals(multimap.values(), constrained.values());
   assertEquals(multimap.keys(), constrained.keys());
   assertEquals(multimap.keySet(), constrained.keySet());
   assertEquals(multimap.toString(), constrained.toString());
   assertEquals(multimap.hashCode(), constrained.hashCode());
   ASSERT
       .that(multimap.entries())
       .has()
       .allOf(
           Maps.immutableEntry(TEST_KEY, TEST_VALUE),
           Maps.immutableEntry("foo", 1),
           Maps.immutableEntry("bar", 2),
           Maps.immutableEntry("baz", 3),
           Maps.immutableEntry("qux", 4),
           Maps.immutableEntry("zig", 5),
           Maps.immutableEntry("zag", 6),
           Maps.immutableEntry("bee", 7),
           Maps.immutableEntry("bim", 8),
           Maps.immutableEntry("bop", 9),
           Maps.immutableEntry("dig", 10),
           Maps.immutableEntry("dag", 11))
       .inOrder();
   assertFalse(constrained.asMap().values() instanceof Serializable);
   Iterator<Collection<Integer>> iterator = constrained.asMap().values().iterator();
   iterator.next();
   iterator.next().add(12);
   assertTrue(multimap.containsEntry("foo", 12));
 }
Пример #6
0
 public void testMultimapAsMapEntriesContainsNefariousEntry() {
   Multimap<String, Integer> multimap = LinkedListMultimap.create();
   Multimap<String, Integer> constrained =
       MapConstraints.constrainedMultimap(multimap, TEST_CONSTRAINT);
   multimap.put("foo", 1);
   Map.Entry<String, ? extends Collection<Integer>> nefariousEntry =
       nefariousMapEntry(TEST_KEY, Collections.singleton(TEST_VALUE));
   Set<Map.Entry<String, Collection<Integer>>> entries = constrained.asMap().entrySet();
   assertFalse(entries.contains(nefariousEntry));
   assertFalse(multimap.containsValue(TEST_VALUE));
   assertFalse(entries.containsAll(Collections.singleton(nefariousEntry)));
   assertFalse(multimap.containsValue(TEST_VALUE));
 }
Пример #7
0
 public void testMultimapAsMapValuesToArray() {
   Multimap<String, Integer> multimap = LinkedListMultimap.create();
   Multimap<String, Integer> constrained =
       MapConstraints.constrainedMultimap(multimap, TEST_CONSTRAINT);
   multimap.put("foo", 1);
   @SuppressWarnings("unchecked")
   Collection<Integer> collection =
       (Collection<Integer>) constrained.asMap().values().toArray()[0];
   try {
     collection.add(TEST_VALUE);
     fail("TestValueException expected");
   } catch (TestValueException expected) {
   }
   assertFalse(multimap.containsValue(TEST_VALUE));
 }
Пример #8
0
 public void testMultimapAsMapEntriesToArray() {
   Multimap<String, Integer> multimap = LinkedListMultimap.create();
   Multimap<String, Integer> constrained =
       MapConstraints.constrainedMultimap(multimap, TEST_CONSTRAINT);
   multimap.put("foo", 1);
   @SuppressWarnings("unchecked")
   Map.Entry<String, Collection<Integer>> entry =
       (Map.Entry<String, Collection<Integer>>) constrained.asMap().entrySet().toArray()[0];
   try {
     entry.setValue(Collections.<Integer>emptySet());
     fail("UnsupportedOperationException expected");
   } catch (UnsupportedOperationException expected) {
   }
   try {
     entry.getValue().add(TEST_VALUE);
     fail("TestValueException expected");
   } catch (TestValueException expected) {
   }
   assertFalse(multimap.containsValue(TEST_VALUE));
 }
Пример #9
0
 @SuppressWarnings("unchecked")
 public void testConstrainedMultimapIllegal() {
   Multimap<String, Integer> multimap = LinkedListMultimap.create();
   Multimap<String, Integer> constrained =
       MapConstraints.constrainedMultimap(multimap, TEST_CONSTRAINT);
   try {
     constrained.put(TEST_KEY, 1);
     fail("TestKeyException expected");
   } catch (TestKeyException expected) {
   }
   try {
     constrained.put("foo", TEST_VALUE);
     fail("TestValueException expected");
   } catch (TestValueException expected) {
   }
   try {
     constrained.put(TEST_KEY, TEST_VALUE);
     fail("TestKeyException expected");
   } catch (TestKeyException expected) {
   }
   try {
     constrained.get(TEST_KEY).add(1);
     fail("TestKeyException expected");
   } catch (TestKeyException expected) {
   }
   try {
     constrained.get("foo").add(TEST_VALUE);
     fail("TestValueException expected");
   } catch (TestValueException expected) {
   }
   try {
     constrained.get(TEST_KEY).add(TEST_VALUE);
     fail("TestKeyException expected");
   } catch (TestKeyException expected) {
   }
   try {
     constrained.get(TEST_KEY).addAll(Arrays.asList(1));
     fail("TestKeyException expected");
   } catch (TestKeyException expected) {
   }
   try {
     constrained.get("foo").addAll(Arrays.asList(1, TEST_VALUE));
     fail("TestValueException expected");
   } catch (TestValueException expected) {
   }
   try {
     constrained.get(TEST_KEY).addAll(Arrays.asList(1, TEST_VALUE));
     fail("TestKeyException expected");
   } catch (TestKeyException expected) {
   }
   try {
     constrained.putAll(TEST_KEY, Arrays.asList(1));
     fail("TestKeyException expected");
   } catch (TestKeyException expected) {
   }
   try {
     constrained.putAll("foo", Arrays.asList(1, TEST_VALUE));
     fail("TestValueException expected");
   } catch (TestValueException expected) {
   }
   try {
     constrained.putAll(TEST_KEY, Arrays.asList(1, TEST_VALUE));
     fail("TestKeyException expected");
   } catch (TestKeyException expected) {
   }
   try {
     constrained.putAll(
         new ImmutableMultimap.Builder<String, Integer>().put(TEST_KEY, 2).put("foo", 1).build());
     fail("TestKeyException expected");
   } catch (TestKeyException expected) {
   }
   try {
     constrained.putAll(
         new ImmutableMultimap.Builder<String, Integer>()
             .put("bar", TEST_VALUE)
             .put("foo", 1)
             .build());
     fail("TestValueException expected");
   } catch (TestValueException expected) {
   }
   try {
     constrained.putAll(
         new ImmutableMultimap.Builder<String, Integer>()
             .put(TEST_KEY, TEST_VALUE)
             .put("foo", 1)
             .build());
     fail("TestKeyException expected");
   } catch (TestKeyException expected) {
   }
   try {
     constrained.entries().add(Maps.immutableEntry(TEST_KEY, 1));
     fail("UnsupportedOperationException expected");
   } catch (UnsupportedOperationException expected) {
   }
   try {
     constrained
         .entries()
         .addAll(Arrays.asList(Maps.immutableEntry("foo", 1), Maps.immutableEntry(TEST_KEY, 2)));
     fail("UnsupportedOperationException expected");
   } catch (UnsupportedOperationException expected) {
   }
   assertTrue(multimap.isEmpty());
   assertTrue(constrained.isEmpty());
   constrained.put("foo", 1);
   try {
     constrained.asMap().get("foo").add(TEST_VALUE);
     fail("TestValueException expected");
   } catch (TestValueException expected) {
   }
   try {
     constrained.asMap().values().iterator().next().add(TEST_VALUE);
     fail("TestValueException expected");
   } catch (TestValueException expected) {
   }
   try {
     ((Collection<Integer>) constrained.asMap().values().toArray()[0]).add(TEST_VALUE);
     fail("TestValueException expected");
   } catch (TestValueException expected) {
   }
   ASSERT
       .that(ImmutableList.copyOf(multimap.entries()))
       .is(ImmutableList.copyOf(constrained.entries()));
   assertEquals(multimap.asMap(), constrained.asMap());
   assertEquals(multimap.values(), constrained.values());
   assertEquals(multimap.keys(), constrained.keys());
   assertEquals(multimap.keySet(), constrained.keySet());
   assertEquals(multimap.toString(), constrained.toString());
   assertEquals(multimap.hashCode(), constrained.hashCode());
 }