View Javadoc
1   /*
2    * Copyright (C) 2011 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.collect;
18  
19  import com.google.common.annotations.GwtCompatible;
20  
21  import junit.framework.TestCase;
22  
23  import java.util.EnumMap;
24  import java.util.Map;
25  import java.util.Set;
26  
27  @GwtCompatible
28  public class WellBehavedMapTest extends TestCase {
29    enum Foo {
30      X, Y, Z, T
31    }
32    
33    public void testEntrySet_contain() {
34      WellBehavedMap<Foo, Integer> map = WellBehavedMap.wrap(
35          new EnumMap<Foo, Integer>(Foo.class));
36      map.putAll(ImmutableMap.of(Foo.X, 1, Foo.Y, 2, Foo.Z, 3));
37  
38      // testing with the exact entry
39      assertTrue(map.entrySet().contains(Maps.immutableEntry(Foo.X, 1)));
40      assertTrue(map.entrySet().contains(Maps.immutableEntry(Foo.Y, new Integer(2))));
41      
42      // testing an entry with a contained key, but not the same value
43      assertFalse(map.entrySet().contains(Maps.immutableEntry(Foo.X, 5)));
44  
45      // testing a non-existent key
46      assertFalse(map.entrySet().contains(Maps.immutableEntry(Foo.T, 0)));
47    }
48    
49    public void testEntry_setValue() {
50      WellBehavedMap<Foo, Integer> map = WellBehavedMap.wrap(
51          new EnumMap<Foo, Integer>(Foo.class));
52      map.putAll(ImmutableMap.of(Foo.X, 1, Foo.Y, 2, Foo.Z, 3));
53      
54      for (Map.Entry<Foo, Integer> entry : map.entrySet()) {
55        entry.setValue(entry.getValue() + 5);
56      }
57      
58      assertEquals(ImmutableMap.of(Foo.X, 6, Foo.Y, 7, Foo.Z, 8), map);
59    }
60  
61    public void testEntriesAreMutableAndConsistent() {
62      WellBehavedMap<Foo, Integer> map = WellBehavedMap.wrap(
63          new EnumMap<Foo, Integer>(Foo.class));
64      map.putAll(ImmutableMap.of(Foo.X, 1));
65      
66      Map.Entry<Foo, Integer> entry1 = Iterables.getOnlyElement(map.entrySet());
67      Map.Entry<Foo, Integer> entry2 = Iterables.getOnlyElement(map.entrySet());
68      
69      // the entries are constructed and forgotten, thus different
70      assertNotSame(entry1, entry2);
71      
72      Set<Map.Entry<Foo, Integer>> entrySet = map.entrySet();
73      
74      assertTrue(entrySet.contains(entry1));
75      assertTrue(entrySet.contains(entry2));
76      
77      // mutating entry
78      entry1.setValue(2);
79      
80      // entry2 is also modified
81      assertEquals(entry1.getValue(), entry2.getValue());
82      
83      // and both are still contained in the set
84      assertTrue(entrySet.contains(entry1));
85      assertTrue(entrySet.contains(entry2));
86    }
87    
88    public void testEntrySet_remove() {
89      WellBehavedMap<Foo, Integer> map = WellBehavedMap.wrap(
90          new EnumMap<Foo, Integer>(Foo.class));
91      map.putAll(ImmutableMap.of(Foo.X, 1, Foo.Y, 2, Foo.Z, 3));
92      Set<Map.Entry<Foo, Integer>> entrySet = map.entrySet();
93      
94      // removing an existing entry, verifying consistency
95      Map.Entry<Foo, Integer> entry = Maps.immutableEntry(Foo.Y, 2); 
96      assertTrue(entrySet.remove(entry));
97      assertFalse(map.containsKey(Foo.Y));
98      assertNull(map.get(Foo.Y));
99      assertFalse(entrySet.contains(entry));
100     
101     // we didn't have that entry, not removed
102     assertFalse(entrySet.remove(Maps.immutableEntry(Foo.T, 4)));
103     
104     // we didn't have that entry, only <Z, 3>, must not remove
105     assertFalse(entrySet.remove(Maps.immutableEntry(Foo.Z, 5)));
106   }
107 }