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 // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
043@SuppressWarnings("JUnit4ClassUsedInJUnit3")
044public class MapToStringTester<K, V> extends AbstractMapTester<K, V> {
045  public void testToString_minimal() {
046    assertNotNull("toString() should not return null", getMap().toString());
047  }
048
049  @CollectionSize.Require(ZERO)
050  @CollectionFeature.Require(absent = NON_STANDARD_TOSTRING)
051  public void testToString_size0() {
052    assertEquals("emptyMap.toString should return {}", "{}", getMap().toString());
053  }
054
055  @CollectionSize.Require(ONE)
056  @CollectionFeature.Require(absent = NON_STANDARD_TOSTRING)
057  public void testToString_size1() {
058    assertEquals("size1Map.toString should return {entry}", "{" + e0() + "}", getMap().toString());
059  }
060
061  @CollectionSize.Require(absent = ZERO)
062  @CollectionFeature.Require(absent = NON_STANDARD_TOSTRING)
063  @MapFeature.Require(ALLOWS_NULL_KEYS)
064  public void testToStringWithNullKey() {
065    initMapWithNullKey();
066    testToString_formatting();
067  }
068
069  @CollectionSize.Require(absent = ZERO)
070  @CollectionFeature.Require(absent = NON_STANDARD_TOSTRING)
071  @MapFeature.Require(ALLOWS_NULL_VALUES)
072  public void testToStringWithNullValue() {
073    initMapWithNullValue();
074    testToString_formatting();
075  }
076
077  @CollectionFeature.Require(absent = NON_STANDARD_TOSTRING)
078  public void testToString_formatting() {
079    assertEquals(
080        "map.toString() incorrect", expectedToString(getMap().entrySet()), getMap().toString());
081  }
082
083  private String expectedToString(Set<Entry<K, V>> entries) {
084    Map<K, V> reference = new LinkedHashMap<>();
085    for (Entry<K, V> entry : entries) {
086      reference.put(entry.getKey(), entry.getValue());
087    }
088    return reference.toString();
089  }
090}