Debian Codegraph @

guava-testlib/src/com/google/common/collect/testing/testers/MapToStringTester.java

raw
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
15 package com.google.common.collect.testing.testers;
16
17 import static com.google.common.collect.testing.features.CollectionFeature.NON_STANDARD_TOSTRING;
18 import static com.google.common.collect.testing.features.CollectionSize.ONE;
19 import static com.google.common.collect.testing.features.CollectionSize.ZERO;
20 import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEYS;
21 import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES;
22
23 import com.google.common.annotations.GwtCompatible;
24 import com.google.common.collect.testing.AbstractMapTester;
25 import com.google.common.collect.testing.features.CollectionFeature;
26 import com.google.common.collect.testing.features.CollectionSize;
27 import com.google.common.collect.testing.features.MapFeature;
28 import java.util.LinkedHashMap;
29 import java.util.Map;
30 import java.util.Map.Entry;
31 import java.util.Set;
32 import org.junit.Ignore;
33
34 /**
35 * A generic JUnit test which tests {@code toString()} operations on a map. Can't be invoked
36 * directly; please see {@link com.google.common.collect.testing.MapTestSuiteBuilder}.
37 *
38 * @author Kevin Bourrillion
39 * @author Louis Wasserman
40 */
41 @GwtCompatible
42 @Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
43 public class MapToStringTester<K, V> extends AbstractMapTester<K, V> {
44 public void testToString_minimal() {
45 assertNotNull("toString() should not return null", getMap().toString());
46 }
47
48 @CollectionSize.Require(ZERO)
49 @CollectionFeature.Require(absent = NON_STANDARD_TOSTRING)
50 public void testToString_size0() {
51 assertEquals("emptyMap.toString should return {}", "{}", getMap().toString());
52 }
53
54 @CollectionSize.Require(ONE)
55 @CollectionFeature.Require(absent = NON_STANDARD_TOSTRING)
56 public void testToString_size1() {
57 assertEquals("size1Map.toString should return {entry}", "{" + e0() + "}", getMap().toString());
58 }
59
60 @CollectionSize.Require(absent = ZERO)
61 @CollectionFeature.Require(absent = NON_STANDARD_TOSTRING)
62 @MapFeature.Require(ALLOWS_NULL_KEYS)
63 public void testToStringWithNullKey() {
64 initMapWithNullKey();
65 testToString_formatting();
66 }
67
68 @CollectionSize.Require(absent = ZERO)
69 @CollectionFeature.Require(absent = NON_STANDARD_TOSTRING)
70 @MapFeature.Require(ALLOWS_NULL_VALUES)
71 public void testToStringWithNullValue() {
72 initMapWithNullValue();
73 testToString_formatting();
74 }
75
76 @CollectionFeature.Require(absent = NON_STANDARD_TOSTRING)
77 public void testToString_formatting() {
78 assertEquals(
79 "map.toString() incorrect", expectedToString(getMap().entrySet()), getMap().toString());
80 }
81
82 private String expectedToString(Set<Entry<K, V>> entries) {
83 Map<K, V> reference = new LinkedHashMap<>();
84 for (Entry<K, V> entry : entries) {
85 reference.put(entry.getKey(), entry.getValue());
86 }
87 return reference.toString();
88 }
89 }