|
Java example source code file (WellBehavedMapTest.java)
The WellBehavedMapTest.java Java example source code
/*
* Copyright (C) 2011 The Guava Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.common.collect;
import com.google.common.annotations.GwtCompatible;
import junit.framework.TestCase;
import java.util.EnumMap;
import java.util.Map;
import java.util.Set;
@GwtCompatible
public class WellBehavedMapTest extends TestCase {
enum Foo {
X, Y, Z, T
}
public void testEntrySet_contain() {
WellBehavedMap<Foo, Integer> map = WellBehavedMap.wrap(
new EnumMap<Foo, Integer>(Foo.class));
map.putAll(ImmutableMap.of(Foo.X, 1, Foo.Y, 2, Foo.Z, 3));
// testing with the exact entry
assertTrue(map.entrySet().contains(Maps.immutableEntry(Foo.X, 1)));
assertTrue(map.entrySet().contains(Maps.immutableEntry(Foo.Y, new Integer(2))));
// testing an entry with a contained key, but not the same value
assertFalse(map.entrySet().contains(Maps.immutableEntry(Foo.X, 5)));
// testing a non-existent key
assertFalse(map.entrySet().contains(Maps.immutableEntry(Foo.T, 0)));
}
public void testEntry_setValue() {
WellBehavedMap<Foo, Integer> map = WellBehavedMap.wrap(
new EnumMap<Foo, Integer>(Foo.class));
map.putAll(ImmutableMap.of(Foo.X, 1, Foo.Y, 2, Foo.Z, 3));
for (Map.Entry<Foo, Integer> entry : map.entrySet()) {
entry.setValue(entry.getValue() + 5);
}
assertEquals(ImmutableMap.of(Foo.X, 6, Foo.Y, 7, Foo.Z, 8), map);
}
public void testEntriesAreMutableAndConsistent() {
WellBehavedMap<Foo, Integer> map = WellBehavedMap.wrap(
new EnumMap<Foo, Integer>(Foo.class));
map.putAll(ImmutableMap.of(Foo.X, 1));
Map.Entry<Foo, Integer> entry1 = Iterables.getOnlyElement(map.entrySet());
Map.Entry<Foo, Integer> entry2 = Iterables.getOnlyElement(map.entrySet());
// the entries are constructed and forgotten, thus different
assertNotSame(entry1, entry2);
Set<Map.Entry
Other Java examples (source code examples)Here is a short list of links related to this Java WellBehavedMapTest.java source code file: |
| ... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.