/* * Copyright (C) 2009 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.testing; 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 junit.framework.Test; import junit.framework.TestSuite; import java.lang.reflect.Method; import java.util.Collection; import java.util.Collections; import java.util.Comparator; import java.util.EnumMap; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.TreeMap; import java.util.concurrent.ConcurrentHashMap; /** * Generates a test suite covering the {@link Map} implementations in the * {@link java.util} package. Can be subclassed to specify tests that should * be suppressed. * * @author Kevin Bourrillion */ public class TestsForMapsInJavaUtil { public static Test suite() { return new TestsForMapsInJavaUtil().allTests(); } public Test allTests() { TestSuite suite = new TestSuite("java.util Maps"); suite.addTest(testsForEmptyMap()); suite.addTest(testsForSingletonMap()); suite.addTest(testsForHashMap()); suite.addTest(testsForLinkedHashMap()); suite.addTest(testsForTreeMap()); suite.addTest(testsForEnumMap()); suite.addTest(testsForConcurrentHashMap()); return suite; } protected Collection suppressForEmptyMap() { return Collections.emptySet(); } protected Collection suppressForSingletonMap() { return Collections.emptySet(); } protected Collection suppressForHashMap() { return Collections.emptySet(); } protected Collection suppressForLinkedHashMap() { return Collections.emptySet(); } protected Collection suppressForTreeMap() { return Collections.emptySet(); } protected Collection suppressForEnumMap() { return Collections.emptySet(); } protected Collection suppressForConcurrentHashMap() { return Collections.emptySet(); } public Test testsForEmptyMap() { return MapTestSuiteBuilder .using(new TestStringMapGenerator() { @Override protected Map create( Entry[] entries) { return Collections.emptyMap(); } }) .named("emptyMap") .withFeatures( CollectionFeature.NONE, CollectionSize.ZERO) .suppressing(suppressForEmptyMap()) .createTestSuite(); } public Test testsForSingletonMap() { return MapTestSuiteBuilder .using(new TestStringMapGenerator() { @Override protected Map create( Entry[] entries) { return Collections.singletonMap( entries[0].getKey(), entries[0].getValue()); } }) .named("singletonMap") .withFeatures( MapFeature.ALLOWS_NULL_KEYS, MapFeature.ALLOWS_NULL_VALUES, CollectionSize.ONE) .suppressing(suppressForSingletonMap()) .createTestSuite(); } public Test testsForHashMap() { return MapTestSuiteBuilder .using(new TestStringMapGenerator() { @Override protected Map create( Entry[] entries) { return toHashMap(entries); } @Override public Iterable> order( List> insertionOrder) { /* * For convenience, make this test double as a test that no tester * calls order() on a container without the KNOWN_ORDER feature. */ throw new UnsupportedOperationException(); } }) .named("HashMap") .withFeatures( MapFeature.GENERAL_PURPOSE, MapFeature.ALLOWS_NULL_KEYS, MapFeature.ALLOWS_NULL_VALUES, CollectionSize.ANY) .suppressing(suppressForHashMap()) .createTestSuite(); } public Test testsForLinkedHashMap() { return MapTestSuiteBuilder .using(new TestStringMapGenerator() { @Override protected Map create( Entry[] entries) { return populate(new LinkedHashMap(), entries); } }) .named("LinkedHashMap") .withFeatures( MapFeature.GENERAL_PURPOSE, MapFeature.ALLOWS_NULL_KEYS, MapFeature.ALLOWS_NULL_VALUES, CollectionFeature.KNOWN_ORDER, CollectionSize.ANY) .suppressing(suppressForLinkedHashMap()) .createTestSuite(); } public Test testsForTreeMap() { return NavigableMapTestSuiteBuilder .using(new TestStringMapGenerator() { @Override protected Map create( Entry[] entries) { return populate(new TreeMap( arbitraryNullFriendlyComparator()), entries); } }) .named("TreeMap") .withFeatures( MapFeature.GENERAL_PURPOSE, MapFeature.ALLOWS_NULL_KEYS, MapFeature.ALLOWS_NULL_VALUES, CollectionFeature.KNOWN_ORDER, CollectionSize.ANY) .suppressing(suppressForTreeMap()) .createTestSuite(); } public Test testsForEnumMap() { return MapTestSuiteBuilder .using(new TestEnumMapGenerator() { @Override protected Map create( Entry[] entries) { return populate( new EnumMap(AnEnum.class), entries); } }) .named("EnumMap") .withFeatures( MapFeature.GENERAL_PURPOSE, MapFeature.ALLOWS_NULL_VALUES, MapFeature.RESTRICTS_KEYS, CollectionFeature.KNOWN_ORDER, CollectionSize.ANY) .suppressing(suppressForEnumMap()) .createTestSuite(); } public Test testsForConcurrentHashMap() { return MapTestSuiteBuilder .using(new TestStringMapGenerator() { @Override protected Map create( Entry[] entries) { return populate(new ConcurrentHashMap(), entries); } }) .named("ConcurrentHashMap") .withFeatures( MapFeature.GENERAL_PURPOSE, CollectionSize.ANY) .suppressing(suppressForConcurrentHashMap()) .createTestSuite(); } // TODO: IdentityHashMap, AbstractMap private static Map toHashMap( Entry[] entries) { return populate(new HashMap(), entries); } // TODO: call conversion constructors or factory methods instead of using // populate() on an empty map private static Map populate( Map map, Entry[] entries) { for (Entry entry : entries) { map.put(entry.getKey(), entry.getValue()); } return map; } static Comparator arbitraryNullFriendlyComparator() { return new Comparator() { @Override public int compare(T left, T right) { return String.valueOf(left).compareTo(String.valueOf(right)); } }; } }