001/*
002 * Copyright (C) 2013 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005 * in compliance with the License. You may obtain a copy of the License at
006 *
007 * http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software distributed under the License
010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011 * or implied. See the License for the specific language governing permissions and limitations under
012 * the License.
013 */
014
015package com.google.common.collect.testing.testers;
016
017import static com.google.common.collect.testing.features.CollectionFeature.NON_STANDARD_TOSTRING;
018import static com.google.common.collect.testing.features.CollectionSize.ONE;
019import static com.google.common.collect.testing.features.CollectionSize.ZERO;
020import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEYS;
021import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES;
022
023import com.google.common.annotations.GwtCompatible;
024import com.google.common.collect.testing.AbstractMapTester;
025import com.google.common.collect.testing.features.CollectionFeature;
026import com.google.common.collect.testing.features.CollectionSize;
027import com.google.common.collect.testing.features.MapFeature;
028import java.util.LinkedHashMap;
029import java.util.Map;
030import java.util.Map.Entry;
031import java.util.Set;
032import org.junit.Ignore;
033
034/**
035 * A generic JUnit test which tests {@code toString()} operations on a map. Can't be invoked
036 * directly; please see {@link com.google.common.collect.testing.MapTestSuiteBuilder}.
037 *
038 * @author Kevin Bourrillion
039 * @author Louis Wasserman
040 */
041@GwtCompatible
042@Ignore("test runners must not instantiate and run this directly, only via suites we build")
043// @Ignore affects the Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
044@SuppressWarnings("JUnit4ClassUsedInJUnit3")
045public class MapToStringTester<K, V> extends AbstractMapTester<K, V> {
046  public void testToString_minimal() {
047    assertNotNull("toString() should not return null", getMap().toString());
048  }
049
050  @CollectionSize.Require(ZERO)
051  @CollectionFeature.Require(absent = NON_STANDARD_TOSTRING)
052  public void testToString_size0() {
053    assertEquals("emptyMap.toString should return {}", "{}", getMap().toString());
054  }
055
056  @CollectionSize.Require(ONE)
057  @CollectionFeature.Require(absent = NON_STANDARD_TOSTRING)
058  public void testToString_size1() {
059    assertEquals("size1Map.toString should return {entry}", "{" + e0() + "}", getMap().toString());
060  }
061
062  @CollectionSize.Require(absent = ZERO)
063  @CollectionFeature.Require(absent = NON_STANDARD_TOSTRING)
064  @MapFeature.Require(ALLOWS_NULL_KEYS)
065  public void testToStringWithNullKey() {
066    initMapWithNullKey();
067    testToString_formatting();
068  }
069
070  @CollectionSize.Require(absent = ZERO)
071  @CollectionFeature.Require(absent = NON_STANDARD_TOSTRING)
072  @MapFeature.Require(ALLOWS_NULL_VALUES)
073  public void testToStringWithNullValue() {
074    initMapWithNullValue();
075    testToString_formatting();
076  }
077
078  @CollectionFeature.Require(absent = NON_STANDARD_TOSTRING)
079  public void testToString_formatting() {
080    assertEquals(
081        "map.toString() incorrect", expectedToString(getMap().entrySet()), getMap().toString());
082  }
083
084  private String expectedToString(Set<Entry<K, V>> entries) {
085    Map<K, V> reference = new LinkedHashMap<>();
086    for (Entry<K, V> entry : entries) {
087      reference.put(entry.getKey(), entry.getValue());
088    }
089    return reference.toString();
090  }
091}