1/*
2 * Copyright (C) 2013 The Guava Authors
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 * in compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 * or implied. See the License for the specific language governing permissions and limitations under
12 * the License.
13 */
14
15package com.google.common.collect.testing.testers;
16
17import static com.google.common.collect.testing.features.CollectionSize.ONE;
18import static com.google.common.collect.testing.features.CollectionSize.ZERO;
19import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEYS;
20import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES;
21
22import com.google.common.annotations.GwtCompatible;
23import com.google.common.collect.testing.AbstractMapTester;
24import com.google.common.collect.testing.features.CollectionSize;
25import com.google.common.collect.testing.features.MapFeature;
26
27import java.util.LinkedHashMap;
28import java.util.Map;
29import java.util.Map.Entry;
30import java.util.Set;
31
32/**
33 * A generic JUnit test which tests {@code toString()} operations on a map. Can't be invoked
34 * directly; please see {@link com.google.common.collect.testing.MapTestSuiteBuilder}.
35 *
36 * @author Kevin Bourrillion
37 * @author Louis Wasserman
38 */
39@GwtCompatible
40public class MapToStringTester<K, V> extends AbstractMapTester<K, V> {
41  public void testToString_minimal() {
42    assertNotNull("toString() should not return null", getMap().toString());
43  }
44
45  @CollectionSize.Require(ZERO)
46  public void testToString_size0() {
47    assertEquals("emptyMap.toString should return {}", "{}", getMap().toString());
48  }
49
50  @CollectionSize.Require(ONE)
51  public void testToString_size1() {
52    assertEquals(
53        "size1Map.toString should return {entry}", "{" + samples.e0 + "}", getMap().toString());
54  }
55
56  @CollectionSize.Require(absent = ZERO)
57  @MapFeature.Require(ALLOWS_NULL_KEYS)
58  public void testToStringWithNullKey() {
59    initMapWithNullKey();
60    testToString_formatting();
61  }
62
63  @CollectionSize.Require(absent = ZERO)
64  @MapFeature.Require(ALLOWS_NULL_VALUES)
65  public void testToStringWithNullValue() {
66    initMapWithNullValue();
67    testToString_formatting();
68  }
69
70  public void testToString_formatting() {
71    assertEquals(
72        "map.toString() incorrect", expectedToString(getMap().entrySet()), getMap().toString());
73  }
74
75  private String expectedToString(Set<Entry<K, V>> entries) {
76    Map<K, V> reference = new LinkedHashMap<K, V>();
77    for (Map.Entry<K, V> entry : entries) {
78      reference.put(entry.getKey(), entry.getValue());
79    }
80    return reference.toString();
81  }
82}
83