Exemplo n.º 1
0
 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"));
 }
Exemplo n.º 2
0
 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());
 }
Exemplo n.º 3
0
 public void testConstrainedBiMapLegal() {
   BiMap<String, Integer> map =
       new AbstractBiMap<String, Integer>(
           Maps.<String, Integer>newLinkedHashMap(), Maps.<Integer, String>newLinkedHashMap()) {};
   BiMap<String, Integer> constrained = MapConstraints.constrainedBiMap(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(map.values(), constrained.values());
   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();
 }
Exemplo n.º 4
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());
 }
Exemplo n.º 5
0
 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();
 }
Exemplo n.º 6
0
 public void testConstrainedTypePreservingSet() {
   SetMultimap<String, Integer> multimap =
       MapConstraints.constrainedSetMultimap(
           LinkedHashMultimap.<String, Integer>create(), TEST_CONSTRAINT);
   multimap.put("foo", 1);
   Map.Entry<String, Collection<Integer>> entry = multimap.asMap().entrySet().iterator().next();
   assertTrue(entry.getValue() instanceof Set);
 }
Exemplo n.º 7
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"));
 }
Exemplo n.º 8
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"));
 }
Exemplo n.º 9
0
 public void testConstrainedTypePreservingRandomAccessList() {
   ListMultimap<String, Integer> multimap =
       MapConstraints.constrainedListMultimap(
           ArrayListMultimap.<String, Integer>create(), TEST_CONSTRAINT);
   multimap.put("foo", 1);
   Map.Entry<String, Collection<Integer>> entry = multimap.asMap().entrySet().iterator().next();
   assertTrue(entry.getValue() instanceof List);
   assertFalse(multimap.entries() instanceof Set);
   assertTrue(multimap.get("foo") instanceof RandomAccess);
 }
Exemplo n.º 10
0
 public void testPutWithAllowedKeyForbiddenValue() {
   BiMap<String, String> map =
       MapConstraints.constrainedBiMap(HashBiMap.<String, String>create(), TEST_CONSTRAINT);
   try {
     map.put("allowed", TEST_VALUE);
     fail("Expected IllegalArgumentException");
   } catch (IllegalArgumentException expected) {
     // success
   }
 }
Exemplo n.º 11
0
 @Override
 protected BiMap<String, String> create(Entry<String, String>[] entries) {
   BiMap<String, String> bimap =
       MapConstraints.constrainedBiMap(HashBiMap.<String, String>create(), TEST_CONSTRAINT);
   for (Entry<String, String> entry : entries) {
     checkArgument(!bimap.containsKey(entry.getKey()));
     bimap.put(entry.getKey(), entry.getValue());
   }
   return bimap;
 }
Exemplo n.º 12
0
 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));
 }
Exemplo n.º 13
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));
 }
Exemplo n.º 14
0
 public void testConstrainedTypePreservingSortedSet() {
   Comparator<Integer> comparator = Collections.reverseOrder();
   SortedSetMultimap<String, Integer> delegate =
       TreeMultimap.create(Ordering.<String>natural(), comparator);
   SortedSetMultimap<String, Integer> multimap =
       MapConstraints.constrainedSortedSetMultimap(delegate, TEST_CONSTRAINT);
   multimap.put("foo", 1);
   Map.Entry<String, Collection<Integer>> entry = multimap.asMap().entrySet().iterator().next();
   assertTrue(entry.getValue() instanceof SortedSet);
   assertSame(comparator, multimap.valueComparator());
   assertSame(comparator, multimap.get("foo").comparator());
 }
Exemplo n.º 15
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));
 }
Exemplo n.º 16
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));
 }
Exemplo n.º 17
0
 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));
 }
Exemplo n.º 18
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));
 }
Exemplo n.º 19
0
 public void testConstrainedBiMapIllegal() {
   BiMap<String, Integer> map =
       new AbstractBiMap<String, Integer>(
           Maps.<String, Integer>newLinkedHashMap(), Maps.<Integer, String>newLinkedHashMap()) {};
   BiMap<String, Integer> constrained = MapConstraints.constrainedBiMap(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) {
   }
   try {
     constrained.forcePut(TEST_KEY, 3);
     fail("TestKeyException expected");
   } catch (TestKeyException expected) {
   }
   try {
     constrained.inverse().forcePut(TEST_VALUE, "baz");
     fail("TestValueException expected");
   } catch (TestValueException expected) {
   }
   try {
     constrained.inverse().forcePut(3, TEST_KEY);
     fail("TestKeyException expected");
   } catch (TestKeyException expected) {
   }
   assertEquals(Collections.emptySet(), map.entrySet());
   assertEquals(Collections.emptySet(), constrained.entrySet());
 }
Exemplo n.º 20
0
 public void testNotNull() {
   MapConstraint<Object, Object> constraint = MapConstraints.notNull();
   constraint.checkKeyValue("foo", 1);
   assertEquals("Not null", constraint.toString());
   try {
     constraint.checkKeyValue(null, 1);
     fail("NullPointerException expected");
   } catch (NullPointerException expected) {
   }
   try {
     constraint.checkKeyValue("foo", null);
     fail("NullPointerException expected");
   } catch (NullPointerException expected) {
   }
   try {
     constraint.checkKeyValue(null, null);
     fail("NullPointerException expected");
   } catch (NullPointerException expected) {
   }
 }
Exemplo n.º 21
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));
 }
Exemplo n.º 22
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());
 }