|
Java example source code file (ImmutableMapTest.java)
The ImmutableMapTest.java Java example source code
/*
* Copyright (C) 2008 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 static com.google.common.testing.SerializableTester.reserialize;
import com.google.common.annotations.GwtCompatible;
import com.google.common.annotations.GwtIncompatible;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableMap.Builder;
import com.google.common.collect.testing.AnEnum;
import com.google.common.collect.testing.CollectionTestSuiteBuilder;
import com.google.common.collect.testing.ListTestSuiteBuilder;
import com.google.common.collect.testing.MapInterfaceTest;
import com.google.common.collect.testing.MapTestSuiteBuilder;
import com.google.common.collect.testing.MinimalSet;
import com.google.common.collect.testing.SampleElements.Colliders;
import com.google.common.collect.testing.SampleElements.Unhashables;
import com.google.common.collect.testing.UnhashableObject;
import com.google.common.collect.testing.features.CollectionFeature;
import com.google.common.collect.testing.features.CollectionSize;
import com.google.common.collect.testing.features.MapFeature;
import com.google.common.collect.testing.google.MapGenerators.ImmutableMapCopyOfEntriesGenerator;
import com.google.common.collect.testing.google.MapGenerators.ImmutableMapCopyOfEnumMapGenerator;
import com.google.common.collect.testing.google.MapGenerators.ImmutableMapCopyOfGenerator;
import com.google.common.collect.testing.google.MapGenerators.ImmutableMapEntryListGenerator;
import com.google.common.collect.testing.google.MapGenerators.ImmutableMapGenerator;
import com.google.common.collect.testing.google.MapGenerators.ImmutableMapKeyListGenerator;
import com.google.common.collect.testing.google.MapGenerators.ImmutableMapUnhashableValuesGenerator;
import com.google.common.collect.testing.google.MapGenerators.ImmutableMapValueListGenerator;
import com.google.common.testing.EqualsTester;
import com.google.common.testing.NullPointerTester;
import com.google.common.testing.SerializableTester;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;
import java.util.EnumMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
/**
* Tests for {@link ImmutableMap}.
*
* @author Kevin Bourrillion
* @author Jesse Wilson
*/
@GwtCompatible(emulated = true)
public class ImmutableMapTest extends TestCase {
@GwtIncompatible // suite
public static Test suite() {
TestSuite suite = new TestSuite();
suite.addTestSuite(ImmutableMapTest.class);
suite.addTest(MapTestSuiteBuilder.using(new ImmutableMapGenerator())
.withFeatures(
CollectionSize.ANY,
CollectionFeature.SERIALIZABLE_INCLUDING_VIEWS,
CollectionFeature.KNOWN_ORDER,
MapFeature.REJECTS_DUPLICATES_AT_CREATION,
CollectionFeature.ALLOWS_NULL_QUERIES)
.named("ImmutableMap")
.createTestSuite());
suite.addTest(MapTestSuiteBuilder.using(new ImmutableMapCopyOfGenerator())
.withFeatures(
CollectionSize.ANY,
CollectionFeature.SERIALIZABLE_INCLUDING_VIEWS,
CollectionFeature.KNOWN_ORDER,
CollectionFeature.ALLOWS_NULL_QUERIES)
.named("ImmutableMap.copyOf[Map]")
.createTestSuite());
suite.addTest(MapTestSuiteBuilder.using(new ImmutableMapCopyOfEntriesGenerator())
.withFeatures(
CollectionSize.ANY,
MapFeature.REJECTS_DUPLICATES_AT_CREATION,
CollectionFeature.SERIALIZABLE_INCLUDING_VIEWS,
CollectionFeature.KNOWN_ORDER,
CollectionFeature.ALLOWS_NULL_QUERIES)
.named("ImmutableMap.copyOf[Iterable<Entry>]")
.createTestSuite());
suite.addTest(MapTestSuiteBuilder.using(new ImmutableMapCopyOfEnumMapGenerator())
.withFeatures(
CollectionSize.ANY,
CollectionFeature.SERIALIZABLE_INCLUDING_VIEWS,
CollectionFeature.KNOWN_ORDER,
CollectionFeature.ALLOWS_NULL_QUERIES)
.named("ImmutableMap.copyOf[EnumMap]")
.createTestSuite());
suite.addTest(CollectionTestSuiteBuilder.using(
new ImmutableMapUnhashableValuesGenerator())
.withFeatures(CollectionSize.ANY, CollectionFeature.KNOWN_ORDER,
CollectionFeature.ALLOWS_NULL_QUERIES)
.named("ImmutableMap.values, unhashable")
.createTestSuite());
suite.addTest(ListTestSuiteBuilder.using(
new ImmutableMapKeyListGenerator())
.named("ImmutableMap.keySet.asList")
.withFeatures(CollectionSize.ANY,
CollectionFeature.SERIALIZABLE,
CollectionFeature.REJECTS_DUPLICATES_AT_CREATION,
CollectionFeature.ALLOWS_NULL_QUERIES)
.createTestSuite());
suite.addTest(ListTestSuiteBuilder.using(
new ImmutableMapEntryListGenerator())
.named("ImmutableMap.entrySet.asList")
.withFeatures(CollectionSize.ANY,
CollectionFeature.SERIALIZABLE,
CollectionFeature.REJECTS_DUPLICATES_AT_CREATION,
CollectionFeature.ALLOWS_NULL_QUERIES)
.createTestSuite());
suite.addTest(ListTestSuiteBuilder.using(
new ImmutableMapValueListGenerator())
.named("ImmutableMap.values.asList")
.withFeatures(CollectionSize.ANY,
CollectionFeature.SERIALIZABLE,
CollectionFeature.ALLOWS_NULL_QUERIES)
.createTestSuite());
return suite;
}
public abstract static class AbstractMapTests<K, V>
extends MapInterfaceTest<K, V> {
public AbstractMapTests() {
super(false, false, false, false, false);
}
@Override protected Map<K, V> makeEmptyMap() {
throw new UnsupportedOperationException();
}
private static final Joiner joiner = Joiner.on(", ");
@Override protected void assertMoreInvariants(Map<K, V> map) {
// TODO: can these be moved to MapInterfaceTest?
for (Entry<K, V> entry : map.entrySet()) {
assertEquals(entry.getKey() + "=" + entry.getValue(),
entry.toString());
}
assertEquals("{" + joiner.join(map.entrySet()) + "}",
map.toString());
assertEquals("[" + joiner.join(map.entrySet()) + "]",
map.entrySet().toString());
assertEquals("[" + joiner.join(map.keySet()) + "]",
map.keySet().toString());
assertEquals("[" + joiner.join(map.values()) + "]",
map.values().toString());
assertEquals(MinimalSet.from(map.entrySet()), map.entrySet());
assertEquals(Sets.newHashSet(map.keySet()), map.keySet());
}
}
public static class MapTests extends AbstractMapTests<String, Integer> {
@Override protected Map<String, Integer> makeEmptyMap() {
return ImmutableMap.of();
}
@Override protected Map<String, Integer> makePopulatedMap() {
return ImmutableMap.of("one", 1, "two", 2, "three", 3);
}
@Override protected String getKeyNotInPopulatedMap() {
return "minus one";
}
@Override protected Integer getValueNotInPopulatedMap() {
return -1;
}
}
public static class SingletonMapTests
extends AbstractMapTests<String, Integer> {
@Override protected Map<String, Integer> makePopulatedMap() {
return ImmutableMap.of("one", 1);
}
@Override protected String getKeyNotInPopulatedMap() {
return "minus one";
}
@Override protected Integer getValueNotInPopulatedMap() {
return -1;
}
}
@GwtIncompatible // SerializableTester
public static class ReserializedMapTests extends AbstractMapTests<String, Integer> {
@Override protected Map<String, Integer> makePopulatedMap() {
return SerializableTester.reserialize(
ImmutableMap.of("one", 1, "two", 2, "three", 3));
}
@Override protected String getKeyNotInPopulatedMap() {
return "minus one";
}
@Override protected Integer getValueNotInPopulatedMap() {
return -1;
}
}
public static class MapTestsWithBadHashes
extends AbstractMapTests<Object, Integer> {
@Override protected Map<Object, Integer> makeEmptyMap() {
throw new UnsupportedOperationException();
}
@Override protected Map<Object, Integer> makePopulatedMap() {
Colliders colliders = new Colliders();
return ImmutableMap.of(
colliders.e0(), 0,
colliders.e1(), 1,
colliders.e2(), 2,
colliders.e3(), 3);
}
@Override protected Object getKeyNotInPopulatedMap() {
return new Colliders().e4();
}
@Override protected Integer getValueNotInPopulatedMap() {
return 4;
}
}
@GwtIncompatible // GWT's ImmutableMap emulation is backed by java.util.HashMap.
public static class MapTestsWithUnhashableValues
extends AbstractMapTests<Integer, UnhashableObject> {
@Override protected Map<Integer, UnhashableObject> makeEmptyMap() {
return ImmutableMap.of();
}
@Override protected Map<Integer, UnhashableObject> makePopulatedMap() {
Unhashables unhashables = new Unhashables();
return ImmutableMap.of(
0, unhashables.e0(), 1, unhashables.e1(), 2, unhashables.e2());
}
@Override protected Integer getKeyNotInPopulatedMap() {
return 3;
}
@Override protected UnhashableObject getValueNotInPopulatedMap() {
return new Unhashables().e3();
}
}
@GwtIncompatible // GWT's ImmutableMap emulation is backed by java.util.HashMap.
public static class MapTestsWithSingletonUnhashableValue extends MapTestsWithUnhashableValues {
@Override protected Map<Integer, UnhashableObject> makePopulatedMap() {
Unhashables unhashables = new Unhashables();
return ImmutableMap.of(0, unhashables.e0());
}
}
public static class CreationTests extends TestCase {
public void testEmptyBuilder() {
ImmutableMap<String, Integer> map
= new Builder<String, Integer>().build();
assertEquals(Collections.<String, Integer>emptyMap(), map);
}
public void testSingletonBuilder() {
ImmutableMap<String, Integer> map = new Builder
Other Java examples (source code examples)Here is a short list of links related to this Java ImmutableMapTest.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.